SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE Pa_Actualizar (@pmTabla VARCHAR(50),@pmCampos VARCHAR(2000),@pmCriterio VARCHAR(1000))
AS

BEGIN
 Declare @pmQry VARCHAR(2000)
 SET @pmQry=''
 SET @pmQry=@pmQry + 'UPDATE '+ @pmTabla+' SET ' + @pmCampos
 IF LEN(@pmCriterio)>0 
  BEGIN
   SET  @pmQry=@pmQry + ' WHERE ' + @pmCriterio
  END

EXECUTE  (@pmQry)

RETURN
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE Pa_Agregar (@pmTabla VARCHAR(50),@pmCampos VARCHAR(1000),@pmValores VARCHAR(2000))
AS

BEGIN
 Declare @pmQry VARCHAR(2000)
 SET @pmQry=''
 SET @pmQry=@pmQry + 'INSERT INTO '+ @pmTabla+' '
 SET @pmQry=@pmQry + '('+ @pmCampos+') VALUES '
 SET @pmQry=@pmQry + '('+ @pmValores+')'
 EXECUTE  (@pmQry)

RETURN
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE Pa_Eliminar (@pmTabla VARCHAR(50),@pmCriterio VARCHAR(500))
AS

BEGIN
 Declare @pmQry VARCHAR(500)
 SET @pmQry=''
 SET @pmQry=@pmQry + 'DELETE FROM '+ @pmTabla
 IF LEN(@pmCriterio)>0 
  BEGIN
   SET @pmQry=@pmQry +'  WHERE ' + @pmCriterio
  END

EXECUTE  (@pmQry)

RETURN
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE Pa_InsertSelect  
(@pmDB VARCHAR(50),
@pmOwn VARCHAR(50),
@pmTabla VARCHAR(50),
@pmCampos VARCHAR(1000),
@pmQuery VARCHAR(2000))

AS

BEGIN
 Declare @pmQry VARCHAR(2500)
 SET @pmQry=''
 SET @pmQry=@pmQry + 'INSERT INTO '+ @pmDB + '.'+ @pmOwn +'.'+@pmTabla
 SET @pmQry=@pmQry + ' ('+ @pmCampos+') '+ @pmQuery
 EXECUTE  (@pmQry)

RETURN
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE Pa_Query 
(@pmCampos VARCHAR(3500),
 @pmTabla VARCHAR(1500),
@pmCriterio VARCHAR(2000),
@pmOrden VARCHAR(200),
@pmGroup VARCHAR(500),
@pmHaving VARCHAR(100))

AS

BEGIN
 Declare @pmQry VARCHAR(7800)
 SET @pmQry=''
 SET @pmQry=@pmQry + 'SELECT '+ @pmCampos + '  FROM ' + @pmTabla
 IF Len(@pmCriterio)>0
 BEGIN
  SET @pmQry=@pmQry + '  WHERE '+ @pmCriterio
 END
 IF Len(@pmGroup)>0 
 BEGIN
  SET @pmQry=@pmQry + ' GROUP BY ' + @pmGroup
 END
 IF Len(@pmHaving)>0 
 BEGIN
  SET @pmQry=@pmQry + ' HAVING '+ @pmHaving
 END
 IF Len(@pmOrden)>0 
 BEGIN
  SET @pmQry=@pmQry + ' ORDER BY '+ @pmOrden
 END
EXECUTE  (@pmQry)

RETURN
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelActivos @pmIdActivo VARCHAR(30)
 AS 
 BEGIN
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnActivos'
  BEGIN TRANSACTION @TrnNombre
  DELETE FROM Activos_Anexo  WHERE IdActivo=@pmIdActivo
  DELETE FROM Activos  WHERE IdActivo=@pmIdActivo
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelActivos_Anexo @pmIdActivo VARCHAR(30)
 AS 
 DELETE FROM Activos_Anexo  WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCaja @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 DELETE FROM AcuCaja
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelAcuCajaBuses @pmFecha SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmIdCia CHAR(2)
AS
 DELETE FROM  AcuCajaBuses WHERE Fecha=@pmFecha
 AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuClientes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16)
 AS 
 DELETE FROM AcuClientes
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCuentas @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM AcuCuentas
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCuentasAge @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM AcuCuentasAge
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCuentasAux @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 DELETE FROM AcuCuentasAux
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCuentasCc @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM AcuCuentasCc
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCuentasNit @pmnAnno INT,@pmnMes INT
,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM AcuCuentasNit
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuCuentasVeh @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM AcuCuentasVeh
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuInvLotes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)
 AS 
 DELETE FROM AcuInvLotes
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuInvTanques @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)
 AS 
 DELETE FROM AcuInvTanques
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuInvUbica @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)
 AS 
 DELETE FROM AcuInvUbica
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuInventario @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)
 AS 
 DELETE FROM AcuInventario
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAcuPrestamos @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM AcuPrestamos
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paDelAcuPropietarios  @pmnMes INT, @pmnAnno INT,@pmIdCia CHAR(2)=Null
AS
 DELETE FROM  AcuPropietarios 
 WHERE  nMes=@pmnMes AND nAnno=@pmnAnno AND  IdCia LIKE ISNULL(@pmIdCia, '%%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfAmortiza @pmNumero INT
 AS 
 DELETE FROM Trn_AfAmortiza
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfAmtComp @pmNumero INT
 AS 
 DELETE FROM Trn_AfAmtComp
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfAmtDetalle @pmNumero INT,@pmIdDiferido VARCHAR(30)
 AS 
 DELETE FROM Trn_AfAmtDetalle
 WHERE Numero=@pmNumero AND IdDiferido LIKE ISNULL(@pmIdDiferido,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfCausales @pmIdCausal VARCHAR(4)
 AS 
 DELETE FROM AfCausales
 WHERE IdCausal=@pmIdCausal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfClasesCue @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM AfClasesCue
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfDeprecia @pmNumero INT
 AS 
 DELETE FROM Trn_AfDeprecia
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfDetalle @pmNumero INT,@pmIdActivo VARCHAR(30)
 AS 
 DELETE FROM Trn_AfDetalle
 WHERE Numero=@pmNumero AND IdActivo LIKE ISNULL(@pmIdActivo,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

 CREATE PROCEDURE paDelAfDifIngreso @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfDifIngreso
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfDifMovi @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfDifMovi
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfDifRetiro @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfDifRetiro
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfDifTraslado @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfDifTraslado
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfDifValoriza @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfDifValoriza
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfEstados @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM AfEstados
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfGrupos @pmIdGrupo VARCHAR(4)
 AS 
 BEGIN
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnAfGrupos'
  BEGIN TRANSACTION @TrnNombre
  DELETE FROM AfGruposCue  WHERE IdGrupo=@pmIdGrupo
  DELETE FROM AfGrupos  WHERE IdGrupo=@pmIdGrupo
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfGruposCue @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM AfGruposCue
 WHERE IdGrupo=@pmIdGrupo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfLineas @pmIdLinea VARCHAR(4)
 AS 
 DELETE FROM AfLineas
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfMantnmto @pmNumero INT
 AS 
 DELETE FROM Trn_AfMantnmto
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfMovIngreso @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfMovIngreso
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfMovRetiro @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfMovRetiro
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfMovValoriza @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfMovValoriza
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfMovimiento @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AfMovimiento
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfPignora @pmNumero INT
 AS 
 DELETE FROM Trn_AfPignora
 WHERE Numero=@pmNumero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfTiposDif @pmIdTipDif VARCHAR(4)
 AS 
 DELETE FROM AfTiposDif
 WHERE IdTipDif=@pmIdTipDif
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAfTiposMov @pmIdMov VARCHAR(4)
 AS 
 DELETE FROM AfTiposMov
 WHERE IdMov=@pmIdMov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAgencias @pmIdAgencia VARCHAR(16)
 AS 
 DELETE FROM Agencias
 WHERE IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAjuInv @pmTipDoc VARCHAR(3),@pmAjuste INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_AjuInv
 WHERE TipDoc=@pmTipDoc AND Ajuste=@pmAjuste AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelAlmBodegas @pmIdBodega VARCHAR(4)
 AS 
 DELETE FROM AlmBodegas
 WHERE IdBodega=@pmIdBodega

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAprobacion @pmTipDoc VARCHAR(3),@pmAprobacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Aprobacion
 WHERE TipDoc=@pmTipDoc AND Aprobacion=@pmAprobacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAreas @pmIdArea VARCHAR(4)
 AS 
 DELETE FROM Areas
 WHERE IdArea=@pmIdArea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelAutoriza @pmTipAut VARCHAR(3),@pmAutoriza INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Autoriza
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelBancos @pmIdBanco VARCHAR(4)
 AS 
 DELETE FROM Bancos
 WHERE IdBanco=@pmIdBanco
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelBodUbic @pmIdUbic VARCHAR(10)
 AS 
 DELETE FROM BodUbic
 WHERE IdUbic=@pmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelBodegas @pmIdBodega VARCHAR(4)
 AS 
 DELETE FROM Bodegas
 WHERE IdBodega=@pmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelBusClaseCue @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM BusClaseCue
 WHERE IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCalndario @pmFechaDia SMALLDATETIME
 AS 
 DELETE FROM Calndario
 WHERE FechaDia=@pmFechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCalndarioMes @pmMes INT,@pmAnno INT
 AS 
 DELETE FROM Calndario
 WHERE Month(FechaDia)=@pmMes AND Year(FechaDia)=@pmAnno
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCargos @pmIdCargo VARCHAR(4)
 AS 
 DELETE FROM Cargos
 WHERE IdCargo=@pmIdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCausaVales @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_CausaVales
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelCausacVence @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2) 
 AS 
 DELETE FROM Trn_CausacVence
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelCausacVenceItem @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2) 
,@pmItem INT
 AS 
 DELETE FROM Trn_CausacVence
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelCausacion @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Causacion
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCentroCosto @pmIdCCosto VARCHAR(16)
 AS 
 DELETE FROM CentroCosto
 WHERE IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCentrosServ @pmIdCenSer VARCHAR(4)
 AS 
 DELETE FROM CentrosServ
 WHERE IdCenSer=@pmIdCenSer
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCheques @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Cheques
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelChequesDoc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
 AS 
 DELETE FROM Trn_Cheques
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc 
 AND  IdCia=@pmIdCiaDoc AND Recibo<=0

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelChequesRec @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Cheques
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseBan @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseBan
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseCta @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseCta
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseDiv @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseDiv
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseFon @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseFon
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClasePol @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClasePol
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseRie @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseRie
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseSub @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseSub
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClaseTar @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ClaseTar
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelClieContado @pmIdCliente VARCHAR(16)
 AS 
 DELETE FROM ClieContado
 WHERE IdCliente=@pmIdCliente
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCnxextna @pmIdReg INT
 AS 
 DELETE FROM Sys_Cnxextna
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelComDetalle @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT=Null
 AS 
 DELETE FROM Trn_ComDetalle
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelComFactura @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCia CHAR(2),@pmIdProveedor VARCHAR(16),@pmItem INT=Null
 AS 
 DELETE FROM Trn_ComFactura
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura AND IdCia=@pmIdCia 
 AND IdProveedor=@pmIdProveedor   AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelComFacturaCmp @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItemCom INT=Null
AS
 DELETE FROM  Trn_ComFactura  
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia 
 AND (ItemCom>=ISNULL(@pmItemCom,0)  AND ItemCom<=ISNULL(@pmItemCom,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelComFacturaDoc @pmTipFac VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmFactura VARCHAR(15)
 AS 
 DELETE FROM Trn_ComFactura
 WHERE TipFac=@pmTipFac AND TipDoc=@pmTipFac AND Documento=@pmDocumento AND IdCia=@pmIdCia AND IdCiaDoc=@pmIdCia
 AND  Factura LIKE ISNULL(@pmFactura,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCompCaja @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_CompCaja
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCompCajaDet @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_CompCajaDet
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelCompanias @pmIdCia CHAR(2)
 AS 
 DELETE FROM Companias
 WHERE IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelComprobantes @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Comprobantes
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelConcDivCue @pmIdConcepto VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM ConcDivCue
 WHERE IdConcepto=@pmIdConcepto AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelConcDiversos @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM ConcDiversos
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelConceptos @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM Conceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelConceptosAdmon @pmIdConcepto VARCHAR(4),@pmIdAdmon VARCHAR(4),@pmIdClase INT
 AS 
 DELETE FROM ConceptosAdmon
 WHERE IdConcepto=@pmIdConcepto AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND (IdClase>=ISNULL(@pmIdClase,0)  AND IdClase<=ISNULL(@pmIdClase,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelConceptosCue @pmIdConcepto VARCHAR(4),@pmIdClase INT
 AS 
 DELETE FROM ConceptosCue
 WHERE IdConcepto=@pmIdConcepto
 AND (IdClase>=ISNULL(@pmIdClase,0)  AND IdClase<=ISNULL(@pmIdClase,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelConsigna @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Consigna
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCotizacion @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Cotizacion
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelCtasCorrientes @pmIdCta VARCHAR(4)
 AS 
 DELETE FROM CtasCorrientes
 WHERE IdCta=@pmIdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDepartamentos @pmIdDep VARCHAR(8)
 AS 
 DELETE FROM Departamentos
 WHERE IdDep=@pmIdDep

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDependencias @pmIdDep VARCHAR(4)
 AS 
 DELETE FROM Dependencias
 WHERE IdDep=@pmIdDep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelDespachos @pmTipDoc VARCHAR(3),@pmCartulina INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Despachos
 WHERE TipDoc=@pmTipDoc AND Cartulina=@pmCartulina AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDetCuentas @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmConcepto VARCHAR(10),@pmItem INT
 AS 
 DELETE FROM Trn_DetCuentas
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND Concepto LIKE ISNULL(@pmConcepto,'%')
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevCaj @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevCaj
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelDevCausacion  @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)

AS
 DELETE FROM  Trn_DevCausacion 
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion  AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevCns @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevCns
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevEnt @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevEnt
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevFco @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevFco
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevFcr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevFcr
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelDevFvc @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevFvc
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevNpr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevNpr
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevPas @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevPas
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevPla @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevPla
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelDevPrestamos @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevPrestamos
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelDevRecBuses @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
AS
 DELETE FROM  Trn_DevRecBuses  WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND  IdCia=@pmIdCia



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevSal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevSal
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDevVal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DevVal
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDiarios @pmIdDiario VARCHAR(4)
 AS 
 DELETE FROM Diarios
 WHERE IdDiario=@pmIdDiario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDiferidos @pmIdDiferido VARCHAR(30)
 AS 
 DELETE FROM Diferidos
 WHERE IdDiferido=@pmIdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelDisciplinario @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Disciplinario
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelDocMemo @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_DocMemo
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEdsCalibra @pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EdsCalibra
 WHERE TipDoc=@pmTipDoc AND nNota=@pmnNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEdsCortes @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EdsCortes
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEdsForItems @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
 AS 
 DELETE FROM Trn_EdsForItems
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEdsFormas @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
 AS 
 DELETE FROM Trn_EdsFormas
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEdsFormasPla @pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEdsRangos @pmTipoRango VARCHAR(3),@pmIdRango INT
 AS 
 DELETE FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelEdsServicio @pmIdEstacion VARCHAR(4)
 AS 
 DELETE FROM EdsServicio
 WHERE IdEstacion=@pmIdEstacion



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEgrFactura @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT=Null
 AS 
 DELETE FROM Trn_EgrFactura
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEgrFacturaDoc @pmTipDev VARCHAR(3),@pmNumDev INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EgrFactura
 WHERE TipCom=@pmTipDev AND Comprobante=@pmNumDev AND IdCia=@pmIdCia
 AND TipDev=@pmTipDev AND NumDev=@pmNumDev AND EsEgreso=0

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEmp_Contrato @pmNContrato INT
 AS 
 DELETE FROM Emp_Contrato
 WHERE NContrato=@pmNContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEmp_Estudios @pmIdEmpleado VARCHAR(16),@pmItem INT=Null
 AS 
 DELETE FROM Emp_Estudios
 WHERE IdEmpleado=@pmIdEmpleado 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEmp_ExpLaboral @pmIdEmpleado VARCHAR(16),@pmItem INT=Null
 AS 
 DELETE FROM Emp_ExpLaboral
 WHERE IdEmpleado=@pmIdEmpleado
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEmp_Parientes @pmIdEmpleado VARCHAR(16),@pmItem INT=Null
 AS 
 DELETE FROM Emp_Parientes
 WHERE IdEmpleado=@pmIdEmpleado   AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEmpaques @pmIdEmp VARCHAR(4)
 AS 
 DELETE FROM Empaques
 WHERE IdEmp=@pmIdEmp
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEmpleados @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEntArticulos @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_EntArticulos
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEntidades @pmIdTabla VARCHAR(20)
 AS 
 DELETE FROM Sys_Entidades
 WHERE IdTabla=@pmIdTabla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEntradas @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Entradas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstaciones @pmIdEstacion CHAR(2)
 AS 
 DELETE FROM Sys_Estaciones
 WHERE IdEstacion=@pmIdEstacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstadoCiv @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoCiv
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstadoDoc @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoDoc
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstadoPro @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoPro
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstadoSer @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoSer
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstadoTer @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoTer
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstadoVeh @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM EstadoVeh
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelEstratos @pmIdEstrato VARCHAR(4)
 AS 
 DELETE FROM Estratos
 WHERE IdEstrato=@pmIdEstrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelExpLicencias @pmIdLugar VARCHAR(8)
 AS 
 DELETE FROM ExpLicencias
 WHERE IdLugar=@pmIdLugar
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFactVales @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FactVales
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFactcon @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Factcon
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFacturas @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Facturas
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFcCausacion @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FcCausacion
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFcNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FcNotas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFcSemanas @pmIdVehiculo VARCHAR(10),@pmnSemana INT
 AS 
 DELETE FROM Trn_FcSemanas
 WHERE IdVehiculo=@pmIdVehiculo AND nSemana=@pmnSemana
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFields @pmIdEntidad VARCHAR(30),@pmIdCampo VARCHAR(20)=Null
 AS 
 DELETE FROM Sys_Fields
 WHERE IdEntidad=@pmIdEntidad AND IdCampo LIKE ISNULL(@pmIdCampo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelFijos @pmIdCargo VARCHAR(4)
AS
 DELETE FROM  Fijos  WHERE IdCargo=@pmIdCargo



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelFijos_Cla @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4)
AS
 DELETE FROM  Fijos_Cla   WHERE IdCargo=@pmIdCargo AND IdClase LIKE ISNULL(@pmIdClase,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paDelFijos_Cue  @pmIdCargo VARCHAR(4), @pmIdAdmon VARCHAR(4)
AS
  DELETE FROM  Fijos_Cue
 WHERE   IdCargo=@pmIdCargo AND  IdAdmon LIKE ISNULL(@pmIdAdmon,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelFijos_Veh @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10)
AS
  DELETE FROM  Fijos_Veh  WHERE IdCargo=@pmIdCargo AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelFinAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinAfavor
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFinAlmacenes @pmIdAlmacen VARCHAR(4)
 AS 
 DELETE FROM FinAlmacenes
 WHERE IdAlmacen=@pmIdAlmacen

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinCuotas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFinDevFac @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinDevFac
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinDevPre
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinDevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFinFactint @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinFactint
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinNotas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinPrestamo
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_FinRecibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFmtDoc @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2)
 AS 
 DELETE FROM Sys_FmtDoc
 WHERE IdDoc=@pmIdDoc AND IdForma LIKE ISNULL(@pmIdForma,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFmtFactura @pmnFormato VARCHAR(10)
 AS 
 DELETE FROM Sys_FmtFactura
 WHERE nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFondos @pmIdFondo VARCHAR(8)
 AS 
 DELETE FROM Fondos
 WHERE IdFondo=@pmIdFondo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFormasCue @pmIdForma VARCHAR(4),@pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM FormasCue
 WHERE IdForma=@pmIdForma AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelFormaspago @pmIdForma VARCHAR(4)
 AS 
 DELETE FROM Formaspago
 WHERE IdForma=@pmIdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelFuentesPago @pmIdFuente VARCHAR(4)
 AS 
 DELETE FROM FuentesPago
 WHERE IdFuente=@pmIdFuente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGrupos @pmIdGrupo VARCHAR(8)
 AS 
 DELETE FROM Grupos
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGruposAR @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposAR
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelGruposAgenc @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposAgenc
 WHERE IdGrupo=@pmIdGrupo



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGruposCli @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposCli
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelGruposMcia @pmIdGrupo VARCHAR(10)
 AS 
 DELETE FROM GruposMcia
 WHERE IdGrupo=@pmIdGrupo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGruposObj @pmIdGrupo VARCHAR(6)
 AS 
 DELETE FROM Sys_GruposObj
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGruposPro @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposPro
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGruposRot @pmIdGrupo VARCHAR(4)
 AS 
 DELETE FROM GruposRot
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGruposUsu @pmIdGrupo INT
 AS 
 DELETE FROM Sys_GruposUsu
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelGuia @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Guia
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelInstalaciones @pmIdInstala VARCHAR(4)
 AS 
 DELETE FROM Instalaciones
 WHERE IdInstala=@pmIdInstala
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelInvDetalle @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelInvDetalleEst @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdEstacion CHAR(2)
 AS 
 DELETE FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND IdEstacion=@pmIdEstacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelInvFisico @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_InvFisico
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelJornadas @pmIdJornada VARCHAR(4)
 AS 
 DELETE FROM Jornadas
 WHERE IdJornada=@pmIdJornada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurAutoridades @pmIdAutoridad VARCHAR(4)
 AS 
 DELETE FROM JurAutoridades
 WHERE IdAutoridad=@pmIdAutoridad


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurCalifica @pmIdCalifica VARCHAR(4)
 AS 
 DELETE FROM JurCalifica
 WHERE IdCalifica=@pmIdCalifica


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurCasos @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_JurCasos
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelJurCasosHis  @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2), @pmActualizaID INT=Null
AS
 DELETE FROM  Trn_JurCasosHis
 WHERE  TipDoc=@pmTipDoc AND  CasoID=@pmCasoID AND  IdCia=@pmIdCia 
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelJurCasosQtn @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2), @pmItem INT=Null
AS
 DELETE FROM  Trn_JurCasosQtn
 WHERE  TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND  IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurClaseAcc @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM JurClaseAcc
 WHERE IdClase=@pmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurDocumtos @pmIdDoc VARCHAR(4)
 AS 
 DELETE FROM JurDocumtos
 WHERE IdDoc=@pmIdDoc


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurEstados @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM JurEstados
 WHERE IdEstado=@pmIdEstado


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelJurInvesHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT
 AS 
 DELETE FROM Trn_JurInvesHis
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia 
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurInvestiga @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_JurInvestiga
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurPreguntas @pmIdPregunta VARCHAR(4)
 AS 
 DELETE FROM JurPreguntas
 WHERE IdPregunta=@pmIdPregunta


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelJurTipos @pmIdTipoPro VARCHAR(4)
 AS 
 DELETE FROM JurTipos
 WHERE IdTipoPro=@pmIdTipoPro


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelKarUbic @pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_KarUbic
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelKardex @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelLineas @pmIdLinea VARCHAR(8)
 AS 
 DELETE FROM Lineas
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelLocalidades @pmIdLocal VARCHAR(8)
 AS 
 DELETE FROM Localidades
 WHERE IdLocal=@pmIdLocal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelLog_Nomina @pmIdNom VARCHAR(4),@pmIdPeriodo VARCHAR(8)
 AS 
 DELETE FROM Log_Nomina
 WHERE IdNom LIKE ISNULL(@pmIdNom,'%') AND IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelLog_Periodos @pmAnno INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Log_Periodos
 WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (Anno>=ISNULL(@pmAnno,0)  AND Anno<=ISNULL(@pmAnno,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelLotMov @pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_LotMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelLotes @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
,@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10)
 AS 
 DELETE FROM Trn_Lotes
 WHERE NumLote=@pmNumLote AND IdProducto=@pmIdProducto AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdBodega LIKE ISNULL(@pmIdBodega,'%') AND IdUbic LIKE ISNULL(@pmIdUbic,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMarcas @pmIdMarca VARCHAR(4)
 AS 
 DELETE FROM Marcas
 WHERE IdMarca=@pmIdMarca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMarcasLin @pmIdLinea VARCHAR(10)
 AS 
 DELETE FROM MarcasLin
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayAnotaciones @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_MayAnotaciones
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayBonCuentas @pmIdBonif VARCHAR(4),@pmTipoCue INT,@pmCdTipo VARCHAR(4)
 AS 
 DELETE FROM MayBonCuentas
 WHERE IdBonif=@pmIdBonif AND TipoCue=@pmTipoCue AND CdTipo LIKE ISNULL(@pmCdTipo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayBonif @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayContDoc @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_MayContDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayContratos @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayContratos
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayEstados @pmIdEstado VARCHAR(4)
 AS 
 DELETE FROM MayEstados
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayInventario @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_MayInventario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayInvmov @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS
 DELETE FROM Trn_MayInvmov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayItemsInv @pmIdArticulo VARCHAR(16)
 AS 
 DELETE FROM MayItemsInv
 WHERE IdArticulo=@pmIdArticulo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayLiqDetalle @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayLiqDetalle
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayLiqTotales @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayLiqTotales
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayLiquida @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_MayLiquida
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayPspto @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_MayPspto
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayTipos @pmIdTipo VARCHAR(4)
 AS 
 DELETE FROM MayTipos
 WHERE IdTipo=@pmIdTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelMayTiposAprob @pmIdAprob VARCHAR(4)
 AS 
 DELETE FROM MayTiposAprob
 WHERE IdAprob=@pmIdAprob

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelMayTiposBienes @pmIdTipBien VARCHAR(4)
 AS 
 DELETE FROM MayTiposBienes
 WHERE IdTipBien=@pmIdTipBien

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayTiposBonif @pmIdBonif VARCHAR(4)
 AS 
 DELETE FROM MayTiposBonif
 WHERE IdBonif=@pmIdBonif
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelMayTiposDoc @pmIdDoc VARCHAR(4)
 AS 
 DELETE FROM MayTiposDoc
 WHERE IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelMayTiposPoliza @pmIdTipoPol VARCHAR(4)
 AS 
 DELETE FROM MayTiposPoliza
 WHERE IdTipoPol=@pmIdTipoPol

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelMercancias @pmIdMercancia VARCHAR(16)
 AS 
 DELETE FROM Mercancias
 WHERE IdMercancia=@pmIdMercancia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelModulos @pmIdMod VARCHAR(3)
 AS 
 DELETE FROM Sys_Modulos
 WHERE IdMod LIKE ISNULL(@pmIdMod,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAbonos @pmNPrestamo INT,@pmItem INT
 AS 
 DELETE FROM Trn_NomAbonos
 WHERE NPrestamo=@pmNPrestamo AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAbonosLiq @pmDscto VARCHAR(3),@pmNLiquida INT,@pmIdEmpleado VARCHAR(16)=Null
AS 
 DELETE FROM  Trn_NomAbonos
 WHERE Dscto=@pmDscto AND NLiquida=@pmNLiquida AND IdEmpleado LIKE  ISNULL(@pmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAbonosPer @pmIdPeriodo VARCHAR(8),@pmDscto VARCHAR(3),@pmIdEmpleado VARCHAR(16)=Null
AS 
 DELETE FROM  Trn_NomAbonos
 WHERE IdPeriodo=@pmIdPeriodo AND Dscto LIKE ISNULL(@pmDscto,'%')  
 AND IdEmpleado LIKE  ISNULL(@pmIdEmpleado,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAlqDet @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT
 AS 
 DELETE FROM Trn_NomAlqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAlqNov @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT
 AS 
 DELETE FROM Trn_NomAlqNov
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAlqTot @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT
 AS 
 DELETE FROM Trn_NomAlqTot
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAportante @pmIdAportnte VARCHAR(16)
 AS 
 DELETE FROM NomAportante
 WHERE IdAportnte=@pmIdAportnte
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAstncia @pmIdEmpleado VARCHAR(16),@pmFechaDia SMALLDATETIME
 AS 
 DELETE FROM Trn_NomAstncia
 WHERE IdEmpleado=@pmIdEmpleado AND FechaDia=ISNULL(@pmFechaDia,FechaDia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomAutoliq @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20)=Null
 AS 
 DELETE FROM Trn_NomAutoliq
 WHERE TipoLiq=@pmTipoLiq AND Numero LIKE ISNULL(@pmNumero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomCAdmon @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomCAdmon
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomCausalesRet @pmIdCausa VARCHAR(4)
 AS 
 DELETE FROM NomCausalesRet
 WHERE IdCausa=@pmIdCausa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomCertf @pmIdEmpleado VARCHAR(16),@pmNumCertf VARCHAR(20)
 AS 
 DELETE FROM Trn_NomCertf
 WHERE IdEmpleado=@pmIdEmpleado AND NumCertf LIKE ISNULL(@pmNumCertf,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomConAuto @pmIdReg INT
 AS 
 DELETE FROM Trn_NomConAuto
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomConCuentas @pmIdConcepto VARCHAR(4),@pmItem INT
 AS 
 DELETE FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomConOpciones @pmIdConcepto VARCHAR(4),@pmIdOpc INT
 AS 
 DELETE FROM NomConOpciones
 WHERE IdConcepto=@pmIdConcepto 
  AND (IdOpc>=ISNULL(@pmIdOpc,0)  AND IdOpc<=ISNULL(@pmIdOpc,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomConceptos @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM NomConceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomDefault @pmnAnno INT,@pmIdOpc VARCHAR(3)
 AS 
 DELETE FROM NomDefault
 WHERE nAnno=@pmnAnno AND IdOpc LIKE ISNULL(@pmIdOpc,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomDetalle @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmItem INT
 AS 
 DELETE FROM Trn_NomDetalle
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomDetalleEmp @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomDetalle 
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomDetallePer @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)=Null,@pmEsRetiro BIT=Null
 AS 
 DELETE Trn_NomDetalle 
 FROM Trn_NomDetalle AS D INNER JOIN Trn_Nomina AS N ON D.IdPeriodo=N.IdPeriodo AND D.KeyRegistro=N.KeyRegistro
 WHERE D.IdPeriodo=@pmIdPeriodo AND EsRetiro=@pmEsRetiro
 AND D.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomFijos @pmIdEmpleado VARCHAR(16),@pmnAnno INT,@pmnMes INT
 AS 
 DELETE FROM Trn_NomFijos
 WHERE IdEmpleado=@pmIdEmpleado 
 AND (nAnno>=ISNULL(@pmnAnno,0)  AND nAnno<=ISNULL(@pmnAnno,2147483647))
 AND (nMes>=ISNULL(@pmnMes,0)  AND nMes<=ISNULL(@pmnMes,2147483647))
 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomLiqDet @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmItem INT
 AS 
 DELETE FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomLiquida @pmTipoLiq VARCHAR(3),@pmNumero INT
 AS 
 DELETE FROM Trn_NomLiquida
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomLogVeh @pmIdEmpleado VARCHAR(16),@pmNumReg INT
 AS 
 DELETE FROM Trn_NomLogVeh
 WHERE IdEmpleado=@pmIdEmpleado AND NumReg=@pmNumReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomNov @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 DELETE FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomNovCar @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)=Null
 AS 
 DELETE FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND OrigCargue='AUTO'
 AND IdEmpleado LIKE ISNULL(@pmIdEmpleado ,'%')



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomNovedad @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado 
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomPrest @pmNPrestamo INT
 AS 
 DELETE FROM Trn_NomPrest
 WHERE NPrestamo=@pmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomProrrogas @pmIdEmpleado VARCHAR(16),@pmNumReg INT
 AS 
 DELETE FROM Trn_NomProrrogas
 WHERE IdEmpleado=@pmIdEmpleado
 AND (NumReg>=ISNULL(@pmNumReg,0)  AND NumReg<=ISNULL(@pmNumReg,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomRangosFsu @pmIdRango INT
 AS 
 DELETE FROM NomRangosFsu
 WHERE IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomRet @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomRetDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT
 AS 
 DELETE FROM Trn_NomRetDet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomRetDetEmp @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomRetDet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomRetEmp @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomSueldos @pmNumero INT,@pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomSueldos
 WHERE Numero=@pmNumero AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomTablaRet @pmnAnno INT,@pmnIntrvalo INT
 AS 
 DELETE FROM NomTablaRet
 WHERE nAnno=@pmnAnno 
 AND (nIntrvalo>=ISNULL(@pmnIntrvalo,0)  AND nIntrvalo<=ISNULL(@pmnIntrvalo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomVac @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 DELETE FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomVacDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT
 AS 
 DELETE FROM Trn_NomVacDet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomVacDetEmp @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomVacDet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomVacEmp @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomVehiculos @pmIdVehiculo VARCHAR(10)
 AS 
 DELETE FROM NomVehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNomina @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30)
 AS 
 DELETE FROM Trn_Nomina
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro like ISNULL(@pmKeyRegistro,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNominaEmp @pmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNominaPer @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16),@pmEsRetiro BIT
 AS 
 DELETE FROM Trn_Nomina
 WHERE IdPeriodo=@pmIdPeriodo AND EsRetiro=@pmEsRetiro AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNotaProd @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_NotaProd
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Notas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelNotasBuses @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_NotasBuses
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelOComb @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OComb
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelOCompra @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_OCompra
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelObjetosApp @pmIdObj VARCHAR(10)
 AS 
 DELETE FROM Sys_ObjetosApp
 WHERE IdObj=@pmIdObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelOpciones @pmIdOpc VARCHAR(3)
 AS 
 DELETE FROM adm_Opciones
 WHERE IdOpc LIKE ISNULL(@pmIdOpc,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelOpedido @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Opedido
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPagos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPaises @pmIdPais VARCHAR(8)
 AS 
 DELETE FROM adm_Paises
 WHERE IdPais=@pmIdPais
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPeajesCat @pmIdCat VARCHAR(4)
 AS 
 DELETE FROM PeajesCat
 WHERE IdCat=@pmIdCat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPeajesClase @pmIdClase VARCHAR(4)
 AS 
 DELETE FROM PeajesClase
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPeajesTarf @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM PeajesTarf
 WHERE IdCat=@pmIdCat AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPermisos @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10)=Null
 AS 
 DELETE FROM adm_Permisos
 WHERE IdUsuario=@pmIdUsuario AND IdObj LIKE ISNULL(@pmIdObj,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPermisosRoles @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10)=Null,@pmIdRole VARCHAR(3)=Null
 AS 
 DELETE FROM adm_PermisosRoles
 WHERE IdUsuario=@pmIdUsuario AND IdObj LIKE ISNULL(@pmIdObj,'%')  AND IdRole LIKE ISNULL(@pmIdRole,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelPicoPlaca @pmFecha SMALLDATETIME
 AS 
 DELETE FROM PicoPlaca
 WHERE Fecha=@pmFecha


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPlazos @pmIdPlazo VARCHAR(4)
 AS 
 DELETE FROM Plazos
 WHERE IdPlazo=@pmIdPlazo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPolizas @pmNumPoliza VARCHAR(30)
 AS 
 DELETE FROM Polizas
 WHERE NumPoliza=@pmNumPoliza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelPreCuotas @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmNumCuota INT
 AS 
 DELETE FROM Trn_PreCuotas
 WHERE TipDoc=@pmTipDoc AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia 
AND (NumCuota>=ISNULL(@pmNumCuota,0)  AND NumCuota<=ISNULL(@pmNumCuota,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paDelPreCuotasCau @pmCausacionID INT
 AS
 DELETE FROM  Trn_PreCuotas 
 WHERE CausacionID=@pmCausacionID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPresDetalle @pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_PresDetalle
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelPrestamos @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Prestamos
 WHERE TipDoc=@pmTipDoc AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelPresupuesto @pmNumero INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Presupuesto
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdAditivos @pmIdProducto VARCHAR(16),@pmIdAditivo VARCHAR(16)
 AS 
 DELETE FROM ProdAditivos
 WHERE IdProducto=@pmIdProducto AND IdAditivo LIKE ISNULL(@pmIdAditivo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdCombo @pmIdProducto VARCHAR(16),@pmItem INT
 AS 
 DELETE FROM ProdCombo
 WHERE IdProducto=@pmIdProducto
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdCostos @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdCostos
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 DELETE FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase LIKE ISNULL(@pmIdClase,'%')
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 DELETE FROM ProdImptos
 WHERE IdProducto=@pmIdProducto AND IdClase LIKE ISNULL(@pmIdClase,'%')
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdMcias @pmIdProducto VARCHAR(16)
 AS 
 DELETE FROM ProdMcias
 WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdPrecios @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdPrecios
 WHERE IdProducto=@pmIdProducto AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdProvee @pmIdProducto VARCHAR(16),@pmIdProv VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdProvee
 WHERE IdProducto=@pmIdProducto AND IdProv LIKE ISNULL(@pmIdProv,'%') 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdSaldoUbc @pmIdProducto VARCHAR(16),@pmIdUbic VARCHAR(10),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdSaldoUbc
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdUbic LIKE ISNULL(@pmIdUbic,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdSaldos @pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 DELETE FROM ProdSaldos
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProdTarf @pmTipo VARCHAR(3),@pmNumero INT
 AS 
 DELETE FROM Trn_ProdTarf
 WHERE Tipo=@pmTipo 
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelProfesiones @pmIdProf VARCHAR(4)
 AS 
 DELETE FROM Profesiones
 WHERE IdProf=@pmIdProf
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuc @pmIdCuenta VARCHAR(16)
 AS 
 DELETE FROM Puc
 WHERE IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuestos @pmIdPto VARCHAR(4)
 AS 
 DELETE FROM Puestos
 WHERE IdPto=@pmIdPto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntBases @pmIdProgrma INT
 AS 
 DELETE FROM PuntBases
 WHERE IdProgrma=@pmIdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntBasesItm @pmIdProgrma INT,@pmCdSubgrupo VARCHAR(8),@pmCdProducto VARCHAR(16)
 AS 
 DELETE FROM PuntBases
 WHERE IdProgrma=@pmIdProgrma AND CdSubgrupo LIKE ISNULL(@pmCdSubgrupo,'%')
 AND CdProducto LIKE ISNULL(@pmCdProducto,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntFormas @pmIdProgrma INT
 AS 
 DELETE FROM PuntFormas
 WHERE IdProgrma=@pmIdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntFormasItm @pmIdProgrma INT,@pmIdForma VARCHAR(4)
 AS 
 DELETE FROM PuntFormas
 WHERE IdProgrma=@pmIdProgrma AND IdForma LIKE ISNULL(@pmIdForma,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntPremios @pmIdProgrma INT
 AS 
 DELETE FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntPremiosItm @pmIdProgrma INT,@pmItem INT
 AS 
 DELETE FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntProg @pmIdProgrma INT
 AS 
 DELETE FROM PuntProg
 WHERE IdProgrma=@pmIdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntTarjetas @pmNumTarjeta VARCHAR(20)
 AS 
 DELETE FROM PuntTarjetas
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelPuntos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Puntos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRadicaVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_RadicaVales
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelRangosPeso @pmIdRango VARCHAR(4)
 AS 
 DELETE FROM RangosPeso
 WHERE IdRango=@pmIdRango

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRecAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_RecAfavor
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelRecBuses @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_RecBuses
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRecCheques @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_RecCheques
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelRecFactura @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_RecFactura
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelRecFacturaVc  @pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 DELETE FROM Trn_RecFactura
  WHERE  TipRec IN ('OD1','OTR')
 AND  IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelRecFactura_Ant  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
,@pmItemDoc INT=Null,@pmAplAntcpo BIT=Null
AS
 DELETE FROM Trn_RecFactura  WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND  IdCiaDoc=@pmIdCiaDoc
 AND (ItemDoc>=ISNULL(@pmItemDoc,0)  AND ItemDoc<=ISNULL(@pmItemDoc,2147483647))
 AND (AplAntcpo=ISNULL(@pmAplAntcpo,0) or AplAntcpo=ISNULL(@pmAplAntcpo,1))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelRecFactura_Doc  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmAplAntcpo BIT=Null
AS
 IF @pmAplAntcpo  IS NULL
  DELETE FROM Trn_RecFactura  WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND  IdCiaDoc=@pmIdCiaDoc
 ELSE
  DELETE FROM Trn_RecFactura  WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND  IdCiaDoc=@pmIdCiaDoc
   AND AplAntcpo=@pmAplAntcpo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelRecTanqueos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_RecTanqueos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Recibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRegimenDian @pmIdRegimen VARCHAR(4)
 AS 
 DELETE FROM RegimenDian
 WHERE IdRegimen=@pmIdRegimen

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRegistro @pmIdCia CHAR(2)
 AS 
 DELETE FROM adm_Registro
 WHERE IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRelaciones @pmIdTabla VARCHAR(20),@pmTablaForanea VARCHAR(20)=Null,@pmCampoForaneo VARCHAR(20)=Null
 AS 
 DELETE FROM Sys_Relaciones
 WHERE IdTabla=@pmIdTabla AND TablaForanea LIKE ISNULL(@pmTablaForanea,'%')
  AND CampoForaneo LIKE ISNULL(@pmCampoForaneo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRemision @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Remision
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelReport @pmTipoRep VARCHAR(3),@pmIdRep CHAR(2)=Null
 AS 
 DELETE FROM Sys_Report
 WHERE TipoRep=@pmTipoRep AND IdRep LIKE ISNULL(@pmIdRep,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelReportFmto @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
 AS 
 DELETE FROM Sys_ReportFmto
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelReportSub @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20),@pmNombreSub VARCHAR(20)=Null
 AS 
 DELETE FROM Sys_ReportSub
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato AND NombreSub LIKE ISNULL(@pmNombreSub,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelReportSubPmt @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmNombreSub VARCHAR(20),@pmIndParam INT
 AS 
 DELETE FROM Sys_ReportSubPmt
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato
  AND NombreSub LIKE ISNULL(@pmNombreSub,'%')
  AND (IndParam>=ISNULL(@pmIndParam,0)  AND IndParam<=ISNULL(@pmIndParam,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelReqDetalle @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRequisicion @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Requisicion
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRoles @pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3)=Null
 AS 
 DELETE FROM Sys_Roles
 WHERE IdObj=@pmIdObj AND IdRole LIKE ISNULL(@pmIdRole,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRutas @pmIdRuta VARCHAR(4)
 AS 
 DELETE FROM Rutas
 WHERE IdRuta=@pmIdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelRutasPlan @pmIdRuta VARCHAR(4),@pmIdPunto INT
 AS 
 DELETE FROM RutasPlan
 WHERE IdRuta=@pmIdRuta
 AND (IdPunto>=ISNULL(@pmIdPunto,0)  AND IdPunto<=ISNULL(@pmIdPunto,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelRutasTramos @pmIdRuta VARCHAR(4),@pmNumTramo INT
 AS 
 DELETE FROM RutasTramos
 WHERE IdRuta=@pmIdRuta 
  AND (NumTramo>=ISNULL(@pmNumTramo,0)  AND NumTramo<=ISNULL(@pmNumTramo,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSAutoriza @pmIdConcepto VARCHAR(4)
 AS 
 DELETE FROM Sys_Autoriza
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSaiArticulos @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_SaiArticulos
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSalidas @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Salidas
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSectoresEco @pmIdSector VARCHAR(8)
 AS 
 DELETE FROM SectoresEco
 WHERE IdSector=@pmIdSector
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSecurity @pmIdLla INT
 AS 
 DELETE FROM Sys_Security
 WHERE IdLla=@pmIdLla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSerMov @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 DELETE FROM Trn_SerMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSeriales @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Seriales
 WHERE IdProducto=@pmIdProducto AND  IdCia=@pmIdCia AND NumSerial LIKE ISNULL(@pmNumSerial,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSobDetalle @pmIdDec VARCHAR(4),@pmNumDeclara INT
 AS 
 DELETE FROM Trn_SobDetalle
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSobEntidades @pmIdEnterr VARCHAR(8)
 AS 
 DELETE FROM SobEntidades
 WHERE IdEnterr=@pmIdEnterr
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSobProductos @pmIdClase INT
 AS 
 DELETE FROM SobProductos
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSobTiposDec @pmIdDec VARCHAR(4)
 AS 
 DELETE FROM SobTiposDec
 WHERE IdDec=@pmIdDec
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSobretasas @pmIdDec VARCHAR(4),@pmNumDeclara INT
 AS 
 DELETE FROM Trn_Sobretasas
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSubCentros @pmIdSubCos VARCHAR(16)
 AS 
 DELETE FROM SubCentros
 WHERE IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSubGrupos @pmIdSubgrupo VARCHAR(8)
 AS 
 DELETE FROM SubGrupos
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelSubGruposAgenc @pmIdSubAgenc VARCHAR(4)
 AS 
 DELETE FROM SubGruposAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSubgruposCue @pmIdSubgrupo VARCHAR(8),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM SubgruposCue
 WHERE IdSubgrupo=@pmIdSubgrupo AND IdClase LIKE ISNULL(@pmIdClase,'%') 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelSubzonas @pmIdSzona VARCHAR(4)
 AS 
 DELETE FROM Subzonas
 WHERE IdSzona=@pmIdSzona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTFmtos @pmnFormato VARCHAR(10)
 AS 
 DELETE FROM Sys_TFmtos
 WHERE nFormato=@pmnFormato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTablapor @pmIdTarifa VARCHAR(4)
 AS 
 DELETE FROM Tablapor
 WHERE IdTarifa=@pmIdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTanqAforo @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 DELETE FROM TanqAforo
 WHERE IdTanque=@pmIdTanque 
 AND (UndAforo>=ISNULL(@pmUndAforo,0)  AND UndAforo<=ISNULL(@pmUndAforo,9999999999))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTanqAforoFrac @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 DELETE FROM TanqAforoFrac
 WHERE IdTanque=@pmIdTanque 
 AND (UndAforo>=ISNULL(@pmUndAforo,0)  AND UndAforo<=ISNULL(@pmUndAforo,9999999999))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTanqDispens @pmIdMngra VARCHAR(10)
 AS 
 DELETE FROM TanqDispens
 WHERE IdMngra=@pmIdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTanqSaldos @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TanqSaldos
 WHERE IdTanque LIKE ISNULL(@pmIdTanque,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTanques @pmIdTanque VARCHAR(4)
 AS 
 DELETE FROM Tanques
 WHERE IdTanque=@pmIdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTarifBuses @pmIdTarifa VARCHAR(4)
 AS 
 DELETE FROM TarifBuses
 WHERE IdTarifa=@pmIdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTarifasFletes @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmItem INT
 AS 
 DELETE FROM TarifasFletes
 WHERE IdOrigen=@pmIdOrigen AND IdDestino=@pmIdDestino  
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTaxRutas @pmIdRuta VARCHAR(4)
 AS 
 DELETE FROM TaxRutas  WHERE IdRuta=@pmIdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTaxTarifClie @pmTipoTarif VARCHAR(10),@pmItem INT
 AS 
 DELETE FROM TaxTarifClie
 WHERE TipoTarif=@pmTipoTarif 
 AND Item BETWEEN ISNULL(@pmItem,0) AND ISNULL(@pmItem,2147483647)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTaxTarifas @pmTipoTarif VARCHAR(10),@pmItem INT
 AS 
 DELETE FROM TaxTarifas
 WHERE TipoTarif=@pmTipoTarif AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercClieCia @pmIdClie VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TercClieCia
 WHERE IdClie=@pmIdClie AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16)
 AS 
 DELETE FROM TercCliePrestamo
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercClieRef @pmIdClie VARCHAR(16),@pmItem INT
 AS 
 DELETE FROM TercClieRef
 WHERE IdClie=@pmIdClie
AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
 AS 
 DELETE FROM TercClieSaldos
 WHERE IdClie LIKE ISNULL(@pmIdClie,'%') AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercClieVeh @pmIdClie VARCHAR(16),@pmpVehiculo VARCHAR(10)
 AS 
 DELETE FROM TercClieVeh
 WHERE IdClie=@pmIdClie AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercCliente @pmIdClie VARCHAR(16)
 AS 
 DELETE FROM TercCliente
 WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelTercCndContrato @pmNumContrato INT
 AS 
 DELETE FROM TercCndContrato
 WHERE NumContrato=@pmNumContrato


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTercCndReferencias @pmIdConductor VARCHAR(16),@pmItem INT
 AS 
 DELETE FROM TercCndReferencias
 WHERE IdConductor=@pmIdConductor  
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercCndTarjetas @pmIdConductor VARCHAR(16),@pmItem INT
 AS 
 DELETE FROM TercCndTarjetas
 WHERE IdConductor=@pmIdConductor 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercCndtores @pmIdConductor VARCHAR(16)
 AS 
 DELETE FROM TercCndtores
 WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelTercPropietario @pmIdPropietario VARCHAR(16)
 AS 
 DELETE FROM TercPropietario
 WHERE IdPropietario=@pmIdPropietario


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercProvee @pmIdProv VARCHAR(16)
 AS 
 DELETE FROM TercProvee
 WHERE IdProv=@pmIdProv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTercVendedor @pmIdVend VARCHAR(16)
 AS 
 DELETE FROM TercVendedor
 WHERE IdVend=@pmIdVend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTerceros @pmIdTercero VARCHAR(16)
 AS 
 DELETE FROM Terceros
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposAdm @pmIdAdmon VARCHAR(4)
 AS 
 DELETE FROM TiposAdm
 WHERE IdAdmon=@pmIdAdmon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposAgenc @pmIdTipAgenc VARCHAR(4)
 AS 
 DELETE FROM TiposAgenc
 WHERE IdTipAgenc=@pmIdTipAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposBan @pmIdBandera VARCHAR(4)
 AS 
 DELETE FROM TiposBan
 WHERE IdBandera=@pmIdBandera
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposBloq @pmIdTipBloq VARCHAR(4)
 AS 
 DELETE FROM TiposBloq
 WHERE IdTipBloq=@pmIdTipBloq

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposCar @pmIdCrceria VARCHAR(4)
 AS 
 DELETE FROM TiposCar
 WHERE IdCrceria=@pmIdCrceria
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposCarga @pmIdTipCarga VARCHAR(4)
 AS 
 DELETE FROM TiposCarga
 WHERE IdTipCarga=@pmIdTipCarga
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposCnd @pmIdTipoCon VARCHAR(4)
 AS 
 DELETE FROM TiposCnd
 WHERE IdTipoCon=@pmIdTipoCon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposCol @pmIdColor VARCHAR(4)
 AS 
 DELETE FROM TiposCol
 WHERE IdColor=@pmIdColor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposCom @pmIdCom VARCHAR(3)
 AS 
 DELETE FROM TiposCom
 WHERE IdCom=@pmIdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposComAgenc @pmIdSubAgenc VARCHAR(4)
 AS 
 DELETE FROM TiposComAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposComCons @pmIdCom VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TiposComCons
 WHERE IdCom=@pmIdCom AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposCon @pmIdTipCon VARCHAR(4)
 AS 
 DELETE FROM TiposCon
 WHERE IdTipCon=@pmIdTipCon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposCot @pmIdCot VARCHAR(4)
 AS 
 DELETE FROM TiposCot
 WHERE IdCot=@pmIdCot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposDoc @pmIdDoc VARCHAR(3)
 AS 
 DELETE FROM Sys_TiposDoc
 WHERE IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposDocCons @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 DELETE FROM TiposDocCons
 WHERE IdDoc=@pmIdDoc AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposDocCue @pmIdDoc VARCHAR(3),@pmIdClase INT
 AS 
 DELETE FROM Sys_TiposDocCue
 WHERE IdDoc=@pmIdDoc  AND (IdClase>=ISNULL(@pmIdClase,0)  AND IdClase<=ISNULL(@pmIdClase,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposEsc @pmIdTipoEsc VARCHAR(4)
 AS 
 DELETE FROM TiposEsc
 WHERE IdTipoEsc=@pmIdTipoEsc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposFil @pmIdFiltro VARCHAR(4)
 AS 
 DELETE FROM TiposFil
 WHERE IdFiltro=@pmIdFiltro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposFuel @pmIdCom VARCHAR(4)
 AS 
 DELETE FROM TiposFuel
 WHERE IdCom=@pmIdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposLla @pmIdTlla VARCHAR(4)
 AS 
 DELETE FROM TiposLla
 WHERE IdTlla=@pmIdTlla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposLub @pmIdLub VARCHAR(4)
 AS 
 DELETE FROM TiposLub
 WHERE IdLub=@pmIdLub
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposMat @pmIdMat VARCHAR(4)
 AS 
 DELETE FROM TiposMat
 WHERE IdMat=@pmIdMat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposMcia @pmIdTmcia VARCHAR(4)
 AS 
 DELETE FROM TiposMcia
 WHERE IdTmcia=@pmIdTmcia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposMnjo @pmIdMnjo VARCHAR(4)
 AS 
 DELETE FROM TiposMnjo
 WHERE IdMnjo=@pmIdMnjo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposMod @pmIdModd VARCHAR(4)
 AS 
 DELETE FROM TiposMod
 WHERE IdModd=@pmIdModd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposMot @pmIdTipoMot VARCHAR(4)
 AS 
 DELETE FROM TiposMot
 WHERE IdTipoMot=@pmIdTipoMot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposNat @pmIdNat VARCHAR(4)
 AS 
 DELETE FROM TiposNat
 WHERE IdNat=@pmIdNat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposNivs @pmIdNiv VARCHAR(4)
 AS 
 DELETE FROM TiposNivs
 WHERE IdNiv=@pmIdNiv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposNom @pmIdNom VARCHAR(4)
 AS 
 DELETE FROM TiposNom
 WHERE IdNom=@pmIdNom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposNov @pmIdNov VARCHAR(4)
 AS 
 DELETE FROM TiposNov
 WHERE IdNov=@pmIdNov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposPcnto @pmIdTipoPre VARCHAR(4)
 AS 
 DELETE FROM TiposPcnto
 WHERE IdTipoPre=@pmIdTipoPre

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposPpt @pmIdPpd VARCHAR(4)
 AS 
 DELETE FROM TiposPpt
 WHERE IdPpd=@pmIdPpd

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paDelTiposTerc @pmIdTipoTerc VARCHAR(4)
 AS 
 DELETE FROM TiposTerc
 WHERE IdTipoTerc=@pmIdTipoTerc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTiposVeh @pmIdTipoVeh VARCHAR(4)
 AS 
 DELETE FROM TiposVeh
 WHERE IdTipoVeh=@pmIdTipoVeh
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelTraslados @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Traslados
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelUOpciones @pmIdUsuario VARCHAR(11),@pmNomOpcion VARCHAR(20)=Null
 AS 
 DELETE FROM adm_UOpciones
 WHERE IdUsuario=@pmIdUsuario AND NomOpcion LIKE ISNULL(@pmNomOpcion,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelUm @pmUndMed VARCHAR(10)
 AS 
 DELETE FROM Sys_Um
 WHERE UndMed=@pmUndMed
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelUndMed @pmIdUnd VARCHAR(4)
 AS 
 DELETE FROM UndMed
 WHERE IdUnd=@pmIdUnd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelUsuarios @pmIdUsuario VARCHAR(11)
AS
BEGIN
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnUsuarios'
  BEGIN TRANSACTION @TrnNombre
  DELETE FROM adm_PermisosRoles WHERE IdUsuario=@pmIdUsuario
  DELETE FROM adm_Permisos WHERE IdUsuario=@pmIdUsuario 
  DELETE FROM  adm_UOpciones  WHERE  IdUsuario=@pmIdUsuario
  DELETE FROM adm_Usuarios WHERE IdUsuario=@pmIdUsuario
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelUsucaja @pmIdUsuario VARCHAR(11),@pmnFormato VARCHAR(10)
 AS 
 DELETE FROM adm_Usucaja
 WHERE IdUsuario=@pmIdUsuario AND nFormato LIKE ISNULL(@pmnFormato,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Vales
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelValesRutas @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_ValesRutas
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehClientes @pmIdVehiculo VARCHAR(10)
 AS 
 DELETE FROM VehClientes
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehConductor @pmIdVehiculo VARCHAR(10),@pmNumero INT
 AS 
 DELETE FROM Trn_VehConductor
 WHERE IdVehiculo=@pmIdVehiculo 
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehLecturas @pmTipoLec VARCHAR(3),@pmNumero INT
 AS 
 DELETE FROM Trn_VehLecturas
 WHERE TipoLec=@pmTipoLec
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehPolizas @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM VehPolizas
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehRemolq @pmIdRemque VARCHAR(10)
 AS 
 DELETE FROM VehRemolq
 WHERE IdRemque=@pmIdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehTraspasos @pmIdVehiculo VARCHAR(10),@pmNumero INT
 AS 
 DELETE FROM Trn_VehTraspasos
 WHERE IdVehiculo=@pmIdVehiculo 
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelVeh_Contrato @pmNumContrato INT
 AS 
 DELETE FROM Veh_Contrato
 WHERE NumContrato=@pmNumContrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelVeh_Cupos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS 
 DELETE FROM Veh_Cupos
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase like ISNULL(@pmIdClase,'%')



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDelVeh_Saldos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 DELETE FROM Veh_Saldos
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase LIKE ISNULL(@pmIdClase,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDelVeh_SaldosAll @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
 IF @pmIdVehiculo IS NULL
  DELETE Veh_Saldos
  FROM Vehiculos AS V INNER JOIN Veh_Saldos  AS S  ON V.IdVehiculo=S.IdVehiculo
  WHERE  IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 ELSE
  DELETE FROM Veh_Saldos  WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVehiculos @pmIdVehiculo VARCHAR(10)
 AS 
 DELETE FROM Vehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVencAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVencAbonosSaf  @pmTipDoc VARCHAR(3),@pmFactura INT
,@pmIdCiaFac CHAR(2),@pmTipoAplica VARCHAR(10)=Null
 AS 
 DELETE FROM Trn_VencAbonos
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND  IdCiaFac=@pmIdCiaFac
 AND TipoAplica LIKE ISNULL(@pmTipoAplica,'%') 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelVencimientos @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 DELETE FROM Trn_Vencimientos
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelZonas @pmIdZona VARCHAR(4)
 AS 
 DELETE FROM Zonas
 WHERE IdZona=@pmIdZona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDelmonedas @pmIdMneda VARCHAR(5)
 AS 
 DELETE FROM adm_monedas
 WHERE IdMneda=@pmIdMneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltasas @pmIdMneda VARCHAR(5),@pmIdMnedaD VARCHAR(5),@pmFecha SMALLDATETIME
 AS 
 DELETE FROM Trn_tasas
 WHERE Fecha=@pmFecha AND IdMnedaD=@pmIdMnedaD AND IdMneda LIKE ISNULL(@pmIdMneda,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AfAmt @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_AfAmt
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AfDep @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_AfDep
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AfIngreso @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_AfIngreso
 WHERE tmNumero=@pmtmNumero  
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AfRetiro @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_AfRetiro
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AfTraslado @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_AfTraslado
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Aportes @pmtmEst  VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null
,@pmtmItem INT=Null
 AS 
 DELETE FROM tm_Aportes
 WHERE tmEst=@pmtmEst AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AutTot @pmtmEst VARCHAR(5),@pmtmItem INT=Null
 AS 
 DELETE FROM tm_AutTot
 WHERE tmEst=@pmtmEst 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_AuxInv @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_AuxInv
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_BalAcum @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalAcum  WHERE tmEst=@pmtmEst 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_BalCom @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalCom
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_BalCompara @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalCompara
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_BalGen @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_BalGen
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Caja @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Caja
 WHERE tmEst=@pmtmEst

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_CajaTot @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_CajaTot
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDeltm_Cargos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_Cargos
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Combo @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_Combo
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_ComboLin @pmtmNumero VARCHAR(5),@pmtmLinea INT
 AS 
 DELETE FROM tm_Combo
 WHERE tmNumero=@pmtmNumero AND tmLinea=@pmtmLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Comisiones @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Comisiones  WHERE tmEst=@pmtmEst 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Comp @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Compau @pmtmNumero VARCHAR(5),@pmtmTipCom VARCHAR(3)
,@pmtmComprobante INT,@pmtmIdCia CHAR(2)
 AS 
 DELETE FROM tm_Compau
 WHERE tmNumero=@pmtmNumero AND tmTipCom LIKE ISNULL(@pmtmTipCom,'%') AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
AND (tmComprobante>=ISNULL(@pmtmComprobante,0)  AND tmComprobante<=ISNULL(@pmtmComprobante,2147483647))



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Contratos @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Contratos WHERE tmEst=@pmtmEst


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paDeltm_Cuotas @pmtmEst CHAR(2),@pmtmPrestamoID INT=Null,@pmtmIdCia CHAR(2)=Null,@pmtmNumCuota INT=Null
AS
 DELETE FROM  tm_Cuotas  
 WHERE tmEst=@pmtmEst
 AND (tmPrestamoID>=ISNULL(@pmtmPrestamoID,0)  AND tmPrestamoID<=ISNULL(@pmtmPrestamoID,2147483647))
 AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 AND (tmNumCuota>=ISNULL(@pmtmNumCuota,0)  AND tmNumCuota<=ISNULL(@pmtmNumCuota,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_CxCobrar @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_CxCobrar
 WHERE tmNumero=@pmtmNumero 
AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_CxPagar @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_CxcAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_CxpAplica @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_DetCue @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmConcepto VARCHAR(10)=Null
 AS 
 DELETE FROM tm_DetCue
 WHERE tmNumero=@pmtmNumero AND tmConcepto LIKE ISNULL(@pmtmConcepto,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Diario @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Diario
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_DifIngreso @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_DifIngreso
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_EdsFormas @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_EstClientes @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_EstClientes WHERE tmEst=@pmtmEst 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_EstadoGyp @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_ForItems @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_InfDiario @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_InfDiario
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Ingresos @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Ingresos  WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_KarUbi @pmtmNumero VARCHAR(5),@pmtmItem INT
,@pmtmLinea INT=Null,@pmtmIdProducto VARCHAR(16)=Null
 AS 
 DELETE FROM tm_KarUbi
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647)) 
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_KarUbiNts @pmtmNumero VARCHAR(5),@pmtmLinea INT=Null
,@pmtmIdProducto VARCHAR(16)=Null
 AS 
 DELETE FROM tm_KarUbi
 WHERE tmNumero=@pmtmNumero AND tmEntradas>0 AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Kdex @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_KdexCom @pmtmNumero VARCHAR(5),@pmtmItemCbo INT
 AS 
 DELETE FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmEsProdBase<>0   
 AND (tmItemCbo>=ISNULL(@pmtmItemCbo,0)  AND tmItemCbo<=ISNULL(@pmtmItemCbo,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_KdexNts @pmtmNumero VARCHAR(5),@pmtmItemCbo INT
 AS 
 DELETE FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmEntradas>0
 AND (tmItemCbo>=ISNULL(@pmtmItemCbo,0)  AND tmItemCbo<=ISNULL(@pmtmItemCbo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_LibOf @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_LibOf
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Liquida @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_ListaPrec @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_ListaPrec
 WHERE tmEst=@pmtmEst  
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Lot @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16)=Null
,@pmtmLinea INT=Null
 AS 
 DELETE FROM tm_Lot
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_LotEnt @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
 AS 
 DELETE FROM tm_Lot
 WHERE tmNumero=@pmtmNumero AND tmEntradas>0 
AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_MovClientes @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovClientes
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_MovCue @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovCue  WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_MovInvent @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovInvent
 WHERE tmEst=@pmtmEst

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_MovTanques @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovTanques
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_MovVentas @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_MovVentas  WHERE tmEst=@pmtmEst 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_NomHoras
 AS 
 DELETE FROM tm_NomHoras
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_NomRes @pmtmNumero VARCHAR(5)
 AS 
 DELETE FROM tm_NomRes
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Nomina @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT
 AS 
 DELETE FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_NominaApo @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 DELETE FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmClaseLiq IN ('APORTE','PROVISION')
 AND tmItem>=1000  AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_NominaCon @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmIdConcepto VARCHAR(4),@pmtmClaseLiq VARCHAR(10)=Null
 AS 
 DELETE FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmIdConcepto=@pmtmIdConcepto
 AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%') AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_NominaNom @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmNContto INT
 AS 
 DELETE FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmClaseLiq<>'NOVEDAD'
 AND  tmIdEmpleado=@pmtmIdEmpleado  AND tmNContto=@pmtmNContto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_OccComp @pmtmTipDoc VARCHAR(3),@pmtmOCargue INT,@pmtmIdCia CHAR(2)
 AS 
 DELETE FROM tm_OccComp
 WHERE tmTipDoc=@pmtmTipDoc AND tmOCargue=@pmtmOCargue AND tmIdCia=@pmtmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Pagos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Plasueldos @pmtmIdPeriodo VARCHAR(8),@pmtmKeyRegistro VARCHAR(30)
 AS 
 DELETE FROM tm_Plasueldos
 WHERE tmIdPeriodo LIKE ISNULL(@pmtmIdPeriodo,'%')
 AND tmKeyRegistro LIKE ISNULL(@pmtmKeyRegistro,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDeltm_RecAplica @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_RecAplica
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_ReqDetalle @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Saldos @pmtmEst CHAR(2)
 AS 
 DELETE FROM tm_Saldos  WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Ser @pmtmNumero VARCHAR(5),@pmtmItem INT
,@pmtmIdProducto VARCHAR(16)=Null,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 DELETE FROM tm_Ser
 WHERE tmNumero=@pmtmNumero   AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_ValesCred @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_ValesCred
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_Ventas @pmtmEst CHAR(2)
 AS
 DELETE FROM tm_VentasRes  WHERE tmEst=@pmtmEst
 DELETE FROM tm_VentasDet  WHERE tmEst=@pmtmEst
 DELETE FROM tm_VentasCom  WHERE tmEst=@pmtmEst
 DELETE FROM tm_Ventas  WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paDeltm_cheq @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 DELETE FROM tm_cheq
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paDeltm_vce
 AS 
 DELETE FROM tm_vce


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsActivos @pmIdActivo VARCHAR(30),@pmNomActivo VARCHAR(250),@pmNumSerie VARCHAR(30),@pmCodInvent VARCHAR(16),@pmCodBarras VARCHAR(20),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdInstala VARCHAR(4),@pmIdDep VARCHAR(4),@pmIdRespons VARCHAR(16),@pmIdCia CHAR(2),@pmIdProv VARCHAR(16),@pmFecCompra SMALLDATETIME
,@pmFecActivacion SMALLDATETIME,@pmNumOrden INT,@pmIdCiaOrden CHAR(2),@pmNumEntrada INT,@pmIdCiaEnt CHAR(2),@pmVrCompra MONEY,@pmVrComercial MONEY,@pmVrBaseDep MONEY,@pmVrMejoras MONEY,@pmVrDisminucion MONEY,@pmVidaUtil INT,@pmFecFinDep SMALLDATETIME,@pmMetodoDep VARCHAR(10),@pmAjustable BIT,@pmContable BIT,@pmCueEnGrupo BIT,@pmVrAjuInfAcum MONEY
,@pmVrDepAcuHis MONEY,@pmVrDepAcuAju MONEY,@pmVrAjuInfDep MONEY,@pmNumMesesDep INT,@pmFecUltDep SMALLDATETIME,@pmMarca VARCHAR(30),@pmModelo VARCHAR(10),@pmNumPlaca VARCHAR(20),@pmNumDoc VARCHAR(20),@pmReferencia VARCHAR(50),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME,@pmIdCausal VARCHAR(4),@pmVrVenta MONEY,@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Activos (IdActivo,NomActivo,NumSerie,CodInvent,CodBarras,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,IdCia,IdProv,FecCompra,FecActivacion,NumOrden,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep,VrMejoras,VrDisminucion,VidaUtil,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,IdEstado,FecRetiro,IdCausal,VrVenta,FecCreacion,IdUsuario)
 VALUES (@pmIdActivo,@pmNomActivo,@pmNumSerie,@pmCodInvent,@pmCodBarras,@pmIdGrupo,@pmIdCCosto,@pmIdSubCos,@pmIdInstala,@pmIdDep,@pmIdRespons,@pmIdCia,@pmIdProv,@pmFecCompra,@pmFecActivacion,@pmNumOrden,@pmIdCiaOrden,@pmNumEntrada,@pmIdCiaEnt,@pmVrCompra,@pmVrComercial,@pmVrBaseDep,@pmVrMejoras,@pmVrDisminucion,@pmVidaUtil,@pmFecFinDep,@pmMetodoDep,@pmAjustable,@pmContable,@pmCueEnGrupo,@pmVrAjuInfAcum,@pmVrDepAcuHis
,@pmVrDepAcuAju,@pmVrAjuInfDep,@pmNumMesesDep,@pmFecUltDep,@pmMarca,@pmModelo,@pmNumPlaca,@pmNumDoc,@pmReferencia,@pmIdEstado,@pmFecRetiro,@pmIdCausal,@pmVrVenta,@pmFecCreacion,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsActivos_Anexo @pmIdActivo VARCHAR(30),@pmIdCuenta VARCHAR(16),@pmIdCueDep VARCHAR(16),@pmIdCueGasto VARCHAR(16),@pmIdCueAjuste VARCHAR(16),@pmIdCueCorr VARCHAR(16),@pmIdCueAjuDep VARCHAR(16),@pmIdCueCorrAD VARCHAR(16),@pmNumPoliza VARCHAR(30),@pmNitCiaAseg VARCHAR(16),@pmFecPoliza SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmVrAsegurado MONEY,@pmVrCuotaMes MONEY,@pmVendPoliza VARCHAR(150),@pmTeleVend VARCHAR(30),@pmDiasGarantia INT,@pmComentGarantia VARCHAR(250),@pmPignorado BIT
,@pmFecIniPignor SMALLDATETIME,@pmFecFinPignor SMALLDATETIME,@pmGarantPignor VARCHAR(150),@pmMantenimiento BIT,@pmNumContrato VARCHAR(20),@pmFecIniCont SMALLDATETIME,@pmFecFinCont SMALLDATETIME,@pmPeriodicidad INT,@pmFecUltMnmto SMALLDATETIME,@pmNitEmpMnmto VARCHAR(16),@pmComentMnmto VARCHAR(250),@pmDesUbicacion VARCHAR(100),@pmComentarios VARCHAR(250),@pmComentRetiro VARCHAR(250),@pmDatosTecnicos VARCHAR(4000)
 AS 
 INSERT INTO Activos_Anexo (IdActivo,IdCuenta,IdCueDep,IdCueGasto,IdCueAjuste,IdCueCorr,IdCueAjuDep,IdCueCorrAD,NumPoliza,NitCiaAseg,FecPoliza,FecVigencia,VrAsegurado,VrCuotaMes,VendPoliza,TeleVend,DiasGarantia,ComentGarantia,Pignorado,FecIniPignor,FecFinPignor,GarantPignor,Mantenimiento,NumContrato,FecIniCont,FecFinCont,Periodicidad,FecUltMnmto,NitEmpMnmto,ComentMnmto,DesUbicacion,Comentarios,ComentRetiro,DatosTecnicos)
 VALUES (@pmIdActivo,@pmIdCuenta,@pmIdCueDep,@pmIdCueGasto,@pmIdCueAjuste,@pmIdCueCorr,@pmIdCueAjuDep,@pmIdCueCorrAD,@pmNumPoliza,@pmNitCiaAseg,@pmFecPoliza,@pmFecVigencia,@pmVrAsegurado,@pmVrCuotaMes,@pmVendPoliza,@pmTeleVend,@pmDiasGarantia,@pmComentGarantia,@pmPignorado,@pmFecIniPignor,@pmFecFinPignor,@pmGarantPignor,@pmMantenimiento,@pmNumContrato,@pmFecIniCont,@pmFecFinCont,@pmPeriodicidad,@pmFecUltMnmto,@pmNitEmpMnmto,@pmComentMnmto,@pmDesUbicacion,@pmComentarios,@pmComentRetiro,@pmDatosTecnicos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCaja @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11),@pmSaldoAnt MONEY
,@pmFacturas MONEY,@pmDevFacturas MONEY,@pmPlanillas MONEY,@pmDevPlanillas MONEY,@pmRecibos MONEY,@pmDevRecibos MONEY
,@pmIngCaja MONEY,@pmDevIngCaja MONEY,@pmEgrCaja MONEY,@pmDevEgrCaja MONEY,@pmConsignac MONEY,@pmDevConsig MONEY
,@pmNotasCalibra MONEY,@pmDevCalibra MONEY,@pmOtrosIng MONEY,@pmOtrosEgr MONEY
 AS 
 INSERT INTO AcuCaja (FechaDia,NumCorte,IdCia,IdUsuario,SaldoAnt,Facturas,DevFacturas,Planillas,DevPlanillas,Recibos,DevRecibos,IngCaja,DevIngCaja
,EgrCaja,DevEgrCaja,Consignac,DevConsig,NotasCalibra,DevCalibra,OtrosIng,OtrosEgr)
 VALUES (@pmFechaDia,@pmNumCorte,@pmIdCia,@pmIdUsuario,@pmSaldoAnt,@pmFacturas,@pmDevFacturas,@pmPlanillas
,@pmDevPlanillas,@pmRecibos,@pmDevRecibos,@pmIngCaja,@pmDevIngCaja,@pmEgrCaja,@pmDevEgrCaja,@pmConsignac,@pmDevConsig,@pmNotasCalibra,@pmDevCalibra,@pmOtrosIng,@pmOtrosEgr)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsAcuCajaBuses @pmIdUsuario VARCHAR(11),@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmSaldoAnterior MONEY
, @pmTotalDebitos MONEY, @pmTotalCreditos MONEY, @pmNuevoSaldo  MONEY

AS
  INSERT INTO  AcuCajaBuses  (IdUsuario, IdCia, Fecha, SaldoAnterior, TotalDebitos, TotalCreditos, NuevoSaldo )
  VALUES (@pmIdUsuario,@pmIdCia,@pmFecha,@pmSaldoAnterior, @pmTotalDebitos, @pmTotalCreditos, @pmNuevoSaldo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCajaFco @pmFechaDia SMALLDATETIME,@pmNumCorte INT
,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO AcuCaja (FechaDia,NumCorte,IdCia,IdUsuario,Facturas)
 SELECT Fecha,@pmNumCorte,IdCia,IdUsuario,SUM(VrPagado-VrCambio)
 FROM Trn_Pagos
 WHERE TipDoc IN ('FCO','FO1','FO2','FO3','FO4','FO5')  AND Fecha=@pmFechaDia AND EsCaja<>0 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY Fecha,IdCia,IdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuClientes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmSaldoAnt MONEY,@pmFacturas MONEY,@pmDevFacturas MONEY,@pmRecibos MONEY,@pmDevRecibos MONEY,@pmNotasDeb MONEY,@pmNotasCre MONEY
 AS 
 INSERT INTO AcuClientes (nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Facturas,DevFacturas,Recibos,DevRecibos,NotasDeb,NotasCre)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCliente,@pmIdAgencia,@pmSaldoAnt,@pmFacturas,@pmDevFacturas,@pmRecibos,@pmDevRecibos,@pmNotasDeb,@pmNotasCre)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuClientesFcr @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 INSERT INTO AcuClientes (nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Facturas,DevFacturas,Recibos,DevRecibos,NotasDeb,NotasCre)
 SELECT YEAR(Fecha),MONTH(Fecha),IdCia,IdCliente,IdAgencia,0,SUM(VrFactura),0,0,0,0,0
 FROM Trn_Vencimientos
 WHERE TipDoc<>'NDB'  AND YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY YEAR(Fecha),MONTH(Fecha),IdCia,IdCliente,IdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuClientes_Uni @pmIdCliente VARCHAR(16),@pmIdClienteDos VARCHAR(16),@pmNewCliente VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCliente FROM AcuClientes WHERE IdCliente=@pmIdCliente OR IdCliente=@pmIdClienteDos)
  INSERT INTO AcuClientes (nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Facturas,DevFacturas,Recibos,DevRecibos,NotasDeb,NotasCre)
  SELECT  nAnno,nMes,IdCia,@pmNewCliente,IdAgencia,SUM(SaldoAnt),SUM(Facturas),SUM(DevFacturas),SUM(Recibos),SUM(DevRecibos),SUM(NotasDeb),SUM(NotasCre)
  FROM AcuClientes WHERE IdCliente=@pmIdCliente OR IdCliente=@pmIdClienteDos
  GROUP BY nAnno,nMes,IdCia,IdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentas @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasAge @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasAge_Sel  @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 --nit-veh
  INSERT INTO AcuCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(Fecha),@pmnMes,IdCia,D.IdCuenta,IdTercero,IdVehiculo,'NV',0,SUM(VrDebito),SUM(VrCredito)
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Tercero<>0 AND Vehiculo<>0
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta,IdTercero,IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasAge_Unf  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuCuentasAge WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdTercero,CodAgncia,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
 FROM AcuCuentasAge WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
 GROUP BY  nAnno,nMes,IdCia,IdTercero,CodAgncia,TipoAcum
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasAge_Uni @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdTercero FROM AcuCuentasAge WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO AcuCuentasAge (nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewTercero,CodAgncia,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
 FROM AcuCuentasAge WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
 GROUP BY nAnno,nMes,IdCia,IdCuenta,CodAgncia,TipoAcum
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasAux @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmnReg INT,@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmSubAuxiliar VARCHAR(16),@pmTipoAcum VARCHAR(4),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuCuentasAux (nAnno,nMes,IdCia,nReg,IdCuenta,IdTercero,CodAgncia,IdCCosto,IdSubCos,SubAuxiliar,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmnReg,@pmIdCuenta,@pmIdTercero,@pmCodAgncia,@pmIdCCosto,@pmIdSubCos
,@pmSubAuxiliar,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasCc @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos
,@pmTipoAcum,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasCc_Sel @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SET @TrnNombre = 'AcuCuentasCc'
  BEGIN TRANSACTION @TrnNombre
  INSERT INTO AcuCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(Fecha),@pmnMes,IdCia,D.IdCuenta,IdCCosto,'-1','CC',0,SUM(VrDebito),SUM(VrCredito)
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND CentroCosto<>0 
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta,IdCCosto
 --centro de costo-subcentro
  INSERT INTO AcuCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT YEAR(Fecha),@pmnMes,IdCia,D.IdCuenta,IdCCosto,IdSubCos,'CS',0,SUM(VrDebito),SUM(VrCredito)
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND CentroCosto<>0 AND SubCentro<>0
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta,IdCCosto,IdSubCos
 COMMIT TRANSACTION  @TrnNombre
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasCc_Uni  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuCuentasCc WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuCuentasCc (nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdCCosto,IdSubCos,TipoAcum,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuCuentasCc WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia,IdCCosto,IdSubCos,TipoAcum 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasNit @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdTercero,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasNit_Sel @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(Fecha),@pmnMes,IdCia,D.IdCuenta,IdTercero,0,SUM(VrDebito),SUM(VrCredito)
 FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Tercero<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta,IdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasNit_Unf  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuCuentasNit WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdTercero,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuCuentasNit WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia,IdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasNit_Uni @pmIdTercero VARCHAR(16),@pmIdTerceroDos VARCHAR(16),@pmNewTercero VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdTercero FROM AcuCuentasNit WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos)
  INSERT INTO AcuCuentasNit (nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,IdCuenta,@pmNewTercero,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuCuentasNit WHERE IdTercero=@pmIdTercero OR IdTercero=@pmIdTerceroDos
  GROUP BY nAnno,nMes,IdCia,IdCuenta 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasVeh @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
 AS 
 INSERT INTO AcuCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCuenta,@pmIdVehiculo,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasVeh_Sel @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(Fecha),@pmnMes,IdCia,D.IdCuenta,IdVehiculo,0,SUM(VrDebito),SUM(VrCredito)
 FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Vehiculo<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta,IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentasVeh_Uni  @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuCuentasVeh WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
   INSERT INTO AcuCuentasVeh (nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos)
   SELECT nAnno,nMes,IdCia,@pmNewCuenta,IdVehiculo,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
   FROM AcuCuentasVeh WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
   GROUP BY nAnno,nMes,IdCia,IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentas_Sel @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
 INSERT INTO AcuCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
 SELECT YEAR(Fecha),@pmnMes,IdCia,D.IdCuenta,0,SUM(VrDebito),SUM(VrCredito)
 FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
 GROUP BY  YEAR(Fecha),IdCia,D.IdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuCuentas_Uni @pmIdCuenta VARCHAR(16),@pmIdCuentaDos VARCHAR(16),@pmNewCuenta VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdCuenta FROM AcuCuentas WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos)
  INSERT INTO AcuCuentas (nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos)
  SELECT nAnno,nMes,IdCia,@pmNewCuenta,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  FROM AcuCuentas
  WHERE IdCuenta=@pmIdCuenta OR IdCuenta=@pmIdCuentaDos
  GROUP BY nAnno,nMes,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInvLotes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4)
,@pmNumLote VARCHAR(30),@pmSaldoAnt DECIMAL(14,4),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4),@pmCostoAnt MONEY,@pmCostoEnt MONEY,@pmCostoSal MONEY
 AS 
 INSERT INTO AcuInvLotes (nAnno,nMes,IdCia,IdProducto,IdBodega,NumLote,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdProducto,@pmIdBodega,@pmNumLote,@pmSaldoAnt,@pmEntradas,@pmSalidas,@pmCostoAnt,@pmCostoEnt,@pmCostoSal)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInvLotesKar @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 INSERT INTO AcuInvLotes (nAnno,nMes,IdCia,IdProducto,IdBodega,NumLote,Entradas,Salidas,CostoEnt,CostoSal,SaldoAnt,CostoAnt)
 SELECT @pmnAnno,@pmnMes,IdCia,L.IdProducto,L.IdBodega,NumLote,SUM(Entradas),SUM(Salidas),0,0,0,0
 FROM Trn_LotMov AS L INNER JOIN ProdMcias AS P ON L.IdProducto=P.IdProducto
 WHERE  YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes AND TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
 AND TipoRef<>'SERVICIO' AND Lotes<>0  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND L.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,L.IdProducto,L.IdBodega,NumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInvTanques @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16),@pmIdTanque VARCHAR(4)
,@pmSaldoAnt DECIMAL(14,4),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4),@pmCostoAnt MONEY,@pmCostoEnt MONEY,@pmCostoSal MONEY
 AS 
 INSERT INTO AcuInvTanques (nAnno,nMes,IdCia,IdProducto,IdTanque,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdProducto,@pmIdTanque,@pmSaldoAnt,@pmEntradas,@pmSalidas,@pmCostoAnt,@pmCostoEnt,@pmCostoSal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInvTanquesKar @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 INSERT INTO AcuInvTanques (nAnno,nMes,IdCia,IdProducto,IdTanque,Entradas,Salidas,CostoEnt,CostoSal,SaldoAnt,CostoAnt)
 SELECT @pmnAnno,@pmnMes,IdCia,K.IdProducto,CASE WHEN CdTanque IS NULL THEN '0' WHEN LEN(CdTanque)<1 THEN '0' ELSE CdTanque END
,SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal),0,0
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE  YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes AND TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
 AND TipoRef<>'SERVICIO' AND Tanques<>0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,K.IdProducto,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInvUbica @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdUbic VARCHAR(10),@pmSaldoAnt DECIMAL(14,4),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4)
,@pmCostoAnt MONEY,@pmCostoEnt MONEY,@pmCostoSal MONEY
 AS 
 INSERT INTO AcuInvUbica (nAnno,nMes,IdCia,IdProducto,IdUbic,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdProducto,@pmIdUbic,@pmSaldoAnt,@pmEntradas,@pmSalidas
,@pmCostoAnt,@pmCostoEnt,@pmCostoSal)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInvUbicaKar @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 INSERT INTO AcuInvUbica (nAnno,nMes,IdCia,IdProducto,IdUbic,Entradas,Salidas,CostoEnt,CostoSal,SaldoAnt,CostoAnt)
 SELECT @pmnAnno,@pmnMes,IdCia,U.IdProducto,U.IdUbic,SUM(Entradas),SUM(Salidas),0,0,0,0
 FROM  Trn_KarUbic AS U INNER JOIN ProdMcias AS P ON U.IdProducto=P.IdProducto
 WHERE  YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes AND TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
 AND TipoRef<>'SERVICIO' AND Tanques=0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND U.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,U.IdProducto,U.IdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInventario @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmSaldoAnt DECIMAL(14,4),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4)
,@pmCostoAnt MONEY,@pmCostoEnt MONEY,@pmCostoSal MONEY
 AS 
 INSERT INTO AcuInventario (nAnno,nMes,IdCia,IdProducto,IdBodega,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdProducto,@pmIdBodega,@pmSaldoAnt,@pmEntradas,@pmSalidas
,@pmCostoAnt,@pmCostoEnt,@pmCostoSal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuInventarioKar @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 INSERT INTO AcuInventario (nAnno,nMes,IdCia,IdProducto,IdBodega,Entradas,Salidas,CostoEnt,CostoSal,SaldoAnt,CostoAnt)
 SELECT @pmnAnno,@pmnMes,IdCia,K.IdProducto,K.IdBodega,SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal),0,0
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE  YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes AND TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
 AND TipoRef<>'SERVICIO' AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdCia,K.IdProducto,K.IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuPrestamos @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmSaldoAnt MONEY
,@pmPrestamos MONEY,@pmDevPrestamos MONEY,@pmRecibos MONEY,@pmDevRecibos MONEY,@pmNotasDeb MONEY,@pmNotasCre MONEY
,@pmFacturas MONEY,@pmDevFacturas MONEY
 AS 
 INSERT INTO AcuPrestamos (nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Prestamos,DevPrestamos,Recibos,DevRecibos,NotasDeb,NotasCre,Facturas,DevFacturas)
 VALUES (@pmnAnno,@pmnMes,@pmIdCia,@pmIdCliente,@pmIdAgencia,@pmSaldoAnt,@pmPrestamos,@pmDevPrestamos,@pmRecibos,@pmDevRecibos,@pmNotasDeb,@pmNotasCre,@pmFacturas,@pmDevFacturas)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAcuPrestamosCau  @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 INSERT INTO AcuPrestamos (nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Prestamos,DevPrestamos,Recibos,DevRecibos,NotasDeb,NotasCre,Facturas,DevFacturas)
 SELECT YEAR(Fecha),MONTH(Fecha),IdCia,IdCliente,IdAgencia,0,SUM(CASE TipDoc WHEN 'PR1' THEN VrTotal ELSE 0 END),0,0,0
 ,SUM(CASE TipDoc WHEN 'ND2' THEN VrTotal ELSE 0 END),0,SUM(CASE TipDoc WHEN 'FIF' THEN VrTotal ELSE 0 END),0
 FROM Trn_FinCuotas
 WHERE TipDoc IN ('PR1','ND2','FIF')  AND YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY YEAR(Fecha),MONTH(Fecha),IdCia,IdCliente,IdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paInsAcuPropietarios @pmIdReg INT,@pmIdPropietario VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4), @pmIdCia CHAR(2)
,@pmnMes INT, @pmnAnno INT,@pmSaldoAnterior MONEY, @pmTotalDebitos MONEY, @pmTotalCreditos MONEY
AS
 INSERT INTO AcuPropietarios (nAnno,IdReg,nMes,IdPropietario,IdVehiculo,IdClase,IdCia,SaldoAnterior,TotalDebitos,TotalCreditos)
 VALUES (@pmnAnno,@pmIdReg,@pmnMes,@pmIdPropietario,@pmIdVehiculo,@pmIdClase,@pmIdCia,@pmSaldoAnterior
,@pmTotalDebitos,@pmTotalCreditos)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfAmortiza @pmNumero INT,@pmFecha SMALLDATETIME,@pmEstado INT
,@pmIdUsuario VARCHAR(11),@pmFechaCrea SMALLDATETIME,@pmIdCia CHAR(2)
 AS 
 INSERT INTO Trn_AfAmortiza (Numero,Fecha,Estado,IdUsuario,FechaCrea,IdCia)
 VALUES (@pmNumero,@pmFecha,@pmEstado,@pmIdUsuario,@pmFechaCrea,@pmIdCia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfAmtComp @pmNumero INT,@pmIdCia CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT
 AS 
 INSERT INTO Trn_AfAmtComp (Numero,IdCia,TipCom,Comprobante)
 VALUES (@pmNumero,@pmIdCia,@pmTipCom,@pmComprobante)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfAmtDetalle @pmNumero INT,@pmItem INT,@pmIdDiferido VARCHAR(30),@pmValorTotal MONEY,@pmVrAdiciones MONEY
,@pmVrDisminucion MONEY,@pmVrAcuAjustes MONEY,@pmVrAmortizaHis MONEY,@pmVrAmortizaMes MONEY,@pmVrAjustesInf MONEY
,@pmVrAjustesAmtza MONEY,@pmVrAmtzaHisAcum MONEY,@pmVrAmortizaAcum MONEY,@pmVrAjuAmtzaAcum MONEY,@pmVrCorrMonAcum MONEY
,@pmVrBaseCorrMon MONEY,@pmVrCorrMonDif MONEY,@pmTarifaAju DECIMAL(14,4),@pmIdCia CHAR(2),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16)
,@pmCdDep VARCHAR(4),@pmNitRespons VARCHAR(16),@pmFecInicioAmtza SMALLDATETIME,@pmNPeriodos INT,@pmCuotaGal DECIMAL(14,4),@pmVentaGal DECIMAL(14,4),@pmVrInversion MONEY
 AS 
 INSERT INTO Trn_AfAmtDetalle (Numero,Item,IdDiferido,ValorTotal,VrAdiciones,VrDisminucion,VrAcuAjustes,VrAmortizaHis,VrAmortizaMes,VrAjustesInf,VrAjustesAmtza
,VrAmtzaHisAcum,VrAmortizaAcum,VrAjuAmtzaAcum,VrCorrMonAcum,VrBaseCorrMon,VrCorrMonDif,TarifaAju,IdCia,CdCCosto,CdSubCos,CdDep,NitRespons,FecInicioAmtza,NPeriodos,CuotaGal,VentaGal,VrInversion)
 VALUES (@pmNumero,@pmItem,@pmIdDiferido,@pmValorTotal,@pmVrAdiciones,@pmVrDisminucion,@pmVrAcuAjustes,@pmVrAmortizaHis,@pmVrAmortizaMes
,@pmVrAjustesInf,@pmVrAjustesAmtza,@pmVrAmtzaHisAcum,@pmVrAmortizaAcum,@pmVrAjuAmtzaAcum,@pmVrCorrMonAcum,@pmVrBaseCorrMon,@pmVrCorrMonDif,@pmTarifaAju
,@pmIdCia,@pmCdCCosto,@pmCdSubCos,@pmCdDep,@pmNitRespons,@pmFecInicioAmtza,@pmNPeriodos,@pmCuotaGal,@pmVentaGal ,@pmVrInversion)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfCausales @pmIdCausal VARCHAR(4),@pmCausal VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO AfCausales (IdCausal,Causal,Inactivo)
 VALUES (@pmIdCausal,@pmCausal,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfClasesCue @pmIdClase VARCHAR(4),@pmClaseCue VARCHAR(50),@pmConcepto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO AfClasesCue (IdClase,ClaseCue,Concepto,Inactivo)
 VALUES (@pmIdClase,@pmClaseCue,@pmConcepto,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDeprecCom @pmNumero INT,@pmIdCia CHAR(2),@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmTipComAju VARCHAR(3),@pmCompAjustes INT
 AS 
 INSERT INTO Trn_AfDeprecCom (Numero,IdCia,TipCom,Comprobante,TipComAju,CompAjustes)
 VALUES (@pmNumero,@pmIdCia,@pmTipCom,@pmComprobante,@pmTipComAju,@pmCompAjustes)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDeprecia @pmNumero INT,@pmFecha SMALLDATETIME
,@pmEstado INT,@pmIdUsuario VARCHAR(11),@pmFechaCrea SMALLDATETIME,@pmIdCia CHAR(2)
 AS 
 INSERT INTO Trn_AfDeprecia (Numero,Fecha,Estado,IdUsuario,FechaCrea,IdCia)
 VALUES (@pmNumero,@pmFecha,@pmEstado,@pmIdUsuario,@pmFechaCrea,@pmIdCia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDetalle @pmNumero INT,@pmItem INT,@pmIdActivo VARCHAR(30),@pmVrCostoIni MONEY,@pmVrMejoras MONEY,@pmVrDisminucion MONEY,@pmVrAcuAjustes MONEY,@pmVrDepreciaHis MONEY,@pmVrDepreciaMes MONEY
,@pmVrAjustesInf MONEY,@pmVrAjustesDep MONEY,@pmVrDepreciaAcum MONEY,@pmVrAjuDepAcum MONEY,@pmTarifaAju DECIMAL(14,4),@pmIdCia CHAR(2),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmCdInstala VARCHAR(4)
,@pmCdDep VARCHAR(4),@pmNitRespons VARCHAR(16),@pmFecIniDep SMALLDATETIME,@pmMesesVida INT,@pmMetodoDeprec VARCHAR(10)
 AS 
 INSERT INTO Trn_AfDetalle (Numero,Item,IdActivo,VrCostoIni,VrMejoras,VrDisminucion,VrAcuAjustes,VrDepreciaHis,VrDepreciaMes,VrAjustesInf,VrAjustesDep,VrDepreciaAcum,VrAjuDepAcum,TarifaAju,IdCia,CdCCosto,CdSubCos,CdInstala,CdDep,NitRespons
,FecIniDep,MesesVida,MetodoDeprec)
 VALUES (@pmNumero,@pmItem,@pmIdActivo,@pmVrCostoIni,@pmVrMejoras,@pmVrDisminucion,@pmVrAcuAjustes,@pmVrDepreciaHis,@pmVrDepreciaMes,@pmVrAjustesInf,@pmVrAjustesDep,@pmVrDepreciaAcum,@pmVrAjuDepAcum
,@pmTarifaAju,@pmIdCia,@pmCdCCosto,@pmCdSubCos,@pmCdInstala,@pmCdDep,@pmNitRespons,@pmFecIniDep,@pmMesesVida,@pmMetodoDeprec)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDifIngreso @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdDiferido VARCHAR(30)
,@pmFecActivacion SMALLDATETIME,@pmNumPeriodos INT,@pmVrBase MONEY,@pmVrMejoras MONEY,@pmVrDisminucion MONEY
,@pmVrAjuInfAcum MONEY,@pmVrAmortizaHis MONEY,@pmVrAmortizaAju MONEY,@pmVrAjuInfAmtza MONEY,@pmNumMesesAmtza INT
,@pmVrCorrMonDif MONEY,@pmNumMesesCorr INT,@pmFecUltAmtza SMALLDATETIME,@pmConcepto VARCHAR(150)
 AS 
 INSERT INTO Trn_AfDifIngreso (IdMov,Numero,IdCia,Item,IdDiferido,FecActivacion,NumPeriodos,VrBase,VrMejoras,VrDisminucion
,VrAjuInfAcum,VrAmortizaHis,VrAmortizaAju,VrAjuInfAmtza,NumMesesAmtza,VrCorrMonDif,NumMesesCorr,FecUltAmtza,Concepto)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdDiferido,@pmFecActivacion,@pmNumPeriodos,@pmVrBase
,@pmVrMejoras,@pmVrDisminucion,@pmVrAjuInfAcum,@pmVrAmortizaHis,@pmVrAmortizaAju,@pmVrAjuInfAmtza,@pmNumMesesAmtza
,@pmVrCorrMonDif,@pmNumMesesCorr,@pmFecUltAmtza,@pmConcepto)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDifMovi @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME
,@pmAnulado BIT,@pmObservacion VARCHAR(250),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_AfDifMovi (IdMov,Numero,IdCia,Fecha,Anulado,Observacion,TipCom,Comprobante,IdCiaCom,FecCreacion,IdUsuario)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmFecha,@pmAnulado,@pmObservacion,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmFecCreacion,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDifRetiro @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdDiferido VARCHAR(30),@pmIdCausal VARCHAR(4)
,@pmVrCostoIni MONEY,@pmVrAdiciones MONEY,@pmVrDisminucion MONEY,@pmVrAcuAjustes MONEY,@pmVrAmortizaAcum MONEY,@pmVrAjuAmtzaAcum MONEY
,@pmFechaRet SMALLDATETIME,@pmConcepto VARCHAR(250)
 AS 
 INSERT INTO Trn_AfDifRetiro (IdMov,Numero,IdCia,Item,IdDiferido,IdCausal,VrCostoIni,VrAdiciones,VrDisminucion,VrAcuAjustes,VrAmortizaAcum,VrAjuAmtzaAcum,FechaRet,Concepto)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdDiferido,@pmIdCausal,@pmVrCostoIni,@pmVrAdiciones,@pmVrDisminucion,@pmVrAcuAjustes,@pmVrAmortizaAcum,@pmVrAjuAmtzaAcum,@pmFechaRet,@pmConcepto)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDifTraslado @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdDiferido VARCHAR(30),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmIdDep VARCHAR(4),@pmIdRespons VARCHAR(16),@pmUbicacion VARCHAR(150),@pmCdCiaDest CHAR(2),@pmCdGrupoDest VARCHAR(4),@pmCdCCostoDest VARCHAR(16)
,@pmCdSubCosDest VARCHAR(16),@pmCdDepDest VARCHAR(4),@pmCdResponsDest VARCHAR(16),@pmUbicacionDest VARCHAR(150),@pmConcepto VARCHAR(150)
 AS 
 INSERT INTO Trn_AfDifTraslado (IdMov,Numero,IdCia,Item,IdDiferido,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,Ubicacion,CdCiaDest,CdGrupoDest,CdCCostoDest,CdSubCosDest,CdDepDest,CdResponsDest,UbicacionDest,Concepto)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdDiferido,@pmIdGrupo,@pmIdCCosto,@pmIdSubCos,@pmIdDep,@pmIdRespons,@pmUbicacion,@pmCdCiaDest,@pmCdGrupoDest,@pmCdCCostoDest
,@pmCdSubCosDest,@pmCdDepDest,@pmCdResponsDest,@pmUbicacionDest,@pmConcepto)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfDifValoriza @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT
,@pmIdDiferido VARCHAR(30),@pmConcepto VARCHAR(250),@pmValor MONEY,@pmNitProvee VARCHAR(16)
 AS 
 INSERT INTO Trn_AfDifValoriza (IdMov,Numero,IdCia,Item,IdDiferido,Concepto,Valor,NitProvee)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdDiferido,@pmConcepto,@pmValor,@pmNitProvee)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfEstados @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO AfEstados (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfGrupos @pmIdGrupo VARCHAR(4),@pmGrupo VARCHAR(50),@pmIdLinea VARCHAR(4),@pmVidaUtil INT,@pmInactivo BIT
 AS 
 INSERT INTO AfGrupos (IdGrupo,Grupo,IdLinea,VidaUtil,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupo,@pmIdLinea,@pmVidaUtil,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfGruposCue @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16)
 AS 
 INSERT INTO AfGruposCue (IdGrupo,IdClase,IdCuenta)
 VALUES (@pmIdGrupo,@pmIdClase,@pmIdCuenta)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfLineas @pmIdLinea VARCHAR(4),@pmLinea VARCHAR(50),@pmVidaUtil INT,@pmTarfDep DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO AfLineas (IdLinea,Linea,VidaUtil,TarfDep,Inactivo)
 VALUES (@pmIdLinea,@pmLinea,@pmVidaUtil,@pmTarfDep,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfMantnmto @pmIdCia CHAR(2),@pmIdActivo VARCHAR(30),@pmFecha SMALLDATETIME,@pmNumContrato VARCHAR(20)
,@pmNitEmpresa VARCHAR(16),@pmNomEmpresa VARCHAR(150),@pmFinContrato BIT,@pmIdUsuario VARCHAR(11),@pmDiagnostico VARCHAR(2000),@pmComentarios VARCHAR(4000)
 AS 
 INSERT INTO Trn_AfMantnmto (IdCia,IdActivo,Fecha,NumContrato,NitEmpresa,NomEmpresa,FinContrato,IdUsuario,Diagnostico,Comentarios)
 VALUES (@pmIdCia,@pmIdActivo,@pmFecha,@pmNumContrato,@pmNitEmpresa,@pmNomEmpresa,@pmFinContrato,@pmIdUsuario,@pmDiagnostico,@pmComentarios)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfMovIngreso @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdActivo VARCHAR(30)
,@pmFecActivacion SMALLDATETIME,@pmVrMejoras MONEY,@pmVrDisminucion MONEY,@pmVrAjuInfAcum MONEY,@pmVrDepAcuHis MONEY
,@pmVrDepAcuAju MONEY,@pmVrAjuInfDep MONEY,@pmNumMesesDep INT,@pmFecUltDep SMALLDATETIME,@pmConcepto VARCHAR(150)
 AS 
 INSERT INTO Trn_AfMovIngreso (IdMov,Numero,IdCia,Item,IdActivo,FecActivacion,VrMejoras,VrDisminucion,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep,FecUltDep,Concepto)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmFecActivacion,@pmVrMejoras,@pmVrDisminucion,@pmVrAjuInfAcum,@pmVrDepAcuHis,@pmVrDepAcuAju
,@pmVrAjuInfDep,@pmNumMesesDep,@pmFecUltDep,@pmConcepto)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfMovRetiro @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdActivo VARCHAR(30),@pmIdCausal VARCHAR(4)
,@pmNitTercero VARCHAR(16),@pmNomTercero VARCHAR(150),@pmVrCostoIni MONEY,@pmVrMejoras MONEY,@pmVrDisminucion MONEY,@pmVrAcuAjustes MONEY
,@pmVrDepAcum MONEY,@pmVrAjuDepAcum MONEY,@pmVrIngreso MONEY,@pmFechaRet SMALLDATETIME,@pmIdCueDeb VARCHAR(16),@pmIdCueIng VARCHAR(16),@pmConcepto VARCHAR(250)
 AS 
 INSERT INTO Trn_AfMovRetiro (IdMov,Numero,IdCia,Item,IdActivo,IdCausal,NitTercero,NomTercero,VrCostoIni,VrMejoras,VrDisminucion,VrAcuAjustes,VrDepAcum,VrAjuDepAcum,VrIngreso,FechaRet,IdCueDeb,IdCueIng,Concepto)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmIdCausal,@pmNitTercero,@pmNomTercero,@pmVrCostoIni,@pmVrMejoras,@pmVrDisminucion,@pmVrAcuAjustes
,@pmVrDepAcum,@pmVrAjuDepAcum,@pmVrIngreso,@pmFechaRet,@pmIdCueDeb,@pmIdCueIng,@pmConcepto)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfMovTraslado @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdActivo VARCHAR(30),@pmIdGrupo VARCHAR(4)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdInstala VARCHAR(4),@pmIdDep VARCHAR(4),@pmIdRespons VARCHAR(16),@pmCdCiaDest CHAR(2)
,@pmCdGrupoDest VARCHAR(4),@pmCdCCostoDest VARCHAR(16),@pmCdSubCosDest VARCHAR(16),@pmCdInstalaDest VARCHAR(4),@pmCdDepDest VARCHAR(4)
,@pmCdResponsDest VARCHAR(16),@pmConcepto VARCHAR(150),@pmUbicacion VARCHAR(100),@pmUbicacionDest VARCHAR(100)
 AS 
 INSERT INTO Trn_AfMovTraslado (IdMov,Numero,IdCia,Item,IdActivo,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,CdCiaDest,CdGrupoDest,CdCCostoDest
,CdSubCosDest,CdInstalaDest,CdDepDest,CdResponsDest,Concepto,Ubicacion,UbicacionDest)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmIdGrupo,@pmIdCCosto,@pmIdSubCos,@pmIdInstala,@pmIdDep,@pmIdRespons,@pmCdCiaDest
,@pmCdGrupoDest,@pmCdCCostoDest,@pmCdSubCosDest,@pmCdInstalaDest,@pmCdDepDest,@pmCdResponsDest,@pmConcepto,@pmUbicacion,@pmUbicacionDest)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfMovValoriza @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmIdActivo VARCHAR(30),@pmConcepto VARCHAR(250),@pmValor MONEY
,@pmNitProvee VARCHAR(16),@pmFecAdquis SMALLDATETIME,@pmNumDocmto VARCHAR(20)
,@pmDescripParte VARCHAR(150)
 AS 
 INSERT INTO Trn_AfMovValoriza (IdMov,Numero,IdCia,Item,IdActivo,Concepto,Valor,NitProvee,FecAdquis,NumDocmto,DescripParte)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmItem,@pmIdActivo,@pmConcepto,@pmValor,@pmNitProvee,@pmFecAdquis,@pmNumDocmto,@pmDescripParte)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfMovimiento @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmDocumento VARCHAR(20),@pmAnulado BIT,@pmObservacion VARCHAR(250)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_AfMovimiento (IdMov,Numero,IdCia,Fecha,Documento,Anulado,Observacion,TipCom,Comprobante,IdCiaCom,FecCreacion,IdUsuario)
 VALUES (@pmIdMov,@pmNumero,@pmIdCia,@pmFecha,@pmDocumento,@pmAnulado,@pmObservacion,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmFecCreacion,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfPignora @pmIdActivo VARCHAR(30),@pmFecha SMALLDATETIME,@pmTipoAccion VARCHAR(10),@pmNomTercero VARCHAR(150)
,@pmComentarios VARCHAR(150),@pmGarantia VARCHAR(150),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_AfPignora (IdActivo,Fecha,TipoAccion,NomTercero,Comentarios,Garantia,IdUsuario)
 VALUES (@pmIdActivo,@pmFecha,@pmTipoAccion,@pmNomTercero,@pmComentarios,@pmGarantia,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfTiposDif @pmIdTipDif VARCHAR(4),@pmTipoDiferido VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO AfTiposDif (IdTipDif,TipoDiferido,Inactivo)
 VALUES (@pmIdTipDif,@pmTipoDiferido,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAfTiposMov @pmIdMov VARCHAR(4),@pmTipoMov VARCHAR(50),@pmConsecutivo INT,@pmInactivo BIT
 AS 
 INSERT INTO AfTiposMov (IdMov,TipoMov,Consecutivo,Inactivo)
 VALUES (@pmIdMov,@pmTipoMov,@pmConsecutivo,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAgencias @pmIdAgencia VARCHAR(16),@pmCodAgencia VARCHAR(16),@pmIdClie VARCHAR(16),@pmAgencia VARCHAR(150),@pmDirAgncia VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelAgncia VARCHAR(20),@pmFaxAgncia VARCHAR(20),@pmNitCont VARCHAR(16)
,@pmNomCont VARCHAR(150),@pmemlCont VARCHAR(100),@pmCargoCont VARCHAR(50),@pmIdSzona VARCHAR(4),@pmIdPlazo VARCHAR(4),@pmCdPlazoComb VARCHAR(4),@pmIdForma VARCHAR(4),@pmCdBandera VARCHAR(4),@pmIdVend VARCHAR(16),@pmCdCms VARCHAR(4),@pmCdDct VARCHAR(4),@pmIntMora DECIMAL(14,4)
,@pmDiasGracia INT,@pmFactFletes BIT,@pmFactSold BIT,@pmVrCupoCre MONEY,@pmVrSaldoAct MONEY,@pmFecUpCupo SMALLDATETIME,@pmNContrato INT,@pmCiaCont CHAR(2),@pmFecIngreso SMALLDATETIME,@pmReferencia VARCHAR(50),@pmComentarios VARCHAR(250),@pmCupoGalones DECIMAL(14,4)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTipoZona VARCHAR(10),@pmCodSicom VARCHAR(20),@pmCdTipAgenc VARCHAR(4),@pmCdSubAgenc VARCHAR(4),@pmCdGrupAgenc VARCHAR(4), @pmCdSubGrupAgenc VARCHAR(4), @pmCdFntePago VARCHAR(4), @pmPlazoSobtasa VARCHAR(20)
,@pmDocFinanc VARCHAR(3), @pmVolContMes DECIMAL(14,4), @pmVolContTotal DECIMAL(14,4), @pmVolViabMes DECIMAL(14,4),@pmVolViabTotal DECIMAL(14,4), @pmFecPriVenta SMALLDATETIME, @pmInvCtbObra MONEY, @pmInvCtbCapital MONEY, @pmInvCtbEfectivo MONEY, @pmInvCtbImagen MONEY, @pmInvCtbMntmto MONEY
,@pmFecDesemb SMALLDATETIME, @pmInvContObra MONEY, @pmInvContCapital MONEY, @pmInvContEfectivo MONEY, @pmInvContImagen MONEY, @pmCrtlAprovInv INT, @pmCrtlAprovImg INT,@pmFecVigCond SMALLDATETIME,@pmTasaIntArranq DECIMAL(14,4),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Agencias (IdAgencia,CodAgencia,IdClie,Agencia,DirAgncia,IdLocal,TelAgncia,FaxAgncia,NitCont,NomCont,emlCont,CargoCont,IdSzona,IdPlazo,CdPlazoComb,IdForma,CdBandera,IdVend,CdCms,CdDct,IntMora,DiasGracia,FactFletes,FactSold,VrCupoCre,VrSaldoAct,FecUpCupo,NContrato,CiaCont,CodSicom,FecIngreso,Referencia,Comentarios
,CupoGalones,TipoZona,IdEstado,Inactivo,FechaAdd,IdUsuario,CdTipAgenc, CdSubAgenc, CdGrupAgenc, CdSubGrupAgenc, CdFntePago, PlazoSobtasa, DocFinanc, VolContMes, VolContTotal, VolViabMes,VolViabTotal, FecPriVenta, InvCtbObra, InvCtbCapital, InvCtbEfectivo, InvCtbImagen, InvCtbMntmto, FecDesemb, InvContObra, InvContCapital
,InvContEfectivo, InvContImagen, CrtlAprovInv, CrtlAprovImg,FecVigCond,TasaIntArranq)
 VALUES (@pmIdAgencia,@pmCodAgencia,@pmIdClie,@pmAgencia,@pmDirAgncia,@pmIdLocal,@pmTelAgncia,@pmFaxAgncia,@pmNitCont,@pmNomCont,@pmemlCont,@pmCargoCont,@pmIdSzona,@pmIdPlazo,@pmCdPlazoComb,@pmIdForma,@pmCdBandera,@pmIdVend
,@pmCdCms,@pmCdDct,@pmIntMora,@pmDiasGracia,@pmFactFletes,@pmFactSold,@pmVrCupoCre,@pmVrSaldoAct,@pmFecUpCupo,@pmNContrato,@pmCiaCont,@pmCodSicom,@pmFecIngreso,@pmReferencia,@pmComentarios,@pmCupoGalones,@pmTipoZona,@pmIdEstado,@pmInactivo,@pmFechaAdd,@pmIdUsuario
,@pmCdTipAgenc, @pmCdSubAgenc,@pmCdGrupAgenc,@pmCdSubGrupAgenc,@pmCdFntePago,@pmPlazoSobtasa,@pmDocFinanc,@pmVolContMes,@pmVolContTotal,@pmVolViabMes,@pmVolViabTotal,@pmFecPriVenta
,@pmInvCtbObra, @pmInvCtbCapital,@pmInvCtbEfectivo,@pmInvCtbImagen,@pmInvCtbMntmto,@pmFecDesemb,@pmInvContObra,@pmInvContCapital,@pmInvContEfectivo,@pmInvContImagen,@pmCrtlAprovInv,@pmCrtlAprovImg,@pmFecVigCond,@pmTasaIntArranq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAjuInv @pmTipDoc VARCHAR(3),@pmAjuste INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16)
,@pmVrSubTotal MONEY,@pmCantidad DECIMAL(14,4),@pmBaseAjuInf MONEY,@pmTarifPaag DECIMAL(14,4),@pmNumDcmto INT,@pmNumOrden INT,@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_AjuInv (TipDoc,Ajuste,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,Cantidad,BaseAjuInf,TarifPaag,NumDcmto,NumOrden,IdCCosto,IdSubCos,Modalidad,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmAjuste,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdTercero,@pmVrSubTotal,@pmCantidad,@pmBaseAjuInf,@pmTarifPaag,@pmNumDcmto,@pmNumOrden
,@pmIdCCosto,@pmIdSubCos,@pmModalidad,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsAlmBodegas @pmIdBodega VARCHAR(4),@pmBodega VARCHAR(100),@pmDireccion VARCHAR(100),@pmTelBodega VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO AlmBodegas (IdBodega,Bodega,Direccion,TelBodega,Inactivo)
 VALUES (@pmIdBodega,@pmBodega,@pmDireccion,@pmTelBodega,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAprobacion @pmTipDoc VARCHAR(3),@pmAprobacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipoPed VARCHAR(3)
,@pmPedido INT,@pmIdCiaPed CHAR(2),@pmFecPedido SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmpVehiculo VARCHAR(10)
,@pmVrCupoCred MONEY,@pmVrSaldoAct MONEY,@pmChequesDev BIT,@pmClienteMora BIT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Aprobacion (TipDoc,Aprobacion,IdCia,Fecha,TipoPed,Pedido,IdCiaPed,FecPedido,IdCliente,IdAgencia,pVehiculo,VrCupoCred,VrSaldoAct,ChequesDev,ClienteMora
,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmAprobacion,@pmIdCia,@pmFecha,@pmTipoPed,@pmPedido,@pmIdCiaPed,@pmFecPedido,@pmIdCliente,@pmIdAgencia,@pmpVehiculo
,@pmVrCupoCred,@pmVrSaldoAct,@pmChequesDev,@pmClienteMora,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAreas @pmIdArea VARCHAR(4),@pmArea VARCHAR(50),@pmJefeArea VARCHAR(50)
,@pmIdCCosto VARCHAR(16),@pmInactivo BIT
 AS 
 INSERT INTO Areas (IdArea,Area,JefeArea,IdCCosto,Inactivo)
 VALUES (@pmIdArea,@pmArea,@pmJefeArea,@pmIdCCosto,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsAutoriza @pmTipAut VARCHAR(3),@pmAutoriza INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNumAutoriza INT,@pmIdUsuarioAut VARCHAR(11)
,@pmIdEstacion CHAR(2),@pmNitTercero VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmTipoDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFechaDoc SMALLDATETIME,@pmDocRef INT
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Autoriza (TipAut,Autoriza,IdCia,Fecha,IdConcepto,NumAutoriza,IdUsuarioAut,IdEstacion,NitTercero,pVehiculo,TipoDoc,Documento,IdCiaDoc,FechaDoc,DocRef,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipAut,@pmAutoriza,@pmIdCia,@pmFecha,@pmIdConcepto,@pmNumAutoriza,@pmIdUsuarioAut,@pmIdEstacion,@pmNitTercero,@pmpVehiculo,@pmTipoDoc,@pmDocumento,@pmIdCiaDoc,@pmFechaDoc,@pmDocRef,@pmOrigenAdd
,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsBancos @pmIdBanco VARCHAR(4),@pmBanco VARCHAR(50),@pmIdClase VARCHAR(4),@pmCodEntidad VARCHAR(20)
,@pmDireccion VARCHAR(50),@pmContacto VARCHAR(50),@pmComision DECIMAL(10,4),@pmInactivo BIT
 AS 
 INSERT INTO Bancos (IdBanco,Banco,IdClase,CodEntidad,Direccion,Contacto,Comision,Inactivo)
 VALUES (@pmIdBanco,@pmBanco,@pmIdClase,@pmCodEntidad,@pmDireccion,@pmContacto,@pmComision,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsBodUbic @pmIdUbic VARCHAR(10),@pmUbicacion VARCHAR(50),@pmIdBodega VARCHAR(4),@pmPasillo VARCHAR(10),@pmNivel VARCHAR(10),@pmProfdad VARCHAR(10)
,@pmDimAltura DECIMAL(14,4),@pmDimAnch DECIMAL(14,4),@pmDimLong DECIMAL(14,4),@pmCapcVolm DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmCapcPeso DECIMAL(14,4),@pmUndPes VARCHAR(10)
,@pmCdTmcia VARCHAR(4),@pmCdMnjo VARCHAR(4),@pmNotas VARCHAR(250),@pmInactivo BIT
 AS 
 INSERT INTO BodUbic (IdUbic,Ubicacion,IdBodega,Pasillo,Nivel,Profdad,DimAltura,DimAnch,DimLong,CapcVolm,UndCapc,CapcPeso,UndPes,CdTmcia,CdMnjo,Notas,Inactivo)
 VALUES (@pmIdUbic,@pmUbicacion,@pmIdBodega,@pmPasillo,@pmNivel,@pmProfdad,@pmDimAltura,@pmDimAnch,@pmDimLong,@pmCapcVolm,@pmUndCapc,@pmCapcPeso
,@pmUndPes,@pmCdTmcia,@pmCdMnjo,@pmNotas,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsBodegas @pmIdBodega VARCHAR(4),@pmBodega VARCHAR(50),@pmDescrip VARCHAR(100)
,@pmLtaPre CHAR(1),@pmTipoInv VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Bodegas (IdBodega,Bodega,Descrip,LtaPre,TipoInv,Inactivo)
 VALUES (@pmIdBodega,@pmBodega,@pmDescrip,@pmLtaPre,@pmTipoInv,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsBusClaseCue @pmIdClase VARCHAR(4),@pmClaseCuenta VARCHAR(50),@pmPrioridad INT,@pmInactivo BIT
 AS 
 INSERT INTO BusClaseCue (IdClase,ClaseCuenta,Prioridad,Inactivo)
 VALUES (@pmIdClase,@pmClaseCuenta,@pmPrioridad,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCalndario @pmFechaDia SMALLDATETIME,@pmTipoFtvo VARCHAR(3)
 AS 
 INSERT INTO Calndario (FechaDia,TipoFtvo)
 VALUES (@pmFechaDia,@pmTipoFtvo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCargos @pmIdCargo VARCHAR(4),@pmCargo VARCHAR(50),@pmFunciones VARCHAR(3000),@pmInactivo BIT
 AS 
 INSERT INTO Cargos (IdCargo,Cargo,Funciones,Inactivo)
 VALUES (@pmIdCargo,@pmCargo,@pmFunciones,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCausaVales @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY
,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_CausaVales (TipDoc,Documento,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca,TarifaIva,TarifaRet,TarifaIca,CodTarIva,CodTarRet,CodTarIca,IdVend,TarifaCom,CodTarCom,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado
,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFechaVence,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrCargos,@pmVrOtrDcto,@pmVrCostos,@pmVrNeto,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca
,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsCausacVence @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecEmision SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmValorFactura MONEY,@pmValorAbono MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmVehPropio BIT,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4)
,@pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdConcepto VARCHAR(4),@pmFecUltPago SMALLDATETIME,@pmNotaInt INT,@pmFecUltLiquida SMALLDATETIME,@pmFactura VARCHAR(15),@pmReferencia VARCHAR(20),@pmDetalle VARCHAR(100),@pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCiaRec CHAR(2),@pmValorAtcpo MONEY,@pmIdCuenta VARCHAR(16),@pmCombCant DECIMAL(14,4),@pmCombValor MONEY
 AS 
 INSERT INTO Trn_CausacVence (TipDoc,Documento,IdCia,Item,FecEmision,FecVence,ValorFactura,ValorAbono,IdCliente,IdAgencia,IdVehiculo,VehPropio,IdVendedor,Comision,IdCargo,IdClase,IdConcepto,FecUltPago,NotaInt,FecUltLiquida,Factura,Referencia,Detalle,TipRec,Recibo,IdCiaRec,ValorAtcpo,IdCuenta,CombCant,CombValor)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecEmision,@pmFecVence,@pmValorFactura,@pmValorAbono,@pmIdCliente,@pmIdAgencia,@pmIdVehiculo,@pmVehPropio,@pmIdVendedor,@pmComision,@pmIdCargo,@pmIdClase,@pmIdConcepto,@pmFecUltPago,@pmNotaInt,@pmFecUltLiquida,@pmFactura,@pmReferencia,@pmDetalle,@pmTipRec
,@pmRecibo,@pmIdCiaRec,@pmValorAtcpo,@pmIdCuenta,@pmCombCant,@pmCombValor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsCausacion @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmFactura VARCHAR(15),@pmIdProveedor VARCHAR(16),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmReferencia VARCHAR(20),@pmModalidad VARCHAR(10),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY
,@pmValorDcto MONEY,@pmValorIva MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorFletes MONEY,@pmValorNeto MONEY,@pmValorCCobrar MONEY,@pmBaseIva MONEY,@pmBaseRet MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmIdTarIva VARCHAR(4),@pmIdTarRet VARCHAR(4),@pmIdTarIca VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4)
,@pmCxPagar BIT,@pmCxcVarios BIT,@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmCierreCte BIT,@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Causacion (TipDoc,Documento,IdCia,Fecha,IdConcepto,Factura,IdProveedor,FecFactura,FecVence,Referencia,Modalidad,IdVehiculo,IdPropietario,IdConductor,VehPropio,ValorTotal,ValorDcto,ValorIva,ValorReteFte,ValorReteIva,ValorReteIca,ValorOtros,ValorFletes,ValorNeto,ValorCCobrar,BaseIva,BaseRet,TarifaIva,TarifaRet,TarifaIca,IdTarIva,IdTarRet,IdTarIca,MulPlazos,IdPlazo,CxPagar,CxcVarios,TipEgr,Egreso,IdCiaEgr,Anulado,NumDev,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,CierreCte,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmFecha,@pmIdConcepto,@pmFactura,@pmIdProveedor,@pmFecFactura,@pmFecVence,@pmReferencia,@pmModalidad,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmVehPropio,@pmValorTotal,@pmValorDcto,@pmValorIva,@pmValorReteFte,@pmValorReteIva,@pmValorReteIca,@pmValorOtros,@pmValorFletes,@pmValorNeto,@pmValorCCobrar,@pmBaseIva,@pmBaseRet,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmIdTarIva,@pmIdTarRet,@pmIdTarIca,@pmMulPlazos
,@pmIdPlazo,@pmCxPagar,@pmCxcVarios,@pmTipEgr,@pmEgreso,@pmIdCiaEgr,@pmAnulado,@pmNumDev,@pmFecDev,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmCierreCte,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCentroCosto @pmIdCCosto VARCHAR(16),@pmCCosto VARCHAR(100),@pmFechaAdd SMALLDATETIME
,@pmInactivo BIT
 AS 
 INSERT INTO CentroCosto (IdCCosto,CCosto,FechaAdd,Inactivo)
 VALUES (@pmIdCCosto,@pmCCosto,@pmFechaAdd,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCentrosServ @pmIdCenSer VARCHAR(4),@pmCentroServ VARCHAR(50),@pmDirCentro VARCHAR(100)
,@pmIdLocal VARCHAR(8),@pmTelefonos VARCHAR(30),@pmTipoCentro VARCHAR(10),@pmNitResp VARCHAR(16)
,@pmRespnsable VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO CentrosServ (IdCenSer,CentroServ,DirCentro,IdLocal,Telefonos,TipoCentro,NitResp,Respnsable,Inactivo)
 VALUES (@pmIdCenSer,@pmCentroServ,@pmDirCentro,@pmIdLocal,@pmTelefonos,@pmTipoCentro,@pmNitResp,@pmRespnsable,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCheques @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmVrForma MONEY,@pmCtaForma VARCHAR(30),@pmBeneficiario VARCHAR(150),@pmReferncia1 VARCHAR(50)
,@pmReferncia2 VARCHAR(50),@pmDetalle VARCHAR(150),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmCdVend VARCHAR(16),@pmIdLocal VARCHAR(8),@pmTipRec VARCHAR(3),@pmRecibo INT,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmTipCon VARCHAR(3),@pmNumConsig INT,@pmIdCiaCon CHAR(2),@pmFecConsig SMALLDATETIME,@pmCdCta VARCHAR(4),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Cheques (TipForma,Numero,IdBanco,IdCia,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle,NitCliente,CdAgencia,CdVend,IdLocal,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,Anulado,FecDev,IdEstado,IdCiaCrea,IdUsuario)
 VALUES (@pmTipForma,@pmNumero,@pmIdBanco,@pmIdCia,@pmFecha,@pmFecPago,@pmVrForma,@pmCtaForma,@pmBeneficiario,@pmReferncia1,@pmReferncia2,@pmDetalle,@pmNitCliente,@pmCdAgencia,@pmCdVend,@pmIdLocal,@pmTipRec,@pmRecibo,@pmTipDoc,@pmDocumento,@pmIdCiaDoc
,@pmFecDoc,@pmTipCon,@pmNumConsig,@pmIdCiaCon,@pmFecConsig,@pmCdCta,@pmAnulado,@pmFecDev,@pmIdEstado,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseBan @pmIdClase VARCHAR(4),@pmClaseBanco VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseBan (IdClase,ClaseBanco,Inactivo)
 VALUES (@pmIdClase,@pmClaseBanco,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseCta @pmIdClase VARCHAR(4),@pmClaseCuenta VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseCta (IdClase,ClaseCuenta,Inactivo)
 VALUES (@pmIdClase,@pmClaseCuenta,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseDiv @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseDiv (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseFon @pmIdClase VARCHAR(4),@pmClaseFondo VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseFon (IdClase,ClaseFondo,Inactivo)
 VALUES (@pmIdClase,@pmClaseFondo,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClasePol @pmIdClase VARCHAR(4),@pmClasePoliza VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClasePol (IdClase,ClasePoliza,Inactivo)
 VALUES (@pmIdClase,@pmClasePoliza,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseRie @pmIdClase VARCHAR(4),@pmClaseRiesgo VARCHAR(50),@pmTarifa DECIMAL(10,4),@pmInactivo BIT
 AS 
 INSERT INTO ClaseRie (IdClase,ClaseRiesgo,Tarifa,Inactivo)
 VALUES (@pmIdClase,@pmClaseRiesgo,@pmTarifa,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseSub @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseSub (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClaseTar @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ClaseTar (IdClase,NomClase,Inactivo)
 VALUES (@pmIdClase,@pmNomClase,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsClieContado @pmIdCliente VARCHAR(16),@pmNombre VARCHAR(150),@pmTipoId CHAR(1),@pmDv CHAR(1),@pmDireccion VARCHAR(150)
,@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmemail VARCHAR(100),@pmExcento BIT,@pmPosIniNom INT,@pmInactivo BIT,@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO ClieContado (IdCliente,Nombre,TipoId,Dv,Direccion,IdLocal,Telefono,email,Excento,PosIniNom,Inactivo,FechaAdd)
 VALUES (@pmIdCliente,@pmNombre,@pmTipoId,@pmDv,@pmDireccion,@pmIdLocal,@pmTelefono,@pmemail,@pmExcento,@pmPosIniNom,@pmInactivo,@pmFechaAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCnxextna @pmIdReg INT,@pmCnxEngine VARCHAR(10),@pmCnxDso VARCHAR(250),@pmCnxDbname VARCHAR(50),@pmCnxAutentic VARCHAR(3)
,@pmCnxUid VARCHAR(50),@pmCnxPwd VARCHAR(50),@pmCnxOwner VARCHAR(20),@pmCnxObserv VARCHAR(250),@pmCnxTipo VARCHAR(10),@pmCnxPpties VARCHAR(250)
,@pmCnxDriver VARCHAR(100),@pmCnxPort VARCHAR(30)
 AS 
 INSERT INTO Sys_Cnxextna (IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd,CnxOwner,CnxPpties,CnxObserv,CnxDriver,CnxPort)
 VALUES (@pmIdReg,@pmCnxTipo,@pmCnxEngine,@pmCnxDso,@pmCnxDbname,@pmCnxAutentic,@pmCnxUid,@pmCnxPwd,@pmCnxOwner,@pmCnxPpties,@pmCnxObserv,@pmCnxDriver,@pmCnxPort)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsComDetalle @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmVehPropio BIT,@pmVrBase MONEY,@pmTarifaBase DECIMAL(10,4),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCiaDoc CHAR(2),@pmCodConce VARCHAR(4),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME,@pmCodCta VARCHAR(4),@pmNumCheque VARCHAR(20),@pmIntegrado BIT,@pmTipoAplica CHAR(1),@pmConsolida BIT,@pmCodCargo VARCHAR(4),@pmNitOtros VARCHAR(16),@pmCodSubgpo VARCHAR(8),@pmCiuOrigen VARCHAR(8),@pmCodAgncia VARCHAR(16),@pmReferncia VARCHAR(50)
,@pmNitDoc VARCHAR(16),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_ComDetalle (TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,IdSubCos,CodAgncia,Referncia,NitDoc,TipDocRef,DocRef,IdCiaRef,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmFecha,@pmIdCuenta,@pmDetalle,@pmVrDebito,@pmVrCredito,@pmIdTercero,@pmIdVehiculo,@pmIdCCosto,@pmVehPropio,@pmVrBase,@pmTarifaBase,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmCodConce,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCodCta,@pmNumCheque,@pmIntegrado,@pmTipoAplica,@pmConsolida,@pmCodCargo
,@pmNitOtros,@pmCodSubgpo,@pmCiuOrigen,@pmIdSubCos,@pmCodAgncia,@pmReferncia,@pmNitDoc ,@pmTipDocRef,@pmDocRef,@pmIdCiaRef,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsComFactura @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCia CHAR(2),@pmItem INT,@pmIdProveedor VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecEmision SMALLDATETIME
,@pmFecVence SMALLDATETIME,@pmVrFactura MONEY,@pmVrAbonado MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmItemCom INT,@pmReferencia VARCHAR(50),@pmDetalle VARCHAR(100),@pmpVehiculo VARCHAR(10),@pmVehPropio BIT
,@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmEstadoApr INT
 AS 
 INSERT INTO Trn_ComFactura (TipFac,Factura,IdCia,Item,IdProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr)
 VALUES (@pmTipFac,@pmFactura,@pmIdCia,@pmItem,@pmIdProveedor,@pmIdCuenta,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmFecEmision,@pmFecVence,@pmVrFactura,@pmVrAbonado,@pmTipCom,@pmComprobante,@pmItemCom
,@pmReferencia,@pmDetalle,@pmpVehiculo,@pmVehPropio,@pmTipRef,@pmDocRef,@pmIdCiaRef,@pmEstadoApr)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsComFactura_Uni @pmIdProveedor VARCHAR(16),@pmNewProveedor VARCHAR(16)
 AS 
 INSERT INTO Trn_ComFactura (TipFac,Factura,IdCia,Item,IdProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr)
 SELECT TipFac,Factura,IdCia,Item,@pmNewProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM Trn_ComFactura WHERE IdProveedor=@pmIdProveedor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCompCaja @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmVrEfectivo MONEY,@pmVrOtros MONEY,@pmVrTotal MONEY
,@pmVrTotDebito MONEY,@pmVrTotCredito MONEY,@pmEnEfectivo BIT,@pmCdCta VARCHAR(4),@pmIdTercero VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdCajero VARCHAR(11),@pmNumForma VARCHAR(20),@pmCdBanco VARCHAR(4),@pmReferencia VARCHAR(50),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
,@pmNumVehiculo VARCHAR(10),@pmFacCalidad INT,@pmSemInicial INT,@pmSemFinal INT
 AS 
 INSERT INTO Trn_CompCaja (TipDoc,Recibo,IdCia,Fecha,IdConcepto,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,IdTercero,CdAgencia,IdCCosto,IdSubCos,IdCajero,NumForma,CdBanco,Referencia
,NumVehiculo,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmVrEfectivo,@pmVrOtros,@pmVrTotal,@pmVrTotDebito,@pmVrTotCredito,@pmEnEfectivo,@pmCdCta,@pmIdTercero,@pmCdAgencia,@pmIdCCosto
,@pmIdSubCos,@pmIdCajero,@pmNumForma,@pmCdBanco,@pmReferencia,@pmNumVehiculo,@pmFacCalidad,@pmSemInicial,@pmSemFinal,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCompCajaDet @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250)
,@pmVrDebito MONEY,@pmVrCredito MONEY,@pmNitTercero VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmVrBase MONEY
,@pmTarifaBase DECIMAL(14,4),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME
,@pmCodCta VARCHAR(4),@pmNumCheque VARCHAR(20),@pmTipoAplica CHAR(1),@pmNitOtros VARCHAR(16),@pmCodAgencia VARCHAR(16),@pmReferncia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
 AS 
 INSERT INTO Trn_CompCajaDet (TipDoc,Recibo,IdCia,Item,IdConcepto,IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence
,CodCta,NumCheque,TipoAplica,NitOtros,CodAgencia,Referncia,TipDocRef,DocRef,IdCiaRef)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmItem,@pmIdConcepto,@pmIdCuenta,@pmDetalle,@pmVrDebito,@pmVrCredito,@pmNitTercero,@pmpVehiculo,@pmCdCCosto,@pmCdSubCos,@pmVrBase
,@pmTarifaBase,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCodCta,@pmNumCheque,@pmTipoAplica,@pmNitOtros,@pmCodAgencia,@pmReferncia,@pmTipDocRef,@pmDocRef,@pmIdCiaRef)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsCompGastos @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdPlantilla VARCHAR(4),@pmIdTercero VARCHAR(16),@pmTipFact VARCHAR(3)
,@pmFactura VARCHAR(15),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmVrTotal MONEY,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_CompGastos (TipCom,Comprobante,IdCia,Fecha,IdPlantilla,IdTercero,TipFact,Factura,FecFactura,FecVence,VrTotal,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmFecha,@pmIdPlantilla,@pmIdTercero,@pmTipFact,@pmFactura,@pmFecFactura,@pmFecVence,@pmVrTotal,@pmAnulado,@pmFecDev,@pmObservacion
,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsCompanias @pmIdCia CHAR(2),@pmCompania VARCHAR(50),@pmDirInterface VARCHAR(255),@pmFechaActual SMALLDATETIME
,@pmPeriodoActual SMALLDATETIME,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdTercero VARCHAR(16)
,@pmCodSucsal VARCHAR(10),@pmDireccSuc VARCHAR(250),@pmTelefSuc VARCHAR(20),@pmFaxSuc VARCHAR(20),@pmIdLocal VARCHAR(8),@pmTipoEDS VARCHAR(10)
,@pmCodPlanta VARCHAR(20),@pmFechaAdd SMALLDATETIME,@pmInactivo BIT
 AS 
 INSERT INTO Companias (IdCia,Compania,DirInterface,FechaActual,PeriodoActual,IdCCosto,IdSubCos,IdTercero
,CodSucsal,DireccSuc,TelefSuc,FaxSuc,IdLocal,TipoEDS,CodPlanta,FechaAdd,Inactivo)
 VALUES (@pmIdCia,@pmCompania,@pmDirInterface,@pmFechaActual,@pmPeriodoActual,@pmIdCCosto,@pmIdSubCos,@pmIdTercero
,@pmCodSucsal,@pmDireccSuc,@pmTelefSuc,@pmFaxSuc,@pmIdLocal,@pmTipoEDS,@pmCodPlanta,@pmFechaAdd,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsComprobantes @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrTotal MONEY,@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmTipEgr VARCHAR(3),@pmNEgreso INT,@pmpVehiculo VARCHAR(10),@pmVehPropio BIT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(150),@pmVrDivisa1 MONEY,@pmVrDivisa2 MONEY,@pmVrDivisa3 MONEY, @pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmPropddes VARCHAR(50),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Comprobantes (TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,PermEditar,Integrado,EsEgreso,Anticipo,VrDivisa1,VrDivisa2,VrDivisa3,Observacion,IdEstado,Propddes,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmFecha,@pmIdTercero,@pmVrTotal,@pmIdCta,@pmEnEfectivo,@pmNumCheque,@pmFecCheque,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmAnulado,@pmNumDev,@pmFecDev,@pmTipEgr,@pmNEgreso,@pmpVehiculo,@pmVehPropio,@pmCedCondtor,@pmCodConce,@pmBeneficiario,@pmPermEditar,@pmIntegrado,@pmEsEgreso
,@pmAnticipo,@pmVrDivisa1,@pmVrDivisa2,@pmVrDivisa3,@pmObservacion,@pmIdEstado,@pmPropddes,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsConcDivCue @pmIdConcepto VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmCdCueDev VARCHAR(16)
,@pmTipoMov CHAR(1),@pmTipoTerc CHAR(1),@pmFecAdd SMALLDATETIME
 AS 
 INSERT INTO ConcDivCue (IdConcepto,IdClase,IdCuenta,CdCueDev,TipoMov,TipoTerc,FecAdd)
 VALUES (@pmIdConcepto,@pmIdClase,@pmIdCuenta,@pmCdCueDev,@pmTipoMov,@pmTipoTerc,@pmFecAdd)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsConcDiversos @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmGrupo VARCHAR(20),@pmEsServicio BIT,@pmDeManfsto BIT,@pmCdIva VARCHAR(4)
,@pmCdRet VARCHAR(4),@pmCdIca VARCHAR(4),@pmIdCueDeb VARCHAR(16),@pmIdCueCre VARCHAR(16),@pmInactivo BIT
,@pmCdAutoret VARCHAR(4),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO ConcDiversos (IdConcepto,Concepto,Grupo,EsServicio,DeManfsto,CdIva,CdRet,CdIca,CdAutoret,IdCueDeb,IdCueCre,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdConcepto,@pmConcepto,@pmGrupo,@pmEsServicio,@pmDeManfsto,@pmCdIva,@pmCdRet,@pmCdIca,@pmCdAutoret,@pmIdCueDeb,@pmIdCueCre,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsConceptos @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmIdDoc VARCHAR(3),@pmIdCom VARCHAR(3),@pmVcmto BIT,@pmImptos BIT
,@pmRetfte BIT,@pmRetIca BIT,@pmObservacion VARCHAR(150),@pmPdterminado INT,@pmTrfaSanc DECIMAL(14,4),@pmRndTotal INT,@pmDcmtoEquiv BIT
,@pmInactivo BIT,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Conceptos (IdConcepto,Concepto,IdDoc,IdCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado,TrfaSanc,RndTotal,DcmtoEquiv,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdConcepto,@pmConcepto,@pmIdDoc,@pmIdCom,@pmVcmto,@pmImptos,@pmRetfte,@pmRetIca,@pmObservacion,@pmPdterminado,@pmTrfaSanc
,@pmRndTotal,@pmDcmtoEquiv,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsConceptosAdmon @pmIdConcepto VARCHAR(4),@pmIdAdmon VARCHAR(4),@pmIdClase INT
,@pmIdCuenta VARCHAR(16),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO ConceptosAdmon (IdConcepto,IdAdmon,IdClase,IdCuenta,FechaAdd)
 VALUES (@pmIdConcepto,@pmIdAdmon,@pmIdClase,@pmIdCuenta,@pmFechaAdd)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsConceptosCue @pmIdConcepto VARCHAR(4),@pmIdClase INT,@pmDescripcion VARCHAR(50),@pmIdCuenta VARCHAR(16),@pmTipoMov CHAR(1)
,@pmDetalleMov VARCHAR(150),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipoTerc CHAR(1),@pmFecAdd SMALLDATETIME
 AS 
 INSERT INTO ConceptosCue (IdConcepto,IdClase,Descripcion,IdCuenta,TipoMov,DetalleMov,CdCCosto,CdSubCos,TipoTerc,FecAdd)
 VALUES (@pmIdConcepto,@pmIdClase,@pmDescripcion,@pmIdCuenta,@pmTipoMov,@pmDetalleMov,@pmCdCCosto,@pmCdSubCos,@pmTipoTerc,@pmFecAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsConsigna @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmVrEfectivo MONEY,@pmVrOtros MONEY,@pmVrTotal MONEY,@pmEnEfectivo BIT,@pmIdCta VARCHAR(4)
,@pmIdCuenta VARCHAR(16),@pmIdCliente VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmNitDeptante VARCHAR(16),@pmDepositante VARCHAR(150),@pmTelDeptante VARCHAR(20),@pmReferencia VARCHAR(50)
,@pmReferncia2 VARCHAR(50),@pmNumConsigna VARCHAR(20),@pmRangosDoc VARCHAR(100),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Consigna (TipDoc,Consignacion,IdCia,Fecha,IdConcepto,VrEfectivo,VrOtros,VrTotal,EnEfectivo,IdCta,IdCuenta,IdCliente,IdCCosto,IdSubCos,IdCajero,NitDeptante,Depositante,TelDeptante,Referencia,Referncia2,NumConsigna,RangosDoc,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmConsignacion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmVrEfectivo,@pmVrOtros,@pmVrTotal,@pmEnEfectivo,@pmIdCta,@pmIdCuenta,@pmIdCliente,@pmIdCCosto,@pmIdSubCos,@pmIdCajero,@pmNitDeptante,@pmDepositante
,@pmTelDeptante,@pmReferencia,@pmReferncia2,@pmNumConsigna,@pmRangosDoc,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCotizacion @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY
,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirContacto VARCHAR(250),@pmIdLocal VARCHAR(8),@pmDiasVigencia INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150)
,@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(250),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmModalidad VARCHAR(10),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Cotizacion (TipDoc,Cotizacion,IdCia,Fecha,FechaVence,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,IdVend,TarifaCom,CodTarCom,DirContacto,IdLocal,DiasVigencia,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney
,TipFac,Factura,IdCiaFac,FechaFact,Modalidad,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,NumAprob,FecAprob,CdUsuAprob,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmCotizacion,@pmIdCia,@pmFecha,@pmFechaVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrFletes,@pmVrOtros,@pmVrOtrDcto,@pmVrNeto,@pmCantidad,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmDirContacto,@pmIdLocal,@pmDiasVigencia,@pmNitContac
,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargoContac,@pmIdForma,@pmDetallePago,@pmMulPlazos,@pmIdPlazo,@pmCdMney,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmFechaFact,@pmModalidad,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmNumAprob,@pmFecAprob,@pmCdUsuAprob,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsCtasCorrientes @pmIdCta VARCHAR(4),@pmNumeroCta VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmConsCheques BIT,@pmRangoCons VARCHAR(20)
,@pmConsActual INT,@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT,@pmNumCopias INT,@pmInactivo BIT,@pmIdCiaDef CHAR(2)
 AS 
 INSERT INTO CtasCorrientes (IdCta,NumeroCta,IdBanco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,IdCiaDef,Inactivo)
 VALUES (@pmIdCta,@pmNumeroCta,@pmIdBanco,@pmIdCuenta,@pmConsCheques,@pmRangoCons,@pmConsActual,@pmFormato,@pmTipoPapel,@pmOrientacion
,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmIdCiaDef,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDepartamentos @pmIdDep VARCHAR(8),@pmDepartamento VARCHAR(50)
,@pmCodigo VARCHAR(4), @pmIdPais VARCHAR(8),@pmInactivo BIT
 AS 
 INSERT INTO Departamentos (IdDep,Departamento,Codigo,IdPais,Inactivo)
 VALUES (@pmIdDep,@pmDepartamento,@pmCodigo,@pmIdPais,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDependencias @pmIdDep VARCHAR(4),@pmDependencia VARCHAR(50),@pmJefeDep VARCHAR(50),@pmIdArea VARCHAR(4)
,@pmTipRiesgo DECIMAL(10,4),@pmIdSubCos VARCHAR(16),@pmInactivo BIT
 AS 
 INSERT INTO Dependencias (IdDep,Dependencia,JefeDep,IdArea,IdSubCos,TipRiesgo,Inactivo)
 VALUES (@pmIdDep,@pmDependencia,@pmJefeDep,@pmIdArea,@pmIdSubCos,@pmTipRiesgo,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsDespachos @pmTipDoc VARCHAR(3),@pmCartulina INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdRuta VARCHAR(4),@pmNumOrden INT,@pmFechaRec SMALLDATETIME,@pmIdVehiculo VARCHAR(10)
,@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmIdConductor1 VARCHAR(16),@pmIdRelevador VARCHAR(16),@pmVehPropio BIT,@pmCentInicial INT,@pmCentFinal INT,@pmNPasajeros INT,@pmRecorridos INT,@pmValorProducido MONEY
,@pmValorGastos MONEY,@pmValorAhorro MONEY,@pmValorOtros MONEY,@pmValorVales MONEY,@pmValorAbono MONEY,@pmValorPasaje MONEY,@pmTipoAdmon VARCHAR(10),@pmIdTipRec VARCHAR(3),@pmReciboCaja INT,@pmIdCiaRec CHAR(2)
,@pmNumAutoriza VARCHAR(30),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Despachos (TipDoc,Cartulina,IdCia,Fecha,IdRuta,NumOrden,FechaRec,IdVehiculo,IdPropietario,IdConductor,IdConductor1,IdRelevador,VehPropio,CentInicial,CentFinal,NPasajeros,Recorridos,ValorProducido,ValorGastos,ValorAhorro,ValorOtros
,ValorVales,ValorAbono,ValorPasaje,TipoAdmon,IdTipRec,ReciboCaja,IdCiaRec,NumAutoriza,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmCartulina,@pmIdCia,@pmFecha,@pmIdRuta,@pmNumOrden,@pmFechaRec,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmIdConductor1,@pmIdRelevador,@pmVehPropio,@pmCentInicial,@pmCentFinal
,@pmNPasajeros,@pmRecorridos,@pmValorProducido,@pmValorGastos,@pmValorAhorro,@pmValorOtros,@pmValorVales,@pmValorAbono,@pmValorPasaje,@pmTipoAdmon,@pmIdTipRec,@pmReciboCaja,@pmIdCiaRec,@pmNumAutoriza,@pmAnulado,@pmNumDev
,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDetCuentas @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmConcepto VARCHAR(10),@pmItem INT
,@pmIdCuenta VARCHAR(16),@pmValor MONEY,@pmDetalle VARCHAR(150),@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16),@pmCdCCosto VARCHAR(16)
,@pmCdSubCos VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmVrBase MONEY,@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT
,@pmFecVence SMALLDATETIME,@pmCheque VARCHAR(20),@pmCiuOrigen VARCHAR(8),@pmReferencia VARCHAR(50)
 AS 
 INSERT INTO Trn_DetCuentas (TipDoc,Documento,IdCia,Concepto,Item,IdCuenta,Valor,Detalle,IdTercero,CodAgncia,CdCCosto,CdSubCos,pVehiculo,VrBase,TipFac,Factura,IdCiaFac
,ItemFac,FecVence,Cheque,CiuOrigen,Referencia)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmConcepto,@pmItem,@pmIdCuenta,@pmValor,@pmDetalle,@pmIdTercero,@pmCodAgncia,@pmCdCCosto,@pmCdSubCos
,@pmpVehiculo,@pmVrBase,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence,@pmCheque,@pmCiuOrigen,@pmReferencia)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevCaj @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrEfectivo MONEY
,@pmVrOtros MONEY,@pmVrTotal MONEY,@pmVrTotDebito MONEY,@pmVrTotCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmEnEfectivo BIT,@pmCdCta VARCHAR(4)
,@pmNumForma VARCHAR(20),@pmCdBanco VARCHAR(4),@pmReferencia VARCHAR(50),@pmNumAutoriza INT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11),@pmNumVehiculo VARCHAR(10)
 AS 
 INSERT INTO Trn_DevCaj (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,IdTercero,IdAgencia,IdCCosto,IdSubCos,IdCajero,EnEfectivo,CdCta,NumForma,CdBanco,Referencia,NumAutoriza
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,NumVehiculo)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmRecibo,@pmIdCiaDoc,@pmFecDoc,@pmVrEfectivo,@pmVrOtros,@pmVrTotal,@pmVrTotDebito,@pmVrTotCredito,@pmIdTercero,@pmIdAgencia,@pmIdCCosto,@pmIdSubCos
,@pmIdCajero,@pmEnEfectivo,@pmCdCta,@pmNumForma,@pmCdBanco,@pmReferencia,@pmNumAutoriza,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmNumVehiculo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsDevCausacion @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
,@pmFecDoc SMALLDATETIME,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorIva MONEY,@pmValorReteFte MONEY
,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorFletes MONEY,@pmValorNeto MONEY,@pmValorCxc MONEY,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmFactura VARCHAR(15),@pmIdProveedor VARCHAR(16)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevCausacion (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Documento,IdCiaDoc,FecDoc,IdVehiculo,IdConductor,IdPropietario,VehPropio,ValorTotal,ValorDcto,ValorIva,ValorReteFte,ValorReteIva,ValorReteIca,ValorOtros,ValorFletes,ValorNeto
,ValorCxc,IdPlazo,CxPagar,Factura,IdProveedor,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmFecDoc,@pmIdVehiculo,@pmIdConductor,@pmIdPropietario,@pmVehPropio,@pmValorTotal,@pmValorDcto,@pmValorIva,@pmValorReteFte
,@pmValorReteIva,@pmValorReteIca,@pmValorOtros,@pmValorFletes,@pmValorNeto,@pmValorCxc,@pmIdPlazo,@pmCxPagar,@pmFactura,@pmIdProveedor,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevCns @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrEfectivo MONEY
,@pmVrOtros MONEY,@pmVrTotal MONEY,@pmEnEfectivo BIT,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmIdCliente VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmReferencia VARCHAR(50),@pmReferncia2 VARCHAR(50)
,@pmNumConsigna VARCHAR(20),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevCns (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Consignacion,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,EnEfectivo,IdCta,IdCuenta,IdCliente,IdCCosto,IdSubCos,IdCajero,Referencia,Referncia2,NumConsigna,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmConsignacion,@pmIdCiaDoc,@pmFecDoc,@pmVrEfectivo,@pmVrOtros,@pmVrTotal,@pmEnEfectivo,@pmIdCta,@pmIdCuenta,@pmIdCliente,@pmIdCCosto,@pmIdSubCos,@pmIdCajero,@pmReferencia,@pmReferncia2
,@pmNumConsigna,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevEnt @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdProv VARCHAR(16)
,@pmFactura VARCHAR(15),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrCargos MONEY
,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10)
,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmFletesCosto BIT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevEnt (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Entrada,IdCiaDoc,FecDoc,IdProv,Factura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,CxPagar,DocEquiv
,BaseImp,BaseRet,CdCCosto,CdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmEntrada,@pmIdCiaDoc,@pmFecDoc,@pmIdProv,@pmFactura,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrOtros
,@pmVrSobretasa,@pmVrImpGlobal,@pmVrCargos,@pmVrOtrDcto,@pmVrNeto,@pmCantidad,@pmNitFletes,@pmVrRetFlete,@pmVrIcaFlete,@pmFletesCosto,@pmCxPagar,@pmDocEquiv,@pmBaseImp,@pmBaseRet,@pmCdCCosto,@pmCdSubCos,@pmModalidad,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevFco @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmnClieCon VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4)
,@pmCantPuntos DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmPedido INT,@pmIdCiaPed CHAR(2),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFecPedido SMALLDATETIME,@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmNumAutoriza INT,@pmPlacaVehic VARCHAR(10),@pmKmtVehic INT,@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevFco (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,nClieCon,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,BaseImp,BaseRet,IdCajero,IdVend,TarifaCom
,CodTarCom,NitCliente,CdAgencia,IdCCosto,IdSubCos,IdLocal,EnEfectivo,Pedido,IdCiaPed,Remision,IdCiaRem,FecPedido,Modalidad,ModdDev,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,NumAutoriza,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmFactura,@pmIdCiaDoc,@pmFecDoc,@pmnClieCon,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrCargos,@pmVrOtrDcto,@pmVrCostos
,@pmVrNeto,@pmVrEfectivo,@pmVrPagosOtr,@pmCantidad,@pmCantPuntos,@pmBaseImp,@pmBaseRet,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmNitCliente,@pmCdAgencia,@pmIdCCosto,@pmIdSubCos,@pmIdLocal,@pmEnEfectivo,@pmPedido,@pmIdCiaPed,@pmRemision
,@pmIdCiaRem,@pmFecPedido,@pmModalidad,@pmModdDev,@pmPlacaVehic,@pmKmtVehic,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmNumAutoriza,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevFcr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY
,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrFaltantes MONEY,@pmVrAnticipos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmCantPuntos DECIMAL(14,4),@pmBaseImp MONEY
,@pmBaseRet MONEY,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdLocEnv VARCHAR(8),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmFecPedido SMALLDATETIME,@pmModalidad VARCHAR(10)
,@pmPlacaVehic VARCHAR(10),@pmKmtVehic INT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevFcr (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,IdLocEnv
,Pedido,IdCiaPed,Remision,IdCiaRem,Cotizacion,IdCiaCot,FecPedido,Modalidad,ModdDev,PlacaVehic,KmtVehic,ZonaFrontera,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmFactura,@pmIdCiaDoc,@pmFecDoc,@pmIdCliente,@pmIdAgencia,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrOtros,@pmVrCargos,@pmVrOtrDcto,@pmVrCostos,@pmVrSobretasa,@pmVrImpGlobal,@pmVrFaltantes
,@pmVrAnticipos,@pmVrNeto,@pmCantidad,@pmCantPuntos,@pmBaseImp,@pmBaseRet,@pmIdCCosto,@pmIdSubCos,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmIdLocEnv,@pmPedido,@pmIdCiaPed,@pmRemision,@pmIdCiaRem,@pmCotizacion,@pmIdCiaCot,@pmFecPedido,@pmModalidad,@pmModdDev,@pmPlacaVehic,@pmKmtVehic,@pmZonaFrontera,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsDevFvc @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevFvc (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca,IdVend,TarifaCom,CodTarCom,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmFactura,@pmIdCiaDoc,@pmFecDoc,@pmIdCliente,@pmIdAgencia,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrCargos,@pmVrOtrDcto,@pmVrCostos,@pmVrNeto,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmIdVend
,@pmTarifaCom,@pmCodTarCom,@pmModdDev,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevNpr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmNumNota INT
,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY,@pmVrCostos MONEY,@pmCantEnt DECIMAL(14,4),@pmCantSal DECIMAL(14,4),@pmNumOrden INT
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevNpr (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,NumNota,IdCiaDoc,FecDoc,IdTercero,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,IdCCosto,IdSubCos,Modalidad,ModdDev,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmNumNota,@pmIdCiaDoc,@pmFecDoc,@pmIdTercero,@pmVrSubTotal,@pmVrCostos,@pmCantEnt,@pmCantSal
,@pmNumOrden,@pmIdCCosto,@pmIdSubCos,@pmModalidad,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevPas @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmNitTercero VARCHAR(16),@pmCdJornada VARCHAR(4)
,@pmVrSubTotal MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmModdDev VARCHAR(10),@pmNumAutoriza INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevPas (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,nNota,IdCiaDoc,FecDoc,NitTercero,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto,Cantidad,IdCajero,IdVend,TarifaCom,CodTarCom,IdCCosto,IdSubCos,Referencia,ModdDev,NumAutoriza,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmnNota,@pmIdCiaDoc,@pmFecDoc,@pmNitTercero,@pmCdJornada,@pmVrSubTotal,@pmVrSobretasa,@pmVrImpGlobal,@pmVrOtros,@pmVrCostos,@pmVrNeto,@pmCantidad,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom
,@pmIdCCosto,@pmIdSubCos,@pmReferencia,@pmModdDev,@pmNumAutoriza,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevPla @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3)
,@pmPlanilla INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmNitTercero VARCHAR(16),@pmIdJornada VARCHAR(4),@pmVrSubTotal MONEY,@pmVrSobretasa MONEY
,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrSobrantes MONEY,@pmVrFaltantes MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY
,@pmVrCreditos MONEY,@pmVrGastComb MONEY,@pmVrGastOtros MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4),@pmVrConTotal MONEY,@pmVrConImp MONEY,@pmVrConDcto MONEY,@pmVrConCos MONEY
,@pmConCant DECIMAL(14,4),@pmVrCreTotal MONEY,@pmVrCreImp MONEY,@pmVrCreDcto MONEY,@pmVrCreCos MONEY,@pmCreCant DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitRef VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50)
,@pmModalidad VARCHAR(10),@pmFecCorte SMALLDATETIME,@pmNumCorte INT,@pmModdDev VARCHAR(10),@pmNumAutoriza INT,@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevPla (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Planilla,IdCiaDoc,FecDoc,NitTercero,IdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb
,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,IdVend,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad,FecCorte,NumCorte,ModdDev
,NumAutoriza,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmPlanilla,@pmIdCiaDoc,@pmFecDoc,@pmNitTercero,@pmIdJornada,@pmVrSubTotal,@pmVrSobretasa,@pmVrImpGlobal,@pmVrOtros,@pmVrSobrantes
,@pmVrFaltantes,@pmVrCostos,@pmVrNeto,@pmVrEfectivo,@pmVrCreditos,@pmVrGastComb,@pmVrGastOtros,@pmVrPagosOtr,@pmCantidad,@pmVrConTotal,@pmVrConImp,@pmVrConDcto,@pmVrConCos,@pmConCant,@pmVrCreTotal
,@pmVrCreImp,@pmVrCreDcto,@pmVrCreCos,@pmCreCant,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmNitRef,@pmIdCCosto,@pmIdSubCos,@pmReferencia,@pmModalidad,@pmFecCorte,@pmNumCorte,@pmModdDev
,@pmNumAutoriza,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsDevPrestamos @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmPrestamo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFechaPrest SMALLDATETIME
,@pmTipoPlazo VARCHAR(10),@pmNumCuotas INT,@pmTipoCuota VARCHAR(10),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY,@pmTasaInteres DECIMAL(14,4),@pmCxPagar BIT,@pmFactura VARCHAR(20)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevPrestamos (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Prestamo,IdCiaDoc,FecDoc,FechaPrest,TipoPlazo,NumCuotas,TipoCuota,IdVehiculo,IdPropietario,IdConductor,VehPropio,ValorTotal,TasaInteres,CxPagar,Factura,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmPrestamo,@pmIdCiaDoc,@pmFecDoc,@pmFechaPrest,@pmTipoPlazo,@pmNumCuotas,@pmTipoCuota,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmVehPropio,@pmValorTotal,@pmTasaInteres,@pmCxPagar
,@pmFactura,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrRetencion MONEY
,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrAhorro MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4)
,@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmNumAutoriza INT,@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10)
,@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevRec (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdCCosto,IdSubCos,IdCajero,IdVend,TarifaCom,CodTarCom,IdLocal,EnEfectivo,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,NumAutoriza,Modalidad,ModdDev
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmRecibo,@pmIdCiaDoc,@pmFecDoc,@pmFecPago,@pmIdCliente,@pmIdAgencia,@pmVrRecibido,@pmVrSubTotal,@pmVrDescuento,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrAhorro,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmVrEfectivo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmIdCCosto,@pmIdSubCos
,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmIdLocal,@pmEnEfectivo,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmNumAutoriza,@pmModalidad,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsDevRecBuses @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmModalidad VARCHAR(10),@pmValorRecibido MONEY,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorOtrosDct MONEY
,@pmValorPagMas MONEY,@pmValorAhorro MONEY,@pmValorOConce MONEY,@pmValorNeto MONEY,@pmValorAplicado MONEY,@pmValorAnticipo MONEY,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmIdTarCom VARCHAR(4),@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20)
,@pmIdBanco VARCHAR(4),@pmEsParcial BIT,@pmIdClase VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevRecBuses (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,IdCliente,IdAgencia,IdVehiculo,IdConductor,VehPropio,Modalidad,ValorRecibido,ValorTotal,ValorDcto,ValorReteFte,ValorReteIva,ValorReteIca,ValorOtros,ValorOtrosDct,ValorPagMas,ValorAhorro,ValorOConce,ValorNeto,ValorAplicado,ValorAnticipo,IdVendedor,Comision,IdTarCom,IdCta,EnEfectivo
,NumCheque,IdBanco,EsParcial,IdClase,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmRecibo,@pmIdCiaDoc,@pmFecDoc,@pmIdCliente,@pmIdAgencia,@pmIdVehiculo,@pmIdConductor,@pmVehPropio,@pmModalidad,@pmValorRecibido,@pmValorTotal,@pmValorDcto,@pmValorReteFte,@pmValorReteIva,@pmValorReteIca,@pmValorOtros,@pmValorOtrosDct
,@pmValorPagMas,@pmValorAhorro,@pmValorOConce,@pmValorNeto,@pmValorAplicado,@pmValorAnticipo,@pmIdVendedor,@pmComision,@pmIdTarCom,@pmIdCta,@pmEnEfectivo,@pmNumCheque,@pmIdBanco,@pmEsParcial,@pmIdClase,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevSal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmSalida INT
,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4)
,@pmBaseImp MONEY,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmpVehiculo VARCHAR(10)
,@pmNContrato INT,@pmIdCiaCont CHAR(2),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmModRequis VARCHAR(10)
,@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevSal (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Salida,IdCiaDoc,FecDoc,IdTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,IdCCosto,IdSubCos,TipOrd,NumOrden
,IdCiaOrd,pVehiculo,Modalidad,NContrato,IdCiaCont,NitCliente,CdAgencia,ModRequis,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmSalida,@pmIdCiaDoc,@pmFecDoc,@pmIdTercero,@pmVrSubTotal,@pmVrImpuesto,@pmVrCostos,@pmVrNeto
,@pmCantidad,@pmBaseImp,@pmIdCCosto,@pmIdSubCos,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmpVehiculo,@pmModalidad,@pmNContrato,@pmIdCiaCont,@pmNitCliente,@pmCdAgencia,@pmModRequis,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDevVal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdTercero VARCHAR(16),@pmTotalPagos MONEY,@pmTotalCobros MONEY,@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmVrDescuento MONEY,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_DevVal (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,nRadica,IdCiaDoc,FecDoc,IdTercero,TotalPagos,TotalCobros,Referencia,CxPagar,VrDescuento,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmnRadica,@pmIdCiaDoc,@pmFecDoc,@pmIdTercero,@pmTotalPagos,@pmTotalCobros,@pmReferencia,@pmCxPagar,@pmVrDescuento,@pmModdDev,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDiarios @pmIdDiario VARCHAR(4),@pmDiario VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Diarios (IdDiario,Diario,Inactivo)
 VALUES (@pmIdDiario,@pmDiario,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDiferidos @pmIdDiferido VARCHAR(30),@pmNomDiferido VARCHAR(250),@pmIdTipDif VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdDep VARCHAR(4)
,@pmIdRespons VARCHAR(16),@pmIdCia CHAR(2),@pmFecInicio SMALLDATETIME,@pmValorTotal MONEY,@pmVrAdiciones MONEY,@pmVrDisminucion MONEY,@pmNumPeriodos INT,@pmFecFinAmtza SMALLDATETIME
,@pmAjustable BIT,@pmContable BIT,@pmVrAjuInfAcum MONEY,@pmVrAmtzaAcuHis MONEY,@pmVrAmtzaAcum MONEY,@pmVrAjuInfAmtza MONEY,@pmCuotasAmtza INT,@pmVrCorrMonDifAcu MONEY
,@pmCorrMonDiferida MONEY,@pmNumMesesDif INT,@pmFecUltAmtza SMALLDATETIME,@pmCueEnGrupo BIT,@pmIdCuenta VARCHAR(16),@pmIdCueAmortiza VARCHAR(16),@pmIdCueAjuDif VARCHAR(16),@pmIdCueCorrDif VARCHAR(16)
,@pmIdCueAjuAmtza VARCHAR(16),@pmIdCueCorrAA VARCHAR(16),@pmNumDoc VARCHAR(20),@pmReferencia VARCHAR(50),@pmDesUbicacion VARCHAR(150),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME
,@pmIdCausal VARCHAR(4),@pmComentRetiro VARCHAR(250),@pmComentarios VARCHAR(250),@pmTipoAmtza CHAR(1),@pmNitCliente VARCHAR(16),@pmAgenCliente VARCHAR(16),@pmCantGalones DECIMAL (14,4),@pmIdProv VARCHAR(16)
,@pmNumPlaca  VARCHAR(20),@pmCodInstala VARCHAR(4),@pmFecCreacion SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Diferidos (IdDiferido,NomDiferido,IdTipDif,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum
,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,DesUbicacion,IdEstado,FecRetiro,IdCausal
,ComentRetiro,Comentarios,FecCreacion,IdUsuario,TipoAmtza,NitCliente,AgenCliente,CantGalones,IdProv,NumPlaca,CodInstala)
 VALUES (@pmIdDiferido,@pmNomDiferido,@pmIdTipDif,@pmIdGrupo,@pmIdCCosto,@pmIdSubCos,@pmIdDep,@pmIdRespons,@pmIdCia,@pmFecInicio,@pmValorTotal,@pmVrAdiciones,@pmVrDisminucion,@pmNumPeriodos
,@pmFecFinAmtza,@pmAjustable,@pmContable,@pmVrAjuInfAcum,@pmVrAmtzaAcuHis,@pmVrAmtzaAcum,@pmVrAjuInfAmtza,@pmCuotasAmtza,@pmVrCorrMonDifAcu,@pmCorrMonDiferida,@pmNumMesesDif
,@pmFecUltAmtza,@pmCueEnGrupo,@pmIdCuenta,@pmIdCueAmortiza,@pmIdCueAjuDif,@pmIdCueCorrDif,@pmIdCueAjuAmtza,@pmIdCueCorrAA,@pmNumDoc,@pmReferencia,@pmDesUbicacion,@pmIdEstado,@pmFecRetiro,@pmIdCausal
,@pmComentRetiro,@pmComentarios,@pmFecCreacion,@pmIdUsuario,@pmTipoAmtza,@pmNitCliente,@pmAgenCliente,@pmCantGalones,@pmIdProv,@pmNumPlaca,@pmCodInstala)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsDisciplinario  @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecSuceso SMALLDATETIME,@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmIdRuta VARCHAR(4),@pmLugar VARCHAR(150),@pmNomReporta VARCHAR(150)
,@pmCedReporta VARCHAR(16),@pmVehiculoRep VARCHAR(10),@pmRepCond BIT,@pmCoordinador VARCHAR(150),@pmDespachador VARCHAR(150),@pmCitaDescargos BIT,@pmFecCita SMALLDATETIME,@pmMemoCita VARCHAR(50),@pmImputacion VARCHAR(2500),@pmDescargos VARCHAR(3000),@pmFecDesc SMALLDATETIME,@pmRecibeDesc VARCHAR(150)
,@pmTipoSancion VARCHAR(10),@pmDescSancion VARCHAR(1000),@pmFecSancion SMALLDATETIME,@pmCantSancion DECIMAL(14,4),@pmCodInfracc INT,@pmReferencia VARCHAR(50),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Disciplinario (TipDoc,Numero,IdCia,Fecha,FecSuceso,IdVehiculo,IdPropietario,IdConductor,IdRuta,Lugar,NomReporta,CedReporta,VehiculoRep,RepCond,Coordinador,Despachador,CitaDescargos,FecCita,MemoCita,Imputacion,Descargos,FecDesc,RecibeDesc,TipoSancion,DescSancion,FecSancion,CantSancion,CodInfracc,Referencia,Observacion,Anulado,NumDev,FecDev,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumero,@pmIdCia,@pmFecha,@pmFecSuceso,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmIdRuta,@pmLugar,@pmNomReporta,@pmCedReporta,@pmVehiculoRep,@pmRepCond,@pmCoordinador,@pmDespachador,@pmCitaDescargos,@pmFecCita,@pmMemoCita,@pmImputacion,@pmDescargos,@pmFecDesc,@pmRecibeDesc,@pmTipoSancion
,@pmDescSancion,@pmFecSancion,@pmCantSancion,@pmCodInfracc,@pmReferencia,@pmObservacion,@pmAnulado,@pmNumDev,@pmFecDev,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsDocMemo @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmComentarios VARCHAR(6500)
,@pmNota1 VARCHAR(500),@pmNota2 VARCHAR(500),@pmNota3 VARCHAR(500)
 AS 
 INSERT INTO Trn_DocMemo (TipDoc,Documento,IdCia,Comentarios,Nota1,Nota2,Nota3)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmComentarios,@pmNota1,@pmNota2,@pmNota3)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEdsCalibra @pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitTercero VARCHAR(16),@pmCdJornada VARCHAR(4),@pmVrSubTotal MONEY
,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_EdsCalibra (TipDoc,nNota,IdCia,Fecha,IdConcepto,NitTercero,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto,Cantidad,IdCajero,IdVend,TarifaCom,CodTarCom,IdCCosto,IdSubCos
,Referencia,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmnNota,@pmIdCia,@pmFecha,@pmIdConcepto,@pmNitTercero,@pmCdJornada,@pmVrSubTotal,@pmVrSobretasa,@pmVrImpGlobal,@pmVrOtros,@pmVrCostos,@pmVrNeto,@pmCantidad
,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmIdCCosto,@pmIdSubCos,@pmReferencia,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEdsCortes @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitTercero VARCHAR(16),@pmIdJornada VARCHAR(4),@pmVrSubTotal MONEY,@pmVrSobretasa MONEY
,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrSobrantes MONEY,@pmVrFaltantes MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrCreditos MONEY,@pmVrGastComb MONEY,@pmVrGastOtros MONEY,@pmVrPagosOtr MONEY
,@pmCantidad DECIMAL(14,4),@pmVrConTotal MONEY,@pmVrConImp MONEY,@pmVrConDcto MONEY,@pmVrConCos MONEY,@pmConCant DECIMAL(14,4),@pmVrCreTotal MONEY,@pmVrCreImp MONEY,@pmVrCreDcto MONEY,@pmVrCreCos MONEY,@pmCreCant DECIMAL(14,4)
,@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitRef VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmModalidad VARCHAR(10),@pmFecCorte SMALLDATETIME
,@pmNumCorte INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_EdsCortes (TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,IdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos
,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,IdVend,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad,FecCorte,NumCorte,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmPlanilla,@pmIdCia,@pmFecha,@pmIdConcepto,@pmNitTercero,@pmIdJornada,@pmVrSubTotal,@pmVrSobretasa,@pmVrImpGlobal,@pmVrOtros,@pmVrSobrantes,@pmVrFaltantes,@pmVrCostos,@pmVrNeto,@pmVrEfectivo,@pmVrCreditos,@pmVrGastComb
,@pmVrGastOtros,@pmVrPagosOtr,@pmCantidad,@pmVrConTotal,@pmVrConImp,@pmVrConDcto,@pmVrConCos,@pmConCant,@pmVrCreTotal,@pmVrCreImp,@pmVrCreDcto,@pmVrCreCos,@pmCreCant,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmNitRef,@pmIdCCosto,@pmIdSubCos
,@pmReferencia,@pmModalidad,@pmFecCorte,@pmNumCorte,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEdsForItems @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmItem INT,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCantidad DECIMAL(14,4)
,@pmVrPrecio MONEY,@pmTarifaIva DECIMAL(14,4),@pmVrIva MONEY,@pmTarifaDct DECIMAL(14,4),@pmVrDcto MONEY,@pmServicios BIT,@pmDescripcion VARCHAR(150),@pmListaPrec CHAR(1)
 AS 
 INSERT INTO Trn_EdsForItems (TipoRango,IdRango,NumSerie,NumForma,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,Servicios,Descripcion,ListaPrec)
 VALUES (@pmTipoRango,@pmIdRango,@pmNumSerie,@pmNumForma,@pmItem,@pmIdProducto,@pmIdBodega,@pmCantidad,@pmVrPrecio,@pmTarifaIva,@pmVrIva,@pmTarifaDct,@pmVrDcto,@pmServicios,@pmDescripcion,@pmListaPrec)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEdsFormas @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME
,@pmVrTotal MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCajero VARCHAR(11)
,@pmIdVend VARCHAR(16),@pmCdProducto VARCHAR(16),@pmCantidad DECIMAL(14,4),@pmIdForma VARCHAR(4),@pmItem INT,@pmReferencia VARCHAR(50),@pmCantPuntos DECIMAL(14,4)
,@pmObservacion VARCHAR(150),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmAgencDcm VARCHAR(16),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16)
 AS 
 INSERT INTO Trn_EdsFormas (TipoRango,IdRango,NumSerie,NumForma,Planilla,IdCia,Fecha,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo,IdCajero,IdVend,CdProducto,Cantidad
,IdForma,Item,Referencia,CantPuntos,Observacion,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm,Anulado,NumDev,FecDev,CdCCosto,CdSubCos)
 VALUES (@pmTipoRango,@pmIdRango,@pmNumSerie,@pmNumForma,@pmPlanilla,@pmIdCia,@pmFecha,@pmVrTotal,@pmIdCliente,@pmIdAgencia,@pmIdCuenta,@pmpVehiculo
,@pmnVehiculo,@pmIdCajero,@pmIdVend,@pmCdProducto,@pmCantidad,@pmIdForma,@pmItem,@pmReferencia,@pmCantPuntos,@pmObservacion
,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmFechaDcm,@pmAgencDcm,@pmAnulado,@pmNumDev,@pmFecDev,@pmCdCCosto,@pmCdSubCos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEdsRangos @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmIdForma VARCHAR(4),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCuenta VARCHAR(16),@pmNumSerie VARCHAR(5),@pmNumInicial INT,@pmNumFinal INT,@pmNumTarjeta VARCHAR(30)
,@pmVrCupo MONEY,@pmVrConsumos MONEY,@pmTipoConsumo INT,@pmReqPlaca BIT,@pmReqProd BIT,@pmReqGalones BIT,@pmVldAgencia BIT,@pmVldPlaca VARCHAR(10),@pmModalidad VARCHAR(10)
,@pmAnulado BIT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmNumAuto BIT,@pmNumActual INT,@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_EdsRangos (TipoRango,IdRango,IdForma,Fecha,FecVence,IdCliente,IdAgencia,pVehiculo,nVehiculo,IdCuenta,NumSerie,NumInicial,NumFinal,NumTarjeta,VrCupo,VrConsumos,TipoConsumo,ReqPlaca
,ReqProd,ReqGalones,VldAgencia,VldPlaca,Modalidad,Anulado,Observacion,IdEstado,NumAuto,NumActual,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipoRango,@pmIdRango,@pmIdForma,@pmFecha,@pmFecVence,@pmIdCliente,@pmIdAgencia,@pmpVehiculo,@pmnVehiculo,@pmIdCuenta,@pmNumSerie,@pmNumInicial,@pmNumFinal,@pmNumTarjeta
,@pmVrCupo,@pmVrConsumos,@pmTipoConsumo,@pmReqPlaca,@pmReqProd,@pmReqGalones,@pmVldAgencia,@pmVldPlaca,@pmModalidad,@pmAnulado,@pmObservacion,@pmIdEstado,@pmNumAuto,@pmNumActual,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsEdsServicio @pmIdEstacion VARCHAR(4),@pmEstServicio VARCHAR(50),@pmDireccion VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO EdsServicio (IdEstacion,EstServicio,Direccion,Inactivo)
 VALUES (@pmIdEstacion,@pmEstServicio,@pmDireccion,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEgrFactura @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmIdProveedor VARCHAR(16)
,@pmIdCuenta VARCHAR(16),@pmVrAbono MONEY,@pmEsEgreso BIT,@pmItemCom INT,@pmpVehiculo VARCHAR(10),@pmVehPropio BIT,@pmTipDev VARCHAR(3),@pmNumDev INT,@pmDetAbono VARCHAR(100)
 AS 
 INSERT INTO Trn_EgrFactura (TipCom,Comprobante,IdCia,Item,Fecha,TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,IdCuenta,VrAbono,EsEgreso,ItemCom,pVehiculo,VehPropio,TipDev,NumDev,DetAbono)
 VALUES (@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmFecha,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmIdProveedor,@pmIdCuenta,@pmVrAbono
,@pmEsEgreso,@pmItemCom,@pmpVehiculo,@pmVehPropio,@pmTipDev,@pmNumDev,@pmDetAbono)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEgrFactura_Uni  @pmIdProveedor VARCHAR(16),@pmNewProveedor VARCHAR(16)
 AS 
 INSERT INTO Trn_EgrFactura (TipCom,Comprobante,IdCia,Item,Fecha,TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,IdCuenta,VrAbono,EsEgreso,ItemCom,pVehiculo,VehPropio,TipDev,NumDev,DetAbono)
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,TipFac,Factura,IdCiaFac,ItemFac,@pmNewProveedor,IdCuenta,VrAbono,EsEgreso,ItemCom,pVehiculo,VehPropio,TipDev,NumDev,DetAbono
 FROM Trn_EgrFactura WHERE IdProveedor=@pmIdProveedor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmp_Contrato @pmNContrato INT,@pmIdEmpleado VARCHAR(16),@pmIdTipCon VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmIndefinido BIT,@pmIdCargo VARCHAR(4),@pmIdJornada VARCHAR(4)
,@pmIdDep VARCHAR(4),@pmIdArea VARCHAR(4),@pmIdInstala VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCia CHAR(2),@pmIdNom VARCHAR(4),@pmTipoLiquida CHAR(1),@pmModPromdio CHAR(1),@pmVrSalario MONEY,@pmVrAuxTrans MONEY,@pmSalMinimo BIT,@pmSalIntegral BIT
,@pmFPnomina VARCHAR(10),@pmModoRet INT,@pmFecAfiSeg SMALLDATETIME,@pmATFinMes BIT,@pmSegFinMes BIT,@pmDenyPEN BIT,@pmDenyEPS BIT,@pmDenyARP BIT,@pmDenyCaj BIT,@pmDenyCes BIT,@pmDenyPri BIT,@pmDenyVac BIT,@pmDenyHex BIT,@pmBasePrest CHAR(2)
,@pmBaseSeg CHAR(2),@pmDchDotacion BIT,@pmDchCafeteria BIT,@pmReclmto VARCHAR(50),@pmNotas VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmIdClase VARCHAR(4),@pmTipoTrabj INT,@pmIdCot VARCHAR(4),@pmVrUpc MONEY,@pmIdGrupo VARCHAR(4)
,@pmDenyDcto BIT,@pmSubTipoCot VARCHAR(3),@pmDsoSabado BIT,@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Emp_Contrato (NContrato,IdEmpleado,IdTipCon,FecIngreso,FecVigencia,FecRetiro,Indefinido,IdCargo,IdJornada,IdDep,IdArea,IdInstala,IdCCosto,IdCia,IdNom,TipoLiquida,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS
,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,IdEstado,Inactivo,IdSubCos,IdClase,TipoTrabj,FecAdd,IdUsuario,IdCot,VrUpc,IdGrupo,DenyDcto,SubTipoCot,DsoSabado)
 VALUES (@pmNContrato,@pmIdEmpleado,@pmIdTipCon,@pmFecIngreso,@pmFecVigencia,@pmFecRetiro,@pmIndefinido,@pmIdCargo,@pmIdJornada,@pmIdDep,@pmIdArea,@pmIdInstala,@pmIdCCosto,@pmIdCia,@pmIdNom,@pmTipoLiquida,@pmModPromdio,@pmVrSalario,@pmVrAuxTrans,@pmSalMinimo
,@pmSalIntegral,@pmFPnomina,@pmModoRet,@pmFecAfiSeg,@pmATFinMes,@pmSegFinMes,@pmDenyPEN,@pmDenyEPS,@pmDenyARP,@pmDenyCaj,@pmDenyCes,@pmDenyPri,@pmDenyVac,@pmDenyHex,@pmBasePrest,@pmBaseSeg,@pmDchDotacion,@pmDchCafeteria,@pmReclmto,@pmNotas
,@pmIdEstado,@pmInactivo,@pmIdSubCos,@pmIdClase,@pmTipoTrabj,@pmFecAdd,@pmIdUsuario,@pmIdCot,@pmVrUpc,@pmIdGrupo,@pmDenyDcto,@pmSubTipoCot,@pmDsoSabado)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmp_Estudios @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmEstblecmto VARCHAR(50)
,@pmTitulo VARCHAR(50),@pmFecha SMALLDATETIME
 AS 
 INSERT INTO Emp_Estudios (IdEmpleado,Item,Estblecmto,Titulo,Fecha)
 VALUES (@pmIdEmpleado,@pmItem,@pmEstblecmto,@pmTitulo,@pmFecha)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmp_ExpLaboral @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmEmpresa VARCHAR(50),@pmCargo VARCHAR(20)
,@pmFecInicio SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmDirEmpresa VARCHAR(100),@pmTelEmpresa VARCHAR(20)
 AS 
 INSERT INTO Emp_ExpLaboral (IdEmpleado,Item,Empresa,Cargo,FecInicio,FecRetiro,DirEmpresa,TelEmpresa)
 VALUES (@pmIdEmpleado,@pmItem,@pmEmpresa,@pmCargo,@pmFecInicio,@pmFecRetiro,@pmDirEmpresa,@pmTelEmpresa)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmp_Parientes @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmParntsco VARCHAR(10),@pmNombre VARCHAR(50)
,@pmFecNacmto SMALLDATETIME,@pmUltGrado VARCHAR(20),@pmDirccion VARCHAR(100),@pmTelefno VARCHAR(20)
 AS 
 INSERT INTO Emp_Parientes (IdEmpleado,Item,Parntsco,Nombre,FecNacmto,UltGrado,Dirccion,Telefno)
 VALUES (@pmIdEmpleado,@pmItem,@pmParntsco,@pmNombre,@pmFecNacmto,@pmUltGrado,@pmDirccion,@pmTelefno)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmpaques @pmIdEmp VARCHAR(4),@pmEmpaque VARCHAR(50)
,@pmCodMintra VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Empaques (IdEmp,Empaque,CodMintra,Inactivo)
 VALUES (@pmIdEmp,@pmEmpaque,@pmCodMintra,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmpleados @pmIdEmpleado VARCHAR(16),@pmApellidos VARCHAR(80),@pmNombres VARCHAR(70),@pmCodigo VARCHAR(16),@pmTipoId CHAR(1),@pmDv CHAR(1),@pmIdLugarCed VARCHAR(8),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmTelMovil VARCHAR(20),@pme_mail VARCHAR(100),@pmVvdaPropia BIT, @pmFecNacmto SMALLDATETIME,@pmLugarNacmto VARCHAR(30)
,@pmTipoSangre CHAR(2),@pmFactorRh CHAR(1),@pmSexo CHAR(1),@pmIdEstCivil VARCHAR(4),@pmNHijos INT,@pmLibMilitar VARCHAR(25),@pmClaseLib INT,@pmFechaLib SMALLDATETIME,@pmDistMil VARCHAR(10),@pmCertJudicial VARCHAR(25),@pmFecJudicial SMALLDATETIME,@pmVigJudicial SMALLDATETIME,@pmPasporte VARCHAR(25),@pmVigPaspte SMALLDATETIME,@pmLicencia VARCHAR(25),@pmIdLugar VARCHAR(8)
,@pmCatLicencia INT,@pmFecLicencia SMALLDATETIME,@pmVigLicencia SMALLDATETIME,@pmIdProf VARCHAR(4),@pmTallaCam CHAR(2),@pmTallaPan CHAR(2),@pmTallaZap CHAR(2),@pmUsaGafas BIT,@pmConyugue VARCHAR(50),@pmConyFecNac SMALLDATETIME,@pmConyTrabaja BIT,@pmConyEmpresa VARCHAR(50),@pmConyCargo VARCHAR(20),@pmConyFing SMALLDATETIME,@pmConyTel VARCHAR(20),@pmIdFonPen VARCHAR(8)
,@pmIdFonEps VARCHAR(8),@pmIdFonArp VARCHAR(8),@pmIdFonCes VARCHAR(8),@pmIdCajaCom VARCHAR(8),@pmCdFonAvp VARCHAR(8),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(25),@pmIdBanco VARCHAR(4),@pmNContrato INT,@pmFecVincula SMALLDATETIME,@pmFecUltAum SMALLDATETIME,@pmFecUltCes SMALLDATETIME,@pmFecUltPri SMALLDATETIME,@pmFecUltVac SMALLDATETIME,@pmFecUltIng SMALLDATETIME,@pmFecUltRet SMALLDATETIME,@pmFecUltDot SMALLDATETIME
,@pmFecUltExm SMALLDATETIME,@pmFecUltCap SMALLDATETIME,@pmNCAnterior INT,@pmFecIngAnt SMALLDATETIME,@pmFecRetAnt SMALLDATETIME,@pmFotoEmp VARCHAR(30),@pmFotoLic VARCHAR(30),@pmFotoFirma VARCHAR(30),@pmEsCondtor BIT,@pmpVehiculo VARCHAR(10),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmCdLocCue VARCHAR(8),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Empleados (IdEmpleado,Apellidos,Nombres,Codigo,TipoId,Dv,IdLugarCed,Direccion,IdLocal,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdProf,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,IdFonPen
,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,IdClase,NumCuenta,IdBanco,NContrato,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecUltDot,FecUltExm,FecUltCap,FotoEmp,FotoLic,FotoFirma,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,Observacion,CdFonAvp,IdEstado,Inactivo,CdLocCue,FechaAdd,IdUsuario)
 VALUES (@pmIdEmpleado,@pmApellidos,@pmNombres,@pmCodigo,@pmTipoId,@pmDv,@pmIdLugarCed,@pmDireccion,@pmIdLocal,@pmTelefono,@pmTelMovil,@pme_mail,@pmVvdaPropia,@pmFecNacmto,@pmLugarNacmto,@pmTipoSangre,@pmFactorRh,@pmSexo,@pmIdEstCivil,@pmNHijos,@pmLibMilitar,@pmClaseLib,@pmFechaLib,@pmDistMil,@pmCertJudicial,@pmFecJudicial,@pmVigJudicial,@pmPasporte,@pmVigPaspte,@pmLicencia,@pmIdLugar,@pmCatLicencia,@pmFecLicencia,@pmVigLicencia
,@pmIdProf,@pmTallaCam,@pmTallaPan,@pmTallaZap,@pmUsaGafas,@pmConyugue,@pmConyFecNac,@pmConyTrabaja,@pmConyEmpresa,@pmConyCargo,@pmConyFing,@pmConyTel,@pmIdFonPen,@pmIdFonEps,@pmIdFonArp,@pmIdFonCes,@pmIdCajaCom,@pmIdClase,@pmNumCuenta,@pmIdBanco,@pmNContrato,@pmFecVincula,@pmFecUltAum,@pmFecUltCes,@pmFecUltPri,@pmFecUltVac,@pmFecUltIng,@pmFecUltRet,@pmFecUltDot,@pmFecUltExm,@pmFecUltCap,@pmFotoEmp,@pmFotoLic
,@pmFotoFirma,@pmNCAnterior,@pmFecIngAnt,@pmFecRetAnt,@pmEsCondtor,@pmpVehiculo,@pmObservacion,@pmCdFonAvp,@pmIdEstado,@pmInactivo,@pmCdLocCue,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEmpleados_Sel @pmIdEmpleado VARCHAR(16),@pmNewCedula VARCHAR(16),@pmNewCodigo VARCHAR(16)
,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Empleados (IdEmpleado,Apellidos,Nombres,Codigo,TipoId,Dv,IdLugarCed,Direccion,IdLocal,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdProf,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,IdFonPen
,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,IdClase,NumCuenta,IdBanco,NContrato,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecUltDot,FecUltExm,FecUltCap,FotoEmp,FotoLic,FotoFirma,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,Observacion,IdEstado,CdFonAvp,Inactivo,CdLocCue,FechaAdd,IdUsuario)
 SELECT  @pmNewCedula,Apellidos,Nombres,@pmNewCodigo,TipoId,Dv,IdLugarCed,Direccion,IdLocal,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdProf,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,IdFonPen
,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,IdClase,NumCuenta,IdBanco,NContrato,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecUltDot,FecUltExm,FecUltCap,FotoEmp,FotoLic,FotoFirma,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,Observacion,IdEstado,CdFonAvp,Inactivo,CdLocCue,@pmFechaAdd,@pmIdUsuario
 FROM   Empleados WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEntArticulos @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmEntradas DECIMAL(14,4)
,@pmSalidas DECIMAL(14,4),@pmVrUnitario MONEY,@pmTarifaIva DECIMAL(14,4),@pmVrIva MONEY,@pmTarifaDct DECIMAL(14,4),@pmVrDcto MONEY,@pmTipOrd VARCHAR(3)
,@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2)
 AS 
 INSERT INTO Trn_EntArticulos (TipDoc,Entrada,IdCia,Item,IdProducto,IdBodega,Entradas,Salidas,VrUnitario,TarifaIva,VrIva,TarifaDct,VrDcto,TipOrd,NumOrden,IdCiaOrd,TipSal,NumSalida,IdCiaSal)
 VALUES (@pmTipDoc,@pmEntrada,@pmIdCia,@pmItem,@pmIdProducto,@pmIdBodega,@pmEntradas,@pmSalidas,@pmVrUnitario,@pmTarifaIva,@pmVrIva,@pmTarifaDct
,@pmVrDcto,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmTipSal,@pmNumSalida,@pmIdCiaSal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEntidades @pmIdTabla VARCHAR(20),@pmNivelRel INT,@pmCamposPK VARCHAR(50),@pmFngKey BIT
,@pmSp_Qry VARCHAR(30),@pmSp_Qpmt VARCHAR(50),@pmSp_Ins VARCHAR(30),@pmSp_Upd VARCHAR(30),@pmSp_Del VARCHAR(30)
 AS 
 INSERT INTO Sys_Entidades (IdTabla,NivelRel,CamposPK,FngKey,Sp_Qry,Sp_Qpmt,Sp_Ins,Sp_Upd,Sp_Del)
 VALUES (@pmIdTabla,@pmNivelRel,@pmCamposPK,@pmFngKey,@pmSp_Qry,@pmSp_Qpmt,@pmSp_Ins,@pmSp_Upd,@pmSp_Del)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEntradas @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY
,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmTipOdc VARCHAR(3)
,@pmOCompra INT,@pmIdCiaOdc CHAR(2),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4)
,@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmCodTarRetFle VARCHAR(4),@pmCodTarIcaFle VARCHAR(4),@pmFletesCosto BIT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Entradas (TipDoc,Entrada,IdCia,Fecha,IdConcepto,IdProv,Factura,FechaFac,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,IdPlazo,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva
,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmEntrada,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdProv,@pmFactura,@pmFechaFac,@pmFechaVence,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrOtros,@pmVrSobretasa,@pmVrImpGlobal,@pmVrCargos,@pmVrOtrDcto,@pmVrNeto,@pmCantidad,@pmNitFletes
, @pmVrRetFlete,@pmVrIcaFlete,@pmCodTarRetFle,@pmCodTarIcaFle,@pmFletesCosto,@pmTipOdc,@pmOCompra,@pmIdCiaOdc,@pmMulPlazos,@pmIdPlazo,@pmCxPagar,@pmDocEquiv,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmCdCCosto,@pmCdSubCos,@pmTipEgr,@pmEgreso,@pmIdCiaEgr,@pmModalidad,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstaciones @pmIdEstacion CHAR(2),@pmEstacion VARCHAR(50)
 AS 
 INSERT INTO Sys_Estaciones (IdEstacion,Estacion)
 VALUES (@pmIdEstacion,@pmEstacion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstadoCiv @pmIdEstado VARCHAR(4),@pmEstCivil VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO EstadoCiv (IdEstado,EstCivil,Inactivo)
 VALUES (@pmIdEstado,@pmEstCivil,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstadoDoc @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT
,@pmCodDcmto VARCHAR(3),@pmInactivo BIT
 AS 
 INSERT INTO EstadoDoc (IdEstado,Estado,NColor,CodDcmto,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmCodDcmto,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstadoPro @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO EstadoPro (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstadoSer @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO EstadoSer (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstadoTer @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO EstadoTer (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstadoVeh @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmOutDemand BIT,@pmInactivo BIT
 AS 
 INSERT INTO EstadoVeh (IdEstado,Estado,NColor,OutDemand,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmOutDemand,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsEstratos @pmIdEstrato VARCHAR(4),@pmEstrato VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Estratos (IdEstrato,Estrato,Inactivo)
 VALUES (@pmIdEstrato,@pmEstrato,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsExpLicencias @pmIdLugar VARCHAR(8),@pmLugarLic VARCHAR(50),@pmCodDpto VARCHAR(8)
,@pmDpto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO ExpLicencias (IdLugar,LugarLic,CodDpto,Dpto,Inactivo)
 VALUES (@pmIdLugar,@pmLugarLic,@pmCodDpto,@pmDpto,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFactVales @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT
,@pmTipRad VARCHAR(3),@pmnRadica INT,@pmIdCiaRad CHAR(2),@pmItemRad INT
 AS 
 INSERT INTO Trn_FactVales (TipDoc,Factura,IdCia,Item,TipRad,nRadica,IdCiaRad,ItemRad)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmItem,@pmTipRad,@pmnRadica,@pmIdCiaRad,@pmItemRad)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFactcon @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmnClieCon VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrCargos MONEY
,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4),@pmCantPuntos DECIMAL(14,4),@pmPuntosAcum DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4)
,@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmReferncia1 VARCHAR(50),@pmReferncia2 VARCHAR(50),@pmCdBanco VARCHAR(4),@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFecPedido SMALLDATETIME
,@pmModalidad VARCHAR(10),@pmPlacaVehic VARCHAR(10),@pmKmtVehic INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Factcon (TipDoc,Factura,IdCia,Fecha,IdConcepto,nClieCon,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCajero,IdVend,TarifaCom
,CodTarCom,NitCliente,CdAgencia,IdCCosto,IdSubCos,IdLocal,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,FecPedido,Modalidad,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmFecha,@pmIdConcepto,@pmnClieCon,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrCargos,@pmVrOtrDcto,@pmVrCostos,@pmVrNeto,@pmVrEfectivo,@pmVrPagosOtr,@pmCantidad,@pmCantPuntos,@pmPuntosAcum,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv
,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmNitCliente,@pmCdAgencia,@pmIdCCosto,@pmIdSubCos,@pmIdLocal,@pmEnEfectivo,@pmCdForma,@pmNumForma,@pmDetallePago,@pmReferncia1,@pmReferncia2,@pmCdBanco,@pmTipPed,@pmPedido,@pmIdCiaPed
,@pmTipRem,@pmRemision,@pmIdCiaRem,@pmFecPedido,@pmModalidad,@pmPlacaVehic,@pmKmtVehic,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFacturas @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY
,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrFaltantes MONEY,@pmVrAnticipos MONEY
,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmCantidad DECIMAL(14,4),@pmCantPuntos DECIMAL(14,4),@pmPuntosAcum DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4)
,@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250),@pmIdLocEnv VARCHAR(8)
,@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5)
,@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmCdRuta VARCHAR(4),@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmTipCot VARCHAR(3)
,@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmFecPedido SMALLDATETIME,@pmAutzaMora INT,@pmAutzaCupo INT,@pmModalidad VARCHAR(10),@pmKmtVehic INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmZonaFrontera BIT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Facturas (TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,CdRuta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem
,TipCot,Cotizacion,IdCiaCot,FecPedido,AutzaMora,AutzaCupo,Modalidad,KmtVehic,ZonaFrontera,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFechaVence,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrFletes,@pmVrOtros,@pmVrCargos,@pmVrOtrDcto,@pmVrCostos,@pmVrSobretasa,@pmVrImpGlobal,@pmVrFaltantes,@pmVrAnticipos,@pmVrNeto,@pmVrAplicado,@pmCantidad,@pmCantPuntos,@pmPuntosAcum,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmIdCCosto,@pmIdSubCos,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmDirEnvio
,@pmIdLocEnv,@pmDiasEntraga,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargoContac,@pmIdForma,@pmDetallePago,@pmMulPlazos,@pmIdPlazo,@pmCdMney,@pmNitEmpTrans,@pmEmpTrans,@pmpVehiculo,@pmCdConductor,@pmCdRuta,@pmTipPed,@pmPedido,@pmIdCiaPed,@pmTipRem,@pmRemision,@pmIdCiaRem,@pmTipCot,@pmCotizacion,@pmIdCiaCot,@pmFecPedido,@pmAutzaMora,@pmAutzaCupo,@pmModalidad,@pmKmtVehic,@pmZonaFrontera,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFcAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME
,@pmIdVehiculo VARCHAR(10),@pmnSemana INT,@pmVrAbono MONEY,@pmDescripcion VARCHAR(150)
 AS 
 INSERT INTO Trn_FcAbonos (TipRec,Recibo,IdCia,Item,Fecha,IdVehiculo,nSemana,VrAbono,Descripcion)
 VALUES (@pmTipRec,@pmRecibo,@pmIdCia,@pmItem,@pmFecha,@pmIdVehiculo,@pmnSemana,@pmVrAbono,@pmDescripcion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFcCausacion @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecCausac SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmVrNeto MONEY,@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FcCausacion (TipDoc,NumDoc,IdCia,Fecha,FecCausac,IdConcepto,VrNeto,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumDoc,@pmIdCia,@pmFecha,@pmFecCausac,@pmIdConcepto,@pmVrNeto,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado
,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFcNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT
,@pmVrNota MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmTipRec VARCHAR(3),@pmReciboFc INT,@pmIdCiaRec CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FcNotas (TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdVehiculo,IdPropietario,IdConductor,VehPropio,VrNota,VrNeto,VrAplicado,CdCCosto,CdSubCos,Referencia,TipRec,ReciboFc,IdCiaRec,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmVehPropio,@pmVrNota,@pmVrNeto,@pmVrAplicado,@pmCdCCosto,@pmCdSubCos,@pmReferencia,@pmTipRec,@pmReciboFc,@pmIdCiaRec
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFcSemanas @pmIdVehiculo VARCHAR(10),@pmnSemana INT,@pmNumCargue INT,@pmCiaCargue CHAR(2),@pmTipoSem INT,@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmVrCargo MONEY
,@pmVrOtros MONEY,@pmVrPagado MONEY,@pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCiaRec CHAR(2),@pmFechaPago SMALLDATETIME,@pmObservacion VARCHAR(250)
, @pmFecSemIni SMALLDATETIME, @pmFecSemFin SMALLDATETIME,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FcSemanas (IdVehiculo,nSemana,NumCargue,CiaCargue,TipoSem,Fecha,FecVence,VrCargo,VrOtros,VrPagado,TipRec,Recibo,IdCiaRec,FechaPago,Observacion,FecSemIni,FecSemFin,TimeSys,IdUsuario)
 VALUES (@pmIdVehiculo,@pmnSemana,@pmNumCargue,@pmCiaCargue,@pmTipoSem,@pmFecha,@pmFecVence,@pmVrCargo,@pmVrOtros,@pmVrPagado,@pmTipRec,@pmRecibo,@pmIdCiaRec,@pmFechaPago
,@pmObservacion,@pmFecSemIni,@pmFecSemFin,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFields @pmIdEntidad VARCHAR(30),@pmIdCampo VARCHAR(20),@pmDescripcion VARCHAR(50),@pmTipoDato VARCHAR(15),@pmLongMax INT,@pmRequerido BIT,@pmPmryKey BIT
,@pmFmtValue VARCHAR(20),@pmDftValue VARCHAR(20),@pmAliasFld VARCHAR(20),@pmAliasQry2 VARCHAR(20),@pmIndField INT,@pmTipoEntidad VARCHAR(5),@pmLstTipo VARCHAR(5),@pmLstClnaValor VARCHAR(20)
,@pmLstClnaLista VARCHAR(50),@pmLstQuery VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO Sys_Fields (IdEntidad,IdCampo,Descripcion,TipoDato,LongMax,Requerido,PmryKey,FmtValue,DftValue,AliasFld,AliasQry2,IndField,TipoEntidad,LstTipo,LstClnaValor,LstClnaLista,LstQuery,Inactivo)
 VALUES (@pmIdEntidad,@pmIdCampo,@pmDescripcion,@pmTipoDato,@pmLongMax,@pmRequerido,@pmPmryKey,@pmFmtValue,@pmDftValue,@pmAliasFld,@pmAliasQry2,@pmIndField,@pmTipoEntidad
,@pmLstTipo,@pmLstClnaValor,@pmLstClnaLista,@pmLstQuery,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFijos @pmIdCargo VARCHAR(4),@pmConcepto VARCHAR(100), @pmObligatorio BIT, @pmTipoAplica VARCHAR(10),  @pmDiasSemana VARCHAR(20), @pmDiasMes VARCHAR(100)
, @pmValorMensual MONEY, @pmValorDiario MONEY, @pmIdCuentaDeb VARCHAR(16), @pmIdCuentaCre VARCHAR(16), @pmIdCueDebVp VARCHAR(16), @pmIdCueCreVp VARCHAR(16),@pmInactivo BIT
,@pmNCuotas INT,@pmDiasVcmto VARCHAR(50),@pmOrdenAbono INT,@pmTipoDcto CHAR(1),@pmDctoPpago DECIMAL(14,4),@pmVrCargoAdic MONEY,@pmFecLimiteIni SMALLDATETIME,@pmFecLimiteFin SMALLDATETIME
,@pmFechaAdd SMALLDATETIME,  @pmIdUsuario VARCHAR(11) 
AS
 INSERT INTO Fijos ( IdCargo, Concepto, Obligatorio, TipoAplica, DiasSemana, DiasMes, ValorMensual, ValorDiario, IdCuentaDeb, IdCuentaCre, IdCueDebVp, IdCueCreVp
,NCuotas,DiasVcmto,OrdenAbono,TipoDcto,DctoPpago,VrCargoAdic,FecLimiteIni,FecLimiteFin,Inactivo, FechaAdd, IdUsuario)
 VALUES (@pmIdCargo,@pmConcepto, @pmObligatorio, @pmTipoAplica, @pmDiasSemana, @pmDiasMes
, @pmValorMensual, @pmValorDiario, @pmIdCuentaDeb, @pmIdCuentaCre, @pmIdCueDebVp, @pmIdCueCreVp
,@pmNCuotas,@pmDiasVcmto,@pmOrdenAbono,@pmTipoDcto,@pmDctoPpago,@pmVrCargoAdic,@pmFecLimiteIni,@pmFecLimiteFin,@pmInactivo, @pmFechaAdd,  @pmIdUsuario )
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFijos_Cla @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4),@pmValorMensual MONEY,@pmValorDiario MONEY
AS
 INSERT INTO  Fijos_Cla  (IdCargo,IdClase,ValorMensual,ValorDiario )
 VALUES (@pmIdCargo,@pmIdClase,@pmValorMensual,@pmValorDiario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInsFijos_Cue  @pmIdCargo VARCHAR(4), @pmIdAdmon VARCHAR(4),@pmIdCuentaDeb VARCHAR(16), @pmIdCuentaCre VARCHAR(16)
, @pmIdCueDebVp VARCHAR(16), @pmIdCueCreVp VARCHAR(16)
AS
 INSERT INTO Fijos_Cue ( IdCargo, IdAdmon, IdCuentaDeb, IdCuentaCre, IdCueDebVp, IdCueCreVp)
 VALUES (@pmIdCargo, @pmIdAdmon,@pmIdCuentaDeb, @pmIdCuentaCre, @pmIdCueDebVp, @pmIdCueCreVp)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFijos_Veh @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmValorMensual MONEY,@pmValorDiario MONEY,@pmExcluir BIT
AS
 INSERT INTO Fijos_Veh (IdCargo,IdVehiculo,ValorMensual, ValorDiario,  Excluir)
 VALUES (@pmIdCargo,@pmIdVehiculo,@pmValorMensual, @pmValorDiario,  @pmExcluir)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsFinAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaPre CHAR(2)
,@pmItemPre INT,@pmTotalAbono MONEY,@pmVrBaseCms MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16)
,@pmComision DECIMAL(14,4),@pmCantGal DECIMAL(14,4),@pmDetalle VARCHAR(150),@pmTipoAplica INT,@pmFecPago SMALLDATETIME,@pmTimeSys SMALLDATETIME
 AS 
 INSERT INTO Trn_FinAbonos (TipRec,Recibo,IdCia,Item,Fecha,TipDoc,IdPrestamo,IdCiaPre,ItemPre,TotalAbono,VrBaseCms,IdCliente,IdAgencia,IdVend,Comision,CantGal,Detalle,TipoAplica,FecPago,TimeSys)
 VALUES (@pmTipRec,@pmRecibo,@pmIdCia,@pmItem,@pmFecha,@pmTipDoc,@pmIdPrestamo,@pmIdCiaPre,@pmItemPre,@pmTotalAbono,@pmVrBaseCms,@pmIdCliente,@pmIdAgencia
,@pmIdVend,@pmComision,@pmCantGal,@pmDetalle,@pmTipoAplica,@pmFecPago,@pmTimeSys)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmVrAFavor MONEY
,@pmVrOtrosDb MONEY,@pmVrAbono MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmCodLinCred VARCHAR(4)
 AS 
 INSERT INTO Trn_FinAfavor (TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend,CodLinCred)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmVrAFavor,@pmVrOtrosDb,@pmVrAbono,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmCodLinCred)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinAlmacenes @pmIdAlmacen VARCHAR(4),@pmAlmacen VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO FinAlmacenes (IdAlmacen,Almacen,Inactivo)
 VALUES (@pmIdAlmacen,@pmAlmacen,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinAmtza @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecCausac SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmNumDias INT,@pmVrCuota MONEY,@pmVrCapital MONEY
,@pmVrInteres MONEY,@pmVrSaldo MONEY,@pmTasaEfeMes DECIMAL(16,8),@pmTasaEfeDia DECIMAL(16,8),@pmCausacion INT,@pmCdCiaCausac CHAR(2),@pmVrCuoCausac MONEY,@pmVrIntCausac MONEY,@pmVrSaldoCausac MONEY
 AS 
 INSERT INTO Trn_FinAmtza (TipDoc,IdPrestamo,IdCia,Item,FecCausac,FecVence,NumDias,VrCuota,VrCapital,VrInteres,VrSaldo,TasaEfeMes,TasaEfeDia,Causacion,CdCiaCausac,VrCuoCausac,VrIntCausac,VrSaldoCausac)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmFecCausac,@pmFecVence,@pmNumDias,@pmVrCuota,@pmVrCapital,@pmVrInteres,@pmVrSaldo,@pmTasaEfeMes,@pmTasaEfeDia,@pmCausacion
,@pmCdCiaCausac,@pmVrCuoCausac,@pmVrIntCausac,@pmVrSaldoCausac)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinCauMora @pmTipoProc VARCHAR(3),@pmNumProc INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaPre CHAR(2),@pmItemPre INT,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmFecVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrAbonado MONEY,@pmFecUltLiq SMALLDATETIME,@pmFecIniLiq SMALLDATETIME,@pmFecFinLiq SMALLDATETIME
,@pmDiasLiquida INT,@pmDiasGracia INT,@pmBaseLiquida MONEY,@pmTasaEA DECIMAL(16,8),@pmTasaEM DECIMAL(16,8),@pmTasaDia DECIMAL(16,8),@pmVrLiquida MONEY,@pmTarifaIva DECIMAL(14,4),@pmVrIvaInt MONEY,@pmNumNotaDb INT,@pmNumComp INT
,@pmCdLinCred VARCHAR(4),@pmNCuotaLiq INT
 AS 
 INSERT INTO Trn_FinCauMora (TipoProc,NumProc,IdCia,Item,TipDoc,IdPrestamo,IdCiaPre,ItemPre,IdCliente,IdAgencia,IdVend,FecVence,VrTotal,VrAbonado,FecUltLiq,FecIniLiq,FecFinLiq,DiasLiquida,DiasGracia,BaseLiquida,TasaEA,TasaEM,TasaDia,VrLiquida,TarifaIva,VrIvaInt,NumNotaDb,NumComp,CdLinCred,NCuotaLiq)
 VALUES (@pmTipoProc,@pmNumProc,@pmIdCia,@pmItem,@pmTipDoc,@pmIdPrestamo,@pmIdCiaPre,@pmItemPre,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmFecVence,@pmVrTotal,@pmVrAbonado,@pmFecUltLiq,@pmFecIniLiq,@pmFecFinLiq
,@pmDiasLiquida,@pmDiasGracia,@pmBaseLiquida,@pmTasaEA,@pmTasaEM,@pmTasaDia,@pmVrLiquida,@pmTarifaIva,@pmVrIvaInt,@pmNumNotaDb,@pmNumComp,@pmCdLinCred,@pmNCuotaLiq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinCausacion @pmTipoProc VARCHAR(3),@pmNumProc INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTotalCapital MONEY
,@pmTotalInteres MONEY,@pmTotalIntMora MONEY,@pmTipoCred VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmAnulado BIT,@pmObservacion VARCHAR(250)
,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinCausacion (TipoProc,NumProc,IdCia,Fecha,TotalCapital,TotalInteres,TotalIntMora,TipoCred,TipCom,Comprobante,Anulado,Observacion,TimeSys,IdUsuario)
 VALUES (@pmTipoProc,@pmNumProc,@pmIdCia,@pmFecha,@pmTotalCapital,@pmTotalInteres,@pmTotalIntMora,@pmTipoCred,@pmTipCom,@pmComprobante,@pmAnulado
,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 INSERT INTO FinClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
 VALUES (@pmIdClie,@pmCdAgencia,@pmIdCia,@pmVrSaldo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinClieSaldosLin @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4)
,@pmIdCia CHAR(2),@pmVrSaldoCap MONEY,@pmVrSaldoInt MONEY,@pmVrSaldoOtro MONEY
 AS 
 INSERT INTO FinClieSaldosLin (IdClie,CdAgencia,IdLinea,IdCia,VrSaldoCap,VrSaldoInt,VrSaldoOtro)
 VALUES (@pmIdClie,@pmCdAgencia,@pmIdLinea,@pmIdCia,@pmVrSaldoCap,@pmVrSaldoInt,@pmVrSaldoOtro)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinConcFactura @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmTipoConc VARCHAR(10),@pmGravado BIT,@pmCodTarIva VARCHAR(4)
,@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmCdCueIva VARCHAR(16),@pmCdCueRet VARCHAR(16),@pmCdCueIca VARCHAR(16),@pmInactivo BIT,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO FinConcFactura (IdConcepto,Concepto,TipoConc,Gravado,CodTarIva,CodTarRet,CodTarIca,IdCuenta,CdCueIva,CdCueRet,CdCueIca,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdConcepto,@pmConcepto,@pmTipoConc,@pmGravado,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmIdCuenta
,@pmCdCueIva,@pmCdCueRet,@pmCdCueIca,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinCuotas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumCuota INT,@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrAbonado MONEY
,@pmTipoTasa VARCHAR(10),@pmDTF_EA DECIMAL(16,8),@pmTasaNom DECIMAL(16,8),@pmSpread_TA DECIMAL(16,8),@pmTasaEfe DECIMAL(16,8),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmTipoCuota INT
,@pmNumCausacion INT,@pmIdCiaCau CHAR(2),@pmDetalle VARCHAR(250),@pmFecUltPago SMALLDATETIME,@pmConcepto VARCHAR(10),@pmFecLiqMora SMALLDATETIME,@pmFechaCrea SMALLDATETIME,@pmCodLinCred VARCHAR(4)
,@pmNumFactura VARCHAR(20),@pmVrInteresFact MONEY,@pmDiasLiquida INT,@pmFecUltCausac SMALLDATETIME,@pmVrAcumCapital MONEY,@pmConvenio INT,@pmNitConvenio VARCHAR(16),@pmNumPreMora INT
 AS 
 INSERT INTO Trn_FinCuotas (TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente,IdAgencia,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora
,FechaCrea,CodLinCred,NumFactura,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio,NumPreMora)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumCuota,@pmFecha,@pmFechaVence,@pmVrTotal,@pmVrAbonado,@pmTipoTasa,@pmDTF_EA,@pmTasaNom,@pmSpread_TA,@pmTasaEfe,@pmIdCliente
,@pmIdAgencia,@pmConcepto,@pmTipoCuota,@pmNumCausacion,@pmIdCiaCau,@pmDetalle,@pmFecUltPago,@pmFecLiqMora,@pmFechaCrea,@pmCodLinCred,@pmNumFactura,@pmVrInteresFact,@pmDiasLiquida,@pmFecUltCausac,@pmVrAcumCapital,@pmConvenio,@pmNitConvenio,@pmNumPreMora)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinDevFac @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmIdVend VARCHAR(16),@pmIdLocal VARCHAR(8),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinDevFac (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,IdVend,IdLocal,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmFactura,@pmIdCiaDoc,@pmFecDoc,@pmIdCliente,@pmIdAgencia,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrCargos,@pmVrOtrDcto,@pmVrNeto,@pmIdVend,@pmIdLocal
,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdVend VARCHAR(16),@pmCxPagar BIT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinDevPre (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,IdPrestamo,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrPrestamo,IdVend,CxPagar,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmIdPrestamo,@pmIdCiaDoc,@pmFecDoc,@pmIdCliente,@pmIdAgencia,@pmVrPrestamo,@pmIdVend,@pmCxPagar,@pmModdDev,@pmOrigenAdd,@pmTipCom,@pmComprobante
,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY
,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmReferencia VARCHAR(50),@pmNumAutoriza INT
,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinDevRec (TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,Referencia,NumAutoriza,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDev,@pmDevolucion,@pmIdCia,@pmFecha,@pmIdConcepto,@pmTipDoc,@pmRecibo,@pmIdCiaDoc,@pmFecDoc,@pmFecPago,@pmIdCliente,@pmIdAgencia,@pmVrRecibido,@pmVrSubTotal,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmVrEfectivo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmIdVend,@pmTarifaCom
,@pmCodTarCom,@pmEnEfectivo,@pmReferencia,@pmNumAutoriza,@pmModdDev,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinFactDet @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmTipoReg VARCHAR(10),@pmCantidad DECIMAL(14,4)
,@pmVrUnitario MONEY,@pmVrTotal MONEY,@pmTarifIva DECIMAL(14,4),@pmVrIva MONEY,@pmTarifRet DECIMAL(14,4),@pmVrRetfte MONEY,@pmTarifIca DECIMAL(14,4),@pmVrRetica MONEY,@pmnPrestamo INT,@pmCdCiaPrest CHAR(2)
,@pmFecIniLiq SMALLDATETIME,@pmFecFinLiq SMALLDATETIME,@pmCantDias INT,@pmCdTarifIva VARCHAR(4),@pmCdTarifRet VARCHAR(4),@pmCdTarifIca VARCHAR(4),@pmTarifDcto DECIMAL(14,4),@pmVrDcto MONEY,@pmCdTarifDct VARCHAR(4)
 AS 
 INSERT INTO Trn_FinFactDet (TipDoc,Factura,IdCia,Item,IdConcepto,Concepto,TipoReg,Cantidad,VrUnitario,VrTotal,TarifIva,VrIva,TarifRet,VrRetfte,TarifIca,VrRetica,TarifDcto,VrDcto,nPrestamo,CdCiaPrest,FecIniLiq,FecFinLiq,CantDias,CdTarifIva,CdTarifRet,CdTarifIca,CdTarifDct)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmItem,@pmIdConcepto,@pmConcepto,@pmTipoReg,@pmCantidad,@pmVrUnitario,@pmVrTotal,@pmTarifIva,@pmVrIva,@pmTarifRet,@pmVrRetfte,@pmTarifIca,@pmVrRetica,@pmTarifDcto,@pmVrDcto,@pmnPrestamo
,@pmCdCiaPrest,@pmFecIniLiq,@pmFecFinLiq,@pmCantDias,@pmCdTarifIva,@pmCdTarifRet,@pmCdTarifIca,@pmCdTarifDct)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinFactint @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY
,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4)
,@pmIdLocal VARCHAR(8),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinFactint (TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,IdLocal,MulPlazos,IdPlazo,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFechaVence,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrCargos,@pmVrOtrDcto,@pmVrNeto,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmIdCCosto,@pmIdSubCos
,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmIdLocal,@pmMulPlazos,@pmIdPlazo,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinFacturas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumFactura VARCHAR(20),@pmFecha SMALLDATETIME
,@pmFechaVence SMALLDATETIME,@pmVrFactura MONEY,@pmVrInteres MONEY,@pmDiasLiq DECIMAL(14,4),@pmTasaEfeMes DECIMAL(16,8),@pmTasaEfeDia DECIMAL(16,8),@pmFechaPago SMALLDATETIME,@pmItemPago INT
,@pmFacturado BIT,@pmTipFac VARCHAR(3),@pmFacturaInt INT,@pmFecFactura SMALLDATETIME,@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmVrAbono MONEY,@pmCdCiaFac CHAR(2),@pmFechaLiq SMALLDATETIME
 AS 
 INSERT INTO Trn_FinFacturas (TipDoc,IdPrestamo,IdCia,Item,NumFactura,Fecha,FechaVence,VrFactura,VrInteres,DiasLiq,TasaEfeMes,TasaEfeDia,FechaPago,ItemPago,FechaLiq,FecInicio,FecFinal,VrAbono,Facturado,TipFac,FacturaInt,CdCiaFac,FecFactura)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumFactura,@pmFecha,@pmFechaVence,@pmVrFactura,@pmVrInteres,@pmDiasLiq
,@pmTasaEfeMes,@pmTasaEfeDia,@pmFechaPago,@pmItemPago,@pmFechaLiq,@pmFecInicio,@pmFecFinal,@pmVrAbono,@pmFacturado,@pmTipFac,@pmFacturaInt,@pmCdCiaFac,@pmFecFactura)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrSancion MONEY,@pmVrOtros MONEY,@pmVrNeto MONEY
,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFecDcm SMALLDATETIME,@pmCdForma VARCHAR(4),@pmNumCheque VARCHAR(20),@pmCdBanco VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmModalidad VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinNotas (TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque,CdBanco
,MulPlazos,IdPlazo,Modalidad,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFechaVence,@pmVrSubTotal,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrSancion,@pmVrOtros,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv
,@pmIdCCosto,@pmIdSubCos,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmFecDcm,@pmCdForma,@pmNumCheque,@pmCdBanco,@pmMulPlazos,@pmIdPlazo,@pmModalidad,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmAnulado,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFinPagos @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumero VARCHAR(20),@pmFecha SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmVrCuota MONEY
,@pmEdoForma INT,@pmCdBanco VARCHAR(4),@pmCtaCheque VARCHAR(30),@pmDetalle VARCHAR(150),@pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCiaRec CHAR(2),@pmVrLiqMora MONEY,@pmFecLiqMora SMALLDATETIME
 AS 
 INSERT INTO Trn_FinPagos (TipDoc,IdPrestamo,IdCia,Item,Numero,Fecha,FecPago,VrCuota,EdoForma,CdBanco,CtaCheque,Detalle,TipRec,Recibo,IdCiaRec,VrLiqMora,FecLiqMora)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumero,@pmFecha,@pmFecPago,@pmVrCuota,@pmEdoForma,@pmCdBanco,@pmCtaCheque
,@pmDetalle,@pmTipRec,@pmRecibo,@pmIdCiaRec,@pmVrLiqMora,@pmFecLiqMora)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPrestamo SMALLDATETIME,@pmFecCorte SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdTasa VARCHAR(4),@pmNPlazos INT,@pmCausacion VARCHAR(10),@pmIdMora VARCHAR(4),@pmNContrato INT,@pmCdCiaCon CHAR(2)
,@pmNActaJunta INT,@pmNumCredito VARCHAR(20),@pmIdVend VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoGarantia VARCHAR(10),@pmVrGarantia MONEY,@pmDescGarantia VARCHAR(250),@pmCxPagar BIT,@pmNumCuotaIni INT,@pmAcumCapital MONEY,@pmAcumIntereses MONEY,@pmAcumIntMora MONEY,@pmAcumCuotas INT,@pmPagosCapital MONEY,@pmPagosIntereses MONEY,@pmPagosIntMora MONEY,@pmCuotasPagadas INT
,@pmCuotasMora INT,@pmTipoAprob VARCHAR(20),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmDiaCausac INT,@pmDiaCausac2 INT,@pmDiasVence VARCHAR(50),@pmIdLinea VARCHAR(4),@pmTipoPlazo CHAR(1),@pmTipoCausac INT,@pmCdTasa2 VARCHAR(4),@pmVrSeguro MONEY,@pmVrIntereses MONEY,@pmVrNeto MONEY,@pmVrCuota MONEY
,@pmTipoCredito VARCHAR(10),@pmCalcIntMora BIT,@pmNitEmpConv VARCHAR(16),@pmCuotasConv INT,@pmRefinanciado INT,@pmNumPrestRef INT,@pmCiaPrestRef CHAR(2),@pmVrRefinanc MONEY,@pmDescRefinanc  VARCHAR(150),@pmCdAlmacen VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinPrestamo (TipDoc,IdPrestamo,IdCia,Fecha,FecPrestamo,FecCorte,DiaCausac,DiaCausac2,DiasVence,IdConcepto,IdCliente,IdAgencia,VrPrestamo,IdLinea,IdTasa,NPlazos,TipoPlazo,Causacion,IdMora,NContrato,CdCiaCon,NActaJunta,NumCredito,IdVend,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora
,TipoAprob,TipoCausac,CdTasa2,VrSeguro,VrIntereses,VrNeto,VrCuota,TipoCredito,CalcIntMora,NitEmpConv,CuotasConv,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CdAlmacen,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmFecha,@pmFecPrestamo,@pmFecCorte,@pmDiaCausac,@pmDiaCausac2,@pmDiasVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmVrPrestamo,@pmIdLinea,@pmIdTasa,@pmNPlazos,@pmTipoPlazo,@pmCausacion,@pmIdMora,@pmNContrato,@pmCdCiaCon,@pmNActaJunta,@pmNumCredito,@pmIdVend,@pmpVehiculo,@pmCdConductor,@pmTipoGarantia,@pmVrGarantia,@pmDescGarantia
,@pmCxPagar,@pmNumCuotaIni,@pmAcumCapital,@pmAcumIntereses,@pmAcumIntMora,@pmAcumCuotas,@pmPagosCapital,@pmPagosIntereses,@pmPagosIntMora,@pmCuotasPagadas,@pmCuotasMora,@pmTipoAprob,@pmTipoCausac
,@pmCdTasa2,@pmVrSeguro,@pmVrIntereses,@pmVrNeto,@pmVrCuota,@pmTipoCredito,@pmCalcIntMora,@pmNitEmpConv,@pmCuotasConv,@pmRefinanciado,@pmNumPrestRef,@pmCiaPrestRef,@pmVrRefinanc,@pmDescRefinanc,@pmCdAlmacen,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY
,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrEfectivo MONEY,@pmVrAnticipo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoCredito VARCHAR(10),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_FinRecibos (TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor
,TipoCredito,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFecPago,@pmVrRecibido,@pmVrSubTotal,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrEfectivo,@pmVrAnticipo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarRet,@pmCodTarIca
,@pmCodTarRiv,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmEnEfectivo,@pmCdForma,@pmNumForma,@pmDetallePago,@pmCdBanco,@pmCdCta,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipoCredito,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmOrigenAdd,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFmtDoc @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2),@pmDescripcion VARCHAR(100),@pmFormato VARCHAR(20),@pmPredtmnado BIT,@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT
,@pmNumCopias INT,@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50)
 AS 
 INSERT INTO Sys_FmtDoc (IdDoc,IdForma,Descripcion,Formato,Predtmnado,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location)
 VALUES (@pmIdDoc,@pmIdForma,@pmDescripcion,@pmFormato,@pmPredtmnado,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmsp_Nombre,@pmInd_Location)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFmtFactura @pmnFormato VARCHAR(10),@pmIdDoc VARCHAR(3),@pmCdForma CHAR(2)
 AS 
 INSERT INTO Sys_FmtFactura (nFormato,IdDoc,CdForma)
 VALUES (@pmnFormato,@pmIdDoc,@pmCdForma)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFondos @pmIdFondo VARCHAR(8),@pmFondo VARCHAR(100),@pmIdClase VARCHAR(4)
,@pmIdTercero VARCHAR(16),@pmCodigoEps VARCHAR(10),@pmCodigoPen VARCHAR(10),@pmCodigoArp VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Fondos (IdFondo,Fondo,IdClase,IdTercero,CodigoEps,CodigoPen,CodigoArp,Inactivo)
 VALUES (@pmIdFondo,@pmFondo,@pmIdClase,@pmIdTercero,@pmCodigoEps,@pmCodigoPen,@pmCodigoArp,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFormasCue @pmIdForma VARCHAR(4),@pmIdConcepto VARCHAR(4)
,@pmIdCuenta VARCHAR(16),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO FormasCue (IdForma,IdConcepto,IdCuenta,FechaAdd)
 VALUES (@pmIdForma,@pmIdConcepto,@pmIdCuenta,@pmFechaAdd)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsFormaspago @pmIdForma VARCHAR(4),@pmFormaPago VARCHAR(50),@pmConsecutivo INT,@pmCuadreCaja BIT,@pmRefDcmto BIT
,@pmRefNit BIT,@pmRefBco BIT,@pmRefFech BIT,@pmRefAtza BIT,@pmRefOtro BIT,@pmVldRangos BIT,@pmInactivo BIT
 AS 
 INSERT INTO Formaspago (IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,Inactivo)
 VALUES (@pmIdForma,@pmFormaPago,@pmConsecutivo,@pmCuadreCaja,@pmRefDcmto,@pmRefNit,@pmRefBco,@pmRefFech,@pmRefAtza
,@pmRefOtro,@pmVldRangos,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsFuentesPago @pmIdFuente VARCHAR(4),@pmFuentePago VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO FuentesPago (IdFuente,FuentePago,Inactivo)
 VALUES (@pmIdFuente,@pmFuentePago,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGrupos @pmIdGrupo VARCHAR(8),@pmGrupo VARCHAR(150)
,@pmIdLinea VARCHAR(8),@pmTipoInvent VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Grupos (IdGrupo,Grupo,IdLinea,TipoInvent,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupo,@pmIdLinea,@pmTipoInvent,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGruposAR @pmIdGrupo VARCHAR(4),@pmAltoRiesgo VARCHAR(50),@pmTarfaTrab DECIMAL(10,4),@pmTarfaEmp DECIMAL(10,4),@pmInactivo BIT
 AS 
 INSERT INTO GruposAR (IdGrupo,AltoRiesgo,TarfaTrab,TarfaEmp,Inactivo)
 VALUES (@pmIdGrupo,@pmAltoRiesgo,@pmTarfaTrab,@pmTarfaEmp,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsGruposAgenc @pmIdGrupo VARCHAR(4),@pmGrupoAgenc VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO GruposAgenc (IdGrupo,GrupoAgenc,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoAgenc,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGruposCli @pmIdGrupo VARCHAR(4),@pmGrupoClie VARCHAR(50),@pmModApp VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO GruposCli (IdGrupo,GrupoClie,ModApp,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoClie,@pmModApp,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsGruposMcia @pmIdGrupo VARCHAR(10),@pmGrupoMcia VARCHAR(250),@pmInactivo BIT
 AS 
 INSERT INTO GruposMcia (IdGrupo,GrupoMcia,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoMcia,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGruposObj @pmIdGrupo VARCHAR(6),@pmGrupoObjeto VARCHAR(50),@pmnModulo VARCHAR(10)
 AS 
 INSERT INTO Sys_GruposObj (IdGrupo,GrupoObjeto,nModulo)
 VALUES (@pmIdGrupo,@pmGrupoObjeto,@pmnModulo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGruposPro @pmIdGrupo VARCHAR(4),@pmGrupoProp VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO GruposPro (IdGrupo,GrupoProp,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoProp,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGruposRot @pmIdGrupo VARCHAR(4),@pmGrupoRot VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO GruposRot (IdGrupo,GrupoRot,Inactivo)
 VALUES (@pmIdGrupo,@pmGrupoRot,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGruposUsu @pmIdGrupo INT,@pmGrupo VARCHAR(50),@pmDescripcion VARCHAR(100)
 AS 
 INSERT INTO Sys_GruposUsu (IdGrupo,Grupo,Descripcion)
 VALUES (@pmIdGrupo,@pmGrupo,@pmDescripcion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsGuia @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaDesp SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmCantidad DECIMAL(14,4),@pmDirEnvio VARCHAR(250),@pmIdLocal VARCHAR(8),@pmLugarEnv VARCHAR(50),@pmHorasVig DECIMAL(14,4),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmNitEmpTrans VARCHAR(16)
,@pmEmpTrans VARCHAR(150),@pmCdRuta VARCHAR(4),@pmCdCiuOrigen VARCHAR(8),@pmReferncia VARCHAR(50),@pmTipOcc VARCHAR(3),@pmOCargue INT,@pmIdCiaOcc CHAR(2),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT
,@pmTipoTemp VARCHAR(10),@pmTemperatura DECIMAL(14,4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Guia (TipDoc,Guia,IdCia,Fecha,FechaDesp,FechaVence,IdCliente,IdAgencia,Cantidad,DirEnvio,IdLocal,LugarEnv,HorasVig,IdVehiculo,IdConductor,NitEmpTrans,EmpTrans,CdRuta,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,Factura,IdCiaFac,FechaFact,OrigenAdd,ZonaFrontera,TipoTemp,Temperatura,Anulado,FecDev,Observacion,IdEstado,CdCiuOrigen,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmGuia,@pmIdCia,@pmFecha,@pmFechaDesp,@pmFechaVence,@pmIdCliente,@pmIdAgencia,@pmCantidad,@pmDirEnvio,@pmIdLocal,@pmLugarEnv,@pmHorasVig,@pmIdVehiculo,@pmIdConductor,@pmNitEmpTrans,@pmEmpTrans,@pmCdRuta,@pmReferncia,@pmTipOcc,@pmOCargue,@pmIdCiaOcc,@pmTipFac,@pmFactura,@pmIdCiaFac
,@pmFechaFact,@pmOrigenAdd,@pmZonaFrontera,@pmTipoTemp,@pmTemperatura,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmCdCiuOrigen,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsInstalaciones @pmIdInstala VARCHAR(4),@pmInstlacion VARCHAR(50),@pmDirInstala VARCHAR(100)
,@pmCodCiu VARCHAR(8),@pmCodCia CHAR(2),@pmIdClase VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO Instalaciones (IdInstala,Instlacion,DirInstala,CodCia,CodCiu,IdClase,Inactivo)
 VALUES (@pmIdInstala,@pmInstlacion,@pmDirInstala,@pmCodCia,@pmCodCiu,@pmIdClase,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsInvDetalle @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCdUbic VARCHAR(10),@pmCdTanque VARCHAR(4)
,@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY,@pmCantExist DECIMAL(14,4),@pmDescripcion VARCHAR(250),@pmUnidades DECIMAL(14,4),@pmUndAforo DECIMAL(14,4),@pmUndAgua DECIMAL(14,4)
,@pmCantAgua DECIMAL(14,4),@pmNoConteo BIT,@pmIdUsuario VARCHAR(11),@pmIdEstacion CHAR(2),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO Trn_InvDetalle (TipDoc,nInventario,IdCia,Item,IdProducto,IdBodega,CdUbic,CdTanque,Cantidad,VrUnitario,CantExist,Descripcion,Unidades,UndAforo,UndAgua,CantAgua,NoConteo,IdUsuario,IdEstacion,FechaAdd)
 VALUES (@pmTipDoc,@pmnInventario,@pmIdCia,@pmItem,@pmIdProducto,@pmIdBodega,@pmCdUbic,@pmCdTanque,@pmCantidad,@pmVrUnitario,@pmCantExist,@pmDescripcion,@pmUnidades,@pmUndAforo
,@pmUndAgua,@pmCantAgua,@pmNoConteo,@pmIdUsuario,@pmIdEstacion,@pmFechaAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsInvFisico @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmModalidad VARCHAR(10),@pmCdBodega VARCHAR(4),@pmCantFisico DECIMAL(14,4)
,@pmVrTotalFisico MONEY,@pmExistencias DECIMAL(14,4),@pmVrTotalCosto MONEY,@pmProcesado BIT,@pmFecProceso SMALLDATETIME,@pmNumAjuSob INT,@pmNumAjuFal INT
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_InvFisico (TipDoc,nInventario,IdCia,Fecha,Modalidad,CdBodega,CantFisico,VrTotalFisico,Existencias,VrTotalCosto,OrigenAdd,Procesado,FecProceso,NumAjuSob,NumAjuFal,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmnInventario,@pmIdCia,@pmFecha,@pmModalidad,@pmCdBodega,@pmCantFisico,@pmVrTotalFisico,@pmExistencias,@pmVrTotalCosto,@pmOrigenAdd,@pmProcesado,@pmFecProceso,@pmNumAjuSob
,@pmNumAjuFal,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsJornadas @pmIdJornada VARCHAR(4),@pmJornada VARCHAR(50),@pmHorasDia INT
,@pmRangos VARCHAR(25),@pmInactivo BIT
 AS 
 INSERT INTO Jornadas (IdJornada,Jornada,HorasDia,Rangos,Inactivo)
 VALUES (@pmIdJornada,@pmJornada,@pmHorasDia,@pmRangos,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurAutoridades @pmIdAutoridad VARCHAR(4),@pmNomAutoridad VARCHAR(50),@pmDireccion VARCHAR(100),@pmInactivo BIT
 AS 
 INSERT INTO JurAutoridades (IdAutoridad,NomAutoridad,Direccion,Inactivo)
 VALUES (@pmIdAutoridad,@pmNomAutoridad,@pmDireccion,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurCalifica @pmIdCalifica VARCHAR(4),@pmCalificaCaso VARCHAR(50),@pmIdClase VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO JurCalifica (IdCalifica,CalificaCaso,IdClase,Inactivo)
 VALUES (@pmIdCalifica,@pmCalificaCaso,@pmIdClase,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsJurCasos @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmRadicacion VARCHAR(20),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmSinRecorrido BIT,@pmTipCar VARCHAR(3)
,@pmCartulina INT,@pmIdCiaCar CHAR(2),@pmFechaSuc SMALLDATETIME,@pmIdRuta VARCHAR(4),@pmIdAbogado VARCHAR(16),@pmIdAuxiliar VARCHAR(16),@pmIdAutoridad VARCHAR(4),@pmIdTipoPro VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCalifica VARCHAR(4),@pmLugarAcc VARCHAR(100)
,@pmCausaAcc VARCHAR(3300),@pmComentario VARCHAR(3300),@pmObservacion VARCHAR(250),@pmContraparte VARCHAR(100),@pmDirContraparte VARCHAR(100),@pmTelContraparte VARCHAR(20),@pmValDanosCParte MONEY,@pmValDanosEmp MONEY,@pmValDeducible MONEY
,@pmEmpAsegdora VARCHAR(100),@pmAuxMutuo BIT,@pmArchivoCaso VARCHAR(100),@pmDocumntos VARCHAR(250),@pmCroquis VARCHAR(20),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmReclamaciones VARCHAR(1000),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurCasos (TipDoc,CasoID,IdCia,Fecha,Radicacion,IdVehiculo,IdPropietario,IdConductor,SinRecorrido,TipCar,Cartulina,IdCiaCar,FechaSuc,IdRuta,IdAbogado,IdAuxiliar,IdAutoridad,IdTipoPro,IdClase,IdCalifica,LugarAcc,CausaAcc,Comentario,Observacion,Contraparte,DirContraparte,TelContraparte
,ValDanosCParte,ValDanosEmp,ValDeducible,EmpAsegdora,AuxMutuo,ArchivoCaso,Documntos,Croquis,Anulado,FecDev,IdEstado,TimeSys,IdCiaCrea,IdUsuario,Reclamaciones)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmFecha,@pmRadicacion,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmSinRecorrido,@pmTipCar,@pmCartulina,@pmIdCiaCar,@pmFechaSuc,@pmIdRuta,@pmIdAbogado,@pmIdAuxiliar,@pmIdAutoridad,@pmIdTipoPro,@pmIdClase,@pmIdCalifica
,@pmLugarAcc,@pmCausaAcc,@pmComentario,@pmObservacion,@pmContraparte,@pmDirContraparte,@pmTelContraparte,@pmValDanosCParte,@pmValDanosEmp,@pmValDeducible,@pmEmpAsegdora,@pmAuxMutuo,@pmArchivoCaso,@pmDocumntos,@pmCroquis,@pmAnulado,@pmFecDev,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmReclamaciones)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsJurCasosHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT,@pmFechaUp SMALLDATETIME,@pmHistorial VARCHAR(3500),@pmEstadoAct VARCHAR(3500)
,@pmCierreProc BIT,@pmArchivoProc VARCHAR(100),@pmNombreCparte VARCHAR(100),@pmDirCparte VARCHAR(100),@pmTelCparte VARCHAR(20),@pmDanosCParte MONEY,@pmDanosEmp MONEY
,@pmValorDeducible MONEY,@pmAseguradora VARCHAR(100),@pmAuxilioMutuo BIT,@pmIdEstado VARCHAR(4),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurCasosHis (TipDoc,CasoID,IdCia,ActualizaID,FechaUp,Historial,EstadoAct,CierreProc,ArchivoProc,NombreCparte,DirCparte,TelCparte,DanosCParte,DanosEmp,ValorDeducible,Aseguradora,AuxilioMutuo,IdEstado,IdUsuario)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmActualizaID,@pmFechaUp,@pmHistorial,@pmEstadoAct,@pmCierreProc,@pmArchivoProc,@pmNombreCparte,@pmDirCparte,@pmTelCparte,@pmDanosCParte,@pmDanosEmp
,@pmValorDeducible,@pmAseguradora,@pmAuxilioMutuo,@pmIdEstado,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsJurCasosQtn @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdPregunta VARCHAR(4),@pmDescripcion VARCHAR(250)
 AS 
 INSERT INTO Trn_JurCasosQtn (TipDoc,CasoID,IdCia,Item,IdPregunta,Descripcion)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmItem,@pmIdPregunta,@pmDescripcion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurClaseAcc @pmIdClase VARCHAR(4),@pmClaseAccdte VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurClaseAcc (IdClase,ClaseAccdte,Inactivo)
 VALUES (@pmIdClase,@pmClaseAccdte,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurDocumtos @pmIdDoc VARCHAR(4),@pmDocumnto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurDocumtos (IdDoc,Documnto,Inactivo)
 VALUES (@pmIdDoc,@pmDocumnto,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurEstados @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmInactivo BIT,@pmNColor INT
 AS 
 INSERT INTO JurEstados (IdEstado,Estado,Inactivo,NColor)
 VALUES (@pmIdEstado,@pmEstado,@pmInactivo,@pmNColor)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurInvesHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT,@pmFechaUp SMALLDATETIME,@pmHistorial VARCHAR(3000),@pmEstadoAct VARCHAR(3000),@pmPruebas VARCHAR(1200)
,@pmDocumntos VARCHAR(250),@pmCierreProc BIT,@pmArchivoProc VARCHAR(100),@pmNombreCparte VARCHAR(100),@pmDirCparte VARCHAR(100),@pmTelCparte VARCHAR(20),@pmIdEstado VARCHAR(4),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurInvesHis (TipDoc,CasoID,IdCia,ActualizaID,FechaUp,Historial,EstadoAct,Pruebas,Documntos,CierreProc,ArchivoProc,NombreCparte,DirCparte,TelCparte,IdEstado,IdUsuario)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmActualizaID,@pmFechaUp,@pmHistorial,@pmEstadoAct,@pmPruebas,@pmDocumntos,@pmCierreProc,@pmArchivoProc,@pmNombreCparte,@pmDirCparte,@pmTelCparte,@pmIdEstado,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurInvestiga @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmRadicacion VARCHAR(20),@pmIdAbogado VARCHAR(16),@pmIdAuxiliar VARCHAR(16),@pmIdAutoridad VARCHAR(4),@pmIdEmpresa VARCHAR(16)
,@pmObservacion VARCHAR(250),@pmContraparte VARCHAR(100),@pmDirContraparte VARCHAR(100),@pmTelContraparte VARCHAR(20),@pmArchivoCaso VARCHAR(100),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_JurInvestiga (TipDoc,CasoID,IdCia,Fecha,Radicacion,IdAbogado,IdAuxiliar,IdAutoridad,IdEmpresa,Observacion,Contraparte,DirContraparte,TelContraparte,ArchivoCaso,Anulado,FecDev,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmCasoID,@pmIdCia,@pmFecha,@pmRadicacion,@pmIdAbogado,@pmIdAuxiliar,@pmIdAutoridad,@pmIdEmpresa,@pmObservacion,@pmContraparte,@pmDirContraparte,@pmTelContraparte,@pmArchivoCaso,@pmAnulado,@pmFecDev,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurPreguntas @pmIdPregunta VARCHAR(4),@pmPreguntaBas VARCHAR(250),@pmIdTipoPro VARCHAR(4),@pmTipoDato VARCHAR(15),@pmInactivo BIT
 AS 
 INSERT INTO JurPreguntas (IdPregunta,PreguntaBas,IdTipoPro,TipoDato,Inactivo)
 VALUES (@pmIdPregunta,@pmPreguntaBas,@pmIdTipoPro,@pmTipoDato,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsJurTipos @pmIdTipoPro VARCHAR(4),@pmTipoProceso VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO JurTipos (IdTipoPro,TipoProceso,Inactivo)
 VALUES (@pmIdTipoPro,@pmTipoProceso,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsKarUbic @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4),@pmFecha SMALLDATETIME,@pmLinea INT
 AS 
 INSERT INTO Trn_KarUbic (TipDoc,Documento,IdCia,Item,IdProducto,IdBodega,IdUbic,Entradas,Salidas,Fecha,Linea)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmIdProducto,@pmIdBodega,@pmIdUbic,@pmEntradas,@pmSalidas,@pmFecha,@pmLinea)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsKardex @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCdTanque VARCHAR(4),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4),@pmIdUnd VARCHAR(4),@pmVrUnitario MONEY,@pmVrPrecio MONEY,@pmVrCostProm MONEY,@pmTarifaIva DECIMAL(14,4),@pmVrIvaEnt MONEY,@pmVrIvaSal MONEY,@pmTarifaDct DECIMAL(14,4),@pmVrDctoEnt MONEY,@pmVrDctoSal MONEY,@pmVrCostoEnt MONEY
,@pmVrCostoSal MONEY,@pmTarifaRet DECIMAL(14,4),@pmVrReteEnt MONEY,@pmVrReteSal MONEY,@pmTarifaIca DECIMAL(14,4),@pmVrIcaEnt MONEY,@pmVrIcaSal MONEY,@pmVrBruto MONEY,@pmCdUbic VARCHAR(10),@pmNumLote VARCHAR(30),@pmFechLote SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmCdLocal VARCHAR(8),@pmCdSzona VARCHAR(4),@pmpVehiculo VARCHAR(10),@pmIdVend VARCHAR(16),@pmComision DECIMAL(14,4)
,@pmCdOperario VARCHAR(16),@pmComisnOper DECIMAL(14,4),@pmReferencia VARCHAR(50),@pmDescripcion VARCHAR(250),@pmComptmntos VARCHAR(50),@pmCdProdEquiv VARCHAR(16),@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFactura VARCHAR(15),@pmTipDocDev VARCHAR(3),@pmNumDocDev INT,@pmCdMngra VARCHAR(10),@pmNumInicial DECIMAL(14,4),@pmNumFinal DECIMAL(14,4),@pmSobretasa DECIMAL(14,4),@pmTasaNac DECIMAL(14,4),@pmTasaDep DECIMAL(14,4)
,@pmTasaMun DECIMAL(14,4),@pmSoldicom DECIMAL(14,4),@pmImpGlobal DECIMAL(14,4),@pmOtroImpto DECIMAL(14,4),@pmUnidades DECIMAL(14,4),@pmItemCombo INT,@pmServcios BIT,@pmNoVentas INT,@pmEsCombo BIT,@pmEsProdBase INT,@pmCodTarDct VARCHAR(4),@pmCodTarIva VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarCom VARCHAR(4),@pmCodTarCmc VARCHAR(4),@pmListaPrec CHAR(1),@pmVrBase MONEY,@pmCdMoneda VARCHAR(5),@pmVrTasaCamb MONEY,@pmVrDivisa1 MONEY,@pmVrDivisa2 MONEY
,@pmVrDivisa3 MONEY,@pmReferencia2 VARCHAR(50),@pmFecOrden SMALLDATETIME,@pmgalsbruto DECIMAL(14,4),@pmgalsneto DECIMAL(14,4), @pmTemperatura DECIMAL(14,4),@pmUmTemp VARCHAR(3),@pmDensidad DECIMAL(14,4),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmRec_Costo DECIMAL(14,4)
 AS 
 INSERT INTO Trn_Kardex (TipDoc,Documento,IdCia,Item,Fecha,IdProducto,IdBodega,CdTanque,Entradas,Salidas,IdUnd,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,IdConcepto,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend,Comision,CdOperario,ComisnOper,Referencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,CdMngra,NumInicial
,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,TimeSys,IdUsuario,Rec_Costo)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmIdProducto,@pmIdBodega,@pmCdTanque,@pmEntradas,@pmSalidas,@pmIdUnd,@pmVrUnitario,@pmVrPrecio,@pmVrCostProm,@pmTarifaIva,@pmVrIvaEnt,@pmVrIvaSal,@pmTarifaDct,@pmVrDctoEnt,@pmVrDctoSal,@pmVrCostoEnt,@pmVrCostoSal,@pmTarifaRet,@pmVrReteEnt,@pmVrReteSal,@pmTarifaIca,@pmVrIcaEnt,@pmVrIcaSal,@pmVrBruto,@pmCdUbic,@pmNumLote,@pmFechLote,@pmIdConcepto,@pmIdTercero,@pmCdAgencia,@pmCdCCosto,@pmCdSubCos,@pmCdLocal,@pmCdSzona,@pmpVehiculo,@pmIdVend
,@pmComision,@pmCdOperario,@pmComisnOper,@pmReferencia,@pmDescripcion,@pmComptmntos,@pmCdProdEquiv,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmCotizacion,@pmIdCiaCot,@pmRemision,@pmIdCiaRem,@pmFactura,@pmTipDocDev,@pmNumDocDev,@pmCdMngra,@pmNumInicial,@pmNumFinal,@pmSobretasa,@pmTasaNac,@pmTasaDep,@pmTasaMun,@pmSoldicom,@pmImpGlobal,@pmOtroImpto,@pmUnidades,@pmItemCombo,@pmServcios,@pmNoVentas,@pmEsCombo,@pmEsProdBase,@pmCodTarDct,@pmCodTarIva,@pmCodTarIca,@pmCodTarRet,@pmCodTarCom,@pmCodTarCmc
,@pmListaPrec,@pmVrBase,@pmCdMoneda,@pmVrTasaCamb,@pmVrDivisa1,@pmVrDivisa2,@pmVrDivisa3,@pmReferencia2,@pmFecOrden,@pmgalsbruto,@pmgalsneto,@pmTemperatura,@pmUmTemp,@pmDensidad,@pmTimeSys,@pmIdUsuario,@pmRec_Costo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsLineas @pmIdLinea VARCHAR(8),@pmLinea VARCHAR(150)
,@pmTipoInvent VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Lineas (IdLinea,Linea,TipoInvent,Inactivo)
 VALUES (@pmIdLinea,@pmLinea,@pmTipoInvent,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsLocalidades @pmIdLocal VARCHAR(8),@pmLocalidad VARCHAR(50),@pmIdDep VARCHAR(8),@pmTipLocal VARCHAR(20),@pmDist_Kmt DECIMAL(10,4)
,@pmTime_hh VARCHAR(8),@pmCelRow DECIMAL(14,4),@pmCelCol DECIMAL(14,4),@pmInactivo BIT,@pmDistSede DECIMAL(10,4),@pmTimeSede VARCHAR(8),@pmCodAlias VARCHAR(10)
 AS 
 INSERT INTO Localidades (IdLocal,Localidad,IdDep,TipLocal,Dist_Kmt,Time_hh,CelRow,CelCol,DistSede,TimeSede,CodAlias,Inactivo)
 VALUES (@pmIdLocal,@pmLocalidad,@pmIdDep,@pmTipLocal,@pmDist_Kmt,@pmTime_hh,@pmCelRow,@pmCelCol,@pmDistSede,@pmTimeSede,@pmCodAlias,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsLog_Nomina @pmIdNom VARCHAR(4),@pmIdPeriodo VARCHAR(8),@pmCerrado BIT
,@pmIntCtaCte BIT,@pmLogCtaCte VARCHAR(100)
 AS 
 INSERT INTO Log_Nomina (IdNom,IdPeriodo,Cerrado,IntCtaCte,LogCtaCte)
 VALUES (@pmIdNom,@pmIdPeriodo,@pmCerrado,@pmIntCtaCte,@pmLogCtaCte)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsLog_Periodos @pmAnno INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO Log_Periodos (Anno,IdCia,Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic,CierreAnno)
 VALUES (@pmAnno,@pmIdCia,0,0,0,0,0,0,0,0,0,0,0,0,0)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsLotMov @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmCdUbic VARCHAR(10),@pmEntradas DECIMAL(14,4),@pmSalidas DECIMAL(14,4),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmLinea INT
 AS 
 INSERT INTO Trn_LotMov (TipDoc,Documento,IdCia,Item,NumLote,IdProducto,IdBodega,CdUbic,Entradas,Salidas,Fecha,FecVence,Linea)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmNumLote,@pmIdProducto,@pmIdBodega,@pmCdUbic,@pmEntradas,@pmSalidas,@pmFecha,@pmFecVence,@pmLinea)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsLotes @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10)
,@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmSaldoActual DECIMAL(14,4),@pmNitProv VARCHAR(16),@pmTipDoc VARCHAR(3),@pmEntrada INT,@pmTimeSys SMALLDATETIME
 AS 
 INSERT INTO Trn_Lotes (NumLote,IdProducto,IdCia,IdBodega,IdUbic,Fecha,FecVence,SaldoActual,NitProv,TipDoc,Entrada,TimeSys)
 VALUES (@pmNumLote,@pmIdProducto,@pmIdCia,@pmIdBodega,@pmIdUbic,@pmFecha,@pmFecVence,@pmSaldoActual,@pmNitProv,@pmTipDoc,@pmEntrada,@pmTimeSys)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMarcas @pmIdMarca VARCHAR(4),@pmMarca VARCHAR(50),@pmTipoMarca VARCHAR(20)
,@pmCodigoMT VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO Marcas (IdMarca,Marca,TipoMarca,CodigoMT,Inactivo)
 VALUES (@pmIdMarca,@pmMarca,@pmTipoMarca,@pmCodigoMT,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMarcasLin @pmIdLinea VARCHAR(10),@pmLineaVeh VARCHAR(50),@pmIdMarca VARCHAR(4),@pmCodMT VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO MarcasLin (IdLinea,LineaVeh,IdMarca,CodMT,Inactivo)
 VALUES (@pmIdLinea,@pmLineaVeh,@pmIdMarca,@pmCodMT,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayAnotaciones @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME
,@pmDetalle VARCHAR(5000),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MayAnotaciones (NContrato,IdCia,Item,Fecha,Detalle,IdUsuario)
 VALUES (@pmNContrato,@pmIdCia,@pmItem,@pmFecha,@pmDetalle,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayBonCuentas @pmIdBonif VARCHAR(4),@pmTipoCue INT
,@pmCdTipo VARCHAR(4),@pmIdCuenta VARCHAR(16)
 AS 
 INSERT INTO MayBonCuentas (IdBonif,TipoCue,CdTipo,IdCuenta)
 VALUES (@pmIdBonif,@pmTipoCue,@pmCdTipo,@pmIdCuenta)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayBonif @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoProd VARCHAR(20),@pmVrInicial DECIMAL(14,4),@pmVrFinal DECIMAL(14,4)
,@pmUnidad VARCHAR(10),@pmTarifa DECIMAL(14,4),@pmTipoTarifa CHAR(1),@pmPeriodo VARCHAR(10),@pmCdProducto VARCHAR(16),@pmCdSubgrupo VARCHAR(8)
 AS 
 INSERT INTO Trn_MayBonif (NContrato,IdCia,Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdProducto,CdSubgrupo)
 VALUES (@pmNContrato,@pmIdCia,@pmItem,@pmTipoProd,@pmVrInicial,@pmVrFinal,@pmUnidad,@pmTarifa,@pmTipoTarifa,@pmPeriodo,@pmCdProducto,@pmCdSubgrupo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayContDoc @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdDoc VARCHAR(4),@pmFechaRad SMALLDATETIME
,@pmDependencia VARCHAR(50),@pmEstado VARCHAR(10),@pmFechaAprob SMALLDATETIME,@pmComentarios VARCHAR(250),@pmCdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MayContDoc (NContrato,IdCia,Item,IdDoc,FechaRad,Dependencia,Estado,FechaAprob,Comentarios,CdUsuario)
 VALUES (@pmNContrato,@pmIdCia,@pmItem,@pmIdDoc,@pmFechaRad,@pmDependencia,@pmEstado,@pmFechaAprob,@pmComentarios,@pmCdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayContratos @pmNContrato INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdTipo VARCHAR(4),@pmIdBonif VARCHAR(4),@pmConvBonif BIT,@pmCuotaFija BIT
,@pmCondPago BIT,@pmDiasGracia INT,@pmModPlazo VARCHAR(10),@pmPlazo INT,@pmFechaVence SMALLDATETIME,@pmModRecursos VARCHAR(150),@pmVrInversion MONEY,@pmVrInventario MONEY,@pmVrCuota MONEY,@pmModCuota VARCHAR(10)
,@pmIdBandera VARCHAR(4),@pmAprobJunta INT,@pmFechaAprob SMALLDATETIME,@pmObservAprob VARCHAR(250),@pmObservacion VARCHAR(250),@pmIdVend VARCHAR(16),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
,@pmFechaInicio SMALLDATETIME,@pmCuotaMin DECIMAL(14,4),@pmTipoAprob VARCHAR(10),@pmNumInterno VARCHAR(20),@pmInvCombus DECIMAL(14,4),@pmCuotaBase DECIMAL(14,4),@pmFechaFirma SMALLDATETIME,@pmCdAprobNeg VARCHAR(4),@pmArchivoAprob VARCHAR(50)
,@pmClaseContrato VARCHAR(10),@pmDescTipoCont VARCHAR(250),@pmGarantHipoteca BIT,@pmGarantPrenda BIT,@pmGarantPoliza BIT,@pmGarantBanco BIT,@pmGarantPagare BIT,@pmGarantOtro BIT,@pmCdTipoBien VARCHAR(4),@pmVrAvaludo MONEY,@pmNroEscritura VARCHAR(50)
,@pmCdTipoPoliza VARCHAR(4),@pmDescPoliza VARCHAR(150),@pmVigencPoliza INT,@pmFecVencPoliza SMALLDATETIME,@pmValorPoliza MONEY,@pmEdoGarantias INT,@pmDescGarantOtro VARCHAR(250),@pmInverContab INT,@pmInverContrac INT,@pmVolContMes DECIMAL(14,4)
,@pmVolContTotal DECIMAL(14,4),@pmVolViabMes DECIMAL(14,4),@pmVolViabTotal DECIMAL(14,4),@pmInvCtbObra MONEY,@pmInvCtbCapital MONEY,@pmInvCtbEfectivo MONEY,@pmInvCtbImagen MONEY,@pmInvCtbMntmto MONEY,@pmFecDesemb SMALLDATETIME,@pmInvContObra MONEY
,@pmInvContCapital MONEY,@pmInvContEfectivo MONEY,@pmInvContImagen MONEY,@pmCrtlAprovInv INT,@pmCrtlAprovImg INT,@pmCdTipoBloq VARCHAR(4),@pmEdoDocmtos INT,@pmDescEdoDoc VARCHAR(250),@pmRentabInicial DECIMAL(14,4),@pmGraciaBonif INT
 AS 
 INSERT INTO Trn_MayContratos (NContrato,IdCia,Fecha,IdCliente,IdAgencia,IdTipo,IdBonif,ConvBonif,CuotaFija,CondPago,DiasGracia,ModPlazo,Plazo,FechaVence,ModRecursos,VrInversion,VrInventario,VrCuota,ModCuota,IdBandera,AprobJunta,FechaAprob
,ObservAprob,Observacion,IdVend,IdEstado,TimeSys,IdCiaCrea,IdUsuario,FechaInicio,CuotaMin,TipoAprob,NumInterno,InvCombus,CuotaBase
,FechaFirma,CdAprobNeg,ArchivoAprob,ClaseContrato,DescTipoCont,GarantHipoteca,GarantPrenda,GarantPoliza,GarantBanco,GarantPagare,GarantOtro,CdTipoBien,VrAvaludo,NroEscritura,CdTipoPoliza,DescPoliza,VigencPoliza,FecVencPoliza,ValorPoliza,EdoGarantias,DescGarantOtro,InverContab,InverContrac
,VolContMes,VolContTotal,VolViabMes,VolViabTotal,InvCtbObra,InvCtbCapital,InvCtbEfectivo,InvCtbImagen,InvCtbMntmto,FecDesemb,InvContObra,InvContCapital,InvContEfectivo,InvContImagen,CrtlAprovInv,CrtlAprovImg,CdTipoBloq,EdoDocmtos,DescEdoDoc,RentabInicial,GraciaBonif)
 VALUES (@pmNContrato,@pmIdCia,@pmFecha,@pmIdCliente,@pmIdAgencia,@pmIdTipo,@pmIdBonif,@pmConvBonif,@pmCuotaFija,@pmCondPago,@pmDiasGracia,@pmModPlazo,@pmPlazo,@pmFechaVence,@pmModRecursos,@pmVrInversion
,@pmVrInventario,@pmVrCuota,@pmModCuota,@pmIdBandera,@pmAprobJunta,@pmFechaAprob,@pmObservAprob,@pmObservacion,@pmIdVend,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmFechaInicio,@pmCuotaMin,@pmTipoAprob,@pmNumInterno,@pmInvCombus,@pmCuotaBase
,@pmFechaFirma,@pmCdAprobNeg,@pmArchivoAprob,@pmClaseContrato,@pmDescTipoCont,@pmGarantHipoteca,@pmGarantPrenda,@pmGarantPoliza,@pmGarantBanco,@pmGarantPagare,@pmGarantOtro,@pmCdTipoBien,@pmVrAvaludo,@pmNroEscritura,@pmCdTipoPoliza,@pmDescPoliza,@pmVigencPoliza,@pmFecVencPoliza,@pmValorPoliza
,@pmEdoGarantias,@pmDescGarantOtro,@pmInverContab,@pmInverContrac,@pmVolContMes,@pmVolContTotal,@pmVolViabMes,@pmVolViabTotal,@pmInvCtbObra,@pmInvCtbCapital,@pmInvCtbEfectivo,@pmInvCtbImagen,@pmInvCtbMntmto,@pmFecDesemb,@pmInvContObra,@pmInvContCapital,@pmInvContEfectivo,@pmInvContImagen
,@pmCrtlAprovInv,@pmCrtlAprovImg,@pmCdTipoBloq,@pmEdoDocmtos,@pmDescEdoDoc,@pmRentabInicial,@pmGraciaBonif)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayEstados @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50)
,@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO MayEstados (IdEstado,Estado,NColor,Inactivo)
 VALUES (@pmIdEstado,@pmEstado,@pmNColor,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayInventario @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdArticulo VARCHAR(16),@pmDetalle VARCHAR(150)
,@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY,@pmVrTotal MONEY,@pmTipoArticulo VARCHAR(10),@pmReferencia VARCHAR(50)
,@pmIdTipo VARCHAR(4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmCantEntrega DECIMAL(14,4)
,@pmFecEntrega SMALLDATETIME,@pmEstado VARCHAR(10),@pmCdProd VARCHAR(16),@pmNumReq INT,@pmIdCiaReq CHAR(2),@pmCantReq DECIMAL(14,4)
,@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmCantOrden DECIMAL(14,4)
 AS 
 INSERT INTO Trn_MayInventario (NContrato,IdCia,Item,IdArticulo,Detalle,Cantidad,VrUnitario,VrTotal,TipoArticulo,Referencia,IdTipo,TipDoc,Documento,IdCiaDoc
,CantEntrega,FecEntrega,Estado,CdProd,NumReq,IdCiaReq,CantReq,NumOrden,IdCiaOrd,CantOrden)
 VALUES (@pmNContrato,@pmIdCia,@pmItem,@pmIdArticulo,@pmDetalle,@pmCantidad,@pmVrUnitario,@pmVrTotal,@pmTipoArticulo,@pmReferencia
,@pmIdTipo,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmCantEntrega,@pmFecEntrega,@pmEstado
,@pmCdProd,@pmNumReq,@pmIdCiaReq,@pmCantReq,@pmNumOrden,@pmIdCiaOrd,@pmCantOrden)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayInvmov @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFechaDoc SMALLDATETIME,@pmIdArticulo VARCHAR(16)
,@pmCantidad DECIMAL(14,4),@pmNContrato INT,@pmIdCiaCon CHAR(2),@pmItemArt INT
 AS 
 INSERT INTO Trn_MayInvmov (TipDoc,Documento,IdCia,Item,FechaDoc,IdArticulo,Cantidad,NContrato,IdCiaCon,ItemArt)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFechaDoc,@pmIdArticulo,@pmCantidad,@pmNContrato,@pmIdCiaCon,@pmItemArt)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayItemsInv @pmIdArticulo VARCHAR(16),@pmDescrpcion VARCHAR(150),@pmUndMed VARCHAR(10)
,@pmGrupoArt VARCHAR(50),@pmCdProducto VARCHAR(16),@pmInactivo BIT
 AS 
 INSERT INTO MayItemsInv (IdArticulo,Descrpcion,UndMed,GrupoArt,CdProducto,Inactivo)
 VALUES (@pmIdArticulo,@pmDescrpcion,@pmUndMed,@pmGrupoArt,@pmCdProducto,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayLiqDetalle @pmNLiquida INT,@pmIdCia CHAR(2),@pmNContrato INT,@pmIdCiaCon CHAR(2)
,@pmItem INT,@pmIdSubgrupo VARCHAR(8),@pmCombustible BIT,@pmUnidades DECIMAL(14,4),@pmUnidDev DECIMAL(14,4)
,@pmValorTotal MONEY,@pmValorDev MONEY,@pmIntervalo VARCHAR(50),@pmMargen DECIMAL(14,4)
,@pmTarifa DECIMAL(14,4),@pmTipoTarifa CHAR(1),@pmVrLiquida MONEY,@pmLogDetalle VARCHAR(250),@pmSubsidio  DECIMAL(14,4)
,@pmVenGalones DECIMAL(14,4),@pmVenTotal MONEY
 AS 
 INSERT INTO Trn_MayLiqDetalle (NLiquida,IdCia,NContrato,IdCiaCon,Item,IdSubgrupo,Combustible,Unidades,UnidDev,ValorTotal
,ValorDev,Intervalo,Margen,Tarifa,TipoTarifa,VrLiquida,LogDetalle,Subsidio,VenGalones,VenTotal)
 VALUES (@pmNLiquida,@pmIdCia,@pmNContrato,@pmIdCiaCon,@pmItem,@pmIdSubgrupo,@pmCombustible,@pmUnidades
,@pmUnidDev,@pmValorTotal,@pmValorDev,@pmIntervalo,@pmMargen,@pmTarifa,@pmTipoTarifa,@pmVrLiquida,@pmLogDetalle,@pmSubsidio,@pmVenGalones,@pmVenTotal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayLiqTotales @pmNLiquida INT,@pmIdCia CHAR(2),@pmItem INT,@pmNContrato INT,@pmIdCiaCon CHAR(2)
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmUnidades DECIMAL(14,4),@pmVrBonif MONEY,@pmVrAbono MONEY
,@pmVrCuotaFija MONEY,@pmDiasGracia INT,@pmTipoNcr VARCHAR(3),@pmNotaCred INT,@pmIdCiaNot CHAR(2),@pmLogLiquida VARCHAR(250)
 AS 
 INSERT INTO Trn_MayLiqTotales (NLiquida,IdCia,Item,NContrato,IdCiaCon,IdCliente,IdAgencia,Unidades,VrBonif,VrAbono
,VrCuotaFija,DiasGracia,TipoNcr,NotaCred,IdCiaNot,LogLiquida)
 VALUES (@pmNLiquida,@pmIdCia,@pmItem,@pmNContrato,@pmIdCiaCon,@pmIdCliente,@pmIdAgencia,@pmUnidades
,@pmVrBonif,@pmVrAbono,@pmVrCuotaFija,@pmDiasGracia,@pmTipoNcr,@pmNotaCred,@pmIdCiaNot,@pmLogLiquida)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayLiquida @pmNLiquida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmEstado INT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_MayLiquida (NLiquida,IdCia,Fecha,Observacion,Estado,FecDev,TipCom,Comprobante,IdCiaCom,TimeSys,IdUsuario)
 VALUES (@pmNLiquida,@pmIdCia,@pmFecha,@pmObservacion,@pmEstado,@pmFecDev,@pmTipCom,@pmComprobante
,@pmIdCiaCom,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayPspto @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoProd VARCHAR(20),@pmCantidad DECIMAL(14,4),@pmUnidad VARCHAR(10)
,@pmCantMes DECIMAL(14,4),@pmCdProducto VARCHAR(16),@pmCdSubgrupo VARCHAR(8)
 AS 
 INSERT INTO Trn_MayPspto (NContrato,IdCia,Item,TipoProd,Cantidad,Unidad,CantMes,CdProducto,CdSubgrupo)
 VALUES (@pmNContrato,@pmIdCia,@pmItem,@pmTipoProd,@pmCantidad,@pmUnidad,@pmCantMes,@pmCdProducto,@pmCdSubgrupo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayTipos @pmIdTipo VARCHAR(4),@pmTipContrato VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTipos (IdTipo,TipContrato,Inactivo)
 VALUES (@pmIdTipo,@pmTipContrato,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsMayTiposAprob @pmIdAprob VARCHAR(4),@pmTipoAprob VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposAprob (IdAprob,TipoAprob,Inactivo)
 VALUES (@pmIdAprob,@pmTipoAprob,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsMayTiposBienes @pmIdTipBien VARCHAR(4),@pmTipoBien VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposBienes (IdTipBien,TipoBien,Inactivo)
 VALUES (@pmIdTipBien,@pmTipoBien,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayTiposBonif @pmIdBonif VARCHAR(4),@pmTipoBonif VARCHAR(50)
,@pmAboCartera BIT,@pmAboPstmo BIT,@pmInactivo BIT
 AS 
 INSERT INTO MayTiposBonif (IdBonif,TipoBonif,AboCartera,AboPstmo,Inactivo)
 VALUES (@pmIdBonif,@pmTipoBonif,@pmAboCartera,@pmAboPstmo,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsMayTiposDoc @pmIdDoc VARCHAR(4),@pmNombreDoc VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposDoc (IdDoc,NombreDoc,Inactivo)
 VALUES (@pmIdDoc,@pmNombreDoc,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsMayTiposPoliza @pmIdTipoPol VARCHAR(4),@pmTipoPoliza VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO MayTiposPoliza (IdTipoPol,TipoPoliza,Inactivo)
 VALUES (@pmIdTipoPol,@pmTipoPoliza,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsMercancias @pmIdMercancia VARCHAR(16),@pmDescripMcia VARCHAR(150),@pmCodigoMcia VARCHAR(16),@pmIdGrupo VARCHAR(10),@pmUndMed VARCHAR(10)
,@pmIdUnd VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmContenedor BIT,@pmIdProducto VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Mercancias (IdMercancia,DescripMcia,CodigoMcia,IdGrupo,UndMed,IdUnd,IdNat,IdMnjo,IdTmcia,Contenedor,IdProducto,IdEstado,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdMercancia,@pmDescripMcia,@pmCodigoMcia,@pmIdGrupo,@pmUndMed,@pmIdUnd,@pmIdNat,@pmIdMnjo,@pmIdTmcia,@pmContenedor,@pmIdProducto
,@pmIdEstado,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsModulos @pmIdMod VARCHAR(3),@pmConsecutivo INT,@pmDescripcion VARCHAR(50)
,@pmValor BIT,@pmnModulo VARCHAR(10)
 AS 
 INSERT INTO Sys_Modulos (IdMod,Consecutivo,Descripcion,Valor,nModulo)
 VALUES (@pmIdMod,@pmConsecutivo,@pmDescripcion,@pmValor,@pmnModulo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAbonos @pmNPrestamo INT,@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmFechaAbo SMALLDATETIME
,@pmVrAbono MONEY,@pmnCuotas INT,@pmIdPeriodo VARCHAR(8),@pmDscto VARCHAR(3),@pmDetalle VARCHAR(50),@pmNLiquida INT
 AS 
 INSERT INTO Trn_NomAbonos (NPrestamo,Item,IdEmpleado,FechaAbo,VrAbono,nCuotas,IdPeriodo,Dscto,Detalle,NLiquida)
 VALUES (@pmNPrestamo,@pmItem,@pmIdEmpleado,@pmFechaAbo,@pmVrAbono,@pmnCuotas,@pmIdPeriodo,@pmDscto,@pmDetalle,@pmNLiquida)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAlqDet @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmApellido1 VARCHAR(50),@pmApellido2 VARCHAR(50),@pmNombre1 VARCHAR(50),@pmNombre2 VARCHAR(50),@pmIdFonPen VARCHAR(8),@pmIdFonSal VARCHAR(8),@pmIdFonRie VARCHAR(8),@pmIdCajCom VARCHAR(8),@pmSalBasico MONEY
,@pmVrIBCPen MONEY,@pmDiasPen DECIMAL(10,4),@pmTarifPen DECIMAL(10,4),@pmVrPension MONEY,@pmVrApoFsp MONEY,@pmVrApoFsu MONEY,@pmVrApoVolAfi MONEY,@pmVrApoVolApt MONEY,@pmVrNoReten MONEY,@pmVrIBCSal MONEY,@pmDiasSal DECIMAL(10,4),@pmTarifSal DECIMAL(10,4),@pmVrSalud MONEY,@pmVrUpc MONEY,@pmVrIncap MONEY,@pmVrLicencia MONEY,@pmNumAutInc VARCHAR(30)
,@pmNumAutLic VARCHAR(30),@pmVrIBCRie MONEY,@pmDiasRie DECIMAL(10,4),@pmTarifRie DECIMAL(10,4),@pmVrRiesgos MONEY,@pmVrIncIrp MONEY,@pmNumAutIrp VARCHAR(30),@pmIdInstala VARCHAR(4),@pmVrIBCRef MONEY,@pmVrIBCCajas MONEY,@pmDiasCom DECIMAL(10,4),@pmTarifCom DECIMAL(10,4),@pmVrCajas MONEY,@pmTarifSen DECIMAL(10,4),@pmVrSena MONEY,@pmTarifIcb DECIMAL(10,4),@pmVrICBF MONEY
,@pmTarifEsap DECIMAL(10,4),@pmVrESAP MONEY,@pmTarifEdu DECIMAL(10,4),@pmVrMinEdu MONEY,@pmTarifFsp DECIMAL(10,4),@pmTarifFsu DECIMAL(10,4),@pmCdFonAvp VARCHAR(8)
 AS 
 INSERT INTO Trn_NomAlqDet (TipoLiq,Numero,Item,IdEmpleado,NContrato,Apellido1,Apellido2,Nombre1,Nombre2,IdFonPen,IdFonSal,IdFonRie,IdCajCom,SalBasico,VrIBCPen,DiasPen,TarifPen,VrPension,VrApoFsp,VrApoFsu,VrApoVolAfi,VrApoVolApt,VrNoReten,VrIBCSal,DiasSal,TarifSal,VrSalud,VrUpc,VrIncap,VrLicencia,NumAutInc,NumAutLic,VrIBCRie,DiasRie,TarifRie,VrRiesgos,VrIncIrp,NumAutIrp,IdInstala,VrIBCRef,VrIBCCajas,DiasCom,TarifCom,VrCajas
,TarifSen,VrSena,TarifIcb,VrICBF,TarifEsap,VrESAP,TarifEdu,VrMinEdu,TarifFsp,TarifFsu,CdFonAvp)
 VALUES (@pmTipoLiq,@pmNumero,@pmItem,@pmIdEmpleado,@pmNContrato,@pmApellido1,@pmApellido2,@pmNombre1,@pmNombre2,@pmIdFonPen,@pmIdFonSal,@pmIdFonRie,@pmIdCajCom,@pmSalBasico,@pmVrIBCPen,@pmDiasPen,@pmTarifPen,@pmVrPension,@pmVrApoFsp,@pmVrApoFsu,@pmVrApoVolAfi,@pmVrApoVolApt,@pmVrNoReten,@pmVrIBCSal,@pmDiasSal,@pmTarifSal,@pmVrSalud,@pmVrUpc,@pmVrIncap,@pmVrLicencia
,@pmNumAutInc,@pmNumAutLic,@pmVrIBCRie,@pmDiasRie,@pmTarifRie,@pmVrRiesgos,@pmVrIncIrp,@pmNumAutIrp,@pmIdInstala,@pmVrIBCRef,@pmVrIBCCajas,@pmDiasCom,@pmTarifCom,@pmVrCajas,@pmTarifSen,@pmVrSena,@pmTarifIcb,@pmVrICBF,@pmTarifEsap,@pmVrESAP,@pmTarifEdu,@pmVrMinEdu,@pmTarifFsp,@pmTarifFsu,@pmCdFonAvp)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAlqNov @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmNovIng INT,@pmNovRet INT,@pmNovTde INT,@pmNovTae INT,@pmNovTdp INT,@pmNovTap INT
,@pmNovVsp INT,@pmNovVte INT,@pmNovVst INT,@pmNovSln INT,@pmNovIge INT,@pmNovLma INT,@pmNovVac INT,@pmNovAvp INT,@pmNovVct INT,@pmNovIrp INT,@pmCodNewPen VARCHAR(8),@pmCodNewSal VARCHAR(8),@pmCodNewIns VARCHAR(4)
,@pmCodNewRie VARCHAR(4),@pmCodNewGru VARCHAR(4),@pmDiasNov INT
 AS 
 INSERT INTO Trn_NomAlqNov (TipoLiq,Numero,Item,IdEmpleado,NovIng,NovRet,NovTde,NovTae,NovTdp,NovTap,NovVsp,NovVte,NovVst,NovSln,NovIge,NovLma,NovVac,NovAvp,NovVct,NovIrp,CodNewPen,CodNewSal,CodNewIns,CodNewRie,CodNewGru,DiasNov)
 VALUES (@pmTipoLiq,@pmNumero,@pmItem,@pmIdEmpleado,@pmNovIng,@pmNovRet,@pmNovTde,@pmNovTae,@pmNovTdp,@pmNovTap,@pmNovVsp,@pmNovVte,@pmNovVst,@pmNovSln,@pmNovIge,@pmNovLma,@pmNovVac,@pmNovAvp,@pmNovVct
,@pmNovIrp,@pmCodNewPen,@pmCodNewSal,@pmCodNewIns,@pmCodNewRie,@pmCodNewGru,@pmDiasNov)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAlqTot @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT,@pmTipAdm VARCHAR(3),@pmIdFondo VARCHAR(8),@pmCodTipoApo CHAR(2),@pmVrTotalCot MONEY,@pmVrTotalFsp MONEY,@pmVrTotalFsu MONEY
,@pmVrTotalAva MONEY,@pmVrTotalAve MONEY,@pmVrTotalUpc MONEY,@pmVrTotalLic MONEY,@pmVrTotalInc MONEY,@pmVrTotalOtr MONEY,@pmDiasMora INT,@pmTarifMora DECIMAL(10,4),@pmVrIntMora MONEY,@pmVrIMoraOtr MONEY,@pmVrTotalSAF MONEY
,@pmVrSAFUpc MONEY,@pmVrTotalPagar MONEY,@pmVrTotalFon MONEY,@pmTotalAfi INT,@pmNumAutLic VARCHAR(30),@pmNumAutInc VARCHAR(30),@pmNumPlaSAF VARCHAR(20),@pmTipoReg INT
 AS 
 INSERT INTO Trn_NomAlqTot (TipoLiq,Numero,Item,TipAdm,IdFondo,CodTipoApo,VrTotalCot,VrTotalFsp,VrTotalFsu,VrTotalAva,VrTotalAve,VrTotalUpc,VrTotalLic,VrTotalInc,VrTotalOtr,DiasMora,TarifMora,VrIntMora,VrIMoraOtr,VrTotalSAF,VrSAFUpc,VrTotalPagar,VrTotalFon,TotalAfi,NumAutLic,NumAutInc,NumPlaSAF,TipoReg)
 VALUES (@pmTipoLiq,@pmNumero,@pmItem,@pmTipAdm,@pmIdFondo,@pmCodTipoApo,@pmVrTotalCot,@pmVrTotalFsp,@pmVrTotalFsu,@pmVrTotalAva,@pmVrTotalAve,@pmVrTotalUpc,@pmVrTotalLic,@pmVrTotalInc,@pmVrTotalOtr,@pmDiasMora,@pmTarifMora,@pmVrIntMora,@pmVrIMoraOtr,@pmVrTotalSAF
,@pmVrSAFUpc,@pmVrTotalPagar,@pmVrTotalFon,@pmTotalAfi,@pmNumAutLic,@pmNumAutInc,@pmNumPlaSAF,@pmTipoReg)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAportante @pmIdAportnte VARCHAR(16),@pmRazonSocial VARCHAR(200),@pmTipoId CHAR(1),@pmDvNit CHAR(1),@pmIdRepLegal VARCHAR(16),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pme_mail VARCHAR(100)
,@pmClaseApo CHAR(1),@pmTipoEco CHAR(1),@pmTipoPersna CHAR(1),@pmCIIUDane VARCHAR(8),@pmFecIniCndato SMALLDATETIME,@pmTipAccCndato INT,@pmFecFinCndato SMALLDATETIME,@pmCodOperInfo VARCHAR(10),@pmTipoApo CHAR(1),@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO NomAportante (IdAportnte,RazonSocial,TipoId,DvNit,IdRepLegal,Direccion,IdLocal,Telefono,Fax,e_mail,ClaseApo,TipoEco,TipoPersna,CIIUDane,FecIniCndato,TipAccCndato,FecFinCndato,CodOperInfo,TipoApo,FecAdd,IdUsuario)
 VALUES (@pmIdAportnte,@pmRazonSocial,@pmTipoId,@pmDvNit,@pmIdRepLegal,@pmDireccion,@pmIdLocal,@pmTelefono,@pmFax,@pme_mail,@pmClaseApo,@pmTipoEco,@pmTipoPersna,@pmCIIUDane,@pmFecIniCndato,@pmTipAccCndato
,@pmFecFinCndato,@pmCodOperInfo,@pmTipoApo,@pmFecAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAstncia @pmIdEmpleado VARCHAR(16),@pmFechaDia SMALLDATETIME,@pmHHDiurno DECIMAL(10,4),@pmHHNocturno DECIMAL(10,4),@pmHEDiurno DECIMAL(10,4),@pmHENocturno DECIMAL(10,4),@pmTipoJrnada VARCHAR(10)
,@pmEsDmncal BIT,@pmHorario VARCHAR(30),@pmTimeSys SMALLDATETIME, @pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomAstncia (IdEmpleado,FechaDia,HHDiurno,HHNocturno,HEDiurno,HENocturno,TipoJrnada,EsDmncal,Horario,TimeSys,IdUsuario)
 VALUES (@pmIdEmpleado,@pmFechaDia,@pmHHDiurno,@pmHHNocturno,@pmHEDiurno,@pmHENocturno,@pmTipoJrnada,@pmEsDmncal,@pmHorario,@pmTimeSys,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomAutoliq @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmFecha SMALLDATETIME,@pmPerAutoliq VARCHAR(6),@pmPerAutoSal VARCHAR(6),@pmFechaPago SMALLDATETIME,@pmIdAportnte VARCHAR(16),@pmPresentacion CHAR(1),@pmCodCia CHAR(2),@pmCodFondo VARCHAR(8),@pmCodArp VARCHAR(8),@pmCodOper VARCHAR(10),@pmEsCorr BIT,@pmPerCorr VARCHAR(6)
,@pmNumCorr VARCHAR(20),@pmTotalEmp INT,@pmVrNomina MONEY,@pmVrPension MONEY,@pmVrSalud MONEY,@pmVrRiesgos MONEY,@pmVrCajas MONEY,@pmVrSena MONEY,@pmVrIcbf MONEY,@pmVrEsap MONEY,@pmVrMinEduc MONEY,@pmTotalAfipen INT,@pmTotalAfiSal INT,@pmTotalAfiRie INT,@pmTotalAfiCaj INT,@pmTotalAfiSen INT,@pmTotalAfiIcb INT,@pmTotalAfiEsa INT,@pmTotalAfiEduc INT,@pmTotAdmPen INT
,@pmTotAdmSal INT,@pmTotAdmRie INT,@pmTotAdmCom INT,@pmObservacion VARCHAR(250),@pmLogAdmon VARCHAR(250),@pmRedoMiles BIT,@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmTipoPlanilla VARCHAR(3)
 AS 
 INSERT INTO Trn_NomAutoliq (TipoLiq,Numero,Fecha,PerAutoliq,PerAutoSal,FechaPago,IdAportnte,Presentacion,CodCia,CodFondo,CodArp,CodOper,EsCorr,PerCorr,NumCorr,TotalEmp,VrNomina,VrPension,VrSalud,VrRiesgos,VrCajas,VrSena,VrIcbf,VrEsap,VrMinEduc,TotalAfipen,TotalAfiSal,TotalAfiRie,TotalAfiCaj,TotalAfiSen,TotalAfiIcb,TotalAfiEsa,TotalAfiEduc,TotAdmPen,TotAdmSal,TotAdmRie,TotAdmCom,Observacion,LogAdmon,RedoMiles,Anulado,IdEstado,TimeSys,IdUsuario,TipoPlanilla)
 VALUES (@pmTipoLiq,@pmNumero,@pmFecha,@pmPerAutoliq,@pmPerAutoSal,@pmFechaPago,@pmIdAportnte,@pmPresentacion,@pmCodCia,@pmCodFondo,@pmCodArp,@pmCodOper,@pmEsCorr,@pmPerCorr,@pmNumCorr,@pmTotalEmp,@pmVrNomina,@pmVrPension,@pmVrSalud,@pmVrRiesgos,@pmVrCajas,@pmVrSena,@pmVrIcbf,@pmVrEsap,@pmVrMinEduc,@pmTotalAfipen,@pmTotalAfiSal,@pmTotalAfiRie,@pmTotalAfiCaj,@pmTotalAfiSen,@pmTotalAfiIcb
,@pmTotalAfiEsa,@pmTotalAfiEduc,@pmTotAdmPen,@pmTotAdmSal,@pmTotAdmRie,@pmTotAdmCom,@pmObservacion,@pmLogAdmon,@pmRedoMiles,@pmAnulado,@pmIdEstado,@pmTimeSys,@pmIdUsuario,@pmTipoPlanilla)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomCAdmon @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmClaseAdmon VARCHAR(10)
,@pmFecIniNov SMALLDATETIME,@pmNNovedad INT,@pmIdFondo VARCHAR(8),@pmIdFonNew VARCHAR(8),@pmObservacion VARCHAR(100),@pmAnulado BIT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomCAdmon (IdEmpleado,Numero,Fecha,NContrato,ClaseAdmon,FecIniNov,NNovedad,IdFondo,IdFonNew,Observacion,Anulado,TimeSys,IdUsuario)
 VALUES (@pmIdEmpleado,@pmNumero,@pmFecha,@pmNContrato,@pmClaseAdmon,@pmFecIniNov,@pmNNovedad,@pmIdFondo,@pmIdFonNew,@pmObservacion,@pmAnulado,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomCausalesRet @pmIdCausa VARCHAR(4),@pmCausalRet VARCHAR(100),@pmIndmnizar BIT,@pmInactivo BIT
 AS 
 INSERT INTO NomCausalesRet (IdCausa,CausalRet,Indmnizar,Inactivo)
 VALUES (@pmIdCausa,@pmCausalRet,@pmIndmnizar,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomCertf @pmIdEmpleado VARCHAR(16),@pmNumCertf VARCHAR(20),@pmnAnno INT,@pmnMes INT,@pmVrCertf MONEY
,@pmObservacion VARCHAR(100),@pmClaseCert VARCHAR(10),@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomCertf (IdEmpleado,NumCertf,nAnno,nMes,VrCertf,Observacion,FecAdd,IdUsuario,ClaseCert)
 VALUES (@pmIdEmpleado,@pmNumCertf,@pmnAnno,@pmnMes,@pmVrCertf,@pmObservacion,@pmFecAdd,@pmIdUsuario,@pmClaseCert)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomConAuto @pmIdReg INT,@pmIdConcepto VARCHAR(4),@pmFecInicio SMALLDATETIME,@pmVrAplicar MONEY,@pmModalidad CHAR(1)
,@pmOrigenBase CHAR(2),@pmFrecAplica VARCHAR(10),@pmCedEmpleado VARCHAR(16),@pmCodNom VARCHAR(4),@pmCodDep VARCHAR(4),@pmCodCcto VARCHAR(16)
,@pmCritSelect VARCHAR(4000),@pmFecFinal SMALLDATETIME,@pmExceptuar VARCHAR(8),@pmObservacion VARCHAR(50),@pmCodSubCos VARCHAR(16),@pmInactivo BIT
,@pmNumPerMes INT,@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomConAuto (IdReg,IdConcepto,FecInicio,VrAplicar,Modalidad,OrigenBase,FrecAplica,CedEmpleado,CodNom,CodDep,CodCcto,CritSelect,FecFinal,Exceptuar,Observacion,CodSubCos,Inactivo,NumPerMes,FecAdd,IdUsuario)
 VALUES (@pmIdReg,@pmIdConcepto,@pmFecInicio,@pmVrAplicar,@pmModalidad,@pmOrigenBase,@pmFrecAplica,@pmCedEmpleado,@pmCodNom,@pmCodDep,@pmCodCcto,@pmCritSelect,@pmFecFinal,@pmExceptuar
,@pmObservacion,@pmCodSubCos,@pmInactivo,@pmNumPerMes,@pmFecAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomConCuentas @pmIdConcepto VARCHAR(4),@pmItem INT,@pmNClase INT,@pmIdCuenta VARCHAR(16)
,@pmTipoMov CHAR(1),@pmCodNom VARCHAR(4),@pmCodDep VARCHAR(4),@pmCodFon VARCHAR(8),@pmNitTerc CHAR(1)
 AS 
 INSERT INTO NomConCuentas (IdConcepto,Item,NClase,IdCuenta,TipoMov,CodNom,CodDep,CodFon,NitTerc)
 VALUES (@pmIdConcepto,@pmItem,@pmNClase,@pmIdCuenta,@pmTipoMov,@pmCodNom,@pmCodDep,@pmCodFon,@pmNitTerc)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomConOpciones @pmIdConcepto VARCHAR(4),@pmIdOpc INT,@pmNomOpcion VARCHAR(50),@pmAsignado BIT
 AS 
 INSERT INTO NomConOpciones (IdConcepto,IdOpc,NomOpcion,Asignado)
 VALUES (@pmIdConcepto,@pmIdOpc,@pmNomOpcion,@pmAsignado)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomConceptos @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmClaseCon VARCHAR(10),@pmUnidad VARCHAR(10),@pmTarifa DECIMAL(10,4)
,@pmBaseTrfa CHAR(2),@pmPdtermnado BIT,@pmInactivo BIT,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO NomConceptos (IdConcepto,Concepto,ClaseCon,Unidad,Tarifa,BaseTrfa,Pdtermnado,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdConcepto,@pmConcepto,@pmClaseCon,@pmUnidad,@pmTarifa,@pmBaseTrfa,@pmPdtermnado,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomDefault @pmnAnno INT,@pmIdOpc VARCHAR(3),@pmDescripcion VARCHAR(100),@pmValor MONEY
 AS 
 INSERT INTO NomDefault (nAnno,IdOpc,Descripcion,Valor)
 VALUES (@pmnAnno,@pmIdOpc,@pmDescripcion,@pmValor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomDetalle @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmIdEmpleado VARCHAR(16),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantDevg DECIMAL(10,4),@pmCantDed DECIMAL(10,4),@pmVrUnitario MONEY,@pmVrTotDevg MONEY,@pmVrTotDed MONEY
,@pmUnidad VARCHAR(10),@pmClaseLiq VARCHAR(10),@pmVrOrigen MONEY,@pmTarifa DECIMAL(10,4),@pmDiasCalc DECIMAL(10,4),@pmDiasNov DECIMAL(10,4),@pmCodFondo VARCHAR(8),@pmNPrestamo INT,@pmNCuota INT,@pmOrigCargue VARCHAR(10),@pmVrBaseLiq MONEY
 AS 
 INSERT INTO Trn_NomDetalle (IdPeriodo,KeyRegistro,IdEmpleado,Item,IdConcepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,Unidad,ClaseLiq,VrOrigen,Tarifa,DiasCalc,DiasNov,CodFondo,NPrestamo,NCuota,OrigCargue,VrBaseLiq)
 VALUES (@pmIdPeriodo,@pmKeyRegistro,@pmIdEmpleado,@pmItem,@pmIdConcepto,@pmDetalle,@pmCantDevg,@pmCantDed,@pmVrUnitario,@pmVrTotDevg,@pmVrTotDed,@pmUnidad,@pmClaseLiq,@pmVrOrigen,@pmTarifa
,@pmDiasCalc,@pmDiasNov,@pmCodFondo,@pmNPrestamo,@pmNCuota,@pmOrigCargue,@pmVrBaseLiq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomDetalle_Sel @pmIdEmpleado VARCHAR(16),@pmNewCedula VARCHAR(16)
 AS 
 INSERT INTO Trn_NomDetalle (IdPeriodo,KeyRegistro,IdEmpleado,Item,IdConcepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,Unidad,ClaseLiq,VrOrigen
,Tarifa,DiasCalc,DiasNov,CodFondo,NPrestamo,NCuota,OrigCargue,VrBaseLiq)
 SELECT D.IdPeriodo,@pmNewCedula +CAST(NContrato AS VARCHAR(10)) +CASE EsRetiro WHEN 1 THEN '_1' ELSE '_0'  END
,@pmNewCedula,Item,IdConcepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,Unidad,ClaseLiq,VrOrigen
,Tarifa,DiasCalc,DiasNov,CodFondo,NPrestamo,NCuota,OrigCargue,VrBaseLiq
 FROM  Trn_NomDetalle AS D INNER JOIN Trn_Nomina AS N ON D.IdPeriodo=N.IdPeriodo AND D.KeyRegistro=N.KeyRegistro
 WHERE D.IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomFijos @pmIdEmpleado VARCHAR(16),@pmnAnno INT,@pmnMes INT,@pmTarifa DECIMAL(10,4),@pmVrBase MONEY,@pmVrSalarios MONEY,@pmVrPrima MONEY,@pmVrVacacnes MONEY,@pmVrSIntExcto MONEY
,@pmVrOtrosIng MONEY,@pmVrIngresos MONEY,@pmVrPensiones MONEY,@pmVrDedcido MONEY,@pmVrExcento MONEY,@pmVrCertEdu MONEY,@pmVrCertVda MONEY,@pmVrCertAFC MONEY,@pmVrCertfcado MONEY,@pmPeriodos DECIMAL(10,4),@pmVrTotalBase MONEY
,@pmOrignPorc VARCHAR(10),@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmVrSalud MONEY
 AS 
 INSERT INTO Trn_NomFijos (IdEmpleado,nAnno,nMes,Tarifa,VrBase,VrSalarios,VrPrima,VrVacacnes,VrSIntExcto,VrOtrosIng,VrIngresos,VrPensiones,VrDedcido,VrExcento,VrCertEdu,VrCertVda,VrCertAFC,VrCertfcado,Periodos,VrTotalBase,OrignPorc,FecAdd,IdUsuario,VrSalud)
 VALUES (@pmIdEmpleado,@pmnAnno,@pmnMes,@pmTarifa,@pmVrBase,@pmVrSalarios,@pmVrPrima,@pmVrVacacnes,@pmVrSIntExcto,@pmVrOtrosIng,@pmVrIngresos,@pmVrPensiones,@pmVrDedcido,@pmVrExcento,@pmVrCertEdu,@pmVrCertVda,@pmVrCertAFC
,@pmVrCertfcado,@pmPeriodos,@pmVrTotalBase,@pmOrignPorc,@pmFecAdd,@pmIdUsuario,@pmVrSalud)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomLiqDet @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmFecUltLiq SMALLDATETIME,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmVrBase MONEY,@pmCantidad INT
,@pmVrLiquida MONEY,@pmVrDeducido MONEY,@pmFecIniPmd SMALLDATETIME,@pmFecFinPmd SMALLDATETIME,@pmDiasBase INT,@pmDiasNoRem INT,@pmTarifa DECIMAL(10,4),@pmNPrestamo INT,@pmNCuota INT,@pmLogPmdio VARCHAR(600),@pmIdEmpleado VARCHAR(16),@pmNContrato INT
,@pmpVehiculo VARCHAR(10),@pmIdDep VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdNom VARCHAR(4),@pmVrSalario MONEY,@pmVrParcial MONEY
 AS 
 INSERT INTO Trn_NomLiqDet (TipoLiq,Numero,Item,IdConcepto,Detalle,FecUltLiq,FecInicial,FecFinal,VrBase,Cantidad,VrLiquida,VrDeducido,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,Tarifa,NPrestamo,NCuota,LogPmdio,IdEmpleado,NContrato,pVehiculo,IdDep,IdCCosto,IdSubCos,IdNom,VrSalario,VrParcial)
 VALUES (@pmTipoLiq,@pmNumero,@pmItem,@pmIdConcepto,@pmDetalle,@pmFecUltLiq,@pmFecInicial,@pmFecFinal,@pmVrBase,@pmCantidad,@pmVrLiquida,@pmVrDeducido,@pmFecIniPmd,@pmFecFinPmd,@pmDiasBase,@pmDiasNoRem,@pmTarifa,@pmNPrestamo,@pmNCuota,@pmLogPmdio
,@pmIdEmpleado,@pmNContrato,@pmpVehiculo,@pmIdDep,@pmIdCCosto,@pmIdSubCos,@pmIdNom,@pmVrSalario,@pmVrParcial)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomLiquida @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmFecha SMALLDATETIME,@pmSemstre INT,@pmVrTotal MONEY,@pmVrTotDed MONEY,@pmVrAplicado MONEY,@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomLiquida (TipoLiq,Numero,Fecha,Semstre,VrTotal,VrTotDed,VrAplicado,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,TimeSys,IdUsuario)
 VALUES (@pmTipoLiq,@pmNumero,@pmFecha,@pmSemstre,@pmVrTotal,@pmVrTotDed,@pmVrAplicado,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmAnulado,@pmIdEstado,@pmTimeSys,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomLogVeh @pmIdEmpleado VARCHAR(16),@pmNumReg INT,@pmFecha SMALLDATETIME,@pmpVehAnterior VARCHAR(10),@pmNitPropAnt VARCHAR(16)
,@pmpVehNew VARCHAR(10),@pmNitPropNew VARCHAR(16),@pmObservacion VARCHAR(100),@pmNitCondAnt VARCHAR(16),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomLogVeh (IdEmpleado,NumReg,Fecha,pVehAnterior,NitPropAnt,pVehNew,NitPropNew,Observacion,NitCondAnt,TimeSys,IdUsuario)
 VALUES (@pmIdEmpleado,@pmNumReg,@pmFecha,@pmpVehAnterior,@pmNitPropAnt,@pmpVehNew,@pmNitPropNew,@pmObservacion,@pmNitCondAnt,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomNov @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantidad DECIMAL(10,4),@pmVrUnitario MONEY
,@pmVrTotal MONEY,@pmVrOrigen MONEY,@pmTarifa DECIMAL(10,4),@pmOrigCargue VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomNov (IdPeriodo,IdEmpleado,Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrTotal,VrOrigen,Tarifa,OrigCargue,TimeSys,IdUsuario)
 VALUES (@pmIdPeriodo,@pmIdEmpleado,@pmItem,@pmIdConcepto,@pmDetalle,@pmCantidad,@pmVrUnitario,@pmVrTotal,@pmVrOrigen,@pmTarifa,@pmOrigCargue, @pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomNovedad @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmIdConcepto VARCHAR(4),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCantDias INT,@pmDiaInicio INT
,@pmModalidad VARCHAR(10),@pmIdNov VARCHAR(4),@pmRemnrado BIT,@pmObservacion VARCHAR(250),@pmCodInstla VARCHAR(4),@pmInactivo BIT,@pmCodClsRie VARCHAR(4),@pmNumAutza VARCHAR(30),@pmCodNewFon VARCHAR(8)
,@pmCodOldFon VARCHAR(8),@pmVrCotVolAfi MONEY,@pmVrCotVolApo MONEY,@pmVrNoRetenido MONEY,@pmTipoIncap CHAR(1),@pmFechaDcto SMALLDATETIME,@pmVrDescuento MONEY
,@pmTipoBaseLiq VARCHAR(3),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomNovedad (IdEmpleado,Numero,IdConcepto,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,IdNov,Remnrado,Observacion,CodInstla,Inactivo,CodClsRie,NumAutza,CodOldFon,CodNewFon,VrCotVolAfi,VrCotVolApo
,VrNoRetenido,TipoIncap,FechaDcto,VrDescuento,TimeSys,IdUsuario,TipoBaseLiq)
 VALUES (@pmIdEmpleado,@pmNumero,@pmIdConcepto,@pmFecInicio,@pmFecFinal,@pmCantDias,@pmDiaInicio,@pmModalidad,@pmIdNov,@pmRemnrado,@pmObservacion,@pmCodInstla,@pmInactivo,@pmCodClsRie,@pmNumAutza,@pmCodOldFon,@pmCodNewFon
 ,@pmVrCotVolAfi,@pmVrCotVolApo,@pmVrNoRetenido,@pmTipoIncap,@pmFechaDcto,@pmVrDescuento,@pmTimeSys,@pmIdUsuario,@pmTipoBaseLiq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomPrest @pmNPrestamo INT,@pmFecha SMALLDATETIME,@pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmVrPrestamo MONEY
,@pmNumCuotas INT,@pmVrCuota MONEY,@pmFecInicio SMALLDATETIME,@pmFrecDcto VARCHAR(10),@pmExceptuar VARCHAR(8),@pmTotPrestamo MONEY,@pmTotAbonado MONEY
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmCxpagar BIT
,@pmDsctNom INT,@pmDsctPri INT,@pmDsctVac INT,@pmDsctEspPer VARCHAR(8),@pmDsctEspVal MONEY,@pmVrTmAbono MONEY,@pmNumPerMes INT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomPrest (NPrestamo,Fecha,IdEmpleado,IdConcepto,VrPrestamo,NumCuotas,VrCuota,FecInicio,FrecDcto,Exceptuar,TotPrestamo,TotAbonado,Anulado,FecDev,TipCom,Comprobante,IdCiaCom
,Observacion,Cxpagar,DsctNom,DsctPri,DsctVac,DsctEspPer,DsctEspVal,VrTmAbono,TimeSys,IdUsuario,NumPerMes)
 VALUES (@pmNPrestamo,@pmFecha,@pmIdEmpleado,@pmIdConcepto,@pmVrPrestamo,@pmNumCuotas,@pmVrCuota,@pmFecInicio,@pmFrecDcto,@pmExceptuar,@pmTotPrestamo,@pmTotAbonado,@pmAnulado,@pmFecDev,@pmTipCom
,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmCxpagar,@pmDsctNom,@pmDsctPri,@pmDsctVac,@pmDsctEspPer,@pmDsctEspVal,@pmVrTmAbono,@pmTimeSys,@pmIdUsuario,@pmNumPerMes)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomProrrogas @pmIdEmpleado VARCHAR(16),@pmNumReg INT,@pmNContrato INT,@pmFecha SMALLDATETIME,@pmFecAnterior SMALLDATETIME,@pmIndefinido BIT,@pmCdTipCon VARCHAR(4)
,@pmCdCargo VARCHAR(4),@pmCdDep VARCHAR(4),@pmCdArea VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmIdTipCon VARCHAR(4),@pmIdCargo VARCHAR(4),@pmIdArea VARCHAR(4)
,@pmIdDep VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmObservacion VARCHAR(150),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomProrrogas (IdEmpleado,NumReg,NContrato,Fecha,FecAnterior,Indefinido,CdTipCon,CdCargo,CdDep,CdArea,CdCCosto,CdSubCos,IdTipCon,IdCargo,IdArea,IdDep,IdCCosto,IdSubCos,Observacion,TimeSys,IdUsuario)
 VALUES (@pmIdEmpleado,@pmNumReg,@pmNContrato,@pmFecha,@pmFecAnterior,@pmIndefinido,@pmCdTipCon,@pmCdCargo,@pmCdDep,@pmCdArea,@pmCdCCosto,@pmCdSubCos,@pmIdTipCon,@pmIdCargo,@pmIdArea
,@pmIdDep,@pmIdCCosto,@pmIdSubCos,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomRangosFsu @pmIdRango INT,@pmVrInicial DECIMAL(12,2),@pmVrFinal DECIMAL(12,2),@pmTarifa DECIMAL(10,4)
 AS 
 INSERT INTO NomRangosFsu (IdRango,VrInicial,VrFinal,Tarifa)
 VALUES (@pmIdRango,@pmVrInicial,@pmVrFinal,@pmTarifa)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomRet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmIdCausa VARCHAR(4),@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmDiasTrab INT
,@pmDiasNoRem INT,@pmVrTotDev MONEY,@pmVrTotDed MONEY,@pmVrTotCes MONEY,@pmVrTotPri MONEY,@pmVrTotVac MONEY,@pmVrTotInt MONEY,@pmVrTotInd MONEY,@pmVrNeto MONEY,@pmVrBaseCes MONEY,@pmVrBaseVac MONEY,@pmIdPeriodo VARCHAR(8)
,@pmKeyReg VARCHAR(30),@pmNPerVac INT,@pmVrSalario MONEY,@pmIdDep VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdNom VARCHAR(4),@pmTipoLiq CHAR(1),@pmpVehiculo VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmVrParCes MONEY,@pmVrParInt MONEY,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomRet (IdEmpleado,Numero,Fecha,NContrato,IdCausa,FecInicial,FecFinal,DiasTrab,DiasNoRem,VrTotDev,VrTotDed,VrTotCes,VrTotPri,VrTotVac,VrTotInt,VrTotInd,VrNeto,VrBaseCes,VrBaseVac,IdPeriodo,KeyReg,NPerVac,VrSalario,IdDep,IdCCosto,IdSubCos,IdNom,TipoLiq,pVehiculo
,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,VrParCes,VrParInt,TimeSys,IdUsuario)
 VALUES (@pmIdEmpleado,@pmNumero,@pmFecha,@pmNContrato,@pmIdCausa,@pmFecInicial,@pmFecFinal,@pmDiasTrab,@pmDiasNoRem,@pmVrTotDev,@pmVrTotDed,@pmVrTotCes,@pmVrTotPri,@pmVrTotVac,@pmVrTotInt,@pmVrTotInd,@pmVrNeto,@pmVrBaseCes,@pmVrBaseVac,@pmIdPeriodo,@pmKeyReg
,@pmNPerVac,@pmVrSalario,@pmIdDep,@pmIdCCosto,@pmIdSubCos,@pmIdNom,@pmTipoLiq,@pmpVehiculo,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmAnulado,@pmIdEstado,@pmVrParCes,@pmVrParInt,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomRetDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantidad DECIMAL(10,4),@pmVrUnitario MONEY,@pmVrDevngado MONEY,@pmVrDeducido MONEY,@pmTarifa DECIMAL(10,4),@pmVrBase MONEY,@pmDiasBase DECIMAL(10,4)
,@pmDiasNoRem DECIMAL(10,4),@pmFecUltLiq SMALLDATETIME,@pmFecDesde SMALLDATETIME,@pmFecHasta SMALLDATETIME,@pmFecIniPmd SMALLDATETIME,@pmFecFinPmd SMALLDATETIME,@pmGrupoLiq VARCHAR(15),@pmNPrestamo INT,@pmNCuota INT,@pmLogPmdio VARCHAR(600)
,@pmClaseLiq VARCHAR(10),@pmCodFondo VARCHAR(8),@pmVrBaseLiq MONEY
 AS 
 INSERT INTO Trn_NomRetDet (IdEmpleado,Numero,Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrDevngado,VrDeducido,Tarifa,VrBase,DiasBase,DiasNoRem,FecUltLiq,FecDesde,FecHasta,FecIniPmd,FecFinPmd,GrupoLiq,NPrestamo,NCuota,LogPmdio,ClaseLiq,CodFondo,VrBaseLiq)
 VALUES (@pmIdEmpleado,@pmNumero,@pmItem,@pmIdConcepto,@pmDetalle,@pmCantidad,@pmVrUnitario,@pmVrDevngado,@pmVrDeducido,@pmTarifa,@pmVrBase,@pmDiasBase,@pmDiasNoRem,@pmFecUltLiq,@pmFecDesde,@pmFecHasta,@pmFecIniPmd,@pmFecFinPmd,@pmGrupoLiq
,@pmNPrestamo,@pmNCuota,@pmLogPmdio,@pmClaseLiq,@pmCodFondo,@pmVrBaseLiq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomRetDet_Sel @pmIdEmpleado VARCHAR(16),@pmNewCedula VARCHAR(16)
 AS 
 INSERT INTO Trn_NomRetDet (IdEmpleado,Numero,Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrDevngado,VrDeducido,Tarifa,VrBase,DiasBase,DiasNoRem
,FecUltLiq,FecDesde,FecHasta,FecIniPmd,FecFinPmd,GrupoLiq,NPrestamo,NCuota,LogPmdio,ClaseLiq,CodFondo,VrBaseLiq)
 SELECT @pmNewCedula,Numero,Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrDevngado,VrDeducido,Tarifa,VrBase,DiasBase,DiasNoRem
,FecUltLiq,FecDesde,FecHasta,FecIniPmd,FecFinPmd,GrupoLiq,NPrestamo,NCuota,LogPmdio,ClaseLiq,CodFondo,VrBaseLiq
 FROM Trn_NomRetDet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomRet_Sel @pmIdEmpleado VARCHAR(16),@pmNewCedula VARCHAR(16)
 AS 
 INSERT INTO Trn_NomRet (IdEmpleado,Numero,Fecha,NContrato,IdCausa,FecInicial,FecFinal,DiasTrab,DiasNoRem,VrTotDev,VrTotDed,VrTotCes,VrTotPri,VrTotVac
,VrTotInt,VrTotInd,VrNeto,VrBaseCes,VrBaseVac,IdPeriodo,KeyReg,NPerVac,VrSalario,IdDep,IdCCosto,IdSubCos,IdNom,TipoLiq,pVehiculo
,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,VrParCes,VrParInt,TimeSys,IdUsuario)
 SELECT @pmNewCedula,Numero,Fecha,NContrato,IdCausa,FecInicial,FecFinal,DiasTrab,DiasNoRem,VrTotDev,VrTotDed,VrTotCes,VrTotPri,VrTotVac
,VrTotInt,VrTotInd,VrNeto,VrBaseCes,VrBaseVac,IdPeriodo,KeyReg,NPerVac,VrSalario,IdDep,IdCCosto,IdSubCos,IdNom,TipoLiq,pVehiculo
,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,VrParCes,VrParInt,TimeSys,IdUsuario
 FROM  Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomSueldos @pmNumero INT,@pmIdEmpleado VARCHAR(16),@pmFecha SMALLDATETIME,@pmNContrato INT,@pmVrSueAnterior MONEY,@pmVrSueNuevo MONEY,@pmVrAtAnterior MONEY,@pmVrAtNuevo MONEY
,@pmMetodoUp CHAR(1),@pmReemplzado BIT,@pmFecUltAumto SMALLDATETIME,@pmCritSelect VARCHAR(250),@pmObservacion VARCHAR(100),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomSueldos (Numero,IdEmpleado,Fecha,NContrato,VrSueAnterior,VrSueNuevo,VrAtAnterior,VrAtNuevo,MetodoUp,Reemplzado,FecUltAumto,CritSelect,Observacion,TimeSys,IdUsuario)
 VALUES (@pmNumero,@pmIdEmpleado,@pmFecha,@pmNContrato,@pmVrSueAnterior,@pmVrSueNuevo,@pmVrAtAnterior,@pmVrAtNuevo,@pmMetodoUp,@pmReemplzado,@pmFecUltAumto
,@pmCritSelect,@pmObservacion,@pmTimeSys,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomTablaRet @pmnAnno INT,@pmnIntrvalo INT,@pmVrInicial MONEY,@pmVrFinal MONEY,@pmTarifa DECIMAL(10,4),@pmVrRetncion MONEY
,@pmTarExcdnte DECIMAL(10,4),@pmVrBaseExc MONEY,@pmFechaAdd SMALLDATETIME, @pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO NomTablaRet (nAnno,nIntrvalo,VrInicial,VrFinal,Tarifa,VrRetncion,TarExcdnte,VrBaseExc,FechaAdd,IdUsuario)
 VALUES (@pmnAnno,@pmnIntrvalo,@pmVrInicial,@pmVrFinal,@pmTarifa,@pmVrRetncion,@pmTarExcdnte,@pmVrBaseExc,@pmFechaAdd,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomVac @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCantidad DECIMAL(10,4),@pmVrLiquida MONEY,@pmVrBase MONEY,@pmFecIniPmd SMALLDATETIME,@pmFecFinPmd SMALLDATETIME
,@pmDiasBase INT,@pmDiasNoRem INT,@pmDiasVac INT,@pmNNovedad INT,@pmNPeriodos INT,@pmCantRem INT,@pmVrRemndo MONEY,@pmVrTotDed MONEY,@pmVrSalario MONEY,@pmpVehiculo VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmLogPmdio VARCHAR(600)
,@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NomVac (IdEmpleado,Numero,Fecha,NContrato,FecInicial,FecFinal,Cantidad,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,DiasVac,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,VrSalario,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,IdEstado,Anulado,TimeSys,IdUsuario)
 VALUES (@pmIdEmpleado,@pmNumero,@pmFecha,@pmNContrato,@pmFecInicial,@pmFecFinal,@pmCantidad,@pmVrLiquida,@pmVrBase,@pmFecIniPmd,@pmFecFinPmd,@pmDiasBase,@pmDiasNoRem,@pmDiasVac,@pmNNovedad,@pmNPeriodos,@pmCantRem,@pmVrRemndo,@pmVrTotDed,@pmVrSalario,@pmpVehiculo
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmLogPmdio,@pmIdEstado,@pmAnulado,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomVacDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantidad INT,@pmVrDevgado MONEY,@pmVrDeducido MONEY
,@pmTarifa DECIMAL(10,4),@pmVrBaseLiq MONEY,@pmVrCertfcado MONEY,@pmVrPension MONEY,@pmNPrestamo INT,@pmNCuota INT,@pmIdPeriodo VARCHAR(8),@pmVrSalud MONEY
 AS 
 INSERT INTO Trn_NomVacDet (IdEmpleado,Numero,Item,IdConcepto,Detalle,Cantidad,VrDevgado,VrDeducido,Tarifa,VrBaseLiq,VrCertfcado,VrPension,NPrestamo,NCuota,IdPeriodo,VrSalud)
 VALUES (@pmIdEmpleado,@pmNumero,@pmItem,@pmIdConcepto,@pmDetalle,@pmCantidad,@pmVrDevgado,@pmVrDeducido,@pmTarifa,@pmVrBaseLiq,@pmVrCertfcado,@pmVrPension,@pmNPrestamo,@pmNCuota,@pmIdPeriodo,@pmVrSalud)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomVacDet_Sel @pmIdEmpleado VARCHAR(16),@pmNewCedula VARCHAR(16)
 AS 
 INSERT INTO Trn_NomVacDet (IdEmpleado,Numero,Item,IdConcepto,Detalle,Cantidad,VrDevgado,VrDeducido,Tarifa,VrBaseLiq,VrCertfcado,VrPension,NPrestamo,NCuota,IdPeriodo,VrSalud)
 SELECT @pmNewCedula,Numero,Item,IdConcepto,Detalle,Cantidad,VrDevgado,VrDeducido,Tarifa,VrBaseLiq,VrCertfcado,VrPension,NPrestamo,NCuota,IdPeriodo,VrSalud
 FROM  Trn_NomVacDet 
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomVac_Sel @pmIdEmpleado VARCHAR(16),@pmNewCedula VARCHAR(16)
 AS 
 INSERT INTO Trn_NomVac (IdEmpleado,Numero,Fecha,NContrato,FecInicial,FecFinal,Cantidad,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase
,DiasNoRem,DiasVac,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,VrSalario,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,IdEstado,Anulado,TimeSys,IdUsuario)
 SELECT @pmNewCedula,Numero,Fecha,NContrato,FecInicial,FecFinal,Cantidad,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase
,DiasNoRem,DiasVac,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,VrSalario,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,IdEstado,Anulado,TimeSys,IdUsuario
 FROM  Trn_NomVac WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomVehiculos @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmClaseVeh VARCHAR(50),@pmTipoAdmon VARCHAR(50)
,@pmGrupoProp VARCHAR(50),@pmVehPropio BIT,@pmCedCond VARCHAR(16),@pmConductor VARCHAR(50)
 AS 
 INSERT INTO NomVehiculos (IdVehiculo,NumVeh,ClaseVeh,TipoAdmon,GrupoProp,VehPropio,CedCond,Conductor)
 VALUES (@pmIdVehiculo,@pmNumVeh,@pmClaseVeh,@pmTipoAdmon,@pmGrupoProp,@pmVehPropio,@pmCedCond,@pmConductor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomina @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmIdEmpleado VARCHAR(16),@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCantidad DECIMAL(10,4),@pmVrDevengado MONEY,@pmVrDeducido MONEY,@pmNContrato INT,@pmIdDep VARCHAR(4)
,@pmIdInstala VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdNom VARCHAR(4),@pmVrSalario MONEY,@pmVrATransMes MONEY,@pmpVehiculo VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmEsRetiro BIT,@pmNLiqRet INT
,@pmTipoLiq CHAR(1),@pmCantNoRem DECIMAL(10,4),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Nomina (IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal,Cantidad,VrDevengado,VrDeducido,NContrato,IdDep,IdInstala,IdCCosto,IdSubCos,IdNom,VrSalario,VrATransMes,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,EsRetiro,NLiqRet,TipoLiq,CantNoRem,TimeSys,IdUsuario)
 VALUES (@pmIdPeriodo,@pmKeyRegistro,@pmIdEmpleado,@pmFecInicial,@pmFecFinal,@pmCantidad,@pmVrDevengado,@pmVrDeducido,@pmNContrato,@pmIdDep,@pmIdInstala,@pmIdCCosto,@pmIdSubCos,@pmIdNom,@pmVrSalario,@pmVrATransMes,@pmpVehiculo,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado
,@pmEsRetiro,@pmNLiqRet,@pmTipoLiq,@pmCantNoRem,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNomina_Sel  @pmIdEmpleado  VARCHAR(16),@pmNewCedula VARCHAR(16)
 AS 
 INSERT INTO Trn_Nomina (IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal,Cantidad,VrDevengado,VrDeducido,NContrato,IdDep
,IdInstala,IdCCosto,IdSubCos,IdNom,VrSalario,VrATransMes,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,EsRetiro,NLiqRet,TipoLiq,CantNoRem,TimeSys,IdUsuario)
 SELECT  IdPeriodo,@pmNewCedula +CAST(NContrato AS VARCHAR(10)) +CASE EsRetiro WHEN 1 THEN '_1' ELSE '_0'  END,@pmNewCedula,FecInicial,FecFinal,Cantidad,VrDevengado
,VrDeducido,NContrato,IdDep,IdInstala,IdCCosto,IdSubCos,IdNom,VrSalario,VrATransMes,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,EsRetiro,NLiqRet,TipoLiq,CantNoRem,TimeSys,IdUsuario
 FROM Trn_Nomina 
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNotaProd @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4)
,@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY,@pmVrCostos MONEY,@pmCantEnt DECIMAL(14,4),@pmCantSal DECIMAL(14,4),@pmNumOrden INT
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NotaProd (TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,IdCCosto,IdSubCos,Modalidad,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdTercero,@pmVrSubTotal,@pmVrCostos,@pmCantEnt,@pmCantSal,@pmNumOrden
,@pmIdCCosto,@pmIdSubCos,@pmModalidad,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev
,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCosto MONEY,@pmVrSancion MONEY,@pmVrOtros MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmCantidad DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY
,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmCdCajero VARCHAR(11),@pmIdLocal VARCHAR(8),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT
,@pmIdCiaDcm CHAR(2),@pmFecDcm SMALLDATETIME,@pmCdProducto VARCHAR(16),@pmCdForma VARCHAR(4),@pmNumCheque VARCHAR(20),@pmCdBanco VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmPrestCaja BIT,@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Notas (TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCosto,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,Cantidad,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,CdCajero,IdLocal,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdProducto,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,PrestCaja,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFechaVence,@pmVrSubTotal,@pmVrImpuesto,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrCosto,@pmVrSancion,@pmVrOtros,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmCantidad,@pmBaseImp,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaIva,@pmTarifaRet,@pmTarifaIca,@pmTarifaRiv,@pmCodTarIva,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmIdCCosto,@pmIdSubCos,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmCdCajero,@pmIdLocal,@pmReferencia,@pmpVehiculo
,@pmCdConductor,@pmTipDcm,@pmDocumento,@pmIdCiaDcm,@pmFecDcm,@pmCdProducto,@pmCdForma,@pmNumCheque,@pmCdBanco,@pmMulPlazos,@pmIdPlazo,@pmPrestCaja,@pmModalidad,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsNotasBuses @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY,@pmValorIva MONEY,@pmValorReteFte MONEY,@pmValorReteIca MONEY,@pmValorCosto MONEY,@pmValorSancion MONEY,@pmValorNeto MONEY,@pmValorAplicado MONEY
,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmIdTarCom VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmPrestCaja BIT,@pmIntMora BIT,@pmNumCheque VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdClase VARCHAR(4),@pmIdCargo VARCHAR(4),@pmReferencia VARCHAR(20),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmBaseIva MONEY,@pmBaseRet MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmIdTarIva VARCHAR(4)
,@pmIdTarRet VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_NotasBuses (TipDoc,NumNota,IdCia,Fecha,FecVence,IdConcepto,IdCliente,IdAgencia,IdVehiculo,IdConductor,VehPropio,ValorTotal,ValorIva,ValorReteFte,ValorReteIca,ValorCosto,ValorSancion,ValorNeto,ValorAplicado,IdVendedor,Comision,IdTarCom,MulPlazos,IdPlazo,PrestCaja,IntMora,NumCheque,IdBanco,IdCCosto,IdClase,IdCargo,Referencia,TipDcm,Documento,IdCiaDoc,BaseIva,BaseRet,TarifaIva,TarifaRet,IdTarIva,IdTarRet,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmNumNota,@pmIdCia,@pmFecha,@pmFecVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmIdVehiculo,@pmIdConductor,@pmVehPropio,@pmValorTotal,@pmValorIva,@pmValorReteFte,@pmValorReteIca,@pmValorCosto,@pmValorSancion,@pmValorNeto,@pmValorAplicado,@pmIdVendedor,@pmComision,@pmIdTarCom,@pmMulPlazos,@pmIdPlazo,@pmPrestCaja,@pmIntMora,@pmNumCheque,@pmIdBanco,@pmIdCCosto,@pmIdClase,@pmIdCargo,@pmReferencia,@pmTipDcm
,@pmDocumento,@pmIdCiaDoc,@pmBaseIva,@pmBaseRet,@pmTarifaIva,@pmTarifaRet,@pmIdTarIva,@pmIdTarRet,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsOComb @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaDesp SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY
,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250)
,@pmIdLocal VARCHAR(8),@pmLugarEnv VARCHAR(50),@pmDiasEntraga INT,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmCdRuta VARCHAR(4),@pmReferncia VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(150)
,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmTipGui VARCHAR(3),@pmGuia INT,@pmIdCiaGui CHAR(2),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmAutzaMora INT,@pmAutzaCupo INT,@pmAprobacion INT,@pmFechaAprob SMALLDATETIME
,@pmDetalleAprob VARCHAR(250),@pmCdUsuAprob VARCHAR(11),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OComb (TipDoc,OCargue,IdCia,Fecha,FechaDesp,FechaVence,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto,Cantidad,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocal,LugarEnv,DiasEntraga,IdVehiculo,IdConductor,NitEmpTrans,EmpTrans,CdRuta,Referncia
,IdForma,DetallePago,MulPlazos,IdPlazo,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,Factura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo,Aprobacion,FechaAprob,DetalleAprob,CdUsuAprob,OrigenAdd,ZonaFrontera,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmOCargue,@pmIdCia,@pmFecha,@pmFechaDesp,@pmFechaVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrFletes,@pmVrOtros,@pmVrCargos,@pmVrOtrDcto,@pmVrSobretasa,@pmVrImpGlobal,@pmVrNeto,@pmCantidad,@pmIdVend,@pmTarifaCom,@pmCodTarCom
,@pmDirEnvio,@pmIdLocal,@pmLugarEnv,@pmDiasEntraga,@pmIdVehiculo,@pmIdConductor,@pmNitEmpTrans,@pmEmpTrans,@pmCdRuta,@pmReferncia,@pmIdForma,@pmDetallePago,@pmMulPlazos,@pmIdPlazo,@pmTipPed,@pmPedido,@pmIdCiaPed,@pmTipGui,@pmGuia,@pmIdCiaGui,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmFechaFact,@pmAutzaMora,@pmAutzaCupo,@pmAprobacion
,@pmFechaAprob,@pmDetalleAprob,@pmCdUsuAprob,@pmOrigenAdd,@pmZonaFrontera,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsOCompra @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrSobtasa MONEY,@pmVrImpGlobal MONEY
,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdEmpresa VARCHAR(16),@pmDirEntrega VARCHAR(250),@pmIdLocEtga VARCHAR(8),@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4)
,@pmCdMney VARCHAR(5),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmLiqFletes BIT,@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipEnt VARCHAR(3),@pmEntrada INT,@pmIdCiaEnt CHAR(2),@pmNumAutoriza INT,@pmModalidad VARCHAR(10),@pmVigencia VARCHAR(10),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME
,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4),@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmVrReteIVA MONEY,@pmCodTarRiv VARCHAR(4)
,@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_OCompra (TipDoc,OCompra,IdCia,Fecha,FechaVence,IdConcepto,IdProv,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobtasa,VrImpGlobal,VrRetencion,VrReteICA,VrReteIVA,VrNeto,Cantidad,TarifaRet,TarifaIca,CodTarRet,CodTarIca,CodTarRiv,IdEmpresa,DirEntrega,IdLocEtga,DiasEntraga,NitContac,NomContac,TelContac,emlContac,IdCCosto,IdSubCos,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans
,LiqFletes,pVehiculo,CdConductor,TipEnt,Entrada,IdCiaEnt,NumAutoriza,Modalidad,Vigencia,OrigenAdd,Anulado,FecDev,Observacion,NumAprob,FecAprob,CdUsuAprob,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmOCompra,@pmIdCia,@pmFecha,@pmFechaVence,@pmIdConcepto,@pmIdProv,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrFletes,@pmVrOtros,@pmVrCargos,@pmVrOtrDcto,@pmVrSobtasa,@pmVrImpGlobal,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrNeto,@pmCantidad,@pmTarifaRet,@pmTarifaIca,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmIdEmpresa,@pmDirEntrega,@pmIdLocEtga,@pmDiasEntraga,@pmNitContac,@pmNomContac
,@pmTelContac,@pmemlContac,@pmIdCCosto,@pmIdSubCos,@pmIdForma,@pmDetallePago,@pmMulPlazos,@pmIdPlazo,@pmCdMney,@pmNitEmpTrans,@pmEmpTrans,@pmLiqFletes,@pmpVehiculo,@pmCdConductor,@pmTipEnt,@pmEntrada,@pmIdCiaEnt,@pmNumAutoriza,@pmModalidad,@pmVigencia,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmNumAprob,@pmFecAprob,@pmCdUsuAprob,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsObjetosApp @pmIdObj VARCHAR(10),@pmIdGrupo VARCHAR(6),@pmSubModulo VARCHAR(3),@pmIndObj INT,@pmNombre VARCHAR(100),@pmFormulario VARCHAR(10)
,@pmPermisos VARCHAR(20),@pmNivelMinimo INT,@pmNomArchivo VARCHAR(30)
 AS 
 INSERT INTO Sys_ObjetosApp (IdObj,IdGrupo,SubModulo,IndObj,Nombre,Formulario,Permisos,NivelMinimo,NomArchivo)
 VALUES (@pmIdObj,@pmIdGrupo,@pmSubModulo,@pmIndObj,@pmNombre,@pmFormulario,@pmPermisos,@pmNivelMinimo,@pmNomArchivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsOpciones @pmIdOpc VARCHAR(3),@pmOpcion VARCHAR(100),@pmTipoDato VARCHAR(15)
,@pmValor VARCHAR(100),@pmNivUp INT,@pmnModulo VARCHAR(10)
 AS 
 INSERT INTO adm_Opciones (IdOpc,Opcion,TipoDato,Valor,NivUp,nModulo)
 VALUES (@pmIdOpc,@pmOpcion,@pmTipoDato,@pmValor,@pmNivUp,@pmnModulo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsOpedido @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdClieFact VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY
,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250),@pmIdLocEnv VARCHAR(8),@pmLugarEnvio VARCHAR(50)
,@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150)
,@pmAsignarVeh BIT,@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmCdRuta VARCHAR(4),@pmListaPrec CHAR(1),@pmRefPedido VARCHAR(50),@pmModalidad VARCHAR(10),@pmVigencia VARCHAR(10),@pmNumAutoriza INT,@pmNumAutCupo INT,@pmNumAutCheq INT,@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME
,@pmDetalleAprob VARCHAR(250),@pmCdUsuAprob VARCHAR(11),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFechaRem SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmTipoTrans INT,@pmTipoOrden VARCHAR(3),@pmTipoModifica VARCHAR(10),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Opedido (TipDoc,Pedido,IdCia,Fecha,FechaVence,IdConcepto,IdCliente,IdAgencia,IdClieFact,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto,Cantidad,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,LugarEnvio,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago
,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans,AsignarVeh,pVehiculo,CdConductor,CdRuta,ListaPrec,RefPedido,Modalidad,Vigencia,NumAutoriza,NumAutCupo,NumAutCheq,NumAprob,IdCiaApr,FecAprob,DetalleAprob,CdUsuAprob,TipFac,Factura,IdCiaFac,FechaFact,TipRem,Remision,IdCiaRem,FechaRem,OrigenAdd,ZonaFrontera,TipoTrans,TipoOrden,TipoModifica,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmPedido,@pmIdCia,@pmFecha,@pmFechaVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmIdClieFact,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrFletes,@pmVrOtros,@pmVrCargos,@pmVrOtrDcto,@pmVrSobretasa,@pmVrImpGlobal,@pmVrNeto,@pmCantidad,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmDirEnvio,@pmIdLocEnv
,@pmLugarEnvio,@pmDiasEntraga,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargoContac,@pmIdForma,@pmDetallePago,@pmMulPlazos,@pmIdPlazo,@pmCdMney,@pmNitEmpTrans,@pmEmpTrans,@pmAsignarVeh,@pmpVehiculo,@pmCdConductor,@pmCdRuta,@pmListaPrec,@pmRefPedido,@pmModalidad,@pmVigencia,@pmNumAutoriza,@pmNumAutCupo
,@pmNumAutCheq,@pmNumAprob,@pmIdCiaApr,@pmFecAprob,@pmDetalleAprob,@pmCdUsuAprob,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmFechaFact,@pmTipRem,@pmRemision,@pmIdCiaRem,@pmFechaRem,@pmOrigenAdd,@pmZonaFrontera,@pmTipoTrans,@pmTipoOrden,@pmTipoModifica,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPagos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdForma VARCHAR(4),@pmDetalle VARCHAR(150),@pmVrPagado MONEY,@pmVrCambio MONEY
,@pmEsCaja BIT,@pmIdBanco VARCHAR(4),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmNumForma VARCHAR(20),@pmFecForma SMALLDATETIME,@pmCtaForma VARCHAR(30),@pmBeneficiario VARCHAR(150),@pmReferncia1 VARCHAR(50)
,@pmReferncia2 VARCHAR(50),@pmCdLocal VARCHAR(8),@pmNumAutoriza VARCHAR(30),@pmVrDenom MONEY,@pmCantidad INT,@pmTipDenom CHAR(1),@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmItemDoc INT
,@pmCdCta VARCHAR(4),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Pagos (TipDoc,Documento,IdCia,Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,IdUsuario)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmIdForma,@pmDetalle,@pmVrPagado,@pmVrCambio,@pmEsCaja,@pmIdBanco,@pmNitCliente,@pmCdAgencia,@pmNumForma,@pmFecForma,@pmCtaForma,@pmBeneficiario,@pmReferncia1,@pmReferncia2
,@pmCdLocal,@pmNumAutoriza,@pmVrDenom,@pmCantidad,@pmTipDenom,@pmTipRef,@pmDocRef,@pmIdCiaRef,@pmItemDoc,@pmCdCta,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPaises @pmIdPais VARCHAR(8),@pmCodISO VARCHAR(3),@pmNombrePais VARCHAR(50),@pmIdMneda VARCHAR(5)
 AS 
 INSERT INTO adm_Paises (IdPais,CodISO,NombrePais,IdMneda)
 VALUES (@pmIdPais,@pmCodISO,@pmNombrePais,@pmIdMneda)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPeajesCat @pmIdCat VARCHAR(4),@pmCatpeaje VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO PeajesCat (IdCat,Catpeaje,Inactivo)
 VALUES (@pmIdCat,@pmCatpeaje,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPeajesClase @pmIdClase VARCHAR(4),@pmClasePeaje VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO PeajesClase (IdClase,ClasePeaje,Inactivo)
 VALUES (@pmIdClase,@pmClasePeaje,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPeajesTarf @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4),@pmVrTarifa MONEY
 AS 
 INSERT INTO PeajesTarf (IdCat,IdClase,VrTarifa)
 VALUES (@pmIdCat,@pmIdClase,@pmVrTarifa)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPermisos @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10),@pmConsecutivo INT,@pmPerAdd BIT,@pmPerUpdate BIT
,@pmPerDelete BIT,@pmPerAnular BIT,@pmPerRead BIT,@pmPerRename BIT
 AS 
 INSERT INTO adm_Permisos (IdUsuario,IdObj,Consecutivo,PerAdd,PerUpdate,PerDelete,PerAnular,PerRead,PerRename)
 VALUES (@pmIdUsuario,@pmIdObj,@pmConsecutivo,@pmPerAdd,@pmPerUpdate,@pmPerDelete,@pmPerAnular,@pmPerRead,@pmPerRename)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPermisosRoles @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3),@pmAsignado BIT
 AS 
 INSERT INTO adm_PermisosRoles (IdUsuario,IdObj,IdRole,Asignado)
 VALUES (@pmIdUsuario,@pmIdObj,@pmIdRole,@pmAsignado)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsPicoPlaca @pmFecha SMALLDATETIME,@pmUltimoDig VARCHAR(50)
 AS 
 INSERT INTO PicoPlaca (Fecha,UltimoDig)
 VALUES (@pmFecha,@pmUltimoDig)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPlazos @pmIdPlazo VARCHAR(4),@pmPlazo VARCHAR(50),@pmNVmto INT
,@pmDiasPago VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Plazos (IdPlazo,Plazo,NVmto,DiasPago,Inactivo)
 VALUES (@pmIdPlazo,@pmPlazo,@pmNVmto,@pmDiasPago,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPolizas @pmNumPoliza VARCHAR(30),@pmFecha SMALLDATETIME,@pmVigencia SMALLDATETIME,@pmNitCompania VARCHAR(16),@pmCompania VARCHAR(150)
,@pmNitTomador VARCHAR(16),@pmTomador VARCHAR(150),@pmNitAseg VARCHAR(16),@pmAsegurado VARCHAR(150),@pmNitBenef VARCHAR(16),@pmBeneficiario VARCHAR(150)
,@pmInactivo BIT,@pmDescFirma VARCHAR(2000),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Polizas (NumPoliza,Fecha,Vigencia,NitCompania,Compania,NitTomador,Tomador,NitAseg,Asegurado,NitBenef,Beneficiario,DescFirma,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmNumPoliza,@pmFecha,@pmVigencia,@pmNitCompania,@pmCompania,@pmNitTomador,@pmTomador,@pmNitAseg,@pmAsegurado
,@pmNitBenef,@pmBeneficiario,@pmDescFirma,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsPreCausacion @pmCausacionID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmModalidad VARCHAR(10),@pmTotalCapital MONEY,@pmTotalInteres MONEY,@pmTotalCuota MONEY,@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmTipDoc VARCHAR(3),@pmPrestamoID INT
,@pmIdCiaDoc CHAR(2),@pmNumCuota INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_PreCausacion (CausacionID,IdCia,Fecha,Modalidad,TotalCapital,TotalInteres,TotalCuota,IdVehiculo,IdPropietario,TipDoc,PrestamoID,IdCiaDoc,NumCuota,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,TimeSys,IdUsuario)
 VALUES (@pmCausacionID,@pmIdCia,@pmFecha,@pmModalidad,@pmTotalCapital,@pmTotalInteres,@pmTotalCuota,@pmIdVehiculo,@pmIdPropietario,@pmTipDoc,@pmPrestamoID,@pmIdCiaDoc,@pmNumCuota,@pmAnulado,@pmFecDev,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmTimeSys,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsPreCuotas @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmNumCuota INT,@pmFecCausa SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmValorCapital MONEY,@pmValorInteres MONEY,@pmValorCuota MONEY,@pmCausacionID INT,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_PreCuotas (TipDoc,PrestamoID,IdCia,NumCuota,FecCausa,FecVence,ValorCapital,ValorInteres,ValorCuota,CausacionID,IdUsuario)
 VALUES (@pmTipDoc,@pmPrestamoID,@pmIdCia,@pmNumCuota,@pmFecCausa,@pmFecVence,@pmValorCapital,@pmValorInteres,@pmValorCuota,@pmCausacionID,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPresDetalle @pmNumero INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdCuenta VARCHAR(16),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmVrPresupuesto MONEY,@pmSaldoAnterior MONEY,@pmTotalDebitos MONEY,@pmTotalCreditos MONEY
,@pmCdUsuario VARCHAR(11),@pmFecUpdate SMALLDATETIME
 AS 
 INSERT INTO Trn_PresDetalle (Numero,IdCia,Item,IdCuenta,IdCCosto,IdSubCos,VrPresupuesto,SaldoAnterior,TotalDebitos,TotalCreditos,CdUsuario,FecUpdate)
 VALUES (@pmNumero,@pmIdCia,@pmItem,@pmIdCuenta,@pmIdCCosto,@pmIdSubCos,@pmVrPresupuesto,@pmSaldoAnterior,@pmTotalDebitos,@pmTotalCreditos,@pmCdUsuario,@pmFecUpdate)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsPrestamos @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPrestamo SMALLDATETIME,@pmFecInicio SMALLDATETIME,@pmTipoPlazo VARCHAR(10),@pmNumCuotas INT
,@pmDiasCausa VARCHAR(5),@pmDiasVence VARCHAR(5),@pmTipoCuota VARCHAR(10),@pmValorPrestamo MONEY,@pmTasaInteres DECIMAL(14,4),@pmTotalCausado MONEY,@pmTotalInteres MONEY,@pmUltimaCuota INT,@pmCancelado BIT,@pmIdConcepto VARCHAR(4)
,@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmCxPagar BIT,@pmReferencia VARCHAR(20),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME, @pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Prestamos (TipDoc,PrestamoID,IdCia,Fecha,FecPrestamo,FecInicio,TipoPlazo,NumCuotas,DiasCausa,DiasVence,TipoCuota,ValorPrestamo,TasaInteres,TotalCausado,TotalInteres,UltimaCuota,Cancelado,IdConcepto,IdVehiculo,IdPropietario,IdConductor
,VehPropio,CxPagar,Referencia,Anulado,NumDev,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmPrestamoID,@pmIdCia,@pmFecha,@pmFecPrestamo,@pmFecInicio,@pmTipoPlazo,@pmNumCuotas,@pmDiasCausa,@pmDiasVence,@pmTipoCuota,@pmValorPrestamo,@pmTasaInteres,@pmTotalCausado,@pmTotalInteres
,@pmUltimaCuota,@pmCancelado,@pmIdConcepto,@pmIdVehiculo,@pmIdPropietario,@pmIdConductor,@pmVehPropio,@pmCxPagar,@pmReferencia,@pmAnulado,@pmNumDev,@pmFecDev,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsPresupuesto @pmNumero INT,@pmIdCia CHAR(2),@pmnAnno INT,@pmnMes INT
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTipoPsto INT, @pmDetCentros BIT
,@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Presupuesto (Numero,IdCia,nAnno,nMes,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario,TipoPsto,DetCentros)
 VALUES (@pmNumero,@pmIdCia,@pmnAnno,@pmnMes,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario,@pmTipoPsto, @pmDetCentros)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdAditivos @pmIdProducto VARCHAR(16),@pmIdAditivo VARCHAR(16),@pmCant DECIMAL(14,4),@pmUnidad VARCHAR(10)
,@pmCdBodega VARCHAR(4),@pmCdTanq VARCHAR(4)
 AS 
 INSERT INTO ProdAditivos (IdProducto,IdAditivo,Cant,Unidad,CdBodega,CdTanq)
 VALUES (@pmIdProducto,@pmIdAditivo,@pmCant,@pmUnidad,@pmCdBodega,@pmCdTanq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdCombo @pmIdProducto VARCHAR(16),@pmItem INT,@pmIdProdBas VARCHAR(16),@pmCant DECIMAL(14,4)
,@pmCdBodega VARCHAR(4),@pmCdTanq VARCHAR(4),@pmFacMezcla VARCHAR(10),@pmExentoSob BIT
 AS 
 INSERT INTO ProdCombo (IdProducto,Item,IdProdBas,Cant,CdBodega,CdTanq,FacMezcla,ExentoSob)
 VALUES (@pmIdProducto,@pmItem,@pmIdProdBas,@pmCant,@pmCdBodega,@pmCdTanq,@pmFacMezcla,@pmExentoSob)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdCostos @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmCostoAnt MONEY
,@pmCostoUlt MONEY,@pmCostoPmd MONEY,@pmFecUltPmd SMALLDATETIME
 AS 
 INSERT INTO ProdCostos (IdProducto,IdCia,CostoAnt,CostoUlt,CostoPmd,FecUltPmd)
 VALUES (@pmIdProducto,@pmIdCia,@pmCostoAnt,@pmCostoUlt,@pmCostoPmd,@pmFecUltPmd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmIdLocal VARCHAR(8),@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 INSERT INTO ProdImpFron (IdProducto,IdClase,Item,IdLocal,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe)
 VALUES (@pmIdProducto,@pmIdClase,@pmItem,@pmIdLocal,@pmTarifa,@pmSimbTfa,@pmCdTarifa,@pmRngFecha,@pmFecInicial
,@pmFecFinal,@pmLtaPre,@pmInactivo,@pmFecUpdate,@pmTarifZfe)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 INSERT INTO ProdImptos (IdProducto,IdClase,Item,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe)
 VALUES (@pmIdProducto,@pmIdClase,@pmItem,@pmTarifa,@pmSimbTfa,@pmCdTarifa,@pmRngFecha,@pmFecInicial,@pmFecFinal,@pmLtaPre,@pmInactivo,@pmFecUpdate,@pmTarifZfe)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdMcias @pmIdProducto VARCHAR(16),@pmDescripProd VARCHAR(150),@pmDescripAbrv VARCHAR(50),@pmCodBarras VARCHAR(20),@pmReferencia VARCHAR(20),@pmTipoRef VARCHAR(10),@pmIdSubgrupo VARCHAR(8),@pmIdMarca VARCHAR(4),@pmColor VARCHAR(30),@pmTamano VARCHAR(30),@pmMedAlto DECIMAL(14,4)
,@pmMedAncho DECIMAL(14,4),@pmMedLargo DECIMAL(14,4),@pmMedVolm DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmIdUnd VARCHAR(4),@pmIdUndP VARCHAR(4),@pmCdUndS VARCHAR(4),@pmCdUndE VARCHAR(4),@pmCdUndEb VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmIdBodega VARCHAR(4)
,@pmIdUbic VARCHAR(10),@pmDesUbic VARCHAR(50),@pmIdProv VARCHAR(16),@pmGarProv INT,@pmGarClie INT,@pmCdDctCom VARCHAR(4),@pmVrCostAnt MONEY,@pmVrCosto MONEY,@pmVrCostPmd MONEY,@pmIdTarIva VARCHAR(4),@pmIvaInc VARCHAR(10),@pmLtPreDef CHAR(1),@pmPrecio1 MONEY,@pmPrecio2 MONEY,@pmPrecio3 MONEY,@pmPrecio4 MONEY
,@pmPrecio5 MONEY,@pmCdMon1 VARCHAR(5),@pmCdMon2 VARCHAR(5),@pmCdMon3 VARCHAR(5),@pmCdMon4 VARCHAR(5),@pmCdMon5 VARCHAR(5),@pmBaseMgn VARCHAR(10),@pmCdMgn1 VARCHAR(4),@pmCdMgn2 VARCHAR(4),@pmCdMgn3 VARCHAR(4),@pmCdMgn4 VARCHAR(4),@pmCdMgn5 VARCHAR(4),@pmCdDct1 VARCHAR(4),@pmCdDct2 VARCHAR(4)
,@pmCdDct3 VARCHAR(4),@pmCdDct4 VARCHAR(4),@pmCdDct5 VARCHAR(4),@pmCdTarIca VARCHAR(4),@pmCdTarRet VARCHAR(4),@pmExtciaMin DECIMAL(14,4),@pmExtciaMax DECIMAL(14,4),@pmExtciaAct DECIMAL(14,4),@pmFactor1 DECIMAL(14,4),@pmFactor2 DECIMAL(14,4),@pmFactor3 DECIMAL(14,4),@pmFactor4 DECIMAL(14,4),@pmFactor5 DECIMAL(14,4),@pmSeriales BIT
,@pmLotes BIT,@pmCombo BIT,@pmNoAjustes BIT,@pmTanques BIT,@pmValesComb BIT,@pmFecUltcom SMALLDATETIME,@pmFecUltVta SMALLDATETIME,@pmCodMcia VARCHAR(10),@pmDescripLong VARCHAR(250),@pmCmntarios VARCHAR(250),@pmPathFoto VARCHAR(30),@pmCantTpv DECIMAL(14,4),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO ProdMcias (IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,IdSubgrupo,IdMarca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,UndMed,IdUnd,IdUndP,CdUndS,CdUndE,CdUndEb,IdEmp,IdNat,IdMnjo,IdTmcia,IdBodega,IdUbic,DesUbic,IdProv,GarProv,GarClie,CdDctCom,VrCostAnt,VrCosto,VrCostPmd,IdTarIva,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,CdTarIca,CdTarRet,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,IdEstado,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdProducto,@pmDescripProd,@pmDescripAbrv,@pmCodBarras,@pmReferencia,@pmTipoRef,@pmIdSubgrupo,@pmIdMarca,@pmColor,@pmTamano,@pmMedAlto,@pmMedAncho,@pmMedLargo,@pmMedVolm,@pmUndMed,@pmIdUnd,@pmIdUndP,@pmCdUndS,@pmCdUndE,@pmCdUndEb,@pmIdEmp,@pmIdNat,@pmIdMnjo,@pmIdTmcia,@pmIdBodega,@pmIdUbic,@pmDesUbic
,@pmIdProv,@pmGarProv,@pmGarClie,@pmCdDctCom,@pmVrCostAnt,@pmVrCosto,@pmVrCostPmd,@pmIdTarIva,@pmIvaInc,@pmLtPreDef,@pmPrecio1,@pmPrecio2,@pmPrecio3,@pmPrecio4,@pmPrecio5,@pmCdMon1,@pmCdMon2,@pmCdMon3,@pmCdMon4,@pmCdMon5,@pmBaseMgn,@pmCdMgn1,@pmCdMgn2,@pmCdMgn3,@pmCdMgn4,@pmCdMgn5,@pmCdDct1,@pmCdDct2,@pmCdDct3
,@pmCdDct4,@pmCdDct5,@pmCdTarIca,@pmCdTarRet,@pmExtciaMin,@pmExtciaMax,@pmExtciaAct,@pmFactor1,@pmFactor2,@pmFactor3,@pmFactor4,@pmFactor5,@pmSeriales,@pmLotes,@pmCombo,@pmNoAjustes,@pmTanques,@pmValesComb,@pmFecUltcom,@pmFecUltVta,@pmCodMcia,@pmDescripLong,@pmCmntarios,@pmPathFoto,@pmCantTpv,@pmIdEstado,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdMcias_Sel @pmIdProducto VARCHAR(16),@pmNewProducto VARCHAR(16)
 AS 
 INSERT INTO ProdMcias (IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,IdSubgrupo,IdMarca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,UndMed,IdUnd,IdUndP,CdUndS,CdUndE,CdUndEb,IdEmp,IdNat,IdMnjo,IdTmcia,IdBodega,IdUbic,DesUbic,IdProv,GarProv,GarClie,CdDctCom,VrCostAnt,VrCosto,VrCostPmd,IdTarIva,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,CdTarIca,CdTarRet,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,IdEstado,Inactivo,FechaAdd,IdUsuario)
 SELECT @pmNewProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,IdSubgrupo,IdMarca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,UndMed,IdUnd,IdUndP,CdUndS,CdUndE,CdUndEb,IdEmp,IdNat,IdMnjo,IdTmcia,IdBodega,IdUbic,DesUbic,IdProv,GarProv,GarClie,CdDctCom,VrCostAnt,VrCosto,VrCostPmd,IdTarIva,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,CdTarIca,CdTarRet,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,IdEstado,Inactivo,FechaAdd,IdUsuario
 FROM  ProdMcias WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsProdPrecCom @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmNumero INT,@pmTipoTarif VARCHAR(3),@pmnMes INT,@pmnAnno INT
,@pmTarifa DECIMAL(14,4),@pmNitProv VARCHAR(16),@pmInactivo BIT
 AS 
 INSERT INTO ProdPrecCom (IdProducto,IdCia,Numero,TipoTarif,nMes,nAnno,Tarifa,NitProv,Inactivo)
 VALUES (@pmIdProducto,@pmIdCia,@pmNumero,@pmTipoTarif,@pmnMes,@pmnAnno,@pmTarifa,@pmNitProv,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdPrecios @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmVrPrecio1 MONEY,@pmVrPrecio2 MONEY,@pmVrPrecio3 MONEY,@pmVrPrecio4 MONEY
,@pmVrPrecio5 MONEY,@pmIvaInc VARCHAR(10),@pmCdTarIva VARCHAR(4),@pmCdMoney1 VARCHAR(5),@pmCdMoney2 VARCHAR(5),@pmCdMoney3 VARCHAR(5)
,@pmCdMoney4 VARCHAR(5),@pmCdMoney5 VARCHAR(5),@pmCdMargen1 VARCHAR(4),@pmCdMargen2 VARCHAR(4),@pmCdMargen3 VARCHAR(4),@pmCdMargen4 VARCHAR(4)
,@pmCdMargen5 VARCHAR(4),@pmBaseMargen VARCHAR(10),@pmCdDcto1 VARCHAR(4),@pmCdDcto2 VARCHAR(4),@pmCdDcto3 VARCHAR(4),@pmCdDcto4 VARCHAR(4),@pmCdDcto5 VARCHAR(4)
,@pmIdUnd VARCHAR(4),@pmIdBodega VARCHAR(4)
 AS 
 INSERT INTO ProdPrecios (IdProducto,IdCia,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,IvaInc,CdTarIva,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5
,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5,IdUnd,IdBodega)
 VALUES (@pmIdProducto,@pmIdCia,@pmVrPrecio1,@pmVrPrecio2,@pmVrPrecio3,@pmVrPrecio4,@pmVrPrecio5,@pmIvaInc,@pmCdTarIva,@pmCdMoney1,@pmCdMoney2,@pmCdMoney3,@pmCdMoney4,@pmCdMoney5
,@pmCdMargen1,@pmCdMargen2,@pmCdMargen3,@pmCdMargen4,@pmCdMargen5,@pmBaseMargen,@pmCdDcto1,@pmCdDcto2,@pmCdDcto3,@pmCdDcto4,@pmCdDcto5,@pmIdUnd,@pmIdBodega)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdProvee @pmIdProducto VARCHAR(16),@pmIdProv VARCHAR(16),@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmDiasGar INT,@pmCdDct VARCHAR(4)
,@pmVrCostCom MONEY,@pmVrCostPmd MONEY,@pmRefProv VARCHAR(20),@pmDcmento INT
 AS 
 INSERT INTO ProdProvee (IdProducto,IdProv,IdCia,Fecha,DiasGar,CdDct,VrCostCom,VrCostPmd,RefProv,Dcmento)
 VALUES (@pmIdProducto,@pmIdProv,@pmIdCia,@pmFecha,@pmDiasGar,@pmCdDct,@pmVrCostCom,@pmVrCostPmd,@pmRefProv,@pmDcmento)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdSaldoUbc @pmIdProducto VARCHAR(16),@pmIdUbic VARCHAR(10),@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 INSERT INTO ProdSaldoUbc (IdProducto,IdUbic,IdCia,SaldoActual)
 VALUES (@pmIdProducto,@pmIdUbic,@pmIdCia,@pmSaldoActual)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdSaldos @pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4)
,@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 INSERT INTO ProdSaldos (IdProducto,IdBodega,IdCia,SaldoActual)
 VALUES (@pmIdProducto,@pmIdBodega,@pmIdCia,@pmSaldoActual)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProdTarf @pmTipo VARCHAR(3),@pmNumero INT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCdCia CHAR(2),@pmCdLinea VARCHAR(8),@pmCdGrupo VARCHAR(8),@pmCdSubgrupo VARCHAR(8),@pmCdMarca VARCHAR(4),@pmCdProducto VARCHAR(16)
,@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmLtaPre CHAR(1),@pmCdTarifa VARCHAR(4),@pmCdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmCdVend VARCHAR(16),@pmCdCiudad VARCHAR(8),@pmCdZona VARCHAR(4),@pmCdSzona VARCHAR(4),@pmCdGruCli VARCHAR(4),@pmOtrosCrit VARCHAR(250)
,@pmAnulado BIT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_ProdTarf (Tipo,Numero,FecInicial,FecFinal,CdCia,CdLinea,CdGrupo,CdSubgrupo,CdMarca,CdProducto,Tarifa,SimbTfa,LtaPre,CdTarifa,CdClie,CdAgencia,CdVend,CdCiudad,CdZona,CdSzona,CdGruCli,OtrosCrit,Anulado,TimeSys,IdUsuario)
 VALUES (@pmTipo,@pmNumero,@pmFecInicial,@pmFecFinal,@pmCdCia,@pmCdLinea,@pmCdGrupo,@pmCdSubgrupo,@pmCdMarca,@pmCdProducto,@pmTarifa,@pmSimbTfa,@pmLtaPre,@pmCdTarifa,@pmCdClie,@pmCdAgencia,@pmCdVend,@pmCdCiudad,@pmCdZona,@pmCdSzona,@pmCdGruCli,@pmOtrosCrit,@pmAnulado,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsProfesiones @pmIdProf VARCHAR(4),@pmProfesion VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Profesiones (IdProf,Profesion,Inactivo)
 VALUES (@pmIdProf,@pmProfesion,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuc @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(100),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmSubCentro BIT,@pmVehiculo BIT,@pmCentroCosto BIT,@pmVencimiento BIT
,@pmBaseGravable BIT,@pmPorcntaje DECIMAL(10,4),@pmNaturaleza CHAR(1),@pmAjustable BIT,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferncias BIT
,@pmAgencia BIT,@pmFmtDian INT,@pmConDian INT,@pmRenDec INT,@pmCdTipDoc VARCHAR(3),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmInactivo BIT,@pmAfijos INT
 AS 
 INSERT INTO Puc (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,SubCentro,Vehiculo,CentroCosto,Vencimiento,BaseGravable,Porcntaje,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referncias
,Agencia,FmtDian,ConDian,RenDec,CdTipDoc,FechaAdd,IdUsuario,Inactivo,Afijos)
 VALUES (@pmIdCuenta,@pmNomCuenta,@pmMayor,@pmMovimiento,@pmTercero,@pmSubCentro,@pmVehiculo,@pmCentroCosto,@pmVencimiento,@pmBaseGravable,@pmPorcntaje,@pmNaturaleza,@pmAjustable,@pmCuentaAjuDeb,@pmCuentaAjuCre
,@pmCheques,@pmReferncias,@pmAgencia,@pmFmtDian,@pmConDian,@pmRenDec,@pmCdTipDoc,@pmFechaAdd,@pmIdUsuario,@pmInactivo,@pmAfijos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuc_Sel @pmIdCuenta VARCHAR(16),@pmNewCuenta VARCHAR(16)
AS 
 INSERT INTO Puc (IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,SubCentro,Vehiculo,CentroCosto,Vencimiento,BaseGravable,Porcntaje,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referncias
,Agencia,FmtDian,ConDian,RenDec,CdTipDoc,FechaAdd,IdUsuario,Inactivo,Afijos)
 SELECT @pmNewCuenta,NomCuenta,Mayor,Movimiento,Tercero,SubCentro,Vehiculo,CentroCosto,Vencimiento,BaseGravable,Porcntaje,Naturaleza,Ajustable,CuentaAjuDeb,CuentaAjuCre,Cheques,Referncias
,Agencia,FmtDian,ConDian,RenDec,CdTipDoc,FechaAdd,IdUsuario,Inactivo,Afijos
 FROM Puc WHERE IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuestos @pmIdPto VARCHAR(4),@pmPtoControl VARCHAR(100),@pmTagPuesto VARCHAR(50),@pmIdLocal VARCHAR(8)
,@pmUbicacion VARCHAR(100),@pmContacto VARCHAR(150),@pmTelefono VARCHAR(20),@pmTelMovil VARCHAR(20),@pmReferencia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO Puestos (IdPto,PtoControl,TagPuesto,IdLocal,Ubicacion,Contacto,Telefono,TelMovil,Referencia,Inactivo)
 VALUES (@pmIdPto,@pmPtoControl,@pmTagPuesto,@pmIdLocal,@pmUbicacion,@pmContacto,@pmTelefono,@pmTelMovil,@pmReferencia,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuntBases @pmIdProgrma INT,@pmCdSubgrupo VARCHAR(8),@pmCdProducto VARCHAR(16)
,@pmPuntos DECIMAL(14,4),@pmMonto DECIMAL(14,4)
 AS 
 INSERT INTO PuntBases (IdProgrma,CdSubgrupo,CdProducto,Puntos,Monto)
 VALUES (@pmIdProgrma,@pmCdSubgrupo,@pmCdProducto,@pmPuntos,@pmMonto)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuntFormas @pmIdProgrma INT,@pmIdForma VARCHAR(4),@pmPuntos DECIMAL(14,4)
 AS 
 INSERT INTO PuntFormas (IdProgrma,IdForma,Puntos)
 VALUES (@pmIdProgrma,@pmIdForma,@pmPuntos)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuntPremios @pmIdProgrma INT,@pmItem INT,@pmDescripcion VARCHAR(100)
,@pmIdProducto VARCHAR(16),@pmPuntos DECIMAL(14,4),@pmVrBono MONEY
 AS 
 INSERT INTO PuntPremios (IdProgrma,Item,Descripcion,IdProducto,Puntos,VrBono)
 VALUES (@pmIdProgrma,@pmItem,@pmDescripcion,@pmIdProducto,@pmPuntos,@pmVrBono)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuntProg @pmIdProgrma INT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmMesesInact INT,@pmMesesVcmto INT
,@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO PuntProg (IdProgrma,FecInicial,FecFinal,MesesInact,MesesVcmto,Observacion,Anulado,TimeSys,IdUsuario)
 VALUES (@pmIdProgrma,@pmFecInicial,@pmFecFinal,@pmMesesInact,@pmMesesVcmto,@pmObservacion,@pmAnulado,@pmTimeSys,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuntTarjetas @pmNumTarjeta VARCHAR(20),@pmIdClie VARCHAR(16),@pmFecha SMALLDATETIME
,@pmTotalPuntos DECIMAL(14,4),@pmPuntRedim DECIMAL(14,4),@pmFecUltCompra SMALLDATETIME
,@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO PuntTarjetas (NumTarjeta,IdClie,Fecha,TotalPuntos,PuntRedim,FecUltCompra,Inactivo,FechaUpdate,IdUsuario)
 VALUES (@pmNumTarjeta,@pmIdClie,@pmFecha,@pmTotalPuntos,@pmPuntRedim,@pmFecUltCompra,@pmInactivo,@pmFechaUpdate,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsPuntos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmVrBase MONEY,@pmPuntFactura DECIMAL(14,4),@pmPuntExtra DECIMAL(14,4),@pmPuntRedim DECIMAL(14,4)
,@pmIdProgrma INT,@pmNumTarjeta VARCHAR(20),@pmDetalles VARCHAR(50),@pmReferncia VARCHAR(50)
 AS 
 INSERT INTO Trn_Puntos (TipDoc,Documento,IdCia,Item,Fecha,IdCliente,IdAgencia,VrBase,PuntFactura,PuntExtra,PuntRedim,IdProgrma,NumTarjeta,Detalles,Referncia)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmFecha,@pmIdCliente,@pmIdAgencia,@pmVrBase,@pmPuntFactura,@pmPuntExtra,@pmPuntRedim
,@pmIdProgrma,@pmNumTarjeta,@pmDetalles,@pmReferncia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRadicaVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16)
,@pmTotalPagos MONEY,@pmTotalCobros MONEY,@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmVrDescuento MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmOrigenAdd VARCHAR(10),@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_RadicaVales (TipDoc,nRadica,IdCia,Fecha,FechaVence,IdConcepto,IdTercero,TotalPagos,TotalCobros,Referencia,CxPagar,VrDescuento,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmnRadica,@pmIdCia,@pmFecha,@pmFechaVence,@pmIdConcepto,@pmIdTercero,@pmTotalPagos,@pmTotalCobros,@pmReferencia,@pmCxPagar,@pmVrDescuento,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom
,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsRangosPeso @pmIdRango VARCHAR(4),@pmDescripRango VARCHAR(50)
,@pmValorInicial DECIMAL(14,4),@pmValorFinal DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO RangosPeso (IdRango,DescripRango,ValorInicial,ValorFinal,Inactivo)
 VALUES (@pmIdRango,@pmDescripRango,@pmValorInicial,@pmValorFinal,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRecAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmVrAFavor MONEY
,@pmVrAbono MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmVrOtrosDb MONEY
 AS 
 INSERT INTO Trn_RecAfavor (TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrAbono,IdCliente,IdAgencia,IdVend,VrOtrosDb)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmVrAFavor,@pmVrAbono,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmVrOtrosDb)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsRecBuses @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCar VARCHAR(3),@pmCartulina INT,@pmIdCiaCar CHAR(2),@pmIdVehiculo VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmCentInicial INT,@pmCentFinal INT,@pmNPasajeros INT,@pmRecorridos INT,@pmValorRecibido MONEY,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorOtrosDct MONEY
,@pmValorPagMas MONEY,@pmValorNeto MONEY,@pmValorAplicado MONEY,@pmValorAnticipo MONEY,@pmValorAbono MONEY,@pmTotalProducido MONEY,@pmTotalGastos MONEY,@pmTotalAhorro MONEY,@pmTotalOtros MONEY,@pmTotalVales MONEY,@pmTotalAbono MONEY,@pmValorPasaje MONEY,@pmIdVendedor VARCHAR(16)
,@pmComision DECIMAL(14,4),@pmIdTarCom VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmIdBanco VARCHAR(4),@pmReferencia VARCHAR(20),@pmBaseIva MONEY,@pmBaseRet MONEY,@pmBaseOtr MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4)
,@pmTarifaOtr DECIMAL(14,4),@pmIdTarIva VARCHAR(4),@pmIdTarRet VARCHAR(4),@pmIdTarOtr VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdRuta VARCHAR(4),@pmIdRelevador VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_RecBuses (TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,Modalidad,TipCar,Cartulina,IdCiaCar,IdVehiculo,IdConductor,VehPropio,CentInicial,CentFinal,NPasajeros,Recorridos,ValorRecibido,ValorTotal,ValorDcto,ValorReteFte,ValorReteIva,ValorReteIca,ValorOtros,ValorOtrosDct,ValorPagMas,ValorNeto,ValorAplicado,ValorAnticipo,ValorAbono
,TotalProducido,TotalGastos,TotalAhorro,TotalOtros,TotalVales,TotalAbono,ValorPasaje,IdVendedor,Comision,IdTarCom,IdCCosto,IdCta,EnEfectivo,NumCheque,IdBanco,Referencia,BaseIva,BaseRet,BaseOtr,TarifaIva,TarifaRet,TarifaOtr,IdTarIva,IdTarRet,IdTarOtr,IdClase,IdRuta,IdRelevador,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmModalidad,@pmTipCar,@pmCartulina,@pmIdCiaCar,@pmIdVehiculo,@pmIdConductor,@pmVehPropio,@pmCentInicial,@pmCentFinal,@pmNPasajeros,@pmRecorridos,@pmValorRecibido,@pmValorTotal,@pmValorDcto,@pmValorReteFte,@pmValorReteIva,@pmValorReteIca
,@pmValorOtros,@pmValorOtrosDct,@pmValorPagMas,@pmValorNeto,@pmValorAplicado,@pmValorAnticipo,@pmValorAbono,@pmTotalProducido,@pmTotalGastos,@pmTotalAhorro,@pmTotalOtros,@pmTotalVales,@pmTotalAbono,@pmValorPasaje,@pmIdVendedor,@pmComision,@pmIdTarCom,@pmIdCCosto,@pmIdCta,@pmEnEfectivo,@pmNumCheque,@pmIdBanco,@pmReferencia,@pmBaseIva
,@pmBaseRet,@pmBaseOtr,@pmTarifaIva,@pmTarifaRet,@pmTarifaOtr,@pmIdTarIva,@pmIdTarRet,@pmIdTarOtr,@pmIdClase,@pmIdRuta,@pmIdRelevador,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRecCheques @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrTotal MONEY,@pmCantidad DECIMAL(14,4)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmPlacaVehic VARCHAR(10),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_RecCheques (TipDoc,Recibo,IdCia,Fecha,IdCliente,IdAgencia,VrTotal,Cantidad,PlacaVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdCliente,@pmIdAgencia,@pmVrTotal,@pmCantidad,@pmPlacaVehic,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev
,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsRecFactura @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmItemDoc INT,@pmFecha SMALLDATETIME,@pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4),@pmValorAbono MONEY,@pmDevolucion MONEY,@pmDescuento MONEY
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmVehPropio BIT,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmDetalle VARCHAR(100),@pmFecPago SMALLDATETIME,@pmAplAntcpo BIT,@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_RecFactura (TipRec,Recibo,IdCia,Item,TipDoc,Documento,IdCiaDoc,ItemDoc,Fecha,IdCargo,IdClase,ValorAbono,Devolucion,Descuento,IdCliente,IdAgencia,IdVehiculo,VehPropio,IdVendedor,Comision,Detalle,FecPago,AplAntcpo,TimeSys,IdUsuario)
 VALUES (@pmTipRec,@pmRecibo,@pmIdCia,@pmItem,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmItemDoc,@pmFecha,@pmIdCargo,@pmIdClase,@pmValorAbono,@pmDevolucion,@pmDescuento,@pmIdCliente,@pmIdAgencia,@pmIdVehiculo,@pmVehPropio,@pmIdVendedor,@pmComision
,@pmDetalle,@pmFecPago,@pmAplAntcpo,@pmTimeSys,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInsRecTanqueos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmDocumento VARCHAR(10)
,@pmIdTipoCom VARCHAR(4),@pmCantidad DECIMAL(14,4),@pmValorUnitario MONEY,@pmIdEstacion VARCHAR(4)
 AS 
 INSERT INTO Trn_RecTanqueos (TipDoc,Recibo,IdCia,Item,Fecha,Documento,IdTipoCom,Cantidad,ValorUnitario,IdEstacion)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmItem,@pmFecha,@pmDocumento,@pmIdTipoCom,@pmCantidad,@pmValorUnitario,@pmIdEstacion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY
,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrAhorro MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4)
,@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4)
,@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Recibos (TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdCajero,IdVend,TarifaCom,CodTarCom,IdLocal,EnEfectivo
,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRecibo,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmFecPago,@pmVrRecibido,@pmVrSubTotal,@pmVrDescuento,@pmVrRetencion,@pmVrReteICA,@pmVrReteIVA,@pmVrOtros,@pmVrOtrDcto,@pmVrAhorro,@pmVrPagosMas,@pmVrNeto,@pmVrAplicado,@pmVrAnticipo,@pmVrEfectivo,@pmBaseRet,@pmBaseIca,@pmBaseRiv,@pmTarifaRet
,@pmTarifaIca,@pmTarifaRiv,@pmCodTarRet,@pmCodTarIca,@pmCodTarRiv,@pmIdCCosto,@pmIdSubCos,@pmIdCajero,@pmIdVend,@pmTarifaCom,@pmCodTarCom,@pmIdLocal,@pmEnEfectivo,@pmCdForma,@pmNumForma,@pmDetallePago,@pmCdBanco,@pmCdCta,@pmReferencia,@pmpVehiculo,@pmCdConductor,@pmTipDcm,@pmDocumento,@pmIdCiaDcm
,@pmModalidad,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRegimenDian @pmIdRegimen VARCHAR(4),@pmRegimen VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO RegimenDian (IdRegimen,Regimen,Inactivo)
 VALUES (@pmIdRegimen,@pmRegimen,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRegistro @pmIdCia CHAR(2),@pmIdEmpresa VARCHAR(16),@pmNitEmpresa VARCHAR(16),@pmDvNit CHAR(1),@pmRazonSocial VARCHAR(250)
,@pmDescripcion VARCHAR(250),@pmDireccion VARCHAR(250),@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pmIdLocal VARCHAR(8),@pmIdPais VARCHAR(8)
,@pme_mail VARCHAR(100),@pmArchivoLogo VARCHAR(30),@pmNumeroSerial VARCHAR(80),@pmRegimen VARCHAR(50),@pmLicencia VARCHAR(20),@pmVigenciaLic SMALLDATETIME
,@pmPersoneria VARCHAR(40),@pmLeyenda VARCHAR(250),@pmFechaRegistro SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmCurrtyDate SMALLDATETIME
,@pmTipoId CHAR(1),@pmNitRepLegal VARCHAR(16),@pmRepLegal VARCHAR(150),@pmTipoPersna CHAR(1),@pmCIIUDane VARCHAR(8),@pmCurDate1 SMALLDATETIME,@pmFechaPrest SMALLDATETIME
 AS 
 INSERT INTO adm_Registro (IdCia,IdEmpresa,NitEmpresa,DvNit,RazonSocial,Descripcion,Direccion,Telefono,Fax,IdLocal,IdPais,e_mail,ArchivoLogo,NumeroSerial,Regimen,Licencia,VigenciaLic
,Personeria,Leyenda,FechaRegistro,FechaVence,CurrtyDate,TipoId,NitRepLegal,RepLegal,TipoPersna,CIIUDane,CurDate1,FechaPrest)
 VALUES (@pmIdCia,@pmIdEmpresa,@pmNitEmpresa,@pmDvNit,@pmRazonSocial,@pmDescripcion,@pmDireccion,@pmTelefono,@pmFax,@pmIdLocal,@pmIdPais,@pme_mail,@pmArchivoLogo
,@pmNumeroSerial,@pmRegimen,@pmLicencia,@pmVigenciaLic,@pmPersoneria,@pmLeyenda,@pmFechaRegistro,@pmFechaVence,@pmCurrtyDate,@pmTipoId,@pmNitRepLegal,@pmRepLegal,@pmTipoPersna,@pmCIIUDane,@pmCurDate1,@pmFechaPrest)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRelaciones @pmIdTabla VARCHAR(20),@pmTablaForanea VARCHAR(20),@pmCampoForaneo VARCHAR(20)
,@pmBasedeDatos VARCHAR(10),@pmNoValidar BIT
 AS 
 INSERT INTO Sys_Relaciones (IdTabla,TablaForanea,CampoForaneo,BasedeDatos,NoValidar)
 VALUES (@pmIdTabla,@pmTablaForanea,@pmCampoForaneo,@pmBasedeDatos,@pmNoValidar)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRemision @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaDesp SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmUnidades DECIMAL(14,4),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250),@pmIdLocEnv VARCHAR(8),@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50)
,@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT, @pmIdPlazo VARCHAR(4), @pmCdRuta VARCHAR(4)
,@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmDetalleEnvio VARCHAR(250),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmTipPed VARCHAR(3)
,@pmPedido INT,@pmIdCiaPed CHAR(2),@pmFechaPed SMALLDATETIME,@pmNumAutoriza INT,@pmModalidad VARCHAR(10),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Remision (TipDoc,Remision,IdCia,Fecha,FechaDesp,FechaVence,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,Unidades,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac
,IdForma,DetallePago,MulPlazos, IdPlazo, CdRuta,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,DetalleEnvio,TipFac,Factura,IdCiaFac,FechaFact,TipPed,Pedido,IdCiaPed,FechaPed,NumAutoriza,Modalidad,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,NumAprob,FecAprob,CdUsuAprob,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRemision,@pmIdCia,@pmFecha,@pmFechaDesp,@pmFechaVence,@pmIdConcepto,@pmIdCliente,@pmIdAgencia,@pmVrSubTotal,@pmVrDescuento,@pmVrImpuesto,@pmVrFletes,@pmVrOtros,@pmVrOtrDcto,@pmVrNeto,@pmCantidad,@pmUnidades,@pmIdVend,@pmTarifaCom,@pmCodTarCom
,@pmDirEnvio,@pmIdLocEnv,@pmDiasEntraga,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargoContac,@pmIdForma,@pmDetallePago,@pmMulPlazos,@pmIdPlazo,@pmCdRuta,@pmNitEmpTrans,@pmEmpTrans,@pmpVehiculo,@pmCdConductor,@pmDetalleEnvio,@pmTipFac,@pmFactura,@pmIdCiaFac
,@pmFechaFact,@pmTipPed,@pmPedido,@pmIdCiaPed,@pmFechaPed,@pmNumAutoriza,@pmModalidad,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmNumAprob,@pmFecAprob,@pmCdUsuAprob,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsReport @pmTipoRep VARCHAR(3),@pmIdRep CHAR(2),@pmDescripcion VARCHAR(100),@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT
,@pmVerSetup BIT,@pmNumCopias INT,@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50),@pmNomRep VARCHAR(50)
 AS 
 INSERT INTO Sys_Report (TipoRep,IdRep,Descripcion,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location,NomRep)
 VALUES (@pmTipoRep,@pmIdRep,@pmDescripcion,@pmFormato,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmsp_Nombre,@pmInd_Location,@pmNomRep)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsReportFmto @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmTipoOrigen VARCHAR(10),@pmCrVersion INT,@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50)
 AS 
 INSERT INTO Sys_ReportFmto (TipoRep,Formato,TipoOrigen,CrVersion,sp_Nombre,Ind_Location)
 VALUES (@pmTipoRep,@pmFormato,@pmTipoOrigen,@pmCrVersion,@pmsp_Nombre,@pmInd_Location)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsReportSub @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmNombreSub VARCHAR(20),@pmTipoOrigen VARCHAR(10),@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50)
 AS 
 INSERT INTO Sys_ReportSub (TipoRep,Formato,NombreSub,TipoOrigen,sp_Nombre,Ind_Location)
 VALUES (@pmTipoRep,@pmFormato,@pmNombreSub,@pmTipoOrigen,@pmsp_Nombre,@pmInd_Location)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsReportSubPmt @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmNombreSub VARCHAR(20),@pmIndParam INT,@pmNomParam VARCHAR(30),@pmTipoDato VARCHAR(15),@pmValorPred VARCHAR(100)
 AS 
 INSERT INTO Sys_ReportSubPmt (TipoRep,Formato,NombreSub,IndParam,NomParam,TipoDato,ValorPred)
 VALUES (@pmTipoRep,@pmFormato,@pmNombreSub,@pmIndParam,@pmNomParam,@pmTipoDato,@pmValorPred)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsReqDetalle @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2),@pmItem INT,@pmCdProducto VARCHAR(16),@pmDescripcion VARCHAR(250),@pmIdSubgrupo VARCHAR(8)
,@pmCdBodega VARCHAR(4),@pmCantidad DECIMAL(14,4),@pmIdUnd VARCHAR(4),@pmVrUnitario MONEY,@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmTipSal VARCHAR(3),@pmNSalida INT,@pmIdCiaSal CHAR(2),@pmFechaSal SMALLDATETIME
 AS 
 INSERT INTO Trn_ReqDetalle (TipDoc,Requisicion,IdCia,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,Cantidad,IdUnd,VrUnitario,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal)
 VALUES (@pmTipDoc,@pmRequisicion,@pmIdCia,@pmItem,@pmCdProducto,@pmDescripcion,@pmIdSubgrupo,@pmCdBodega,@pmCantidad,@pmIdUnd,@pmVrUnitario,@pmTipOrd
,@pmNumOrden,@pmIdCiaOrd,@pmTipSal,@pmNSalida,@pmIdCiaSal,@pmFechaSal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRequisicion @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdRespons VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdDep VARCHAR(4),@pmVrSubTotal MONEY,@pmCantidad DECIMAL(14,4)
,@pmNContrato INT,@pmIdCiaCont CHAR(2),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmModalidad VARCHAR(10),@pmDirEntrega VARCHAR(250),@pmIdLocEnt VARCHAR(8),@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2),@pmFechaSal SMALLDATETIME,@pmNumAprob INT,@pmFecAprob SMALLDATETIME
,@pmCdUsuAprob VARCHAR(11),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(50),@pmEmailContacto VARCHAR(100),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Requisicion (TipDoc,Requisicion,IdCia,Fecha,FechaVence,IdConcepto,IdRespons,IdCCosto,IdSubCos,IdDep,VrSubTotal,Cantidad,NContrato,IdCiaCont,NitCliente,CdAgencia,Modalidad,DirEntrega,IdLocEnt,TipSal,NumSalida,IdCiaSal,FechaSal,NumAprob,FecAprob,CdUsuAprob,NomContacto,TelsContacto,EmailContacto,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmRequisicion,@pmIdCia,@pmFecha,@pmFechaVence,@pmIdConcepto,@pmIdRespons,@pmIdCCosto,@pmIdSubCos,@pmIdDep,@pmVrSubTotal,@pmCantidad,@pmNContrato,@pmIdCiaCont,@pmNitCliente,@pmCdAgencia,@pmModalidad,@pmDirEntrega,@pmIdLocEnt,@pmTipSal,@pmNumSalida,@pmIdCiaSal
,@pmFechaSal,@pmNumAprob,@pmFecAprob,@pmCdUsuAprob,@pmNomContacto,@pmTelsContacto,@pmEmailContacto,@pmOrigenAdd,@pmAnulado,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRoles @pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3),@pmFuncion VARCHAR(100)
 AS 
 INSERT INTO Sys_Roles (IdObj,IdRole,Funcion)
 VALUES (@pmIdObj,@pmIdRole,@pmFuncion)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRutas @pmIdRuta VARCHAR(4),@pmRuta VARCHAR(100),@pmIdLocOri VARCHAR(8),@pmIdLocDes VARCHAR(8)
,@pmEsLocal BIT,@pmDistkmt DECIMAL(14,4),@pmTmHoras VARCHAR(8),@pmCodigoMt VARCHAR(5),@pmFleteTon MONEY,@pmInactivo BIT
 AS 
 INSERT INTO Rutas (IdRuta,Ruta,IdLocOri,IdLocDes,EsLocal,Distkmt,TmHoras,CodigoMt,FleteTon,Inactivo)
 VALUES (@pmIdRuta,@pmRuta,@pmIdLocOri,@pmIdLocDes,@pmEsLocal,@pmDistkmt,@pmTmHoras,@pmCodigoMt,@pmFleteTon,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsRutasPlan @pmIdRuta VARCHAR(4),@pmIdPunto INT,@pmDescripcion VARCHAR(100),@pmDistancia DECIMAL(14,4)
,@pmTiempo VARCHAR(8),@pmClasePto VARCHAR(10),@pmCdPto VARCHAR(4),@pmIdLocal VARCHAR(8),@pmPosLat DECIMAL(14,4),@pmPosLon DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO RutasPlan (IdRuta,IdPunto,Descripcion,Distancia,Tiempo,ClasePto,CdPto,IdLocal,PosLat,PosLon,Inactivo)
 VALUES (@pmIdRuta,@pmIdPunto,@pmDescripcion,@pmDistancia,@pmTiempo,@pmClasePto,@pmCdPto,@pmIdLocal,@pmPosLat,@pmPosLon,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsRutasTramos @pmIdRuta VARCHAR(4),@pmNumTramo INT,@pmIdLocal VARCHAR(8),@pmkmstramo DECIMAL(14,4),@pmkmstotal DECIMAL(14,4)
 AS 
 INSERT INTO RutasTramos (IdRuta,NumTramo,IdLocal,kmstramo,kmstotal)
 VALUES (@pmIdRuta,@pmNumTramo,@pmIdLocal,@pmkmstramo,@pmkmstotal)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSAutoriza @pmIdConcepto VARCHAR(4),@pmConceptoAut VARCHAR(150),@pmInactivo BIT
 AS 
 INSERT INTO Sys_Autoriza (IdConcepto,ConceptoAut,Inactivo)
 VALUES (@pmIdConcepto,@pmConceptoAut,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSaiArticulos @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipEnt VARCHAR(3)
,@pmEntrada INT,@pmIdCiaEnt CHAR(2),@pmItemEnt INT,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY
 AS 
 INSERT INTO Trn_SaiArticulos (TipDoc,Salida,IdCia,Item,TipEnt,Entrada,IdCiaEnt,ItemEnt,IdProducto,IdBodega,Cantidad,VrUnitario)
 VALUES (@pmTipDoc,@pmSalida,@pmIdCia,@pmItem,@pmTipEnt,@pmEntrada,@pmIdCiaEnt,@pmItemEnt,@pmIdProducto,@pmIdBodega,@pmCantidad,@pmVrUnitario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSalidas @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16)
,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmBaseImp MONEY,@pmTarifaIva DECIMAL(14,4),@pmCodTarIva VARCHAR(4)
,@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmModalidad VARCHAR(10)
,@pmCdConductor VARCHAR(16),@pmCdDep VARCHAR(4),@pmNContrato INT,@pmIdCiaCont CHAR(2),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmModRequis VARCHAR(10)
,@pmDirEntrega VARCHAR(250),@pmIdLocEnt VARCHAR(8),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmGuiaComb INT,@pmCdCiaGuia CHAR(2),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Salidas (TipDoc,Salida,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,TarifaIva,CodTarIva,TipOrd,NumOrden,IdCiaOrd,IdCCosto,IdSubCos
,pVehiculo,CdConductor,CdDep,NContrato,IdCiaCont ,NitCliente,CdAgencia,ModRequis,DirEntrega,IdLocEnt,GuiaComb,CdCiaGuia
,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmSalida,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdTercero,@pmVrSubTotal,@pmVrImpuesto,@pmVrCostos,@pmVrNeto,@pmCantidad,@pmBaseImp,@pmTarifaIva
,@pmCodTarIva,@pmTipOrd,@pmNumOrden,@pmIdCiaOrd,@pmIdCCosto,@pmIdSubCos,@pmpVehiculo
,@pmCdConductor,@pmCdDep,@pmNContrato,@pmIdCiaCont,@pmNitCliente,@pmCdAgencia,@pmModRequis,@pmDirEntrega,@pmIdLocEnt,@pmGuiaComb,@pmCdCiaGuia
,@pmModalidad,@pmOrigenAdd,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmAnulado
,@pmNumDev,@pmFecDev,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSectoresEco @pmIdSector VARCHAR(8),@pmSectorEco VARCHAR(250),@pmInactivo BIT
 AS 
 INSERT INTO SectoresEco (IdSector,SectorEco,Inactivo)
 VALUES (@pmIdSector,@pmSectorEco,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSecurity @pmIdLla INT,@pmDescripcion VARCHAR(50),@pmPwd VARCHAR(10)
 AS 
 INSERT INTO Sys_Security (IdLla,Descripcion,Pwd)
 VALUES (@pmIdLla,@pmDescripcion,@pmPwd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSerMov @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30)
,@pmIdBodega VARCHAR(4),@pmCdUbic VARCHAR(10),@pmLinea INT,@pmMovimiento VARCHAR(3),@pmFecha SMALLDATETIME,@pmObservacion VARCHAR(150),@pmReferencia VARCHAR(50)
 AS 
 INSERT INTO Trn_SerMov (TipDoc,Documento,IdCia,Item,IdProducto,NumSerial,IdBodega,CdUbic,Linea,Movimiento,Fecha,Observacion,Referencia)
 VALUES (@pmTipDoc,@pmDocumento,@pmIdCia,@pmItem,@pmIdProducto,@pmNumSerial,@pmIdBodega,@pmCdUbic,@pmLinea,@pmMovimiento,@pmFecha,@pmObservacion,@pmReferencia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSeriales @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4),@pmCdUbic VARCHAR(10),@pmFecEntrada SMALLDATETIME
,@pmFecSalida SMALLDATETIME,@pmNitProv VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmObservacion VARCHAR(250),@pmReferncia1 VARCHAR(50),@pmReferncia2 VARCHAR(50)
,@pmFecRef1 SMALLDATETIME,@pmFecRef2 SMALLDATETIME,@pmUbicacion VARCHAR(50),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Seriales (IdProducto,NumSerial,IdCia,IdBodega,CdUbic,FecEntrada,FecSalida,NitProv,IdEstado,Inactivo,Observacion,Referncia1,Referncia2,FecRef1,FecRef2,Ubicacion,IdUsuario)
 VALUES (@pmIdProducto,@pmNumSerial,@pmIdCia,@pmIdBodega,@pmCdUbic,@pmFecEntrada,@pmFecSalida,@pmNitProv,@pmIdEstado,@pmInactivo,@pmObservacion,@pmReferncia1,@pmReferncia2
,@pmFecRef1,@pmFecRef2,@pmUbicacion,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSobDetalle @pmIdDec VARCHAR(4),@pmNumDeclara INT,@pmItem INT,@pmConcepto VARCHAR(250)
,@pmCantidad DECIMAL(14,4),@pmVrUnitario MONEY,@pmVrBase MONEY,@pmTarifa DECIMAL(14,4),@pmVrLiquida MONEY
,@pmVrPrecio MONEY,@pmClaseProd VARCHAR(20),@pmTarifGalon DECIMAL(14,4),@pmPorcBase DECIMAL(14,4)
 AS 
 INSERT INTO Trn_SobDetalle (IdDec,NumDeclara,Item,Concepto,Cantidad,VrUnitario,VrBase,Tarifa,VrLiquida,VrPrecio,ClaseProd,TarifGalon,PorcBase)
 VALUES (@pmIdDec,@pmNumDeclara,@pmItem,@pmConcepto,@pmCantidad,@pmVrUnitario,@pmVrBase,@pmTarifa
,@pmVrLiquida,@pmVrPrecio,@pmClaseProd,@pmTarifGalon,@pmPorcBase)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSobEntidades @pmIdEnterr VARCHAR(8),@pmTipoEnterr VARCHAR(10),@pmNitEntidad VARCHAR(16),@pmTipoId CHAR(1),@pmDv CHAR(1)
,@pmDirOficina VARCHAR(250),@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pmSitioWeb VARCHAR(100),@pme_mail VARCHAR(100)
,@pmCedRepLegal VARCHAR(16),@pmRepLegal VARCHAR(150),@pmFormaPago VARCHAR(10),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30)
,@pmIdBanco VARCHAR(4),@pmCdLocCue VARCHAR(8),@pmCargoRep VARCHAR(50),@pmNomTitular VARCHAR(150),@pmNomEntidad VARCHAR(50),@pmInactivo BIT
,@pmCodCtaPag VARCHAR(4)
 AS 
 INSERT INTO SobEntidades (IdEnterr,TipoEnterr,NitEntidad,TipoId,Dv,DirOficina,Telefono,Fax,SitioWeb,e_mail,CedRepLegal,RepLegal,CargoRep,FormaPago,IdClase,NumCuenta,NomTitular,IdBanco,CdLocCue,NomEntidad,Inactivo,CodCtaPag)
 VALUES (@pmIdEnterr,@pmTipoEnterr,@pmNitEntidad,@pmTipoId,@pmDv,@pmDirOficina,@pmTelefono,@pmFax,@pmSitioWeb,@pme_mail,@pmCedRepLegal,@pmRepLegal,@pmCargoRep,@pmFormaPago
,@pmIdClase,@pmNumCuenta,@pmNomTitular,@pmIdBanco,@pmCdLocCue,@pmNomEntidad,@pmInactivo,@pmCodCtaPag)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSobProductos @pmIdClase INT,@pmIdProducto VARCHAR(16),@pmClaseProd VARCHAR(20)
 AS 
 INSERT INTO SobProductos (IdClase,IdProducto,ClaseProd)
 VALUES (@pmIdClase,@pmIdProducto,@pmClaseProd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSobTiposDec @pmIdDec VARCHAR(4),@pmDeclaracion VARCHAR(100),@pmTipoDec VARCHAR(20),@pmNitDec VARCHAR(16),@pmTipoIdDec CHAR(1)
,@pmNombreDec VARCHAR(250),@pmDvDec CHAR(1),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmCedRepLegal VARCHAR(16)
,@pmRepLegal VARCHAR(150),@pmTipoFirma INT,@pmNitRevisor VARCHAR(16),@pmNomRevisor VARCHAR(150),@pmTpRevisor VARCHAR(20),@pmTarifSubsidio DECIMAL(14,4),@pmConsecutivo INT
 AS 
 INSERT INTO SobTiposDec (IdDec,Declaracion,TipoDec,NitDec,TipoIdDec,NombreDec,DvDec,Direccion,IdLocal,Telefono,CedRepLegal,RepLegal,TipoFirma,NitRevisor,NomRevisor,TpRevisor,TarifSubsidio,Consecutivo)
 VALUES (@pmIdDec,@pmDeclaracion,@pmTipoDec,@pmNitDec,@pmTipoIdDec,@pmNombreDec,@pmDvDec,@pmDireccion,@pmIdLocal,@pmTelefono,@pmCedRepLegal,@pmRepLegal
,@pmTipoFirma,@pmNitRevisor,@pmNomRevisor,@pmTpRevisor,@pmTarifSubsidio,@pmConsecutivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSobretasas @pmIdDec VARCHAR(4),@pmNumDeclara INT,@pmFecha SMALLDATETIME,@pmnAnno INT,@pmnMes INT,@pmIdEnterr VARCHAR(8),@pmTipoEntidad VARCHAR(10),@pmVrSobretasa MONEY,@pmVrSanciones MONEY,@pmVrCompensa MONEY,@pmVrIntMora MONEY
,@pmVrTotalPagar MONEY,@pmVrFondoSubs MONEY,@pmVrRecaudoDep MONEY,@pmNitDeclara VARCHAR(16),@pmDvDeclara CHAR(1),@pmCedDeclara VARCHAR(16),@pmDeclarante VARCHAR(250),@pmDirEntidad VARCHAR(250),@pmEmailEnt VARCHAR(100),@pmNomRepres VARCHAR(150)
,@pmCargoRepres VARCHAR(50),@pmTipoFirma INT,@pmNitRevisor VARCHAR(16),@pmNomRevisor VARCHAR(150),@pmTpRevisor VARCHAR(20),@pmNumCheque VARCHAR(30),@pmCodBanco VARCHAR(4),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4)
,@pmCdLocCue VARCHAR(8),@pmTitularCuenta VARCHAR(150),@pmVrEfectivo MONEY,@pmVrOtros MONEY,@pmVrTransf MONEY,@pmVrPagado MONEY,@pmEstado INT,@pmAdhesivo VARCHAR(30),@pmFechaPago SMALLDATETIME,@pmEsCorr BIT,@pmNumCorr VARCHAR(30),@pmFecCorr SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmCodCta VARCHAR(4),@pmTipEgreso VARCHAR(3),@pmNumEgreso INT,@pmCiaEgreso CHAR(2)
 AS 
 INSERT INTO Trn_Sobretasas (IdDec,NumDeclara,Fecha,nAnno,nMes,IdEnterr,TipoEntidad,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar,VrFondoSubs,VrRecaudoDep,NitDeclara,DvDeclara,CedDeclara,Declarante,DirEntidad,EmailEnt,NomRepres,CargoRepres,TipoFirma,NitRevisor
,NomRevisor,TpRevisor,NumCheque,CodBanco,IdClase,NumCuenta,IdBanco,CdLocCue,TitularCuenta,VrEfectivo,VrOtros,VrTransf,VrPagado,Estado,Adhesivo,FechaPago,EsCorr,NumCorr,FecCorr,Observacion,TimeSys,IdUsuario,CodCta,TipEgreso,NumEgreso,CiaEgreso)
 VALUES (@pmIdDec,@pmNumDeclara,@pmFecha,@pmnAnno,@pmnMes,@pmIdEnterr,@pmTipoEntidad,@pmVrSobretasa,@pmVrSanciones,@pmVrCompensa,@pmVrIntMora,@pmVrTotalPagar,@pmVrFondoSubs,@pmVrRecaudoDep,@pmNitDeclara,@pmDvDeclara,@pmCedDeclara,@pmDeclarante
,@pmDirEntidad,@pmEmailEnt,@pmNomRepres,@pmCargoRepres,@pmTipoFirma,@pmNitRevisor,@pmNomRevisor,@pmTpRevisor,@pmNumCheque,@pmCodBanco,@pmIdClase,@pmNumCuenta,@pmIdBanco,@pmCdLocCue,@pmTitularCuenta,@pmVrEfectivo,@pmVrOtros,@pmVrTransf,@pmVrPagado
,@pmEstado,@pmAdhesivo,@pmFechaPago,@pmEsCorr,@pmNumCorr,@pmFecCorr,@pmObservacion,@pmTimeSys,@pmIdUsuario,@pmCodCta,@pmTipEgreso,@pmNumEgreso,@pmCiaEgreso)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSubCentros @pmIdSubCos VARCHAR(16),@pmSubCosto VARCHAR(100),@pmIdCCosto VARCHAR(16),@pmFecAdd SMALLDATETIME,@pmInactivo BIT
 AS 
 INSERT INTO SubCentros (IdSubCos,SubCosto,IdCCosto,FecAdd,Inactivo)
 VALUES (@pmIdSubCos,@pmSubCosto,@pmIdCCosto,@pmFecAdd,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSubGrupos @pmIdSubgrupo VARCHAR(8),@pmSubgrupo VARCHAR(150),@pmIdGrupo VARCHAR(8)
,@pmCdTarIva VARCHAR(4),@pmCdTarAut VARCHAR(4),@pmCdTarRet VARCHAR(4),@pmCdTarIca VARCHAR(4)
,@pmTipoInvent VARCHAR(10),@pmInactivo BIT
 AS 
 INSERT INTO SubGrupos (IdSubgrupo,Subgrupo,IdGrupo,CdTarIva,CdTarAut,CdTarRet,CdTarIca,TipoInvent,Inactivo)
 VALUES (@pmIdSubgrupo,@pmSubgrupo,@pmIdGrupo,@pmCdTarIva,@pmCdTarAut,@pmCdTarRet,@pmCdTarIca,@pmTipoInvent,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsSubGruposAgenc @pmIdSubAgenc VARCHAR(4),@pmSubGrupoAgenc VARCHAR(50),@pmIdGrupo VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO SubGruposAgenc (IdSubAgenc,SubGrupoAgenc,IdGrupo,Inactivo)
 VALUES (@pmIdSubAgenc,@pmSubGrupoAgenc,@pmIdGrupo,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSubgruposCue @pmIdSubgrupo VARCHAR(8),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmCdCueDev VARCHAR(16)
,@pmCdCueCom VARCHAR(16),@pmCdCueCdv VARCHAR(16),@pmFecAdd SMALLDATETIME
 AS 
 INSERT INTO SubgruposCue (IdSubgrupo,IdClase,IdCuenta,CdCueDev,CdCueCom,CdCueCdv,FecAdd)
 VALUES (@pmIdSubgrupo,@pmIdClase,@pmIdCuenta,@pmCdCueDev,@pmCdCueCom,@pmCdCueCdv,@pmFecAdd)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsSubzonas @pmIdSzona VARCHAR(4),@pmSubzona VARCHAR(50),@pmIdZona VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO Subzonas (IdSzona,Subzona,IdZona,Inactivo)
 VALUES (@pmIdSzona,@pmSubzona,@pmIdZona,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTFmtos @pmnFormato VARCHAR(10),@pmFmtoFactura VARCHAR(50),@pmTipFac VARCHAR(3)
 AS 
 INSERT INTO Sys_TFmtos (nFormato,FmtoFactura,TipFac)
 VALUES (@pmnFormato,@pmFmtoFactura,@pmTipFac)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTablapor @pmIdTarifa VARCHAR(4),@pmDescripcion VARCHAR(50),@pmIdClase VARCHAR(4),@pmTarifa DECIMAL(16,6)
,@pmSimbolo CHAR(1),@pmCdLocdad VARCHAR(8),@pmMinimo MONEY,@pmMaximo MONEY,@pmInactivo BIT
 AS 
 INSERT INTO Tablapor (IdTarifa,Descripcion,IdClase,Tarifa,Simbolo,CdLocdad,Minimo,Maximo,Inactivo)
 VALUES (@pmIdTarifa,@pmDescripcion,@pmIdClase,@pmTarifa,@pmSimbolo,@pmCdLocdad,@pmMinimo,@pmMaximo,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTanqAforo @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4),@pmCantidad DECIMAL(14,4)
 AS 
 INSERT INTO TanqAforo (IdTanque,UndAforo,Cantidad)
 VALUES (@pmIdTanque,@pmUndAforo,@pmCantidad)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTanqAforoFrac @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4),@pmFraccion DECIMAL(14,4),@pmMilimetros DECIMAL(14,4)
 AS 
 INSERT INTO TanqAforoFrac (IdTanque,UndAforo,Fraccion,Milimetros)
 VALUES (@pmIdTanque,@pmUndAforo,@pmFraccion,@pmMilimetros)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTanqDispens @pmIdMngra VARCHAR(10),@pmNumDisp VARCHAR(4),@pmNumPos VARCHAR(4),@pmNumMngra VARCHAR(4),@pmIdTanque VARCHAR(4),@pmDescripcion VARCHAR(50)
,@pmLecGalAnt DECIMAL(14,4),@pmLecGalUlt DECIMAL(14,4),@pmLecPesAnt MONEY,@pmLecPesUlt MONEY,@pmLtaPrec CHAR(1),@pmFechaAdd SMALLDATETIME,@pmInactivo BIT,@pmLecGalMax DECIMAL(14,4)
 AS 
 INSERT INTO TanqDispens (IdMngra,NumDisp,NumPos,NumMngra,IdTanque,Descripcion,LecGalAnt,LecGalUlt,LecPesAnt,LecPesUlt,LtaPrec,FechaAdd,Inactivo,LecGalMax)
 VALUES (@pmIdMngra,@pmNumDisp,@pmNumPos,@pmNumMngra,@pmIdTanque,@pmDescripcion,@pmLecGalAnt,@pmLecGalUlt,@pmLecPesAnt,@pmLecPesUlt,@pmLtaPrec,@pmFechaAdd,@pmInactivo,@pmLecGalMax)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTanqSaldos @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 INSERT INTO TanqSaldos (IdTanque,IdCia,SaldoActual)
 VALUES (@pmIdTanque,@pmIdCia,@pmSaldoActual)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTanques @pmIdTanque VARCHAR(4),@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(50),@pmCapTanq DECIMAL(14,4),@pmNivAgua DECIMAL(14,4),@pmCapNeta DECIMAL(14,4)
,@pmSaldoActual DECIMAL(14,4),@pmTnqGas BIT,@pmTnqUnido BIT,@pmTnqVirtual BIT,@pmCdNueTan VARCHAR(4),@pmNColor INT,@pmFechaAdd SMALLDATETIME,@pmInactivo BIT,@pmIdCiaDef CHAR(2)
,@pmCodigoApi VARCHAR(10)
 AS 
 INSERT INTO Tanques (IdTanque,IdProducto,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual,TnqGas,TnqUnido,TnqVirtual,CdNueTan,NColor,FechaAdd,Inactivo,IdCiaDef,CodigoApi)
 VALUES (@pmIdTanque,@pmIdProducto,@pmDescripcion,@pmCapTanq,@pmNivAgua,@pmCapNeta,@pmSaldoActual,@pmTnqGas,@pmTnqUnido,@pmTnqVirtual,@pmCdNueTan,@pmNColor,@pmFechaAdd,@pmInactivo,@pmIdCiaDef,@pmCodigoApi)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTarifBuses @pmIdTarifa VARCHAR(4),@pmDescripcion VARCHAR(20),@pmVrNormal MONEY,@pmVrFestivo MONEY,@pmInactivo BIT
 AS 
 INSERT INTO TarifBuses (IdTarifa,Descripcion,VrNormal,VrFestivo,Inactivo)
 VALUES (@pmIdTarifa,@pmDescripcion,@pmVrNormal,@pmVrFestivo,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTarifasFletes @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmItem INT,@pmTipoTarifa VARCHAR(10),@pmTarifClie MONEY,@pmTarifPago MONEY
,@pmTarifTabla MONEY,@pmIdMneda VARCHAR(5),@pmUndMed VARCHAR(10),@pmNitCliente VARCHAR(16),@pmCdNat VARCHAR(4),@pmCdTipoVeh VARCHAR(4)
,@pmCdRango VARCHAR(4),@pmInactivo BIT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TarifasFletes (IdOrigen,IdDestino,Item,TipoTarifa,TarifClie,TarifPago,TarifTabla,IdMneda,UndMed,NitCliente,CdNat,CdTipoVeh,CdRango,Inactivo,FechaCrea,IdUsuario)
 VALUES (@pmIdOrigen,@pmIdDestino,@pmItem,@pmTipoTarifa,@pmTarifClie,@pmTarifPago,@pmTarifTabla,@pmIdMneda,@pmUndMed,@pmNitCliente
,@pmCdNat,@pmCdTipoVeh,@pmCdRango,@pmInactivo,@pmFechaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsTasasDTF @pmnAnno INT,@pmnMes INT,@pmnSem INT,@pmDTF_EA DECIMAL(14,4),@pmTIBC_EA DECIMAL(14,4),@pmFactor_Mon DECIMAL(14,4)
 AS 
 INSERT INTO TasasDTF (nAnno,nMes,nSem,DTF_EA,TIBC_EA,Factor_Mon)
 VALUES (@pmnAnno,@pmnMes,@pmnSem,@pmDTF_EA,@pmTIBC_EA,@pmFactor_Mon)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsTasasInteres @pmIdTasa VARCHAR(4),@pmDescTasa VARCHAR(50),@pmTipoInteres VARCHAR(10),@pmTipoPeriodo VARCHAR(10),@pmCausaInteres INT,@pmTipoDTF VARCHAR(10)
,@pmDTF_EA DECIMAL(14,4),@pmSpread_TA DECIMAL(14,4),@pmTasaEfectiva DECIMAL(14,4),@pmOpcCompra DECIMAL(14,4),@pmInactivo BIT
 AS 
 INSERT INTO TasasInteres (IdTasa,DescTasa,TipoInteres,TipoPeriodo,CausaInteres,TipoDTF,DTF_EA,Spread_TA,TasaEfectiva,OpcCompra,Inactivo)
 VALUES (@pmIdTasa,@pmDescTasa,@pmTipoInteres,@pmTipoPeriodo,@pmCausaInteres,@pmTipoDTF,@pmDTF_EA,@pmSpread_TA,@pmTasaEfectiva,@pmOpcCompra,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsTasasIntmora @pmIdMora VARCHAR(4),@pmDescMora VARCHAR(50),@pmTipoInteres VARCHAR(10)
,@pmTasaEA DECIMAL(14,4),@pmTasaEM DECIMAL(14,4),@pmDiasGracia INT,@pmInactivo BIT
 AS 
 INSERT INTO TasasIntmora (IdMora,DescMora,TipoInteres,TasaEA,TasaEM,DiasGracia,Inactivo)
 VALUES (@pmIdMora,@pmDescMora,@pmTipoInteres,@pmTasaEA,@pmTasaEM,@pmDiasGracia,@pmInactivo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTaxRutas @pmIdRuta VARCHAR(4),@pmNomRuta VARCHAR(100),@pmTipoRuta VARCHAR(10)
,@pmTarifPago MONEY,@pmTarifCobro MONEY,@pmOrigen VARCHAR(30),@pmDestino VARCHAR(30),@pmCodRuta VARCHAR(20),@pmInactivo BIT
 AS 
 INSERT INTO TaxRutas (IdRuta,NomRuta,TipoRuta,TarifPago,TarifCobro,Origen,Destino,CodRuta,Inactivo)
 VALUES (@pmIdRuta,@pmNomRuta,@pmTipoRuta,@pmTarifPago,@pmTarifCobro,@pmOrigen,@pmDestino,@pmCodRuta,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTaxTarifClie @pmTipoTarif VARCHAR(10),@pmItem INT,@pmIdClie VARCHAR(16),@pmTipoHora INT,@pmIdRuta VARCHAR(4)
,@pmNumInicial DECIMAL(14,4),@pmNumFinal DECIMAL(14,4),@pmTarifPago MONEY,@pmTarifCobro MONEY
 AS 
 INSERT INTO TaxTarifClie (TipoTarif,Item,IdClie,TipoHora,IdRuta,NumInicial,NumFinal,TarifPago,TarifCobro)
 VALUES (@pmTipoTarif,@pmItem,@pmIdClie,@pmTipoHora,@pmIdRuta,@pmNumInicial,@pmNumFinal,@pmTarifPago,@pmTarifCobro)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTaxTarifas @pmTipoTarif VARCHAR(10),@pmItem INT,@pmTipoHora INT,@pmNumInicial DECIMAL(14,4)
,@pmNumFinal DECIMAL(14,4),@pmTarifPago MONEY,@pmTarifCobro MONEY
 AS 
 INSERT INTO TaxTarifas (TipoTarif,Item,TipoHora,NumInicial,NumFinal,TarifPago,TarifCobro)
 VALUES (@pmTipoTarif,@pmItem,@pmTipoHora,@pmNumInicial,@pmNumFinal,@pmTarifPago,@pmTarifCobro)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercClieCia @pmIdClie VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 INSERT INTO TercClieCia (IdClie,IdCia)
 VALUES (@pmIdClie,@pmIdCia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercClieLineas @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmIdTasa VARCHAR(4),@pmnPlazo INT,@pmTipoPlazo CHAR(1),@pmCupoCredito MONEY,@pmSaldoCapital MONEY,@pmSaldoInteres MONEY,@pmMaxCupoSmlv DECIMAL(14,4),@pmTipoCausac VARCHAR(10),@pmDiasVence VARCHAR(20)
,@pmFecPreAprob SMALLDATETIME,@pmPlazoAprob INT,@pmEtdoAprob INT,@pmTipoAprob VARCHAR(20),@pmDescAprob VARCHAR(250),@pmGarantias BIT,@pmTipoGarantia VARCHAR(10),@pmDescGarantia VARCHAR(250),@pmVrGarantia MONEY,@pmReqCheques BIT,@pmFecActCupo SMALLDATETIME,@pmEstadoAct INT
,@pmLiqSeguro BIT,@pmCdTasaConv VARCHAR(4),@pmNitConvenio VARCHAR(16),@pmCuotasConv INT,@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercClieLineas (IdClie,IdAgencia,IdLinea,IdTasa,nPlazo,TipoPlazo,CupoCredito,SaldoCapital,SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,LiqSeguro,FecPreAprob,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia,ReqCheques,FecActCupo,CdTasaConv,NitConvenio,CuotasConv,EstadoAct,FechaCrea,IdUsuario)
 VALUES (@pmIdClie,@pmIdAgencia,@pmIdLinea,@pmIdTasa,@pmnPlazo,@pmTipoPlazo,@pmCupoCredito,@pmSaldoCapital,@pmSaldoInteres,@pmMaxCupoSmlv,@pmTipoCausac,@pmDiasVence,@pmLiqSeguro,@pmFecPreAprob,@pmPlazoAprob,@pmEtdoAprob,@pmTipoAprob,@pmDescAprob,@pmGarantias,@pmTipoGarantia,@pmDescGarantia,@pmVrGarantia
,@pmReqCheques,@pmFecActCupo,@pmCdTasaConv,@pmNitConvenio,@pmCuotasConv,@pmEstadoAct,@pmFechaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercCliePreRef @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmItem INT,@pmTipoRef VARCHAR(10)
,@pmReferencia VARCHAR(150),@pmNitRefncia VARCHAR(16),@pmDirRefncia VARCHAR(150),@pmTelRefncia VARCHAR(30)
,@pmemlRefncia VARCHAR(100),@pmCargRefncia VARCHAR(50),@pmNumCuenta VARCHAR(30),@pmObservcion VARCHAR(250)
 AS 
 INSERT INTO TercCliePreRef (IdClie,IdAgencia,Item,TipoRef,Referencia,NitRefncia,DirRefncia,TelRefncia,emlRefncia,CargRefncia,NumCuenta,Observcion)
 VALUES (@pmIdClie,@pmIdAgencia,@pmItem,@pmTipoRef,@pmReferencia,@pmNitRefncia,@pmDirRefncia,@pmTelRefncia,@pmemlRefncia,@pmCargRefncia,@pmNumCuenta,@pmObservcion)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100)
,@pmCargContac VARCHAR(50),@pmIdSzona VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdTipoTerc VARCHAR(4),@pmIdVend VARCHAR(16),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmEsExento BIT,@pmCalcIntMora BIT,@pmDiasGracia INT
,@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmVrSalBasico MONEY,@pmMaxDcto DECIMAL(14,4),@pmMatMerc VARCHAR(20),@pmFecMat SMALLDATETIME
,@pmNomAgencia VARCHAR(150),@pmDirAgencia VARCHAR(250),@pmIdLocalAge VARCHAR(8),@pmComentarios VARCHAR(2000),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTipoSalario CHAR(1),@pmFechaCrea SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercCliePrestamo (IdClie,IdAgencia,IdSzona,IdGrupo,IdTipoTerc,IdVend,IdClase,NumCuenta,IdBanco,NomAgencia,DirAgencia,IdLocalAge,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,EsExento,CalcIntMora,DiasGracia,FecIngreso,FecVigencia,FecRetiro,VrSalBasico
,MaxDcto,TipoSalario,MatMerc,FecMat,Comentarios,IdEstado,Inactivo,FechaCrea,IdUsuario)
 VALUES (@pmIdClie,@pmIdAgencia,@pmIdSzona,@pmIdGrupo,@pmIdTipoTerc,@pmIdVend,@pmIdClase,@pmNumCuenta,@pmIdBanco,@pmNomAgencia,@pmDirAgencia,@pmIdLocalAge,@pmNitRepLeg,@pmNomRepLeg,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargContac,@pmEsExento
,@pmCalcIntMora,@pmDiasGracia,@pmFecIngreso,@pmFecVigencia,@pmFecRetiro,@pmVrSalBasico,@pmMaxDcto,@pmTipoSalario,@pmMatMerc,@pmFecMat,@pmComentarios,@pmIdEstado,@pmInactivo,@pmFechaCrea,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercClieRef @pmIdClie VARCHAR(16),@pmItem INT,@pmTipoRef VARCHAR(10),@pmReferencia VARCHAR(150)
,@pmNitRefncia VARCHAR(16),@pmDirRefncia VARCHAR(150),@pmTelRefncia VARCHAR(30),@pmemlRefncia VARCHAR(100)
,@pmCargRefncia VARCHAR(50),@pmNumCuenta VARCHAR(30),@pmObservcion VARCHAR(250)
 AS 
 INSERT INTO TercClieRef (IdClie,Item,TipoRef,Referencia,NitRefncia,DirRefncia,TelRefncia,emlRefncia,CargRefncia,NumCuenta,Observcion)
 VALUES (@pmIdClie,@pmItem,@pmTipoRef,@pmReferencia,@pmNitRefncia,@pmDirRefncia,@pmTelRefncia,@pmemlRefncia
,@pmCargRefncia,@pmNumCuenta,@pmObservcion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 INSERT INTO TercClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
 VALUES (@pmIdClie,@pmCdAgencia,@pmIdCia,@pmVrSaldo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercClieSaldos_Uni @pmIdClie VARCHAR(16),@pmIdClieDos VARCHAR(16),@pmNewClie VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdClie FROM TercClieSaldos WHERE IdClie=@pmIdClie OR IdClie=@pmIdClieDos)
  INSERT INTO TercClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
  SELECT @pmNewClie,CdAgencia,IdCia,SUM(VrSaldo)
  FROM TercClieSaldos
  WHERE  IdClie=@pmIdClie OR IdClie=@pmIdClieDos
  GROUP BY CdAgencia,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercClieVeh @pmIdClie VARCHAR(16),@pmpVehiculo VARCHAR(10)
,@pmCdConductor VARCHAR(16),@pmNumVehiculo VARCHAR(10),@pmDescripcion VARCHAR(100)
 AS 
 INSERT INTO TercClieVeh (IdClie,pVehiculo,CdConductor,NumVehiculo,Descripcion)
 VALUES (@pmIdClie,@pmpVehiculo,@pmCdConductor,@pmNumVehiculo,@pmDescripcion)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercCliente @pmIdClie VARCHAR(16),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50),@pmDirEnv VARCHAR(250)
,@pmIdLocEnv VARCHAR(8),@pmDiasEntga INT,@pmIdSzona VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdPlazo VARCHAR(4),@pmIdForma VARCHAR(4),@pmIdEstrato VARCHAR(4),@pmIdVend VARCHAR(16),@pmNitFact VARCHAR(16),@pmIdRuta VARCHAR(4),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30)
,@pmIdBanco VARCHAR(4),@pmCdMney VARCHAR(5),@pmCdDct VARCHAR(4),@pmCdRet VARCHAR(4),@pmCdRiv VARCHAR(4),@pmCdCms VARCHAR(4),@pmExcIva BIT,@pmTrfIntMora DECIMAL(14,4),@pmDiasGracia INT,@pmLiqFletes BIT,@pmFactSold BIT,@pmAutoret BIT,@pmIncRet BIT,@pmIncRiv BIT
,@pmIncIca BIT,@pmFactTipo INT,@pmVrCupo MONEY,@pmVrSaldo MONEY,@pmUidClie VARCHAR(16),@pmPwdClie VARCHAR(10),@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmMatMerc VARCHAR(20),@pmFecMat SMALLDATETIME
,@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmCmntario1 VARCHAR(250),@pmCmntario2 VARCHAR(250),@pmPlazosImp VARCHAR(20),@pmCdBandera VARCHAR(4),@pmContrato BIT,@pmNContrato INT,@pmCiaContMay CHAR(2),@pmCmntario3 VARCHAR(250),@pmPrendGarant VARCHAR(150)
,@pmFecUpCupo SMALLDATETIME,@pmTipoCliente VARCHAR(10),@pmRestric_Cia BIT,@pmCupoGalones DECIMAL(14,4),@pmCdPlazoComb VARCHAR(4),@pmCodClieSicom VARCHAR(20),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFecPlazoDoc SMALLDATETIME,@pmEdoRadicaDoc INT,@pmCdTipBloq VARCHAR(4),@pmDescEdoDoc VARCHAR(250)
,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercCliente (IdClie,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirEnv,IdLocEnv,DiasEntga,IdSzona,IdGrupo,IdPlazo,IdForma,IdEstrato,IdVend,NitFact,IdRuta,IdClase,NumCuenta,IdBanco,CdMney,CdDct,CdRet,CdRiv,CdCms,ExcIva,TrfIntMora,DiasGracia,LiqFletes,FactSold,Autoret
,IncRet,IncRiv,IncIca,FactTipo,VrCupo,VrSaldo,UidClie,PwdClie,CodClieSicom,FecIngreso,FecVigencia,FecRetiro,MatMerc,FecMat,PathFoto,PathFirma,Cmntario1,Cmntario2,PlazosImp,IdEstado,TipoCliente,Inactivo,CdBandera,Contrato,NContrato,CiaContMay,Cmntario3,PrendGarant,FecUpCupo,FechaAdd,IdUsuario,CupoGalones,Restric_Cia,CdPlazoComb,FecPlazoDoc,EdoRadicaDoc,CdTipBloq,DescEdoDoc)
 VALUES (@pmIdClie,@pmNitRepLeg,@pmNomRepLeg,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargContac,@pmDirEnv,@pmIdLocEnv,@pmDiasEntga,@pmIdSzona,@pmIdGrupo,@pmIdPlazo,@pmIdForma,@pmIdEstrato,@pmIdVend,@pmNitFact,@pmIdRuta,@pmIdClase,@pmNumCuenta
,@pmIdBanco,@pmCdMney,@pmCdDct,@pmCdRet,@pmCdRiv,@pmCdCms,@pmExcIva,@pmTrfIntMora,@pmDiasGracia,@pmLiqFletes,@pmFactSold,@pmAutoret,@pmIncRet,@pmIncRiv,@pmIncIca,@pmFactTipo,@pmVrCupo,@pmVrSaldo,@pmUidClie,@pmPwdClie,@pmCodClieSicom,@pmFecIngreso,@pmFecVigencia,@pmFecRetiro
,@pmMatMerc,@pmFecMat,@pmPathFoto,@pmPathFirma,@pmCmntario1,@pmCmntario2,@pmPlazosImp,@pmIdEstado,@pmTipoCliente,@pmInactivo,@pmCdBandera,@pmContrato,@pmNContrato,@pmCiaContMay,@pmCmntario3,@pmPrendGarant,@pmFecUpCupo,@pmFechaAdd,@pmIdUsuario,@pmCupoGalones,@pmRestric_Cia,@pmCdPlazoComb,@pmFecPlazoDoc,@pmEdoRadicaDoc,@pmCdTipBloq,@pmDescEdoDoc)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercCliente_Sel @pmIdClie VARCHAR(16),@pmNewIdClie VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdClie FROM  TercCliente WHERE  IdClie=@pmIdClie)
  INSERT INTO TercCliente (IdClie,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirEnv,IdLocEnv,DiasEntga,IdSzona,IdGrupo,IdPlazo,IdForma,IdEstrato,IdVend,NitFact,IdRuta,IdClase,NumCuenta,IdBanco,CdMney,CdDct,CdRet,CdRiv,CdCms,ExcIva,TrfIntMora,DiasGracia,LiqFletes,FactSold,Autoret
 ,IncRet,IncRiv,IncIca,FactTipo,VrCupo,VrSaldo,UidClie,PwdClie,CodClieSicom,FecIngreso,FecVigencia,FecRetiro,MatMerc,FecMat,PathFoto,PathFirma,Cmntario1,Cmntario2,PlazosImp,IdEstado,TipoCliente,Inactivo,CdBandera,Contrato,NContrato,CiaContMay,Cmntario3,PrendGarant,FecUpCupo,FechaAdd,IdUsuario
,CupoGalones,Restric_Cia,CdPlazoComb,FecPlazoDoc,EdoRadicaDoc,CdTipBloq,DescEdoDoc)
  SELECT @pmNewIdClie,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirEnv,IdLocEnv,DiasEntga,IdSzona,IdGrupo,IdPlazo,IdForma,IdEstrato,IdVend,NitFact,IdRuta,IdClase,NumCuenta,IdBanco,CdMney,CdDct,CdRet,CdRiv,CdCms,ExcIva,TrfIntMora,DiasGracia,LiqFletes,FactSold,Autoret
 ,IncRet,IncRiv,IncIca,FactTipo,VrCupo,VrSaldo,UidClie,PwdClie,CodClieSicom,FecIngreso,FecVigencia,FecRetiro,MatMerc,FecMat,PathFoto,PathFirma,Cmntario1,Cmntario2,PlazosImp,IdEstado,TipoCliente,Inactivo,CdBandera,Contrato,NContrato,CiaContMay,Cmntario3,PrendGarant,FecUpCupo,FechaAdd,IdUsuario,CupoGalones,Restric_Cia,CdPlazoComb
,FecPlazoDoc,EdoRadicaDoc,CdTipBloq,DescEdoDoc
  FROM  TercCliente 
  WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercCndContrato @pmNumContrato INT,@pmIdConductor VARCHAR(16),@pmIdTipCon VARCHAR(4),@pmIdModd VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmFecVigencia SMALLDATETIME
,@pmObservaciones VARCHAR(250),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercCndContrato (NumContrato,IdConductor,IdTipCon,IdModd,FecIngreso,FecRetiro,FecVigencia,Observaciones,IdUsuario)
 VALUES (@pmNumContrato,@pmIdConductor,@pmIdTipCon,@pmIdModd,@pmFecIngreso,@pmFecRetiro,@pmFecVigencia,@pmObservaciones,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTercCndReferencias @pmIdConductor VARCHAR(16),@pmItem INT,@pmTipoRef VARCHAR(10),@pmEmpresaRef VARCHAR(150),@pmNitRef VARCHAR(16),@pmDireccionRef VARCHAR(150)
,@pmTelefonoRef VARCHAR(30),@pmContactoRef VARCHAR(150),@pmEmailRef VARCHAR(100),@pmCargoRef VARCHAR(50),@pmCuentaBanco VARCHAR(30),@pmFechaInicial SMALLDATETIME
,@pmFechaFinal SMALLDATETIME,@pmVerificado VARCHAR(150),@pmObservRef VARCHAR(250)
 AS 
 INSERT INTO TercCndReferencias (IdConductor,Item,TipoRef,EmpresaRef,NitRef,DireccionRef,TelefonoRef,ContactoRef,EmailRef,CargoRef,CuentaBanco,FechaInicial,FechaFinal,Verificado,ObservRef)
 VALUES (@pmIdConductor,@pmItem,@pmTipoRef,@pmEmpresaRef,@pmNitRef,@pmDireccionRef,@pmTelefonoRef,@pmContactoRef,@pmEmailRef,@pmCargoRef,@pmCuentaBanco,@pmFechaInicial,@pmFechaFinal,@pmVerificado,@pmObservRef)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercCndTarjetas @pmIdConductor VARCHAR(16),@pmItem INT,@pmNumeroTarj VARCHAR(30)
,@pmSecuencia INT,@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME
 AS 
 INSERT INTO TercCndTarjetas (IdConductor,Item,NumeroTarj,Secuencia,Fecha,FecVence)
 VALUES (@pmIdConductor,@pmItem,@pmNumeroTarj,@pmSecuencia,@pmFecha,@pmFecVence)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercCndtores @pmIdConductor VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmIdModd VARCHAR(4),@pmTipoSangre CHAR(2),@pmFactorRh CHAR(1),@pmLibMilitar VARCHAR(25),@pmClaseLib INT,@pmFechaLib SMALLDATETIME,@pmDistMil VARCHAR(10)
,@pmCertJudicial VARCHAR(25),@pmFecJudicial SMALLDATETIME,@pmVigJudicial SMALLDATETIME,@pmLicencia VARCHAR(25),@pmIdLugar VARCHAR(8),@pmCatLicencia INT,@pmFecLicencia SMALLDATETIME,@pmVigLicencia SMALLDATETIME
,@pmFecNacmto SMALLDATETIME,@pmLugarNacmto VARCHAR(30),@pmSexo CHAR(1),@pmIdEstCivil VARCHAR(4),@pmNHijos INT,@pmPasporte VARCHAR(25),@pmVigPaspte SMALLDATETIME
,@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmCdLocCue VARCHAR(8),@pmCdFonPen VARCHAR(8),@pmCdFonEps VARCHAR(8),@pmCdFonArp VARCHAR(8),@pmpVehiculo VARCHAR(10),@pmPathFoto VARCHAR(30)
,@pmPathFirma VARCHAR(30),@pmPathLic VARCHAR(30),@pmCmntarios VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmNumTarjeta VARCHAR(30),@pmEdicTarjeta INT,@pmFecTarjeta SMALLDATETIME
,@pmVigTarjeta SMALLDATETIME,@pmPasSeguridad VARCHAR(30),@pmFecPasSeg SMALLDATETIME,@pmVigPasSeg SMALLDATETIME,@pmNitEmpTrans VARCHAR(16),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
,@pmNum_Contrato INT,@pmContrato_Activo BIT,@pmCdTipCon VARCHAR(4),@pmFec_Ingreso SMALLDATETIME,@pmFec_Retiro SMALLDATETIME,@pmFec_Vigencia SMALLDATETIME,@pmFec_IngresoAnt SMALLDATETIME,@pmFec_UltimoAcc SMALLDATETIME
,@pmPasadoJudicial VARCHAR(250),@pmRestriccion VARCHAR(50),@pmFecPagoPen SMALLDATETIME,@pmFecPagoEps SMALLDATETIME,@pmFecPagoArp SMALLDATETIME
 AS 
 INSERT INTO TercCndtores (IdConductor,IdGrupo,IdModd,TipoSangre,FactorRh,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdClase,NumCuenta,IdBanco,CdLocCue,CdFonPen,CdFonEps,CdFonArp,pVehiculo
,FecNacmto,LugarNacmto,Sexo,IdEstCivil,NHijos,Pasporte,VigPaspte,PathFoto,PathFirma,PathLic,Cmntarios,IdEstado,Inactivo,NumTarjeta,EdicTarjeta,FecTarjeta,VigTarjeta,PasSeguridad,FecPasSeg,VigPasSeg,NitEmpTrans,FechaAdd,IdUsuario,Num_Contrato,Contrato_Activo,CdTipCon,Fec_Ingreso
 ,Fec_Retiro,Fec_Vigencia,Fec_IngresoAnt,Fec_UltimoAcc,PasadoJudicial,Restriccion,FecPagoPen,FecPagoEps,FecPagoArp)
 VALUES (@pmIdConductor,@pmIdGrupo,@pmIdModd,@pmTipoSangre,@pmFactorRh,@pmLibMilitar,@pmClaseLib,@pmFechaLib,@pmDistMil,@pmCertJudicial,@pmFecJudicial,@pmVigJudicial,@pmLicencia,@pmIdLugar,@pmCatLicencia,@pmFecLicencia,@pmVigLicencia,@pmIdClase
,@pmNumCuenta,@pmIdBanco,@pmCdLocCue,@pmCdFonPen,@pmCdFonEps,@pmCdFonArp,@pmpVehiculo,@pmFecNacmto,@pmLugarNacmto,@pmSexo,@pmIdEstCivil,@pmNHijos,@pmPasporte,@pmVigPaspte,@pmPathFoto,@pmPathFirma,@pmPathLic,@pmCmntarios
,@pmIdEstado,@pmInactivo,@pmNumTarjeta,@pmEdicTarjeta,@pmFecTarjeta,@pmVigTarjeta,@pmPasSeguridad,@pmFecPasSeg,@pmVigPasSeg,@pmNitEmpTrans,@pmFechaAdd,@pmIdUsuario,@pmNum_Contrato,@pmContrato_Activo,@pmCdTipCon,@pmFec_Ingreso
 ,@pmFec_Retiro,@pmFec_Vigencia,@pmFec_IngresoAnt,@pmFec_UltimoAcc,@pmPasadoJudicial,@pmRestriccion,@pmFecPagoPen,@pmFecPagoEps,@pmFecPagoArp)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercCndtores_Sel @pmIdConductor VARCHAR(16),@pmNewConductor VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdConductor FROM  TercCndtores WHERE IdConductor=@pmIdConductor)
  INSERT INTO TercCndtores (IdConductor,IdGrupo,IdModd,TipoSangre,FactorRh,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdClase,NumCuenta,IdBanco,CdLocCue,CdFonPen,CdFonEps,CdFonArp,pVehiculo
 ,FecNacmto,LugarNacmto,Sexo,IdEstCivil,NHijos,Pasporte,VigPaspte,PathFoto,PathFirma,PathLic,Cmntarios,IdEstado,Inactivo,NumTarjeta,EdicTarjeta,FecTarjeta,VigTarjeta,PasSeguridad,FecPasSeg,VigPasSeg,NitEmpTrans,FechaAdd,IdUsuario
,Num_Contrato,Contrato_Activo,CdTipCon,Fec_Ingreso,Fec_Retiro,Fec_Vigencia,Fec_IngresoAnt,Fec_UltimoAcc,PasadoJudicial,Restriccion,FecPagoPen,FecPagoEps,FecPagoArp)
  SELECT @pmNewConductor,IdGrupo,IdModd,TipoSangre,FactorRh,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdClase,NumCuenta,IdBanco,CdLocCue,CdFonPen,CdFonEps,CdFonArp,pVehiculo
 ,FecNacmto,LugarNacmto,Sexo,IdEstCivil,NHijos,Pasporte,VigPaspte,PathFoto,PathFirma,PathLic,Cmntarios,IdEstado,Inactivo,NumTarjeta,EdicTarjeta,FecTarjeta,VigTarjeta,PasSeguridad,FecPasSeg,VigPasSeg,NitEmpTrans,FechaAdd,IdUsuario
,Num_Contrato,Contrato_Activo,CdTipCon,Fec_Ingreso,Fec_Retiro,Fec_Vigencia,Fec_IngresoAnt,Fec_UltimoAcc,PasadoJudicial,Restriccion,FecPagoPen,FecPagoEps,FecPagoArp
  FROM TercCndtores 
  WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsTercPropietario @pmIdPropietario VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmCupoCredito MONEY,@pmSaldoActual MONEY,@pmAvala BIT,@pmIdAdmon VARCHAR(4),@pmIdTipCon VARCHAR(4)
,@pmContratoActivo BIT,@pmFecIngreso SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercPropietario (IdPropietario,IdGrupo,CupoCredito,SaldoActual,Avala,IdAdmon,IdTipCon,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,Observacion,IdEstado,FechaAdd,IdUsuario)
 VALUES (@pmIdPropietario,@pmIdGrupo,@pmCupoCredito,@pmSaldoActual,@pmAvala,@pmIdAdmon,@pmIdTipCon,@pmContratoActivo,@pmFecIngreso,@pmFecRetiro,@pmFecVigencia,@pmObservacion,@pmIdEstado,@pmFechaAdd,@pmIdUsuario)



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercPropietario_Sel @pmIdPropietario VARCHAR(16),@pmNewPropietario VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdPropietario FROM  TercPropietario WHERE  IdPropietario=@pmIdPropietario)
  INSERT INTO TercPropietario (IdPropietario,IdGrupo,CupoCredito,SaldoActual,Avala,IdAdmon,IdTipCon,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,Observacion,IdEstado,FechaAdd,IdUsuario)
  SELECT @pmNewPropietario,IdGrupo,CupoCredito,SaldoActual,Avala,IdAdmon,IdTipCon,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,Observacion,IdEstado,FechaAdd,IdUsuario
  FROM TercPropietario WHERE  IdPropietario=@pmIdPropietario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercProvee @pmIdProv VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150)
,@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50),@pmDirOrdComp VARCHAR(250),@pmIdLocOrd VARCHAR(8),@pmDiasEntga INT,@pmIdPlazo VARCHAR(4),@pmIdForma VARCHAR(4)
,@pmCdMney VARCHAR(5),@pmCdDct VARCHAR(4),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmAutoret BIT,@pmIncRet BIT,@pmIncRiv BIT,@pmIncIca BIT,@pmLiqFletes BIT,@pmCdRet VARCHAR(4)
,@pmCdRiv VARCHAR(4),@pmGartProd INT,@pmFecIngreso SMALLDATETIME,@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmCmntarios VARCHAR(250),@pmContrato BIT,@pmNContrato VARCHAR(20),@pmFecVigencia SMALLDATETIME
,@pmFecRetiro SMALLDATETIME,@pmObsFinanc VARCHAR(250),@pmObsJuridica VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercProvee (IdProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv
,GartProd,FecIngreso,PathFoto,PathFirma,Cmntarios,IdEstado,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdProv,@pmIdGrupo,@pmNitRepLeg,@pmNomRepLeg,@pmNitContac,@pmNomContac,@pmTelContac,@pmemlContac,@pmCargContac,@pmDirOrdComp,@pmIdLocOrd,@pmDiasEntga,@pmIdPlazo,@pmIdForma,@pmCdMney,@pmCdDct,@pmIdClase
,@pmNumCuenta,@pmIdBanco,@pmAutoret,@pmIncRet,@pmIncRiv,@pmIncIca,@pmLiqFletes,@pmCdRet,@pmCdRiv,@pmGartProd,@pmFecIngreso,@pmPathFoto,@pmPathFirma,@pmCmntarios,@pmIdEstado
,@pmContrato,@pmNContrato,@pmFecVigencia,@pmFecRetiro,@pmObsFinanc,@pmObsJuridica,@pmInactivo,@pmFechaAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercProvee_Sel @pmIdProv VARCHAR(16),@pmNewProv VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdProv FROM  TercProvee WHERE  IdProv=@pmIdProv)
 INSERT INTO TercProvee (IdProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv
,GartProd,FecIngreso,PathFoto,PathFirma,Cmntarios,IdEstado,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Inactivo,FechaAdd,IdUsuario)
 SELECT @pmNewProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv
,GartProd,FecIngreso,PathFoto,PathFirma,Cmntarios,IdEstado,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Inactivo,FechaAdd,IdUsuario
 FROM TercProvee WHERE  IdProv=@pmIdProv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercVendedor @pmIdVend VARCHAR(16),@pmIdTarCms VARCHAR(4),@pmIdTarCm2 VARCHAR(4),@pmCdJorn VARCHAR(4),@pmIdSzona VARCHAR(4)
,@pmIdGrupo VARCHAR(4),@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmComntarios VARCHAR(250),@pmInactivo BIT,@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30)
,@pmIdBanco VARCHAR(4),@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO TercVendedor (IdVend,IdTarCms,IdTarCm2,CdJorn,IdSzona,IdGrupo,PathFoto,PathFirma,Comntarios,Inactivo,IdClase,NumCuenta,IdBanco,FecAdd,IdUsuario)
 VALUES (@pmIdVend,@pmIdTarCms,@pmIdTarCm2,@pmCdJorn,@pmIdSzona,@pmIdGrupo,@pmPathFoto,@pmPathFirma,@pmComntarios,@pmInactivo,@pmIdClase,@pmNumCuenta,@pmIdBanco,@pmFecAdd,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTercVendedor_Sel @pmIdVend VARCHAR(16),@pmNewVend VARCHAR(16)
 AS 
 IF EXISTS (SELECT IdVend FROM  TercVendedor WHERE  IdVend=@pmIdVend)
  INSERT INTO TercVendedor (IdVend,IdTarCms,IdTarCm2,CdJorn,IdSzona,IdGrupo,PathFoto,PathFirma,Comntarios,Inactivo,IdClase,NumCuenta,IdBanco,FecAdd,IdUsuario)
  SELECT @pmNewVend,IdTarCms,IdTarCm2,CdJorn,IdSzona,IdGrupo,PathFoto,PathFirma,Comntarios,Inactivo,IdClase,NumCuenta,IdBanco,FecAdd,IdUsuario
  FROM TercVendedor WHERE  IdVend=@pmIdVend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTerceros @pmIdTercero VARCHAR(16),@pmRazonSocial VARCHAR(250),@pmCodigo VARCHAR(16),@pmTipoId CHAR(1),@pmDv CHAR(1),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8)
,@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pmTelMovil VARCHAR(20),@pmSitioWeb VARCHAR(100),@pme_mail VARCHAR(100),@pmEsCliente BIT,@pmEsVendedor BIT,@pmEsConductor BIT
,@pmEsPropietario BIT,@pmEsProveedor BIT,@pmEsEmpleado BIT,@pmEsOperario BIT,@pmEsAccnista BIT,@pmEsCiaAseg BIT,@pmIdSector VARCHAR(8),@pmIdRegimen VARCHAR(4),@pmIdLugarCed VARCHAR(8),@pmObservacion VARCHAR(250)
,@pmIniStgNom INT,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmNomCial VARCHAR(250),@pmSiglaRaz VARCHAR(50),@pmIdProf VARCHAR(4),@pmTipEnte CHAR(1),@pmFecExpCed SMALLDATETIME,@pmEsCliePres BIT
,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Terceros (IdTercero,RazonSocial,Codigo,TipoId,Dv,Direccion,IdLocal,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor,EsPropietario,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,IdSector,IdRegimen
,IdLugarCed,Observacion,IniStgNom,IdEstado,Inactivo,FechaAdd,IdUsuario,NomCial,SiglaRaz,IdProf,TipEnte,FecExpCed)
 VALUES (@pmIdTercero,@pmRazonSocial,@pmCodigo,@pmTipoId,@pmDv,@pmDireccion,@pmIdLocal,@pmTelefono,@pmFax,@pmTelMovil,@pmSitioWeb,@pme_mail,@pmEsCliente,@pmEsVendedor,@pmEsConductor,@pmEsPropietario,@pmEsProveedor
,@pmEsEmpleado,@pmEsOperario,@pmEsAccnista,@pmEsCiaAseg,@pmEsCliePres,@pmIdSector,@pmIdRegimen,@pmIdLugarCed,@pmObservacion,@pmIniStgNom,@pmIdEstado,@pmInactivo,@pmFechaAdd,@pmIdUsuario
,@pmNomCial,@pmSiglaRaz,@pmIdProf,@pmTipEnte,@pmFecExpCed)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTerceros_Sel @pmIdTercero VARCHAR(16),@pmNewTercero VARCHAR(16),@pmNewCodigo VARCHAR(16)
 AS 
 INSERT INTO Terceros (IdTercero,RazonSocial,Codigo,TipoId,Dv,Direccion,IdLocal,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor,EsPropietario,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,IdSector,IdRegimen
,IdLugarCed,Observacion,IniStgNom,IdEstado,Inactivo,FechaAdd,IdUsuario,NomCial,SiglaRaz,IdProf,TipEnte,FecExpCed)
 SELECT @pmNewTercero,RazonSocial,@pmNewCodigo,TipoId,Dv,Direccion,IdLocal,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor,EsPropietario,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,IdSector,IdRegimen
,IdLugarCed,Observacion,IniStgNom,IdEstado,Inactivo,FechaAdd,IdUsuario,NomCial,SiglaRaz,IdProf,TipEnte,FecExpCed
 FROM  Terceros 
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposAdm @pmIdAdmon VARCHAR(4),@pmTipoAdmon VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposAdm (IdAdmon,TipoAdmon,Inactivo)
 VALUES (@pmIdAdmon,@pmTipoAdmon,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposAgenc @pmIdTipAgenc VARCHAR(4),@pmTipoAgencia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposAgenc (IdTipAgenc,TipoAgencia,Inactivo)
 VALUES (@pmIdTipAgenc,@pmTipoAgencia,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposBan @pmIdBandera VARCHAR(4),@pmTipoBandera VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposBan (IdBandera,TipoBandera,Inactivo)
 VALUES (@pmIdBandera,@pmTipoBandera,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposBloq @pmIdTipBloq VARCHAR(4),@pmTipoBloqueo VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposBloq (IdTipBloq,TipoBloqueo,Inactivo)
 VALUES (@pmIdTipBloq,@pmTipoBloqueo,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposCar @pmIdCrceria VARCHAR(4),@pmTipoCar VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCar (IdCrceria,TipoCar,Inactivo)
 VALUES (@pmIdCrceria,@pmTipoCar,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposCarga @pmIdTipCarga VARCHAR(4),@pmTipoCarga VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCarga (IdTipCarga,TipoCarga,Inactivo)
 VALUES (@pmIdTipCarga,@pmTipoCarga,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposCnd @pmIdTipoCon VARCHAR(4),@pmTipoContndor VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCnd (IdTipoCon,TipoContndor,Inactivo)
 VALUES (@pmIdTipoCon,@pmTipoContndor,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposCol @pmIdColor VARCHAR(4),@pmNomColor VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 INSERT INTO TiposCol (IdColor,NomColor,NColor,Inactivo)
 VALUES (@pmIdColor,@pmNomColor,@pmNColor,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposCom @pmIdCom VARCHAR(3),@pmTipoCom VARCHAR(50),@pmIdDiario VARCHAR(4)
,@pmEsEgreso BIT,@pmConsUnico BIT,@pmInactivo BIT,@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposCom (IdCom,TipoCom,IdDiario,EsEgreso,ConsUnico,Inactivo,FechaAdd)
 VALUES (@pmIdCom,@pmTipoCom,@pmIdDiario,@pmEsEgreso,@pmConsUnico,@pmInactivo,@pmFechaAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposComAgenc @pmIdSubAgenc VARCHAR(4),@pmSubTipoAgenc VARCHAR(50),@pmIdTipAgenc VARCHAR(4),@pmInactivo BIT
 AS 
 INSERT INTO TiposComAgenc (IdSubAgenc,SubTipoAgenc,IdTipAgenc,Inactivo)
 VALUES (@pmIdSubAgenc,@pmSubTipoAgenc,@pmIdTipAgenc,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposComCons @pmIdCom VARCHAR(3),@pmIdCia CHAR(2),@pmLDesde INT,@pmLHasta INT
,@pmNumero INT,@pmNumManual BIT,@pmFormato VARCHAR(20),@pmConfigFecha VARCHAR(10),@pmTipoPapel INT
,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT,@pmNumCopias INT,@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposComCons (IdCom,IdCia,LDesde,LHasta,Numero,NumManual,Formato,ConfigFecha
,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,FechaAdd)
 VALUES (@pmIdCom,@pmIdCia,@pmLDesde,@pmLHasta,@pmNumero,@pmNumManual,@pmFormato,@pmConfigFecha
,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmFechaAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposCon @pmIdTipCon VARCHAR(4),@pmTipoContrato VARCHAR(100)
,@pmDuracion INT,@pmInactivo BIT
 AS 
 INSERT INTO TiposCon (IdTipCon,TipoContrato,Duracion,Inactivo)
 VALUES (@pmIdTipCon,@pmTipoContrato,@pmDuracion,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposCot @pmIdCot VARCHAR(4),@pmCotizante VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposCot (IdCot,Cotizante,Inactivo)
 VALUES (@pmIdCot,@pmCotizante,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposDoc @pmIdDoc VARCHAR(3),@pmTipoDoc VARCHAR(50),@pmConsUnico BIT
,@pmLeyenda VARCHAR(250),@pmInactivo BIT,@pmIntegraCias BIT
 AS 
 INSERT INTO Sys_TiposDoc (IdDoc,TipoDoc,ConsUnico,Leyenda,Inactivo,IntegraCias)
 VALUES (@pmIdDoc,@pmTipoDoc,@pmConsUnico,@pmLeyenda,@pmInactivo,@pmIntegraCias)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposDocCons @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2),@pmLDesde INT,@pmLHasta INT,@pmResolucion VARCHAR(30)
,@pmRangoNum VARCHAR(50),@pmFechaRes SMALLDATETIME,@pmPrefijo VARCHAR(5),@pmNumero INT,@pmNumManual BIT,@pmIntLotes BIT
,@pmConfigFecha VARCHAR(10),@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT
,@pmNumCopias INT,@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO TiposDocCons (IdDoc,IdCia,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero,NumManual,IntLotes,ConfigFecha
,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,FechaAdd)
 VALUES (@pmIdDoc,@pmIdCia,@pmLDesde,@pmLHasta,@pmResolucion,@pmRangoNum,@pmFechaRes,@pmPrefijo,@pmNumero
,@pmNumManual,@pmIntLotes,@pmConfigFecha,@pmFormato,@pmTipoPapel,@pmOrientacion,@pmVistaPrevia,@pmVerSetup,@pmNumCopias,@pmFechaAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposDocCue @pmIdDoc VARCHAR(3),@pmIdClase INT
,@pmClaseCuenta VARCHAR(50),@pmTipoMov CHAR(1)
 AS 
 INSERT INTO Sys_TiposDocCue (IdDoc,IdClase,ClaseCuenta,TipoMov)
 VALUES (@pmIdDoc,@pmIdClase,@pmClaseCuenta,@pmTipoMov)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposEsc @pmIdTipoEsc VARCHAR(4),@pmTipoEscolta VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposEsc (IdTipoEsc,TipoEscolta,Inactivo)
 VALUES (@pmIdTipoEsc,@pmTipoEscolta,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposFil @pmIdFiltro VARCHAR(4),@pmTipoFiltro VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposFil (IdFiltro,TipoFiltro,Inactivo)
 VALUES (@pmIdFiltro,@pmTipoFiltro,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposFuel @pmIdCom VARCHAR(4),@pmTipoComb VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposFuel (IdCom,TipoComb,Inactivo)
 VALUES (@pmIdCom,@pmTipoComb,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposLla @pmIdTlla VARCHAR(4),@pmTipoLlanta VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposLla (IdTlla,TipoLlanta,Inactivo)
 VALUES (@pmIdTlla,@pmTipoLlanta,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposLub @pmIdLub VARCHAR(4),@pmTipoLub VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposLub (IdLub,TipoLub,Inactivo)
 VALUES (@pmIdLub,@pmTipoLub,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposMat @pmIdMat VARCHAR(4),@pmMaterial VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMat (IdMat,Material,Inactivo)
 VALUES (@pmIdMat,@pmMaterial,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposMcia @pmIdTmcia VARCHAR(4),@pmTipoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMcia (IdTmcia,TipoMcia,Inactivo)
 VALUES (@pmIdTmcia,@pmTipoMcia,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposMnjo @pmIdMnjo VARCHAR(4),@pmManejoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMnjo (IdMnjo,ManejoMcia,Inactivo)
 VALUES (@pmIdMnjo,@pmManejoMcia,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposMod @pmIdModd VARCHAR(4),@pmModalidad VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMod (IdModd,Modalidad,Inactivo)
 VALUES (@pmIdModd,@pmModalidad,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposMot @pmIdTipoMot VARCHAR(4),@pmTipoMotor VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposMot (IdTipoMot,TipoMotor,Inactivo)
 VALUES (@pmIdTipoMot,@pmTipoMotor,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposNat @pmIdNat VARCHAR(4),@pmNatlzaprod VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposNat (IdNat,Natlzaprod,Inactivo)
 VALUES (@pmIdNat,@pmNatlzaprod,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposNivs @pmIdNiv VARCHAR(4),@pmNivelServicio VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposNivs (IdNiv,NivelServicio,Inactivo)
 VALUES (@pmIdNiv,@pmNivelServicio,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposNom @pmIdNom VARCHAR(4),@pmTipoNomina VARCHAR(50),@pmAsistencia BIT,@pmNoDevenga BIT
,@pmTipoLiquida CHAR(1),@pmFechaActual SMALLDATETIME,@pmInactivo BIT,@pmCdCiaInt CHAR(2)
 AS 
 INSERT INTO TiposNom (IdNom,TipoNomina,Asistencia,NoDevenga,TipoLiquida,FechaActual,Inactivo,CdCiaInt )
 VALUES (@pmIdNom,@pmTipoNomina,@pmAsistencia,@pmNoDevenga,@pmTipoLiquida,@pmFechaActual,@pmInactivo,@pmCdCiaInt)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposNov @pmIdNov VARCHAR(4),@pmNovedad VARCHAR(50),@pmAdmon VARCHAR(3),@pmInactivo BIT
 AS 
 INSERT INTO TiposNov (IdNov,Novedad,Admon,Inactivo)
 VALUES (@pmIdNov,@pmNovedad,@pmAdmon,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposPcnto @pmIdTipoPre VARCHAR(4),@pmPrecinto VARCHAR(50),@pmInactivo BIT
 AS 
 INSERT INTO TiposPcnto (IdTipoPre,Precinto,Inactivo)
 VALUES (@pmIdTipoPre,@pmPrecinto,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposPpt @pmIdPpd VARCHAR(4),@pmTipoProp VARCHAR(50),@pmVehPropio BIT,@pmInactivo BIT
 AS 
 INSERT INTO TiposPpt (IdPpd,TipoProp,VehPropio,Inactivo)
 VALUES (@pmIdPpd,@pmTipoProp,@pmVehPropio,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsTiposTerc @pmIdTipoTerc VARCHAR(4),@pmTipoTercero VARCHAR(100),@pmInactivo BIT
 AS 
 INSERT INTO TiposTerc (IdTipoTerc,TipoTercero,Inactivo)
 VALUES (@pmIdTipoTerc,@pmTipoTercero,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTiposVeh @pmIdTipoVeh VARCHAR(4),@pmTipoVehiculo VARCHAR(50)
,@pmConfiguracion VARCHAR(6),@pmEjes INT,@pmLlantas INT,@pmInactivo BIT
 AS 
 INSERT INTO TiposVeh (IdTipoVeh,TipoVehiculo,Configuracion,Ejes,Llantas,Inactivo)
 VALUES (@pmIdTipoVeh,@pmTipoVehiculo,@pmConfiguracion,@pmEjes,@pmLlantas,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsTraslados @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16)
,@pmVrSubTotal MONEY,@pmCantidad DECIMAL(14,4),@pmNumDcmto INT,@pmNumOrden INT,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmModalidad VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmOrigenAdd VARCHAR(10),@pmTimeSys SMALLDATETIME
,@pmIdCiaCrea CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Traslados (TipDoc,Traslado,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,Cantidad,NumDcmto,NumOrden,IdCCosto,IdSubCos,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion
,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmTraslado,@pmIdCia,@pmFecha,@pmIdConcepto,@pmIdTercero,@pmVrSubTotal,@pmCantidad,@pmNumDcmto,@pmNumOrden,@pmIdCCosto,@pmIdSubCos,@pmModalidad,@pmOrigenAdd
,@pmTipCom,@pmComprobante,@pmIdCiaCom,@pmObservacion,@pmIdEstado,@pmTimeSys,@pmIdCiaCrea,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsUOpciones @pmIdUsuario VARCHAR(11),@pmNomOpcion VARCHAR(20),@pmValor VARCHAR(50)
 AS 
 INSERT INTO adm_UOpciones (IdUsuario,NomOpcion,Valor)
 VALUES (@pmIdUsuario,@pmNomOpcion,@pmValor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsUm @pmUndMed VARCHAR(10),@pmUnidad VARCHAR(50),@pmCarga BIT
 AS 
 INSERT INTO Sys_Um (UndMed,Unidad,Carga)
 VALUES (@pmUndMed,@pmUnidad,@pmCarga)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsUndMed @pmIdUnd VARCHAR(4),@pmUnidad VARCHAR(50),@pmIdMat VARCHAR(4),@pmIdEmp VARCHAR(4),@pmFormRecp VARCHAR(10),@pmClaseEmp VARCHAR(10),@pmDimAlto DECIMAL(14,4)
,@pmDimAncho DECIMAL(14,4),@pmDimLargo DECIMAL(14,4),@pmDimVolm DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmPesoEpq DECIMAL(14,4),@pmCapcMax DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmSimbUnd VARCHAR(10)
,@pmObserv VARCHAR(250),@pmInactivo BIT
 AS 
 INSERT INTO UndMed (IdUnd,Unidad,IdMat,IdEmp,FormRecp,ClaseEmp,DimAlto,DimAncho,DimLargo,DimVolm,UndMed,PesoEpq,CapcMax,UndCapc,SimbUnd,Observ,Inactivo)
 VALUES (@pmIdUnd,@pmUnidad,@pmIdMat,@pmIdEmp,@pmFormRecp,@pmClaseEmp,@pmDimAlto,@pmDimAncho,@pmDimLargo,@pmDimVolm,@pmUndMed,@pmPesoEpq
,@pmCapcMax,@pmUndCapc,@pmSimbUnd,@pmObserv,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsUsuarios @pmIdUsuario VARCHAR(11),@pmUsuario VARCHAR(50),@pmPwdLog VARCHAR(30),@pmIdGrupo INT,@pmIdEstacion CHAR(2),@pmIdCargo VARCHAR(4),@pmConectado BIT,@pmFechaAcc SMALLDATETIME
,@pmFechaClave SMALLDATETIME,@pmCaducidad INT,@pmLoginSQL VARCHAR(20),@pmPwdSQL VARCHAR(20),@pmModoSQL VARCHAR(3),@pmInactivo BIT
 AS 
 INSERT INTO adm_Usuarios (IdUsuario,Usuario,PwdLog,IdGrupo,IdEstacion,IdCargo,Conectado,FechaAcc,FechaClave,Caducidad,LoginSQL,PwdSQL,ModoSQL,Inactivo)
 VALUES (@pmIdUsuario,@pmUsuario,@pmPwdLog,@pmIdGrupo,@pmIdEstacion,@pmIdCargo,@pmConectado,@pmFechaAcc,@pmFechaClave,@pmCaducidad,@pmLoginSQL
,@pmPwdSQL,@pmModoSQL,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsUsucaja @pmIdUsuario VARCHAR(11),@pmnFormato VARCHAR(10),@pmCdCajero VARCHAR(11),@pmIdConcepto VARCHAR(4),@pmCdBodega VARCHAR(4)
,@pmLtaPrec CHAR(1),@pmIdVend VARCHAR(16),@pmCdCms VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmAbreCajon VARCHAR(10),@pmFechaAdd SMALLDATETIME
 AS 
 INSERT INTO adm_Usucaja (IdUsuario,nFormato,CdCajero,IdConcepto,CdBodega,LtaPrec,IdVend,CdCms,CdCCosto,CdSubCos,AbreCajon,FechaAdd)
 VALUES (@pmIdUsuario,@pmnFormato,@pmCdCajero,@pmIdConcepto,@pmCdBodega,@pmLtaPrec,@pmIdVend,@pmCdCms,@pmCdCCosto,@pmCdSubCos,@pmAbreCajon,@pmFechaAdd)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmFecha SMALLDATETIME
,@pmHoraServ VARCHAR(10),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmNumMovil VARCHAR(10),@pmCentCosto VARCHAR(50),@pmUnidades DECIMAL(14,4),@pmHoraRec DECIMAL(14,4)
,@pmHoraEspera DECIMAL(14,4),@pmFactUnid BIT,@pmFactRutas BIT,@pmFactHoras BIT,@pmVrPago MONEY,@pmVrCobro MONEY,@pmPlacaVeh VARCHAR(10),@pmCdConductor VARCHAR(16),@pmNomConductor VARCHAR(150)
,@pmCantPasaj INT,@pmUsuarios VARCHAR(1000),@pmTipFac VARCHAR(3),@pmNumFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFac SMALLDATETIME,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(1000),@pmFechaUp SMALLDATETIME,@pmCdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_Vales (TipDoc,nRadica,IdCia,Item,TipoRango,IdRango,NumSerie,NumForma,Fecha,HoraServ,IdCliente,IdAgencia,IdCuenta,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,PlacaVeh
,CdConductor,NomConductor,CantPasaj,Usuarios,TipFac,NumFactura,IdCiaFac,FechaFac,Anulado,NumDev,FecDev,Observacion,FechaUp,CdUsuario)
 VALUES (@pmTipDoc,@pmnRadica,@pmIdCia,@pmItem,@pmTipoRango,@pmIdRango,@pmNumSerie,@pmNumForma,@pmFecha,@pmHoraServ,@pmIdCliente,@pmIdAgencia,@pmIdCuenta,@pmNumMovil,@pmCentCosto,@pmUnidades
,@pmHoraRec,@pmHoraEspera,@pmFactUnid,@pmFactRutas,@pmFactHoras,@pmVrPago,@pmVrCobro,@pmPlacaVeh,@pmCdConductor,@pmNomConductor,@pmCantPasaj,@pmUsuarios,@pmTipFac,@pmNumFactura,@pmIdCiaFac
,@pmFechaFac,@pmAnulado,@pmNumDev,@pmFecDev,@pmObservacion,@pmFechaUp,@pmCdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsValesRutas @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdRuta VARCHAR(4)
,@pmTarifPago MONEY,@pmTarifCobro MONEY,@pmDescripRuta VARCHAR(50)
 AS 
 INSERT INTO Trn_ValesRutas (TipDoc,nRadica,IdCia,Item,IdRuta,TarifPago,TarifCobro,DescripRuta)
 VALUES (@pmTipDoc,@pmnRadica,@pmIdCia,@pmItem,@pmIdRuta,@pmTarifPago,@pmTarifCobro,@pmDescripRuta)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehClientes @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmIdTipoVeh VARCHAR(4),@pmIdMarca VARCHAR(4),@pmLinMarca VARCHAR(30),@pmColorVeh VARCHAR(30),@pmIdTipoMot VARCHAR(4),@pmModelo VARCHAR(4),@pmClaseMat VARCHAR(10)
,@pmCilind DECIMAL(14,4),@pmIdCom VARCHAR(4),@pmIdLub VARCHAR(4),@pmIdMarLub VARCHAR(4),@pmIdTlla VARCHAR(4),@pmIdMarlla VARCHAR(4),@pmIdFiltro VARCHAR(4),@pmIdMarFil VARCHAR(4),@pmNumSoat VARCHAR(30),@pmFecSoat SMALLDATETIME,@pmVigSoat SMALLDATETIME
,@pmNitEmpSoat VARCHAR(16),@pmRevTecMec VARCHAR(30),@pmFecTecMec SMALLDATETIME,@pmVigTecMec SMALLDATETIME,@pmCertGases VARCHAR(30),@pmFecCertGas SMALLDATETIME,@pmVigCertGas SMALLDATETIME,@pmKmInicial INT,@pmKmActual INT,@pmNitCliente VARCHAR(16)
,@pmCondPago VARCHAR(10),@pmNumCredito VARCHAR(30),@pmObservacion VARCHAR(250),@pmPathFoto VARCHAR(30),@pmFecPriServ SMALLDATETIME,@pmFecUltServ SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO VehClientes (IdVehiculo,NumVeh,IdTipoVeh,IdMarca,LinMarca,ColorVeh,IdTipoMot,Modelo,ClaseMat,Cilind,IdCom,IdLub,IdMarLub,IdTlla,IdMarlla,IdFiltro,IdMarFil,NumSoat,FecSoat,VigSoat,NitEmpSoat,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,KmInicial,KmActual,NitCliente,CondPago
,NumCredito,Observacion,PathFoto,FecPriServ,FecUltServ,IdEstado,Inactivo,FechaAdd,IdUsuario)
 VALUES (@pmIdVehiculo,@pmNumVeh,@pmIdTipoVeh,@pmIdMarca,@pmLinMarca,@pmColorVeh,@pmIdTipoMot,@pmModelo,@pmClaseMat,@pmCilind,@pmIdCom,@pmIdLub,@pmIdMarLub,@pmIdTlla,@pmIdMarlla,@pmIdFiltro,@pmIdMarFil,@pmNumSoat,@pmFecSoat,@pmVigSoat,@pmNitEmpSoat,@pmRevTecMec
,@pmFecTecMec,@pmVigTecMec,@pmCertGases,@pmFecCertGas,@pmVigCertGas,@pmKmInicial,@pmKmActual,@pmNitCliente,@pmCondPago,@pmNumCredito,@pmObservacion,@pmPathFoto,@pmFecPriServ,@pmFecUltServ,@pmIdEstado,@pmInactivo,@pmFechaAdd,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehConductor @pmIdVehiculo VARCHAR(10),@pmNumero INT,@pmFecha SMALLDATETIME,@pmIdPropietario VARCHAR(16)
,@pmIdConductor VARCHAR(16),@pmIdCondNuevo VARCHAR(16),@pmObservacion VARCHAR(150),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_VehConductor (IdVehiculo,Numero,Fecha,IdPropietario,IdConductor,IdCondNuevo,Observacion,TimeSys,IdUsuario)
 VALUES (@pmIdVehiculo,@pmNumero,@pmFecha,@pmIdPropietario,@pmIdConductor,@pmIdCondNuevo,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehLecturas @pmTipoLec VARCHAR(3),@pmNumero INT,@pmFecha SMALLDATETIME,@pmpVehiculo VARCHAR(10),@pmKmtAnterior INT,@pmKmtActual INT,@pmKm2Anterior INT
,@pmKm2Actual INT,@pmCdRemque VARCHAR(10),@pmKmRemqAnt INT,@pmKmRemqActual INT,@pmOrigenLect VARCHAR(10),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmCiaDoc CHAR(2),@pmObservacion VARCHAR(150),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_VehLecturas (TipoLec,Numero,Fecha,pVehiculo,KmtAnterior,KmtActual,Km2Anterior,Km2Actual,CdRemque,KmRemqAnt,KmRemqActual,OrigenLect,TipDoc,Documento,CiaDoc,Observacion,TimeSys,IdUsuario)
 VALUES (@pmTipoLec,@pmNumero,@pmFecha,@pmpVehiculo,@pmKmtAnterior,@pmKmtActual,@pmKm2Anterior,@pmKm2Actual,@pmCdRemque,@pmKmRemqAnt,@pmKmRemqActual,@pmOrigenLect,@pmTipDoc
,@pmDocumento,@pmCiaDoc,@pmObservacion,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehPolizas @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmNumero VARCHAR(30),@pmFecEmision SMALLDATETIME
,@pmFecVigencia SMALLDATETIME,@pmNitCompania VARCHAR(16),@pmValor MONEY,@pmComntarios VARCHAR(100),@pmDiasAviso INT
 AS 
 INSERT INTO VehPolizas (IdVehiculo,IdClase,Numero,FecEmision,FecVigencia,NitCompania,Valor,Comntarios,DiasAviso)
 VALUES (@pmIdVehiculo,@pmIdClase,@pmNumero,@pmFecEmision,@pmFecVigencia,@pmNitCompania,@pmValor,@pmComntarios,@pmDiasAviso)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehRemolq @pmIdRemque VARCHAR(10),@pmIdMarca VARCHAR(4),@pmIdColor VARCHAR(4),@pmIdCrceria VARCHAR(4),@pmModelo VARCHAR(4),@pmNEjes INT,@pmnLlantas INT,@pmAltura DECIMAL(14,4)
,@pmAncho DECIMAL(14,4),@pmLargo DECIMAL(14,4),@pmVlumen DECIMAL(14,4),@pmPesoKg DECIMAL(14,4),@pmCapneta DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmNumComp INT,@pmCapcComp VARCHAR(50),@pmNumSerial VARCHAR(30)
,@pmIdPropietario VARCHAR(16),@pmNitProv VARCHAR(16),@pmFecCompra SMALLDATETIME,@pmVidaUtil INT,@pmVrComercial MONEY,@pmVrAvaludo MONEY,@pmVrAsegurado MONEY,@pmKilmetraje INT,@pmEquipoAdic VARCHAR(50)
,@pmDescripcion VARCHAR(100),@pmCdCenSer VARCHAR(4),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTarjetaProp VARCHAR(30),@pmFecTarjProp  SMALLDATETIME
,@pmVigTarjProp SMALLDATETIME,@pmConfEjes VARCHAR(5),@pmFecAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO VehRemolq (IdRemque,IdMarca,IdColor,IdCrceria,Modelo,NEjes,nLlantas,ConfEjes,Altura,Ancho,Largo,Vlumen,PesoKg,Capneta,UndCapc,NumComp,CapcComp,NumSerial,IdPropietario,NitProv,FecCompra,VidaUtil,VrComercial,VrAvaludo,VrAsegurado,Kilmetraje
,EquipoAdic,Descripcion,CdCenSer,CdLocal,Ubicacion,Observacion,IdEstado,Inactivo,FecAdd,IdUsuario,TarjetaProp,FecTarjProp,VigTarjProp)
 VALUES (@pmIdRemque,@pmIdMarca,@pmIdColor,@pmIdCrceria,@pmModelo,@pmNEjes,@pmnLlantas,@pmConfEjes,@pmAltura,@pmAncho,@pmLargo,@pmVlumen,@pmPesoKg,@pmCapneta,@pmUndCapc,@pmNumComp,@pmCapcComp,@pmNumSerial,@pmIdPropietario
,@pmNitProv,@pmFecCompra,@pmVidaUtil,@pmVrComercial,@pmVrAvaludo,@pmVrAsegurado,@pmKilmetraje,@pmEquipoAdic,@pmDescripcion,@pmCdCenSer,@pmCdLocal,@pmUbicacion,@pmObservacion,@pmIdEstado,@pmInactivo,@pmFecAdd,@pmIdUsuario,@pmTarjetaProp,@pmFecTarjProp,@pmVigTarjProp)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehTraspasos @pmIdVehiculo VARCHAR(10),@pmNumero INT,@pmFecha SMALLDATETIME,@pmIdPpd VARCHAR(4),@pmIdPropietario VARCHAR(16)
,@pmIdPoseedor VARCHAR(16),@pmIdPropNuevo VARCHAR(16),@pmIdPoseeNuevo VARCHAR(16),@pmIdNewPpd VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmVrComercial MONEY
,@pmVrCupo MONEY,@pmVrAsegurado MONEY,@pmObservacion VARCHAR(150),@pmTipoAfil VARCHAR(10),@pmTipoAfilNew VARCHAR(10),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Trn_VehTraspasos (IdVehiculo,Numero,Fecha,IdPpd,IdPropietario,IdPoseedor,IdPropNuevo,IdPoseeNuevo,IdNewPpd,IdGrupo,VrComercial,VrCupo,VrAsegurado,Observacion,TipoAfil,TipoAfilNew,TimeSys,IdUsuario)
 VALUES (@pmIdVehiculo,@pmNumero,@pmFecha,@pmIdPpd,@pmIdPropietario,@pmIdPoseedor,@pmIdPropNuevo,@pmIdPoseeNuevo,@pmIdNewPpd,@pmIdGrupo,@pmVrComercial,@pmVrCupo
,@pmVrAsegurado,@pmObservacion,@pmTipoAfil,@pmTipoAfilNew,@pmTimeSys,@pmIdUsuario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInsVeh_Contrato @pmNumContrato INT,@pmIdVehiculo VARCHAR(10),@pmIdAdmon VARCHAR(4),@pmIdTipCon VARCHAR(4)
,@pmFecIngreso SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmObservaciones VARCHAR(250)
,@pmNumPagare VARCHAR(20),@pmArchPagare VARCHAR(50),@pmFecUpdate SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Veh_Contrato (NumContrato,IdVehiculo,IdAdmon,IdTipCon,FecIngreso,FecRetiro,FecVigencia,Observaciones,NumPagare,ArchPagare,FecUpdate,IdUsuario)
 VALUES (@pmNumContrato,@pmIdVehiculo,@pmIdAdmon,@pmIdTipCon,@pmFecIngreso,@pmFecRetiro,@pmFecVigencia,@pmObservaciones
,@pmNumPagare,@pmArchPagare,@pmFecUpdate,@pmIdUsuario)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsVeh_Cupos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmValorCupo MONEY,@pmValorSaldo MONEY,@pmFecUpdate SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 INSERT INTO Veh_Cupos (IdVehiculo,IdClase,ValorCupo,ValorSaldo,FecUpdate,IdUsuario)
 VALUES (@pmIdVehiculo,@pmIdClase,@pmValorCupo,@pmValorSaldo,@pmFecUpdate,@pmIdUsuario)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInsVeh_Saldos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmIdCia CHAR(2),@pmValorSaldo MONEY
 AS 
 INSERT INTO Veh_Saldos (IdVehiculo,IdClase,IdCia,ValorSaldo)
 VALUES (@pmIdVehiculo,@pmIdClase,@pmIdCia,@pmValorSaldo)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehiculos @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmClaseVeh VARCHAR(10),@pmIdTipoVeh VARCHAR(4),@pmIdMarca VARCHAR(4),@pmIdLinea VARCHAR(10),@pmIdColor VARCHAR(4),@pmIdTipoMot VARCHAR(4),@pmIdCrceria VARCHAR(4),@pmModelo VARCHAR(4),@pmFecRep SMALLDATETIME,@pmConfig VARCHAR(5),@pmVehArtic BIT,@pmNumLlan INT,@pmNumLlans INT,@pmIdCat VARCHAR(4),@pmCdCatv VARCHAR(4)
,@pmClaseMat VARCHAR(10),@pmCilind DECIMAL(14,4),@pmCapTanq DECIMAL(14,4),@pmIdCom VARCHAR(4),@pmIdLub VARCHAR(4),@pmIdTlla VARCHAR(4),@pmIdMarlla VARCHAR(4),@pmPesoVacio DECIMAL(14,4),@pmPesoMax DECIMAL(14,4),@pmNumMotor VARCHAR(30),@pmSerieChasis VARCHAR(30),@pmNumSerie VARCHAR(30),@pmCdRemque VARCHAR(10),@pmLongitud DECIMAL(14,4),@pmCarrAlto DECIMAL(14,4),@pmCarrAncho DECIMAL(14,4),@pmCarrLargo DECIMAL(14,4)
,@pmCarrCapac DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmComptmtos INT,@pmCapComp VARCHAR(50),@pmPasjerosPie INT,@pmPasjerosSen INT,@pmNitEmpresa VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmIdPpd VARCHAR(4),@pmAdquisc VARCHAR(10),@pmNitProv VARCHAR(16),@pmFecCompra SMALLDATETIME,@pmVrComcial MONEY,@pmVrAseg MONEY,@pmVrAvaludo MONEY,@pmVidaUtil INT
,@pmFecSalida SMALLDATETIME,@pmNContrato INT,@pmIdAdmon VARCHAR(4),@pmIdNiv VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmCdGrupR VARCHAR(4),@pmCdTarifa VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmNumSoat VARCHAR(30),@pmFecSoat SMALLDATETIME,@pmVigSoat SMALLDATETIME,@pmNitEmpSoat VARCHAR(16),@pmTarjProp VARCHAR(30),@pmFecTProp SMALLDATETIME
,@pmVigTProp SMALLDATETIME,@pmCdLugTp VARCHAR(8),@pmUlttramite VARCHAR(150),@pmRespCivil VARCHAR(30),@pmFecRCivil SMALLDATETIME,@pmVigRCivil SMALLDATETIME,@pmRegNalCarga VARCHAR(30),@pmFecRegNal SMALLDATETIME,@pmVigRegNal SMALLDATETIME,@pmKmInicial INT,@pmKmActual INT,@pmKm2Actual INT,@pmRegtradora BIT,@pmCentInicial INT,@pmCentFinal INT,@pmVrLmtCred MONEY,@pmVrSaldoAct MONEY,@pmDescripcion VARCHAR(100)
,@pmObservacion VARCHAR(250),@pmCdCenSer VARCHAR(4),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100),@pmPathFoto VARCHAR(30),@pmFecPriServ SMALLDATETIME,@pmFecUltServ SMALLDATETIME,@pmFecUltAcc SMALLDATETIME,@pmTieneAcc BIT,@pmFecPagImp SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmTipoAfil VARCHAR(10),@pmRevTecMec VARCHAR(30),@pmFecTecMec SMALLDATETIME,@pmVigTecMec SMALLDATETIME,@pmCertGases VARCHAR(30),@pmFecCertGas SMALLDATETIME,@pmVigCertGas SMALLDATETIME,@pmTarjOper VARCHAR(30),@pmFecTarjOper SMALLDATETIME,@pmVigTarjOper SMALLDATETIME,@pmFechaAdd SMALLDATETIME,@pmIdUsuario VARCHAR(11)
,@pmValorCupo MONEY,@pmObligaTProd BIT,@pmGarantiaAcc BIT,@pmDocCompleta BIT,@pmCertMovilizacion VARCHAR(20),@pmFecCertMovil SMALLDATETIME,@pmVigCertMovil SMALLDATETIME,@pmCdRutaHab VARCHAR(4)
 AS 
 INSERT INTO Vehiculos (IdVehiculo,NumVeh,ClaseVeh,IdTipoVeh,IdMarca,IdLinea,IdColor,IdTipoMot,IdCrceria,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans,IdCat,CdCatv,ClaseMat,Cilind,CapTanq,IdCom,IdLub,IdTlla,IdMarlla,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie,PasjerosSen,NitEmpresa,IdPropietario,IdPoseedor,IdConductor,IdPpd,Adquisc,NitProv,FecCompra,VrComcial,VrAseg
,VrAvaludo,VidaUtil,FecSalida,NContrato,IdAdmon,IdNiv,IdGrupo,CdGrupR,CdTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,TarjProp,FecTProp,VigTProp,CdLugTp,Ulttramite,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,Regtradora,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,Descripcion,Observacion,CdCenSer,CdLocal,Ubicacion,PathFoto,FecPriServ,FecUltServ,FecUltAcc,TieneAcc,FecPagImp,IdEstado,Inactivo
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,FechaAdd,IdUsuario,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil,CdRutaHab)
 VALUES (@pmIdVehiculo,@pmNumVeh,@pmClaseVeh,@pmIdTipoVeh,@pmIdMarca,@pmIdLinea,@pmIdColor,@pmIdTipoMot,@pmIdCrceria,@pmModelo,@pmFecRep,@pmConfig,@pmVehArtic,@pmNumLlan,@pmNumLlans,@pmIdCat,@pmCdCatv,@pmClaseMat,@pmCilind,@pmCapTanq,@pmIdCom,@pmIdLub,@pmIdTlla,@pmIdMarlla,@pmPesoVacio,@pmPesoMax,@pmNumMotor,@pmSerieChasis,@pmNumSerie,@pmCdRemque,@pmLongitud,@pmCarrAlto,@pmCarrAncho,@pmCarrLargo,@pmCarrCapac
,@pmUndCapc,@pmComptmtos,@pmCapComp,@pmPasjerosPie,@pmPasjerosSen,@pmNitEmpresa,@pmIdPropietario,@pmIdPoseedor,@pmIdConductor,@pmIdPpd,@pmAdquisc,@pmNitProv,@pmFecCompra,@pmVrComcial,@pmVrAseg,@pmVrAvaludo,@pmVidaUtil,@pmFecSalida,@pmNContrato,@pmIdAdmon,@pmIdNiv,@pmIdGrupo,@pmCdGrupR,@pmCdTarifa,@pmFecIngreso,@pmFecVigencia,@pmFecRetiro,@pmNumSoat,@pmFecSoat,@pmVigSoat,@pmNitEmpSoat,@pmTarjProp,@pmFecTProp
,@pmVigTProp,@pmCdLugTp,@pmUlttramite,@pmRespCivil,@pmFecRCivil,@pmVigRCivil,@pmRegNalCarga,@pmFecRegNal,@pmVigRegNal,@pmKmInicial,@pmKmActual,@pmKm2Actual,@pmRegtradora,@pmCentInicial,@pmCentFinal,@pmVrLmtCred,@pmVrSaldoAct,@pmDescripcion,@pmObservacion,@pmCdCenSer,@pmCdLocal,@pmUbicacion,@pmPathFoto,@pmFecPriServ,@pmFecUltServ,@pmFecUltAcc,@pmTieneAcc,@pmFecPagImp,@pmIdEstado,@pmInactivo
,@pmTipoAfil,@pmRevTecMec,@pmFecTecMec,@pmVigTecMec,@pmCertGases,@pmFecCertGas,@pmVigCertGas,@pmTarjOper,@pmFecTarjOper,@pmVigTarjOper,@pmFechaAdd,@pmIdUsuario,@pmValorCupo,@pmObligaTProd,@pmGarantiaAcc,@pmDocCompleta,@pmCertMovilizacion,@pmFecCertMovil,@pmVigCertMovil,@pmCdRutaHab)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVehiculos_Sel @pmIdVehiculo VARCHAR(10),@pmNewVehiculo VARCHAR(10)
 AS 
 INSERT INTO Vehiculos (IdVehiculo,NumVeh,ClaseVeh,IdTipoVeh,IdMarca,IdLinea,IdColor,IdTipoMot,IdCrceria,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans,IdCat,CdCatv,ClaseMat,Cilind,CapTanq,IdCom,IdLub,IdTlla,IdMarlla,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie,PasjerosSen,NitEmpresa,IdPropietario,IdPoseedor,IdConductor,IdPpd,Adquisc,NitProv,FecCompra,VrComcial,VrAseg
,VrAvaludo,VidaUtil,FecSalida,NContrato,IdAdmon,IdNiv,IdGrupo,CdGrupR,CdTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,TarjProp,FecTProp,VigTProp,CdLugTp,Ulttramite,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,Regtradora,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,Descripcion,Observacion,CdCenSer,CdLocal,Ubicacion,PathFoto,FecPriServ,FecUltServ,FecUltAcc,TieneAcc,FecPagImp,IdEstado,Inactivo
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,FechaAdd,IdUsuario,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil,CdRutaHab)
SELECT  @pmNewVehiculo,NumVeh,ClaseVeh,IdTipoVeh,IdMarca,IdLinea,IdColor,IdTipoMot,IdCrceria,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans,IdCat,CdCatv,ClaseMat,Cilind,CapTanq,IdCom,IdLub,IdTlla,IdMarlla,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie,PasjerosSen,NitEmpresa,IdPropietario,IdPoseedor,IdConductor,IdPpd,Adquisc,NitProv,FecCompra,VrComcial,VrAseg
,VrAvaludo,VidaUtil,FecSalida,NContrato,IdAdmon,IdNiv,IdGrupo,CdGrupR,CdTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,TarjProp,FecTProp,VigTProp,CdLugTp,Ulttramite,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,Regtradora,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,Descripcion,Observacion,CdCenSer,CdLocal,Ubicacion,PathFoto,FecPriServ,FecUltServ,FecUltAcc,TieneAcc,FecPagImp,IdEstado,Inactivo
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,FechaAdd,IdUsuario,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil,CdRutaHab
 FROM Vehiculos WHERE  IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVencAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmItemFac INT
,@pmVrAbono MONEY,@pmVrDescto MONEY,@pmVrBaseCms MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmComision DECIMAL(14,4),@pmDetalle VARCHAR(150)
,@pmFecPago SMALLDATETIME,@pmTipoAplica VARCHAR(10),@pmTimeSys SMALLDATETIME
 AS 
 INSERT INTO Trn_VencAbonos (TipRec,Recibo,IdCia,Item,Fecha,TipDoc,Factura,IdCiaFac,ItemFac,VrAbono,VrDescto,VrBaseCms,IdCliente,IdAgencia,IdVend,Comision,Detalle,FecPago,TipoAplica,TimeSys)
 VALUES (@pmTipRec,@pmRecibo,@pmIdCia,@pmItem,@pmFecha,@pmTipDoc,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmVrAbono,@pmVrDescto,@pmVrBaseCms,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmComision,@pmDetalle,@pmFecPago,@pmTipoAplica,@pmTimeSys)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsVencimientos @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrFactura MONEY,@pmVrAbonado MONEY
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmComision DECIMAL(14,4),@pmTarifDcto DECIMAL(14,4),@pmTipoDcto CHAR(1),@pmTipoBase VARCHAR(10),@pmFecLmtDcto SMALLDATETIME
,@pmIdConcepto VARCHAR(4),@pmReferencia VARCHAR(50),@pmDetalle VARCHAR(150),@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmFecUltPago SMALLDATETIME,@pmNumNota INT,@pmIdCiaNot CHAR(2)
,@pmTarifInt DECIMAL(14,4),@pmFecLiqMora SMALLDATETIME,@pmFecPlazoCal SMALLDATETIME
 AS 
 INSERT INTO Trn_Vencimientos (TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,IdCliente,IdAgencia,IdVend,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto,Referencia,Detalle
,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,FecPlazoCal)
 VALUES (@pmTipDoc,@pmFactura,@pmIdCia,@pmItem,@pmFecha,@pmFechaVence,@pmVrFactura,@pmVrAbonado,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmComision,@pmTarifDcto,@pmTipoDcto
,@pmTipoBase,@pmFecLmtDcto,@pmIdConcepto,@pmReferencia,@pmDetalle,@pmTipRef,@pmDocRef,@pmIdCiaRef,@pmFecUltPago,@pmNumNota,@pmIdCiaNot,@pmTarifInt,@pmFecLiqMora,@pmFecPlazoCal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsZonas @pmIdZona VARCHAR(4),@pmZona VARCHAR(50),@pmCdSubCos VARCHAR(16),@pmInactivo BIT
 AS 
 INSERT INTO Zonas (IdZona,Zona,CdSubCos,Inactivo)
 VALUES (@pmIdZona,@pmZona,@pmCdSubCos,@pmInactivo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInsmonedas @pmIdMneda VARCHAR(5),@pmMneda VARCHAR(50),@pmSimbolo VARCHAR(10),@pmMneLocal BIT
,@pmVrCambio DECIMAL(12,6),@pmInactivo BIT
 AS 
 INSERT INTO adm_monedas (IdMneda,Mneda,Simbolo,MneLocal,VrCambio,Inactivo)
 VALUES (@pmIdMneda,@pmMneda,@pmSimbolo,@pmMneLocal,@pmVrCambio,@pmInactivo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstasas @pmIdMneda VARCHAR(5),@pmIdMnedaD VARCHAR(5),@pmFecha SMALLDATETIME,@pmTasaDia DECIMAL(14,6)
 AS 
 INSERT INTO Trn_tasas (IdMneda,IdMnedaD,Fecha,TasaDia)
 VALUES (@pmIdMneda,@pmIdMnedaD,@pmFecha,@pmTasaDia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfAmt @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdDiferido VARCHAR(30),@pmtmCostoIni MONEY,@pmtmAdiciones MONEY,@pmtmDisminucion MONEY,@pmtmAcuAjustes MONEY,@pmtmAmortizaHis MONEY
,@pmtmAmortizaMes MONEY,@pmtmAjustesInf MONEY,@pmtmAjustesAmtza MONEY,@pmtmAmortizaAcum MONEY,@pmtmAjuAmtzaAcum MONEY,@pmtmCorrMonAcum MONEY,@pmtmBaseCorrMon MONEY,@pmtmCorrMonDif MONEY,@pmtmAmtzaHisAcum MONEY,@pmtmTarifa DECIMAL(14,4)
,@pmtmIdCia CHAR(2),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmCdDep VARCHAR(4),@pmtmNitRespons VARCHAR(16),@pmtmNumPeriodos INT,@pmtmFechaIni SMALLDATETIME,@pmtmEstadoReg INT,@pmtmObservacion VARCHAR(150)
,@pmtmCuota DECIMAL(14,4),@pmtmGalones DECIMAL(14,4),@pmtmNitProvee VARCHAR(16),@pmtmInversion MONEY
 AS 
 INSERT INTO tm_AfAmt (tmNumero,tmItem,tmIdDiferido,tmCostoIni,tmAdiciones,tmDisminucion,tmAcuAjustes,tmAmortizaHis,tmAmortizaMes,tmAjustesInf,tmAjustesAmtza,tmAmortizaAcum,tmAjuAmtzaAcum,tmCorrMonAcum,tmBaseCorrMon,tmCorrMonDif
,tmAmtzaHisAcum,tmTarifa,tmIdCia,tmCdCCosto,tmCdSubCos,tmCdDep,tmNitRespons,tmNumPeriodos,tmFechaIni,tmEstadoReg,tmObservacion,tmCuota,tmGalones,tmNitProvee,tmInversion)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdDiferido,@pmtmCostoIni,@pmtmAdiciones,@pmtmDisminucion,@pmtmAcuAjustes,@pmtmAmortizaHis,@pmtmAmortizaMes,@pmtmAjustesInf,@pmtmAjustesAmtza,@pmtmAmortizaAcum,@pmtmAjuAmtzaAcum
,@pmtmCorrMonAcum,@pmtmBaseCorrMon,@pmtmCorrMonDif,@pmtmAmtzaHisAcum,@pmtmTarifa,@pmtmIdCia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmCdDep,@pmtmNitRespons,@pmtmNumPeriodos,@pmtmFechaIni,@pmtmEstadoReg,@pmtmObservacion,@pmtmCuota,@pmtmGalones,@pmtmNitProvee,@pmtmInversion)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfAmt_Sel @pmtmNumero VARCHAR(5),@pmNumero INT
 AS 
 INSERT INTO tm_AfAmt (tmNumero,tmItem,tmIdDiferido,tmCostoIni,tmAdiciones,tmDisminucion,tmAcuAjustes,tmAmortizaHis,tmAmortizaMes,tmAjustesInf,tmAjustesAmtza,tmAmortizaAcum
,tmAjuAmtzaAcum,tmCorrMonAcum,tmBaseCorrMon,tmCorrMonDif,tmAmtzaHisAcum,tmTarifa,tmIdCia,tmCdCCosto,tmCdSubCos,tmCdDep,tmNitRespons,tmNumPeriodos,tmFechaIni
,tmEstadoReg,tmObservacion,tmCuota,tmGalones,tmNitProvee,tmInversion)
 SELECT @pmtmNumero,Item,DT.IdDiferido,DT.ValorTotal,DT.VrAdiciones,DT.VrDisminucion,VrAcuAjustes,VrAmortizaHis,VrAmortizaMes,VrAjustesInf,VrAjustesAmtza,VrAmortizaAcum,VrAjuAmtzaAcum
,VrCorrMonAcum,VrBaseCorrMon,VrCorrMonDif,VrAmtzaHisAcum,TarifaAju,DT.IdCia,CdCCosto,CdSubCos,CdDep,NitRespons,NPeriodos,FecInicioAmtza,1,'',CuotaGal,VentaGal,IdProv,VrInversion
 FROM Trn_AfAmtDetalle AS DT INNER JOIN Diferidos AS D ON DT.IdDiferido=D.IdDiferido
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfDep @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdActivo VARCHAR(30),@pmtmCostoIni MONEY,@pmtmMejoras MONEY
,@pmtmDisminucion MONEY,@pmtmAcuAjustes MONEY,@pmtmDepreciaHis MONEY,@pmtmDepreciaMes MONEY,@pmtmAjustesInf MONEY,@pmtmAjustesDep MONEY
,@pmtmDepreciaAcum MONEY,@pmtmAjuDepAcum MONEY,@pmtmTarifa DECIMAL(14,4),@pmtmIdCia CHAR(2),@pmtmCdCCosto VARCHAR(16)
,@pmtmCdSubCos VARCHAR(16),@pmtmCdInstala VARCHAR(4),@pmtmCdDep VARCHAR(4),@pmtmNitRespons VARCHAR(16),@pmtmVidaUtil INT
,@pmtmMetodo VARCHAR(10),@pmtmFechaIni SMALLDATETIME,@pmtmEstadoReg INT,@pmtmObservacion VARCHAR(150)
 AS 
 INSERT INTO tm_AfDep (tmNumero,tmItem,tmIdActivo,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepreciaHis,tmDepreciaMes,tmAjustesInf,tmAjustesDep,tmDepreciaAcum,tmAjuDepAcum,tmTarifa
,tmIdCia,tmCdCCosto,tmCdSubCos,tmCdInstala,tmCdDep,tmNitRespons,tmVidaUtil,tmMetodo,tmFechaIni,tmEstadoReg,tmObservacion)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmCostoIni,@pmtmMejoras,@pmtmDisminucion,@pmtmAcuAjustes,@pmtmDepreciaHis,@pmtmDepreciaMes,@pmtmAjustesInf,@pmtmAjustesDep
,@pmtmDepreciaAcum,@pmtmAjuDepAcum,@pmtmTarifa,@pmtmIdCia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmCdInstala,@pmtmCdDep,@pmtmNitRespons,@pmtmVidaUtil,@pmtmMetodo,@pmtmFechaIni,@pmtmEstadoReg,@pmtmObservacion)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfDep_Sel @pmtmNumero VARCHAR(5),@pmNumero INT
 AS 
 INSERT INTO tm_AfDep (tmNumero,tmItem,tmIdActivo,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepreciaHis,tmDepreciaMes,tmAjustesInf,tmAjustesDep,tmDepreciaAcum,tmAjuDepAcum,tmTarifa
,tmIdCia,tmCdCCosto,tmCdSubCos,tmCdInstala,tmCdDep,tmNitRespons,tmVidaUtil,tmMetodo,tmFechaIni,tmEstadoReg,tmObservacion)
 SELECT @pmtmNumero,Item,IdActivo,VrCostoIni,VrMejoras,VrDisminucion,VrAcuAjustes,VrDepreciaHis,VrDepreciaMes,VrAjustesInf,VrAjustesDep,VrDepreciaAcum,VrAjuDepAcum,TarifaAju
,IdCia,CdCCosto,CdSubCos,CdInstala,CdDep,NitRespons,MesesVida,MetodoDeprec,FecIniDep,0,''
 FROM Trn_AfDetalle
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfIngreso @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdActivo VARCHAR(30),@pmtmFecha SMALLDATETIME
,@pmtmVrMejoras MONEY,@pmtmVrDisminucion MONEY,@pmtmVrAjuInfAcum MONEY,@pmtmVrDepAcuHis MONEY,@pmtmVrDepAcuAju MONEY
,@pmtmVrAjuInfDep MONEY,@pmtmNumMeses INT,@pmtmVidaUtil INT,@pmtmBaseDep MONEY,@pmtmMetodo VARCHAR(10),@pmtmFecUltDep SMALLDATETIME,@pmtmConcepto VARCHAR(150)
 AS 
 INSERT INTO tm_AfIngreso (tmNumero,tmItem,tmIdActivo,tmFecha,tmVrMejoras,tmVrDisminucion,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses
,tmVidaUtil,tmBaseDep,tmMetodo,tmFecUltDep,tmConcepto)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmFecha,@pmtmVrMejoras,@pmtmVrDisminucion,@pmtmVrAjuInfAcum,@pmtmVrDepAcuHis,@pmtmVrDepAcuAju
,@pmtmVrAjuInfDep,@pmtmNumMeses,@pmtmVidaUtil,@pmtmBaseDep,@pmtmMetodo,@pmtmFecUltDep,@pmtmConcepto)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfIngreso_Sel @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_AfIngreso (tmNumero,tmItem,tmIdActivo,tmFecha,tmVrMejoras,tmVrDisminucion,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses
,tmVidaUtil,tmBaseDep,tmMetodo,tmFecUltDep,tmConcepto)
 SELECT  @pmtmNumero,Item,I.IdActivo,I.FecActivacion,I.VrMejoras,I.VrDisminucion,I.VrAjuInfAcum,I.VrDepAcuHis,I.VrDepAcuAju,I.VrAjuInfDep,I.NumMesesDep
,VidaUtil,VrBaseDep,MetodoDep,I.FecUltDep,Concepto
 FROM Trn_AfMovIngreso AS I INNER JOIN Activos AS A ON I.IdActivo=A.IdActivo
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND I.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfRetiro @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdActivo VARCHAR(30),@pmtmIdCausal VARCHAR(4)
,@pmtmNitTercero VARCHAR(16),@pmtmNomTercero VARCHAR(150),@pmtmCostoIni MONEY,@pmtmMejoras MONEY,@pmtmDisminucion MONEY
,@pmtmAcuAjustes MONEY,@pmtmDepAcum MONEY,@pmtmAjuDepAcum MONEY,@pmtmVrIngreso MONEY,@pmtmFechaRet SMALLDATETIME
,@pmtmIdCueDeb VARCHAR(16),@pmtmIdCueIng VARCHAR(16),@pmtmConcepto VARCHAR(250),@pmtmIdCia CHAR(2),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmNitRespons VARCHAR(16)
 AS 
 INSERT INTO tm_AfRetiro (tmNumero,tmItem,tmIdActivo,tmIdCausal,tmNitTercero,tmNomTercero,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepAcum
,tmAjuDepAcum,tmVrIngreso,tmFechaRet,tmIdCueDeb,tmIdCueIng,tmConcepto,tmIdCia,tmCdCCosto,tmCdSubCos,tmNitRespons)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmIdCausal,@pmtmNitTercero,@pmtmNomTercero,@pmtmCostoIni,@pmtmMejoras
,@pmtmDisminucion,@pmtmAcuAjustes,@pmtmDepAcum,@pmtmAjuDepAcum,@pmtmVrIngreso,@pmtmFechaRet,@pmtmIdCueDeb,@pmtmIdCueIng
,@pmtmConcepto,@pmtmIdCia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmNitRespons)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfRetiro_Dif @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_AfRetiro (tmNumero,tmItem,tmIdActivo,tmIdCausal,tmNitTercero,tmNomTercero,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepAcum
,tmAjuDepAcum,tmVrIngreso,tmFechaRet,tmIdCueDeb,tmIdCueIng,tmConcepto,tmIdCia,tmCdCCosto,tmCdSubCos,tmNitRespons)
 SELECT  @pmtmNumero,Item,R.IdDiferido,R.IdCausal,'0','',VrCostoIni,R.VrAdiciones,R.VrDisminucion,VrAcuAjustes,VrAmortizaAcum,VrAjuAmtzaAcum,0,FechaRet
,'','',Concepto,R.IdCia,IdCCosto,IdSubCos,IdRespons
 FROM Trn_AfDifRetiro AS R INNER JOIN Diferidos AS D ON R.IdDiferido=D.IdDiferido
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND R.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfRetiro_Sel @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_AfRetiro (tmNumero,tmItem,tmIdActivo,tmIdCausal,tmNitTercero,tmNomTercero,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepAcum
,tmAjuDepAcum,tmVrIngreso,tmFechaRet,tmIdCueDeb,tmIdCueIng,tmConcepto,tmIdCia,tmCdCCosto,tmCdSubCos,tmNitRespons)
 SELECT  @pmtmNumero,Item,R.IdActivo,R.IdCausal,NitTercero,NomTercero,VrCostoIni,R.VrMejoras,R.VrDisminucion,VrAcuAjustes,VrDepAcum
,VrAjuDepAcum,VrIngreso,FechaRet,IdCueDeb,IdCueIng,Concepto,R.IdCia,IdCCosto,IdSubCos,IdRespons
 FROM Trn_AfMovRetiro AS R INNER JOIN  Activos AS A ON R.IdActivo=A.IdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfTraslado @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdActivo VARCHAR(30),@pmtmIdCia CHAR(2),@pmtmIdGrupo VARCHAR(4)
,@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdInstala VARCHAR(4),@pmtmIdDep VARCHAR(4),@pmtmIdRespons VARCHAR(16)
,@pmtmCdCiaDest CHAR(2),@pmtmCdGrupoDest VARCHAR(4),@pmtmCdCCostoDest VARCHAR(16),@pmtmCdSubCosDest VARCHAR(16)
,@pmtmCdInstalaDest VARCHAR(4),@pmtmCdDepDest VARCHAR(4),@pmtmCdResponsDest VARCHAR(16),@pmtmConcepto VARCHAR(150)
,@pmtmUbicacion VARCHAR(100),@pmtmUbicacionDest VARCHAR(100)
 AS 
 INSERT INTO tm_AfTraslado (tmNumero,tmItem,tmIdActivo,tmIdCia,tmIdGrupo,tmIdCCosto,tmIdSubCos,tmIdInstala,tmIdDep,tmIdRespons,tmCdCiaDest
,tmCdGrupoDest,tmCdCCostoDest,tmCdSubCosDest,tmCdInstalaDest,tmCdDepDest,tmCdResponsDest,tmConcepto,tmUbicacion,tmUbicacionDest)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdActivo,@pmtmIdCia,@pmtmIdGrupo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdInstala,@pmtmIdDep
,@pmtmIdRespons,@pmtmCdCiaDest,@pmtmCdGrupoDest,@pmtmCdCCostoDest,@pmtmCdSubCosDest,@pmtmCdInstalaDest,@pmtmCdDepDest
,@pmtmCdResponsDest,@pmtmConcepto,@pmtmUbicacion,@pmtmUbicacionDest)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfTraslado_Dif @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_AfTraslado (tmNumero,tmItem,tmIdActivo,tmIdCia,tmIdGrupo,tmIdCCosto,tmIdSubCos,tmIdInstala,tmIdDep,tmIdRespons,tmCdCiaDest
,tmCdGrupoDest,tmCdCCostoDest,tmCdSubCosDest,tmCdInstalaDest,tmCdDepDest,tmCdResponsDest,tmConcepto,tmUbicacion,tmUbicacionDest)
SELECT  @pmtmNumero,Item,IdDiferido,IdCia,IdGrupo,IdCCosto,IdSubCos,'0',IdDep,IdRespons,CdCiaDest
,CdGrupoDest,CdCCostoDest,CdSubCosDest,'0',CdDepDest,CdResponsDest,Concepto,Ubicacion,UbicacionDest
 FROM Trn_AfDifTraslado
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AfTraslado_Sel @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_AfTraslado (tmNumero,tmItem,tmIdActivo,tmIdCia,tmIdGrupo,tmIdCCosto,tmIdSubCos,tmIdInstala,tmIdDep,tmIdRespons,tmCdCiaDest
,tmCdGrupoDest,tmCdCCostoDest,tmCdSubCosDest,tmCdInstalaDest,tmCdDepDest,tmCdResponsDest,tmConcepto,tmUbicacion,tmUbicacionDest)
SELECT  @pmtmNumero,Item,IdActivo,IdCia,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,CdCiaDest
,CdGrupoDest,CdCCostoDest,CdSubCosDest,CdInstalaDest,CdDepDest,CdResponsDest,Concepto,Ubicacion,UbicacionDest
 FROM Trn_AfMovTraslado
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Aportes @pmtmEst VARCHAR(5),@pmtmItem INT,@pmtmIdEmpleado VARCHAR(16),@pmtmApellido1 VARCHAR(50),@pmtmApellido2 VARCHAR(50),@pmtmNombre1 VARCHAR(50),@pmtmNombre2 VARCHAR(50),@pmtmNContto INT,@pmtmDiasPen DECIMAL(10,4),@pmtmDiasSal DECIMAL(10,4),@pmtmDiasRie DECIMAL(10,4),@pmtmDiasCom DECIMAL(10,4),@pmtmBasePen MONEY,@pmtmBaseSal MONEY,@pmtmBaseRie MONEY,@pmtmBaseCom MONEY,@pmtmBaseRef MONEY,@pmtmValorPen MONEY,@pmtmValorSal MONEY,@pmtmValorRie MONEY,@pmtmValorCom MONEY,@pmtmValorSena MONEY
,@pmtmValorIcbf MONEY,@pmtmValorFsp MONEY,@pmtmValorFsu MONEY,@pmtmValorESAP MONEY,@pmtmValorMin MONEY,@pmtmTarfaPen DECIMAL(10,4),@pmtmTarfaSal DECIMAL(10,4),@pmtmTarfaRie DECIMAL(10,4),@pmtmTarfaFsp DECIMAL(10,4),@pmtmTarfaFsu DECIMAL(10,4),@pmtmTarfaEsa DECIMAL(10,4),@pmtmTarfaMin DECIMAL(10,4),@pmtmTarfaCom DECIMAL(10,4),@pmtmTarfaIcb DECIMAL(10,4),@pmtmTarfaSen DECIMAL(10,4),@pmtmNovIng INT,@pmtmNovRet INT,@pmtmNovTda INT,@pmtmNovTaa INT,@pmtmNovTdp INT,@pmtmNovTap INT,@pmtmNovVsp INT,@pmtmNovVte INT,@pmtmNovVst INT,@pmtmNovSln INT,@pmtmNovIge INT,@pmtmNovLma INT,@pmtmNovVac INT
,@pmtmNovAvp INT,@pmtmNovIrp INT,@pmtmNovVct INT,@pmtmCodNewEps VARCHAR(8),@pmtmCodNewPen VARCHAR(8),@pmtmCodNewRie VARCHAR(8),@pmtmCodNewIns VARCHAR(4),@pmtmCodNewTar VARCHAR(4),@pmtmCodNewGru VARCHAR(4),@pmtmVrIncap MONEY,@pmtmVrLicencia MONEY,@pmtmVrVolAfi MONEY,@pmtmVrVolApo MONEY,@pmtmVrNoReten MONEY,@pmtmVrUpc MONEY,@pmtmVrIncIrp MONEY,@pmtmNAutLic VARCHAR(30),@pmtmNAutInc VARCHAR(30),@pmtmNAutIrp VARCHAR(30),@pmtmIdFonPen VARCHAR(8),@pmtmIdFonSal VARCHAR(8),@pmtmIdFonRie VARCHAR(8),@pmtmIdFonCaj VARCHAR(8),@pmtmIdInstala VARCHAR(4)
,@pmtmIdDep VARCHAR(4),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmVehiculo VARCHAR(10),@pmtmVrBasico MONEY,@pmtmDiasNov DECIMAL(10,4),@pmtmCdFonAvp VARCHAR(8)
 AS 
 INSERT INTO tm_Aportes (tmEst,tmItem,tmIdEmpleado,tmApellido1,tmApellido2,tmNombre1,tmNombre2,tmNContto,tmDiasPen,tmDiasSal,tmDiasRie,tmDiasCom,tmBasePen,tmBaseSal,tmBaseRie,tmBaseCom,tmBaseRef,tmValorPen,tmValorSal,tmValorRie,tmValorCom,tmValorSena,tmValorIcbf,tmValorFsp,tmValorFsu,tmTarfaPen,tmTarfaSal,tmTarfaRie,tmTarfaFsp,tmTarfaFsu,tmTarfaEsa,tmTarfaMin,tmTarfaCom,tmTarfaIcb,tmTarfaSen,tmNovIng,tmNovRet,tmNovTda,tmNovTaa,tmNovTdp,tmNovTap,tmNovVsp,tmNovVte,tmNovVst,tmNovSln,tmNovIge,tmNovLma,tmNovVac,tmNovAvp,tmNovIrp,tmNovVct,tmCodNewEps,tmCodNewPen,tmCodNewRie,tmCodNewIns,tmCodNewTar,tmCodNewGru
,tmVrIncap,tmVrLicencia,tmVrVolAfi,tmVrVolApo,tmVrNoReten,tmVrUpc,tmVrIncIrp,tmNAutLic,tmNAutInc,tmNAutIrp,tmIdFonPen,tmIdFonSal,tmIdFonRie,tmIdFonCaj,tmIdInstala,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmVehiculo,tmVrBasico,tmDiasNov,tmValorESAP,tmValorMin,tmCdFonAvp)
 VALUES (@pmtmEst,@pmtmItem,@pmtmIdEmpleado,@pmtmApellido1,@pmtmApellido2,@pmtmNombre1,@pmtmNombre2,@pmtmNContto,@pmtmDiasPen,@pmtmDiasSal,@pmtmDiasRie,@pmtmDiasCom,@pmtmBasePen,@pmtmBaseSal,@pmtmBaseRie,@pmtmBaseCom,@pmtmBaseRef,@pmtmValorPen,@pmtmValorSal,@pmtmValorRie,@pmtmValorCom,@pmtmValorSena,@pmtmValorIcbf,@pmtmValorFsp,@pmtmValorFsu,@pmtmTarfaPen,@pmtmTarfaSal,@pmtmTarfaRie,@pmtmTarfaFsp,@pmtmTarfaFsu,@pmtmTarfaEsa,@pmtmTarfaMin,@pmtmTarfaCom,@pmtmTarfaIcb,@pmtmTarfaSen,@pmtmNovIng,@pmtmNovRet,@pmtmNovTda,@pmtmNovTaa,@pmtmNovTdp,@pmtmNovTap
,@pmtmNovVsp,@pmtmNovVte,@pmtmNovVst,@pmtmNovSln,@pmtmNovIge,@pmtmNovLma,@pmtmNovVac,@pmtmNovAvp,@pmtmNovIrp,@pmtmNovVct,@pmtmCodNewEps,@pmtmCodNewPen,@pmtmCodNewRie,@pmtmCodNewIns,@pmtmCodNewTar,@pmtmCodNewGru,@pmtmVrIncap,@pmtmVrLicencia,@pmtmVrVolAfi,@pmtmVrVolApo,@pmtmVrNoReten,@pmtmVrUpc,@pmtmVrIncIrp,@pmtmNAutLic,@pmtmNAutInc,@pmtmNAutIrp,@pmtmIdFonPen,@pmtmIdFonSal,@pmtmIdFonRie,@pmtmIdFonCaj,@pmtmIdInstala,@pmtmIdDep,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdNom,@pmtmVehiculo,@pmtmVrBasico,@pmtmDiasNov,@pmtmValorESAP,@pmtmValorMin,@pmtmCdFonAvp)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Aportes_Sel  @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmtmEst VARCHAR(5)
 AS 
 INSERT INTO tm_Aportes (tmEst,tmItem,tmIdEmpleado,tmApellido1,tmApellido2,tmNombre1,tmNombre2,tmNContto,tmDiasPen,tmDiasSal,tmDiasRie,tmDiasCom,tmBasePen,tmBaseSal,tmBaseRie,tmBaseCom,tmBaseRef,tmValorPen,tmValorSal,tmValorRie,tmValorCom,tmValorSena,tmValorIcbf,tmValorFsp,tmValorFsu
,tmTarfaPen,tmTarfaSal,tmTarfaRie,tmTarfaFsp,tmTarfaFsu,tmTarfaEsa,tmTarfaMin,tmTarfaCom,tmTarfaIcb,tmTarfaSen,tmVrIncap,tmVrLicencia,tmVrVolAfi,tmVrVolApo,tmVrNoReten,tmVrUpc,tmVrIncIrp,tmNAutLic,tmNAutInc,tmNAutIrp,tmIdFonPen,tmIdFonSal,tmIdFonRie,tmIdFonCaj,tmIdInstala,tmIdDep,tmIdCCosto
,tmIdSubCos,tmIdNom,tmVehiculo,tmVrBasico,tmValorESAP,tmValorMin,tmCdFonAvp)
 SELECT @pmtmEst,Item,IdEmpleado,Apellido1,Apellido2,Nombre1,Nombre2,NContrato,DiasPen,DiasSal,DiasRie,DiasCom,VrIBCPen,VrIBCSal,VrIBCRie,VrIBCCajas,VrIBCRef,VrPension,VrSalud,VrRiesgos,VrCajas,VrSena,VrICBF,VrApoFsp,VrApoFsu,TarifPen,TarifSal,TarifRie,TarifFsp,TarifFsu,TarifEsap,TarifEdu
,TarifCom,TarifIcb,TarifSen,VrIncap,VrLicencia,VrApoVolAfi,VrApoVolApt,VrNoReten,VrUpc,VrIncIrp,NumAutLic,NumAutInc,NumAutIrp,IdFonPen,IdFonSal,IdFonRie,IdCajCom,IdInstala,'0','0','0','0','',SalBasico,VrESAP,VrMinEdu,CdFonAvp
 FROM Trn_NomAlqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AutTot @pmtmEst VARCHAR(5),@pmtmItem INT,@pmtmTipAdm VARCHAR(3),@pmtmIdFondo VARCHAR(8),@pmtmCodTipoApo CHAR(2),@pmtmTotalCot MONEY,@pmtmTotalFsp MONEY,@pmtmTotalFsu MONEY
,@pmtmTotalAva MONEY,@pmtmTotalAve MONEY,@pmtmTotalUpc MONEY,@pmtmTotalLic MONEY,@pmtmTotalInc MONEY,@pmtmTotalOtr MONEY,@pmtmDiasMora INT,@pmtmTarifMora DECIMAL(10,4),@pmtmIntMora MONEY,@pmtmIMoraOtr MONEY
,@pmtmTotalSAF MONEY,@pmtmSAFUpc MONEY,@pmtmTotalPagar MONEY,@pmtmTotalFon MONEY,@pmtmTotalAfi INT,@pmtmNumAutLic VARCHAR(30),@pmtmNumAutInc VARCHAR(30),@pmtmNumPlaSAF VARCHAR(20)
 AS 
 INSERT INTO tm_AutTot (tmEst,tmItem,tmTipAdm,tmIdFondo,tmCodTipoApo,tmTotalCot,tmTotalFsp,tmTotalFsu,tmTotalAva,tmTotalAve,tmTotalUpc,tmTotalLic,tmTotalInc,tmTotalOtr,tmDiasMora,tmTarifMora,tmIntMora,tmIMoraOtr,tmTotalSAF,tmSAFUpc,tmTotalPagar
,tmTotalFon,tmTotalAfi,tmNumAutLic,tmNumAutInc,tmNumPlaSAF)
 VALUES (@pmtmEst,@pmtmItem,@pmtmTipAdm,@pmtmIdFondo,@pmtmCodTipoApo,@pmtmTotalCot,@pmtmTotalFsp,@pmtmTotalFsu,@pmtmTotalAva,@pmtmTotalAve,@pmtmTotalUpc,@pmtmTotalLic,@pmtmTotalInc,@pmtmTotalOtr,@pmtmDiasMora
,@pmtmTarifMora,@pmtmIntMora,@pmtmIMoraOtr,@pmtmTotalSAF,@pmtmSAFUpc,@pmtmTotalPagar,@pmtmTotalFon,@pmtmTotalAfi,@pmtmNumAutLic,@pmtmNumAutInc,@pmtmNumPlaSAF)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AutTot_Sel @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmtmEst VARCHAR(5)
 AS 
 INSERT INTO tm_AutTot (tmEst,tmItem,tmTipAdm,tmIdFondo,tmCodTipoApo,tmTotalCot,tmTotalFsp,tmTotalFsu,tmTotalAva,tmTotalAve,tmTotalUpc,tmTotalLic,tmTotalInc,tmTotalOtr,tmDiasMora,tmTarifMora,tmIntMora,tmIMoraOtr,tmTotalSAF,tmSAFUpc,tmTotalPagar
,tmTotalFon,tmTotalAfi,tmNumAutLic,tmNumAutInc,tmNumPlaSAF)
 SELECT @pmtmEst,Item,TipAdm,IdFondo,CodTipoApo,VrTotalCot,VrTotalFsp,VrTotalFsu,VrTotalAva,VrTotalAve,VrTotalUpc,VrTotalLic,VrTotalInc,VrTotalOtr,DiasMora,TarifMora,VrIntMora,VrIMoraOtr,VrTotalSAF,VrSAFUpc,VrTotalPagar
,VrTotalFon,TotalAfi,NumAutLic,NumAutInc,NumPlaSAF
 FROM Trn_NomAlqTot
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmIdCia CHAR(2),@pmtmNumLote VARCHAR(30),@pmtmSaldoAnt DECIMAL(14,4)
,@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmCostoAnt MONEY,@pmtmCostoEnt MONEY,@pmtmCostoSal MONEY,@pmtmCostoProm MONEY,@pmtmCdTanque VARCHAR(4),@pmtmVtaCant DECIMAL(14,4)
,@pmtmVtaCantDev DECIMAL(14,4),@pmtmVtaTotal MONEY,@pmtmVtaTotalDev MONEY,@pmtmVtaCosto MONEY,@pmtmVtaCostoDev MONEY,@pmtmVtaIva MONEY,@pmtmVtaIvaDev MONEY,@pmtmVtaDcto MONEY
,@pmtmVtaDctoDev MONEY,@pmtmVtaRet MONEY,@pmtmVtaRetDev MONEY,@pmtmVtaIca MONEY,@pmtmVtaIcaDev MONEY,@pmtmVtaTasa MONEY,@pmtmVtaTasaDev MONEY,@pmtmVtaGlobal MONEY
,@pmtmVtaGlobalDev MONEY,@pmtmVtaSol MONEY,@pmtmVtaSolDev MONEY
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdBodega,tmIdCia,tmNumLote,tmSaldoAnt,tmEntradas,tmSalidas,tmCostoAnt,tmCostoEnt,tmCostoSal,tmCostoProm,tmCdTanque,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev
,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev)
 VALUES (@pmtmEst,@pmtmIdProducto,@pmtmIdBodega,@pmtmIdCia,@pmtmNumLote,@pmtmSaldoAnt,@pmtmEntradas,@pmtmSalidas,@pmtmCostoAnt,@pmtmCostoEnt,@pmtmCostoSal,@pmtmCostoProm,@pmtmCdTanque
,@pmtmVtaCant,@pmtmVtaCantDev,@pmtmVtaTotal,@pmtmVtaTotalDev,@pmtmVtaCosto,@pmtmVtaCostoDev,@pmtmVtaIva,@pmtmVtaIvaDev,@pmtmVtaDcto,@pmtmVtaDctoDev,@pmtmVtaRet,@pmtmVtaRetDev
,@pmtmVtaIca,@pmtmVtaIcaDev,@pmtmVtaTasa,@pmtmVtaTasaDev,@pmtmVtaGlobal,@pmtmVtaGlobalDev,@pmtmVtaSol,@pmtmVtaSolDev)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInvSan @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmIdCia CHAR(2),@pmtmNumLote VARCHAR(30),@pmtmSaldoAnt DECIMAL(14,4)
,@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmCostoAnt MONEY,@pmtmCostoEnt MONEY,@pmtmCostoSal MONEY,@pmtmCostoProm MONEY,@pmtmCdTanque VARCHAR(4)
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdBodega,tmIdCia,tmNumLote,tmSaldoAnt,tmEntradas,tmSalidas,tmCostoAnt,tmCostoEnt,tmCostoSal,tmCostoProm,tmCdTanque)
 VALUES (@pmtmEst,@pmtmIdProducto,@pmtmIdBodega,@pmtmIdCia,@pmtmNumLote,@pmtmSaldoAnt,@pmtmEntradas,@pmtmSalidas,@pmtmCostoAnt,@pmtmCostoEnt,@pmtmCostoSal,@pmtmCostoProm,@pmtmCdTanque)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_Sel @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDetCia BIT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 IF @pmDetCia <>0
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,IdCia,'0','0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia
 ELSE
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,'00','0','0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto
 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelBod @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDetCia BIT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 IF @pmDetCia <>0
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,IdCia,K.IdBodega,'0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia,K.IdBodega
 ELSE
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,'00',K.IdBodega,'0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,K.IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelIns @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDetCia BIT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 IF @pmDetCia <>0
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,IdCia,'0','0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND TipoRef='INSUMO'
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia
 ELSE
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,'00','0','0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipoRef='INSUMO' 
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelInsBod @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDetCia BIT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 IF @pmDetCia <>0
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,IdCia,K.IdBodega,'0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND TipoRef='INSUMO'
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia,K.IdBodega
 ELSE
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,'00',K.IdBodega,'0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipoRef='INSUMO' 
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,K.IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelTanq @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDetCia BIT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmInactivo BIT=Null
 AS 
 IF @pmDetCia <>0
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,IdCia,CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS CdBodga,'0'
 ,CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS NumTanque
 ,SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND Tanques<>0 AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia,CdTanque
 ELSE
  INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal)
  SELECT @pmtmEst,K.IdProducto,'00',CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS CdBodga
  ,'0',CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS NumTanque
  ,SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND Tanques<>0 AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelVen @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal
 ,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev
 ,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev) 
 SELECT @pmtmEst,K.IdProducto,'00','0','0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
--variables de ventas
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Salidas ELSE 0 END ) AS VtaCant
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Entradas ELSE 0 END ) AS VtaCantDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrPrecio*Salidas ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*Entradas ELSE 0 END ) AS VtaTotalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrCostoSal ELSE 0 END ) AS VtaCosto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrCostoEnt ELSE 0 END ) AS VtaCostoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIvaSal ELSE 0 END ) AS VtaIva
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIvaEnt ELSE 0 END ) AS VtaIvaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrDctoSal ELSE 0 END ) AS VtaDcto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrDctoEnt ELSE 0 END ) AS VtaDctoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrReteSal ELSE 0 END ) AS VtaRet
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrReteEnt ELSE 0 END ) AS VtaRetDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIcaSal ELSE 0 END ) AS VtaIca
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIcaEnt ELSE 0 END ) AS VtaIcaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Sobretasa*Salidas ELSE 0 END ) AS VtaTasa
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Sobretasa*Entradas ELSE 0 END ) AS VtaTasaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN ImpGlobal*Salidas ELSE 0 END ) AS VtaGlobal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN ImpGlobal*Entradas ELSE 0 END ) AS VtaGlobalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Soldicom*Salidas ELSE 0 END ) AS VtaSol
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Soldicom*Entradas ELSE 0 END ) AS VtaSolDev
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelVenBod @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal
 ,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev
 ,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev) 
 SELECT @pmtmEst,K.IdProducto,'00',K.IdBodega,'0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
--variables de ventas
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Salidas ELSE 0 END ) AS VtaCant
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Entradas ELSE 0 END ) AS VtaCantDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrPrecio*Salidas ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*Entradas ELSE 0 END ) AS VtaTotalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrCostoSal ELSE 0 END ) AS VtaCosto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrCostoEnt ELSE 0 END ) AS VtaCostoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIvaSal ELSE 0 END ) AS VtaIva
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIvaEnt ELSE 0 END ) AS VtaIvaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrDctoSal ELSE 0 END ) AS VtaDcto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrDctoEnt ELSE 0 END ) AS VtaDctoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrReteSal ELSE 0 END ) AS VtaRet
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrReteEnt ELSE 0 END ) AS VtaRetDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIcaSal ELSE 0 END ) AS VtaIca
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIcaEnt ELSE 0 END ) AS VtaIcaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Sobretasa*Salidas ELSE 0 END ) AS VtaTasa
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Sobretasa*Entradas ELSE 0 END ) AS VtaTasaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN ImpGlobal*Salidas ELSE 0 END ) AS VtaGlobal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN ImpGlobal*Entradas ELSE 0 END ) AS VtaGlobalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Soldicom*Salidas ELSE 0 END ) AS VtaSol
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Soldicom*Entradas ELSE 0 END ) AS VtaSolDev
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,K.IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelVenBodCia @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal
 ,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev
 ,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev) 
 SELECT @pmtmEst,K.IdProducto,IdCia,K.IdBodega,'0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
--variables de ventas
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Salidas ELSE 0 END ) AS VtaCant
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Entradas ELSE 0 END ) AS VtaCantDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrPrecio*Salidas ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*Entradas ELSE 0 END ) AS VtaTotalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrCostoSal ELSE 0 END ) AS VtaCosto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrCostoEnt ELSE 0 END ) AS VtaCostoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIvaSal ELSE 0 END ) AS VtaIva
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIvaEnt ELSE 0 END ) AS VtaIvaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrDctoSal ELSE 0 END ) AS VtaDcto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrDctoEnt ELSE 0 END ) AS VtaDctoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrReteSal ELSE 0 END ) AS VtaRet
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrReteEnt ELSE 0 END ) AS VtaRetDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIcaSal ELSE 0 END ) AS VtaIca
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIcaEnt ELSE 0 END ) AS VtaIcaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Sobretasa*Salidas ELSE 0 END ) AS VtaTasa
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Sobretasa*Entradas ELSE 0 END ) AS VtaTasaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN ImpGlobal*Salidas ELSE 0 END ) AS VtaGlobal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN ImpGlobal*Entradas ELSE 0 END ) AS VtaGlobalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Soldicom*Salidas ELSE 0 END ) AS VtaSol
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Soldicom*Entradas ELSE 0 END ) AS VtaSolDev
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia,K.IdBodega

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelVenCia @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal
 ,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev
 ,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev) 
 SELECT @pmtmEst,K.IdProducto,IdCia,'0','0','0',SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
--variables de ventas
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Salidas ELSE 0 END ) AS VtaCant
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Entradas ELSE 0 END ) AS VtaCantDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrPrecio*Salidas ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*Entradas ELSE 0 END ) AS VtaTotalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrCostoSal ELSE 0 END ) AS VtaCosto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrCostoEnt ELSE 0 END ) AS VtaCostoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIvaSal ELSE 0 END ) AS VtaIva
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIvaEnt ELSE 0 END ) AS VtaIvaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrDctoSal ELSE 0 END ) AS VtaDcto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrDctoEnt ELSE 0 END ) AS VtaDctoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrReteSal ELSE 0 END ) AS VtaRet
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrReteEnt ELSE 0 END ) AS VtaRetDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIcaSal ELSE 0 END ) AS VtaIca
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIcaEnt ELSE 0 END ) AS VtaIcaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Sobretasa*Salidas ELSE 0 END ) AS VtaTasa
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Sobretasa*Entradas ELSE 0 END ) AS VtaTasaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN ImpGlobal*Salidas ELSE 0 END ) AS VtaGlobal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN ImpGlobal*Entradas ELSE 0 END ) AS VtaGlobalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Soldicom*Salidas ELSE 0 END ) AS VtaSol
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Soldicom*Entradas ELSE 0 END ) AS VtaSolDev
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelVenTanq @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmInactivo BIT=Null
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal
 ,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev
 ,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev) 
 SELECT @pmtmEst,K.IdProducto,'00',CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS CdBodga,'0'
,CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS NumTanque
,SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
--variables de ventas
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Salidas ELSE 0 END ) AS VtaCant
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Entradas ELSE 0 END ) AS VtaCantDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrPrecio*Salidas ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*Entradas ELSE 0 END ) AS VtaTotalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrCostoSal ELSE 0 END ) AS VtaCosto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrCostoEnt ELSE 0 END ) AS VtaCostoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIvaSal ELSE 0 END ) AS VtaIva
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIvaEnt ELSE 0 END ) AS VtaIvaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrDctoSal ELSE 0 END ) AS VtaDcto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrDctoEnt ELSE 0 END ) AS VtaDctoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrReteSal ELSE 0 END ) AS VtaRet
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrReteEnt ELSE 0 END ) AS VtaRetDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIcaSal ELSE 0 END ) AS VtaIca
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIcaEnt ELSE 0 END ) AS VtaIcaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Sobretasa*Salidas ELSE 0 END ) AS VtaTasa
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Sobretasa*Entradas ELSE 0 END ) AS VtaTasaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN ImpGlobal*Salidas ELSE 0 END ) AS VtaGlobal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN ImpGlobal*Entradas ELSE 0 END ) AS VtaGlobalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Soldicom*Salidas ELSE 0 END ) AS VtaSol
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Soldicom*Entradas ELSE 0 END ) AS VtaSolDev
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND Tanques<>0 AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_AuxInv_SelVenTanqCia @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmInactivo BIT=Null
 AS 
 INSERT INTO tm_AuxInv (tmEst,tmIdProducto,tmIdCia,tmIdBodega,tmNumLote,tmCdTanque,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal
 ,tmVtaCant,tmVtaCantDev,tmVtaTotal,tmVtaTotalDev,tmVtaCosto,tmVtaCostoDev,tmVtaIva,tmVtaIvaDev,tmVtaDcto,tmVtaDctoDev
 ,tmVtaRet,tmVtaRetDev,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev) 
 SELECT @pmtmEst,K.IdProducto,IdCia,CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS CdBodga
,'0',CASE CdTanque WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdTanque  END AS NumTanque
,SUM(Entradas),SUM(Salidas),SUM(VrCostoEnt),SUM(VrCostoSal)
--variables de ventas
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Salidas ELSE 0 END ) AS VtaCant
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Entradas ELSE 0 END ) AS VtaCantDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrPrecio*Salidas ELSE 0 END ) AS VtaTotal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrPrecio*Entradas ELSE 0 END ) AS VtaTotalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrCostoSal ELSE 0 END ) AS VtaCosto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrCostoEnt ELSE 0 END ) AS VtaCostoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIvaSal ELSE 0 END ) AS VtaIva
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIvaEnt ELSE 0 END ) AS VtaIvaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrDctoSal ELSE 0 END ) AS VtaDcto
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrDctoEnt ELSE 0 END ) AS VtaDctoDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrReteSal ELSE 0 END ) AS VtaRet
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrReteEnt ELSE 0 END ) AS VtaRetDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN VrIcaSal ELSE 0 END ) AS VtaIca
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN VrIcaEnt ELSE 0 END ) AS VtaIcaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Sobretasa*Salidas ELSE 0 END ) AS VtaTasa
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Sobretasa*Entradas ELSE 0 END ) AS VtaTasaDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN ImpGlobal*Salidas ELSE 0 END ) AS VtaGlobal
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN ImpGlobal*Entradas ELSE 0 END ) AS VtaGlobalDev
,SUM(CASE WHEN NoVentas IN (1,3,5) THEN Soldicom*Salidas ELSE 0 END ) AS VtaSol
,SUM(CASE WHEN NoVentas IN (2,4,6) THEN Soldicom*Entradas ELSE 0 END ) AS VtaSolDev
  FROM Trn_Kardex  AS K INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND TipoRef<>'INSUMO'  AND Tanques<>0 AND  TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY K.IdProducto,IdCia,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_BalAcum @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT,@pmtmIdCia CHAR(2),@pmtmIdTercero VARCHAR(16)
,@pmtmIdVehiculo VARCHAR(10),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmCodAgncia VARCHAR(16),@pmtmTipo VARCHAR(4)
,@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY
 AS 
 INSERT INTO tm_BalAcum (tmEst,tmIdCuenta,tmItem,tmIdCia,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdCia,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia
,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_BalCom @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT,@pmtmIdTercero VARCHAR(16)
,@pmtmIdVehiculo VARCHAR(10),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmCodAgncia VARCHAR(16)
,@pmtmTipo VARCHAR(4),@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY
,@pmtmVrPresupto MONEY
 AS 
 INSERT INTO tm_BalCom (tmEst,tmIdCuenta,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior
,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmVrPresupto)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia
,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmVrPresupto)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_BalCom_Sel @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCCosto VARCHAR(16)=Null
,@pmIdSubCos VARCHAR(16)=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null
AS
 IF @pmCueIni IS NULL --NO RANGOS
  INSERT INTO tm_BalCom (tmEst,tmIdCuenta,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmNuevoSaldo,tmTotalDebitos,tmTotalCreditos)
  SELECT @pmtmEst,IdCuenta,0,'0','0','0','0','0','0',0,0,SUM(VrDebito),SUM(VrCredito)
  FROM   Trn_ComDetalle
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  GROUP BY  IdCuenta
 ELSE --RANGOS 
  INSERT INTO tm_BalCom (tmEst,tmIdCuenta,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmNuevoSaldo,tmTotalDebitos,tmTotalCreditos)
  SELECT @pmtmEst,IdCuenta,0,'0','0','0','0','0','0',0,0,SUM(VrDebito),SUM(VrCredito)
  FROM   Trn_ComDetalle
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta BETWEEN @pmCueIni AND @pmCueFin
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
 AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  GROUP BY  IdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_BalCom_SelAcc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCCosto VARCHAR(16)=Null
,@pmIdSubCos VARCHAR(16)=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null
AS
--terceros Accionistas
 IF @pmCueIni IS NULL --NO RANGOS
  INSERT INTO tm_BalCom (tmEst,tmIdCuenta,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmNuevoSaldo,tmTotalDebitos,tmTotalCreditos)
  SELECT @pmtmEst,IdCuenta,0,'0','0','0','0','0','0',0,0,SUM(VrDebito),SUM(VrCredito)
  FROM   Trn_ComDetalle AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (EsAccnista<>0 OR D.IdTercero='0') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  GROUP BY  IdCuenta
 ELSE --RANGOS 
  INSERT INTO tm_BalCom (tmEst,tmIdCuenta,tmItem,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmNuevoSaldo,tmTotalDebitos,tmTotalCreditos)
  SELECT @pmtmEst,IdCuenta,0,'0','0','0','0','0','0',0,0,SUM(VrDebito),SUM(VrCredito)
  FROM   Trn_ComDetalle AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND (EsAccnista<>0 OR D.IdTercero='0') AND IdCuenta BETWEEN @pmCueIni AND @pmCueFin
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
 AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  GROUP BY  IdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_BalCompara @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT,@pmtmIdCia CHAR(2),@pmtmIdTercero VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmCodAgncia VARCHAR(16)
,@pmtmTipo VARCHAR(4),@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY,@pmtmSalAnt3 MONEY,@pmtmDebitos3 MONEY
,@pmtmCreditos3 MONEY,@pmtmSaldo3 MONEY
 AS 
 INSERT INTO tm_BalCompara (tmEst,tmIdCuenta,tmItem,tmIdCia,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmSalAnt3,tmDebitos3,tmCreditos3,tmSaldo3)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdCia,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmSalAnt2,@pmtmDebitos2
,@pmtmCreditos2,@pmtmSaldo2,@pmtmSalAnt3,@pmtmDebitos3,@pmtmCreditos3,@pmtmSaldo3)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_BalGen @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2),@pmtmSaldoAnterior MONEY
,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY
,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 INSERT INTO tm_BalGen (tmEst,tmIdCuenta,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmIdCia,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo,@pmtmSalAnt2
,@pmtmDebitos2,@pmtmCreditos2,@pmtmSaldo2)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_BalGen_Sel @pmtmEst CHAR(2),@pmNiv INT,@pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT,@pmDetCia BIT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmCue8 VARCHAR(1)=Null,@pmCue9 VARCHAR(1)=Null
 AS 
 IF @pmDetCia <>0 --Detalle por compañias 
  INSERT INTO tm_BalGen (tmEst,tmIdCuenta,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
 ,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2)
  SELECT @pmtmEst,SUBSTRING(A.IdCuenta,1,@pmNiv),IdCia,SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  ,SUM(SaldoAnterior+TotalDebitos-TotalCreditos),0,0,0,0
 FROM  AcuCuentas AS A INNER JOIN  Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes BETWEEN @pmnMesIni AND @pmnMesFin
 AND Movimiento<>0 AND SUBSTRING(A.IdCuenta,1,1) IN ('1','2','3',@pmCue8,@pmCue9)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),IdCia
 ELSE
  INSERT INTO tm_BalGen (tmEst,tmIdCuenta,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
 ,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2)
  SELECT @pmtmEst,SUBSTRING(A.IdCuenta,1,@pmNiv),'00',SUM(SaldoAnterior),SUM(TotalDebitos),SUM(TotalCreditos)
  ,SUM(SaldoAnterior+TotalDebitos-TotalCreditos),0,0,0,0
 FROM  AcuCuentas AS A INNER JOIN  Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes BETWEEN @pmnMesIni AND @pmnMesFin
 AND Movimiento<>0 AND SUBSTRING(A.IdCuenta,1,1) IN ('1','2','3',@pmCue8,@pmCue9)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv)
 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CajaTot @pmtmEst CHAR(2),@pmtmTipo INT,@pmtmItem INT
,@pmtmConcepto VARCHAR(150),@pmtmValorTotal MONEY,@pmtmValorOtros MONEY
 AS 
 INSERT INTO tm_CajaTot (tmEst,tmTipo,tmItem,tmConcepto,tmValorTotal,tmValorOtros)
 VALUES (@pmtmEst,@pmtmTipo,@pmtmItem,@pmtmConcepto,@pmtmValorTotal,@pmtmValorOtros)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Cns @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,5,TipDoc,Consignacion,IdCia,Fecha,IdConcepto,IdCliente,'0',VrTotal,0,0,0,VrOtros,0,0,0,0,VrEfectivo AS ValNeto,VrEfectivo
,0,0,0,0,0,0,0,EnEfectivo,'EFE',NumConsigna,'0',Depositante,IdCta,Null,NitDeptante,0,'0',IdCajero,'0',Referencia,Referncia2
,'CONSIGNA','','',0,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Consigna
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Dfo @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.NitCliente,D.CdAgencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrRetencion+D.VrReteICA+D.VrReteIVA,D.VrFletes,D.VrCargos,D.VrOtrDcto,0,0,D.VrNeto,D.VrEfectivo
,D.VrPagosOtr,0,0,0,D.Cantidad,D.CantPuntos,0,D.EnEfectivo,F.CdForma,F.NumForma,F.CdBanco,SUBSTRING(F.DetallePago,1,150),'0',D.FecPedido,D.IdVend,D.TarifaCom,D.nClieCon,D.IdCajero,D.IdLocal,Referncia1,Referncia2
,D.Modalidad,'','PED',D.Pedido,D.IdCiaPed,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Factura,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevFco AS D INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Dnp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,0,VrSobretasa,0,0,0,0,VrImpGlobal,VrOtros,VrNeto,VrNeto,0,0,0,0,Cantidad
,0,0,1,'EFE','','0','','0',Null,IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','NOTACALIB','','0',0,'00',TipCom,Comprobante,IdCiaCom,0,nNota,FecDoc,Observacion,TimeSys
 FROM Trn_DevPas
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Drc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,CASE TipDoc WHEN 'RCO' THEN 3 ELSE 4 END,TipDev,Devolucion,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrTotal,0,0,0,0,VrTotDebito,VrTotCredito,0,0,VrEfectivo AS ValNeto,VrEfectivo
,0,0,0,0,0,0,0,EnEfectivo,'EFE',NumForma,CdBanco,'',CdCta,Null,'0',0,'0',IdCajero,'0',Referencia,'','RECIBOS','','',0,'00',TipCom,Comprobante,IdCiaCom,0,Recibo,FecDoc,Observacion,TimeSys
 FROM Trn_DevCaj
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Dvc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,5,TipDev,Devolucion,IdCia,Fecha,IdConcepto,IdCliente,'0',VrTotal,0,0,0,VrOtros,0,0,0,0,VrEfectivo AS ValNeto,VrEfectivo
,0,0,0,0,0,0,0,EnEfectivo,'EFE',NumConsigna,'0','',IdCta,Null,'0',0,'0',IdCajero,'0',Referencia,Referncia2
,ModdDev,'','',0,'00',TipCom,Comprobante,IdCiaCom,0,Consignacion,FecDoc,Observacion,TimeSys
 FROM Trn_DevCns
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Dvp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,VrConDcto+VrCreDcto,VrSobretasa,0,VrCreTotal+VrConTotal+VrCreImp+VrConImp,VrSobrantes,VrFaltantes,VrImpGlobal,VrOtros,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,0,0,0,'EFE','','0','','0',FecCorte,IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'',Modalidad,'','COR',NumCorte,'00'
,TipCom,Comprobante,IdCiaCom,0,Planilla,FecDoc,Observacion,TimeSys
 FROM Trn_DevPla
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Egr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,6,TipCom,Comprobante,IdCia,Fecha,'0',IdTercero,'0',VrTotal,0,0,0,0,0,0,0,0,VrTotal AS ValNeto,CASE EnEfectivo WHEN 1 THEN VrTotal ELSE 0 END
,0,0,0,0,0,0,0,EnEfectivo,CASE EnEfectivo WHEN 1 THEN  'EFE' ELSE 'CHE' END,NumCheque,IdBanco,SUBSTRING(Beneficiario,1,150),C.IdCta,FecCheque,'0',0,'0',IdUsuario,'0','',''
,'EGRESO',pVehiculo,TipDoc,Documento,IdCiaDoc,CASE WHEN TipEgr IS NULL OR LEN(TipEgr)=0 THEN '0' ELSE TipEgr END ,NEgreso,'00',Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Comprobantes AS C LEFT JOIN CtasCorrientes AS CT ON C.IdCta=CT.IdCta
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsEgreso<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Fco @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,1,TipDoc,Factura,IdCia,Fecha,IdConcepto,NitCliente,CdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion+VrReteICA+VrReteIVA,VrFletes,VrCargos,VrOtrDcto,0,0,VrNeto,VrEfectivo
,VrPagosOtr,0,0,0,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,SUBSTRING(DetallePago,1,150),'0',FecPedido,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referncia1,Referncia2
,Modalidad,'',TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Factcon
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Not @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,7,TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,0,VrImpuesto,VrRetencion+VrReteICA+VrReteIVA,VrSancion,0,0,0,0,VrOtros AS ValNeto,VrOtros
,0,0,0,0,Cantidad,0,0,1,CdForma,NumCheque,CdBanco,'','0',FecDcm,IdVend,TarifaCom,'0',CdCajero,IdLocal,Referencia,''
,Modalidad,pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,0,0,Null,Observacion,TimeSys
 FROM Trn_Notas
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND PrestCaja<>0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND CdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Caja_NotBus @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,7,TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,ValorTotal,0,ValorIva,ValorReteFte+ValorReteIca,ValorSancion,0,0,0,0,ValorNeto ,ValorNeto
,0,0,0,0,0,0,0,1,'EFE',NumCheque,IdBanco,'','0',FecVence,IdVendedor,Comision,'0',IdUsuario,'0',Referencia,''
,'CAJA',IdVehiculo,TipDcm,Documento,IdCiaDoc,TipCom,Comprobante,IdCiaCom,0,0,Null,Observacion,TimeSys
 FROM Trn_NotasBuses
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND PrestCaja<>0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Od1 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDev, Devolucion, D.IdCia, D.Fecha, D.IdConcepto,D.IdCliente, D.IdAgencia,D.ValorTotal, D.ValorDcto,D.ValorReteIva,D.ValorReteFte+D.ValorReteIca,D.ValorPagMas,D.ValorOtros,D.ValorOtrosDct,0,D.ValorAhorro,D.ValorNeto,0
,0,0,0,0,R.NPasajeros,0,0,D.EnEfectivo,'EFE',D.NumCheque,D.IdBanco,'',D.IdCta,D.FecDoc, D.IdVendedor, D.Comision,'0',D.IdUsuario,'0',R.Referencia,'',D.Modalidad,D.IdVehiculo,D.TipDoc, D.Recibo, D.IdCiaDoc,D.TipCom, D.Comprobante, D.IdCiaCom,0,0,Null, D.Observacion,D.TimeSys
 FROM Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Caja_Od2 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,D.VrSubTotal,0,D.VrReteIVA,D.VrRetencion+D.VrReteICA,D.VrPagosMas,D.VrOtros,D.VrOtrDcto,0,0,D.VrNeto,D.VrEfectivo
,0,0,0,0,0,0,0,D.EnEfectivo,CdForma,NumForma,CdBanco,SUBSTRING(DetallePago,1,150),CdCta,D.FecPago,D.IdVend,D.TarifaCom,'0',D.IdUsuario,'0',D.Referencia,''
,'RC2',R.pVehiculo,R.TipDcm,R.Documento,R.IdCiaDcm,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Recibo,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_FinDevRec AS D INNER JOIN Trn_FinRecibos AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Odb @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,D.VrSubTotal,D.VrDescuento,D.VrReteIVA,D.VrRetencion+D.VrReteICA,D.VrPagosMas,D.VrOtros,D.VrOtrDcto,0,D.VrAhorro,D.VrNeto,D.VrEfectivo
,0,0,0,0,0,0,0,D.EnEfectivo,CdForma,NumForma,CdBanco,SUBSTRING(DetallePago,1,150),CdCta,D.FecPago,D.IdVend,D.TarifaCom,'0',D.IdCajero,D.IdLocal,D.Referencia,''
,D.Modalidad,D.pVehiculo,D.TipDcm,D.Documento,D.IdCiaDcm,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Recibo,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevRec AS D INNER JOIN Trn_Recibos AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Pas @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDoc,nNota,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,0,VrSobretasa,0,0,0,0,VrImpGlobal,VrOtros,VrNeto,VrNeto,0,0,0,0,Cantidad
,0,0,1,'EFE','','0','','0',Null,IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','NOTACALIB','','0',0,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCalibra
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Pla @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,VrConDcto+VrCreDcto,VrSobretasa,0,VrCreTotal+VrConTotal+VrCreImp+VrConImp,VrSobrantes,VrFaltantes,VrImpGlobal,VrOtros,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,0,0,0,'EFE','','0','','0',FecCorte,IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'',Modalidad,'','COR',NumCorte,'00'
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCortes
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Rc1 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDoc, Recibo, IdCia, Fecha,IdConcepto, IdCliente, IdAgencia,ValorTotal, ValorDcto,ValorReteIva, ValorReteFte+ValorReteIca,ValorPagMas,ValorOtros,ValorOtrosDct,0,TotalAhorro,ValorNeto,0
,0,0,0,0,NPasajeros,0,0,EnEfectivo,'EFE',NumCheque,IdBanco,'',IdCta,Null, IdVendedor, Comision, '0',IdUsuario,'0',Referencia,''
,Modalidad,IdVehiculo,TipCar, Cartulina, IdCiaCar,TipCom, Comprobante, IdCiaCom, Anulado, NumDev, FecDev, Observacion,TimeSys
FROM Trn_RecBuses
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Caja_Rc2 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,0,VrReteIVA,VrRetencion+VrReteICA,VrPagosMas,VrOtros,VrOtrDcto,0,0,VrNeto,VrEfectivo
,0,0,0,0,0,0,0,EnEfectivo,CdForma,NumForma,CdBanco,SUBSTRING(DetallePago,1,150),CdCta,FecPago,IdVend,TarifaCom,'0',IdUsuario,'0',Referencia,''
,'RC2',pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_FinRecibos
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Rco @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,CASE TipDoc WHEN 'RCO' THEN 3 ELSE 4 END,TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdTercero,CdAgencia,VrTotal,0,0,0,0,VrTotDebito,VrTotCredito,0,0,VrEfectivo AS ValNeto,VrEfectivo
,0,0,0,0,0,0,0,EnEfectivo,'EFE',NumForma,CdBanco,'',CdCta,Null,'0',0,'0',IdCajero,'0',Referencia,'','RECIBOS','','',0,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_CompCaja
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Caja_Rec @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Caja (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo
,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros,Cantidad,CantPuntos,PuntosAcum,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrReteIVA,VrRetencion+VrReteICA,VrPagosMas,VrOtros,VrOtrDcto,0,VrAhorro,VrNeto,VrEfectivo
,0,0,0,0,0,0,0,EnEfectivo,CdForma,NumForma,CdBanco,SUBSTRING(DetallePago,1,150),CdCta,FecPago,IdVend,TarifaCom,'0',IdCajero,IdLocal,Referencia,''
,Modalidad,pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Recibos
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Cargos @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdVehiculo VARCHAR(10),@pmtmVehPropio BIT,@pmtmIdConductor VARCHAR(16),@pmtmIdCliente VARCHAR(16),@pmtmIdCargo VARCHAR(4),@pmtmConcepto VARCHAR(100),@pmtmReferencia VARCHAR(20)
,@pmtmFactura VARCHAR(15),@pmtmValor MONEY,@pmtmDetalle VARCHAR(100),@pmtmFecVence SMALLDATETIME,@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCiaDoc CHAR(2),@pmtmConductor VARCHAR(100),@pmtmAbonado MONEY,@pmtmTipDocAnt VARCHAR(3)
,@pmtmDocAnticipo INT,@pmtmCiaDocAnt CHAR(2),@pmtmPrestamoID INT,@pmtmIdCiaPre CHAR(2),@pmtmNumCuota INT,@pmtmFecCausa SMALLDATETIME,@pmtmValorCapital MONEY,@pmtmValorInteres MONEY,@pmtmIdCuenta VARCHAR(16),@pmtmCombCant DECIMAL(14,4),@pmtmCombValor MONEY
 AS 
 INSERT INTO tm_Cargos (tmNumero,tmItem,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdCliente,tmIdCargo,tmConcepto,tmReferencia,tmFactura,tmValor,tmDetalle,tmFecVence,tmTipDoc,tmDocumento,tmIdCiaDoc,tmConductor,tmAbonado,tmTipDocAnt,tmDocAnticipo,tmCiaDocAnt,tmPrestamoID,tmIdCiaPre,tmNumCuota
,tmFecCausa,tmValorCapital,tmValorInteres,tmIdCuenta,tmCombCant,tmCombValor)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdVehiculo,@pmtmVehPropio,@pmtmIdConductor,@pmtmIdCliente,@pmtmIdCargo,@pmtmConcepto,@pmtmReferencia,@pmtmFactura,@pmtmValor,@pmtmDetalle,@pmtmFecVence,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCiaDoc,@pmtmConductor,@pmtmAbonado
,@pmtmTipDocAnt,@pmtmDocAnticipo,@pmtmCiaDocAnt,@pmtmPrestamoID,@pmtmIdCiaPre,@pmtmNumCuota,@pmtmFecCausa,@pmtmValorCapital,@pmtmValorInteres,@pmtmIdCuenta,@pmtmCombCant,@pmtmCombValor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Cargos_Vce  @pmTipDoc VARCHAR(3), @pmDocumento INT, @pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
AS 
 INSERT INTO  tm_Cargos ( tmNumero, tmItem, tmIdVehiculo, tmVehPropio, tmIdCliente, tmIdCargo, tmConcepto, tmReferencia, tmFactura, tmValor, tmDetalle,tmIdConductor,tmFecVence,tmTipDoc, tmDocumento 
, tmIdCiaDoc, tmConductor,tmAbonado,tmTipDocAnt , tmDocAnticipo, tmCiaDocAnt ,tmIdCuenta,tmCombCant,tmCombValor)
 SELECT @pmtmNumero,Item,IdVehiculo,VehPropio,IdCliente,F.IdCargo,Concepto,Referencia,Factura,ValorFactura,Detalle,IdVendedor,FecVence,TipDoc,Documento,IdCia
,RazonSocial,ValorAtcpo,TipRec, Recibo, IdCiaRec,IdCuenta,CombCant,CombValor
 FROM  Trn_CausacVence AS V INNER JOIN Terceros AS C ON V.IdVendedor=C.IdTercero
 INNER JOIN Fijos AS F ON V.IdCargo=F.IdCargo
 WHERE   TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Cartera @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrFactura MONEY,@pmVrAbonado MONEY,@pmVrSaldo MONEY
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmComision DECIMAL(14,4),@pmTarifDcto DECIMAL(14,4),@pmTipoDcto CHAR(1),@pmTipoBase VARCHAR(10),@pmFecLmtDcto SMALLDATETIME,@pmIdConcepto VARCHAR(4)
,@pmReferencia VARCHAR(50),@pmDetalle VARCHAR(150),@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmFecUltPago SMALLDATETIME,@pmNumNota INT,@pmIdCiaNot CHAR(2),@pmTarifInt DECIMAL(14,4),@pmFecLiqMora SMALLDATETIME
,@pmCtaForma VARCHAR(30),@pmBeneficiario VARCHAR(150),@pmRefCheque VARCHAR(50),@pmNumCheque VARCHAR(20),@pmCdBanco VARCHAR(4)
 AS 
 INSERT INTO tm_Cartera (tmEst,TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,VrSaldo,IdCliente,IdAgencia,IdVend,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto,Referencia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot
,TarifInt,FecLiqMora,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 VALUES (@pmtmEst,@pmTipDoc,@pmFactura,@pmIdCia,@pmItem,@pmFecha,@pmFechaVence,@pmVrFactura,@pmVrAbonado,@pmVrSaldo,@pmIdCliente,@pmIdAgencia,@pmIdVend,@pmComision,@pmTarifDcto,@pmTipoDcto,@pmTipoBase,@pmFecLmtDcto
,@pmIdConcepto,@pmReferencia,@pmDetalle,@pmTipRef,@pmDocRef,@pmIdCiaRef,@pmFecUltPago,@pmNumNota,@pmIdCiaNot,@pmTarifInt,@pmFecLiqMora,@pmCtaForma,@pmBeneficiario,@pmRefCheque,@pmNumCheque,@pmCdBanco)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Cartera_Saf @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_Cartera (tmEst,TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,VrSaldo,IdCliente,IdAgencia,IdVend,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto
,Referencia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
SELECT @pmtmEst,'SAF',MAX(Recibo),IdCia,0,getdate(),getdate(),SUM(VrAFavor-VrOtrosDb),SUM(VrAbono),SUM((VrAFavor-VrOtrosDb)-VrAbono)
,IdCliente,IdAgencia,IdVend,0,0,'%','TOTAL',NULL,'0','SALDO A FAVOR','SALDO A FAVOR','0',0,'00',Null,0,'00',0,Null,'0','','','',''
 FROM Trn_RecAfavor
 WHERE  (VrAFavor-VrOtrosDb)>VrAbono AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 GROUP BY IdCia,IdCliente,IdAgencia,IdVend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Cartera_Sel @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 INSERT INTO tm_Cartera (tmEst,TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,VrSaldo,IdCliente,IdAgencia,IdVend,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto
,Referencia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
SELECT @pmtmEst,TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,VrFactura-VrAbonado,IdCliente,IdAgencia,IdVend,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto
,Referencia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,'0','','','',''
 FROM Trn_Vencimientos 
 WHERE  VrFactura>VrAbonado  AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Combo @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdProdBase VARCHAR(16)
,@pmtmCantidad DECIMAL(14,4),@pmtmIdBodega VARCHAR(4),@pmtmCdTanque VARCHAR(4),@pmtmIdUnd VARCHAR(4),@pmtmFactor DECIMAL(14,4)
,@pmtmUndFac VARCHAR(10),@pmtmServcios BIT,@pmtmLinea INT,@pmtmVrUnitario MONEY
 AS 
 INSERT INTO tm_Combo (tmNumero,tmItem,tmIdProducto,tmIdProdBase,tmCantidad,tmIdBodega,tmCdTanque,tmIdUnd,tmFactor,tmUndFac,tmServcios,tmLinea,tmVrUnitario)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmIdProdBase,@pmtmCantidad,@pmtmIdBodega,@pmtmCdTanque,@pmtmIdUnd,@pmtmFactor
,@pmtmUndFac,@pmtmServcios,@pmtmLinea,@pmtmVrUnitario)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Combo_Sel @pmtmNumero VARCHAR(5), @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_Combo (tmNumero,tmItem,tmIdProducto,tmIdProdBase,tmCantidad,tmIdBodega,tmCdTanque,tmIdUnd,tmFactor,tmUndFac,tmServcios,tmLinea,tmVrUnitario)
 SELECT @pmtmNumero,Item,CdProdEquiv,IdProducto,Entradas+Salidas,IdBodega,CdTanque,IdUnd,Unidades,SUBSTRING(Referencia,1,10),Servcios,ItemCombo,VrUnitario
 FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc  AND Documento=@pmDocumento AND  IdCia=@pmIdCia AND EsProdBase=1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Dfc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,1,Fecha,FecDoc,IdVend,TarifaCom,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,IdConcepto,'0',Cantidad,'0',IdUsuario,'0',0,IdLocEnv,'0',Modalidad,TipDoc,Factura,IdCiaDoc,0,Observacion,'',ModdDev
 FROM Trn_DevFcr
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%') AND TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_DfcDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdLinea VARCHAR(8)=Null
,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,Item,D.Fecha,FecDoc,K.IdVend,Comision,IdCliente,IdAgencia,VrPrecio*Entradas,VrDctoEnt,VrIvaEnt,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,D.IdConcepto,K.IdProducto,Entradas,'0',D.IdUsuario,CdOperario,ComisnOper,CdLocal,CdSzona,Modalidad,D.TipDoc,D.Factura,IdCiaDoc,0,Observacion,Descripcion,K.Referencia
 FROM Trn_DevFcr AS D INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND CdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND (Comision>ISNULL(@pmTarifaCom,-1) OR ComisnOper>ISNULL(@pmTarifaCom,-1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') 
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%') AND IdMarca LIKE ISNULL(@pmIdMarca,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Dfo @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,1,Fecha,FecDoc,IdVend,TarifaCom,NitCliente,CdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,IdConcepto,'0',Cantidad,nClieCon,IdCajero,'0',0,IdLocal,'0',Modalidad,TipDoc,Factura,IdCiaDoc,0,Observacion,'',ModdDev
 FROM Trn_DevFco
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND NitCliente LIKE ISNULL(@pmIdCliente,'%') AND CdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_DfoDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdLinea VARCHAR(8)=Null
,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,Item,D.Fecha,FecDoc,K.IdVend,Comision,NitCliente,D.CdAgencia,VrPrecio*Entradas,VrDctoEnt,VrIvaEnt,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,D.IdConcepto,K.IdProducto,Entradas,nClieCon,IdCajero,CdOperario,ComisnOper,CdLocal,CdSzona,Modalidad,D.TipDoc,D.Factura,IdCiaDoc,0,Observacion,Descripcion,K.Referencia
 FROM Trn_DevFco AS D INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.NitCliente LIKE ISNULL(@pmIdCliente,'%') AND D.CdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND CdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND (Comision>ISNULL(@pmTarifaCom,-1) OR ComisnOper>ISNULL(@pmTarifaCom,-1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') 
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%') AND IdMarca LIKE ISNULL(@pmIdMarca,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Dvp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,1,Fecha,FecDoc,IdVend,TarifaCom,NitTercero,'0',VrSubTotal+VrConTotal+VrCreTotal,VrConDcto+VrCreDcto,VrConImp+VrCreImp,VrSobretasa+VrImpGlobal,VrOtros
,VrSobrantes,VrFaltantes,IdConcepto,'0',Cantidad+ConCant+CreCant,NitRef,IdCajero,'0',0,'0','0',Modalidad,TipDoc,Planilla,IdCiaDoc,0,Observacion,ModdDev,Referencia
 FROM Trn_DevPla
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND  TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_DvpDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmTarifaCom DECIMAL(14,4),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)

SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,Item,D.Fecha,FecDoc,K.IdVend,Comision,NitTercero,'0',VrPrecio*Entradas,VrDctoEnt,VrIvaEnt,VrSobretasa+VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes
,D.IdConcepto,K.IdProducto,Entradas,NitRef,IdCajero,CdOperario,ComisnOper,CdLocal,CdSzona,Modalidad,D.TipDoc,Planilla,IdCiaDoc,0,Observacion,Descripcion,K.Referencia
 FROM Trn_DevPla AS D INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND CdLocal LIKE ISNULL(@pmIdLocEnv,'%') 
 AND (Comision>ISNULL(@pmTarifaCom,-1) OR ComisnOper>ISNULL(@pmTarifaCom,-1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') 
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%') AND IdMarca LIKE ISNULL(@pmIdMarca,'%') 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Fco @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDoc,Factura,IdCia,1,Fecha,Fecha AS FecVence,IdVend,TarifaCom,NitCliente,CdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,IdConcepto,'0',Cantidad,nClieCon,IdCajero,'0',0,IdLocal,'0',Modalidad,TipPed,Pedido,IdCiaPed,Anulado,Observacion,DetallePago,Referncia1
 FROM Trn_Factcon
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND NitCliente LIKE ISNULL(@pmIdCliente,'%') AND CdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_FcoDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdLinea VARCHAR(8)=Null
,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
 SELECT @pmtmEst,F.TipDoc,F.Factura,F.IdCia,Item,F.Fecha,K.Fecha,K.IdVend,Comision,NitCliente,F.CdAgencia,VrPrecio*Salidas,VrDctoSal,VrIvaSal,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,F.IdConcepto,K.IdProducto,Salidas,nClieCon,IdCajero,CdOperario,ComisnOper,CdLocal,CdSzona,Modalidad,TipPed,Pedido,IdCiaPed,Anulado,Observacion,Descripcion,K.Referencia
 FROM Trn_Factcon AS F INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  F.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND NitCliente LIKE ISNULL(@pmIdCliente,'%') AND F.CdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND CdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND (Comision>ISNULL(@pmTarifaCom,-1) OR ComisnOper>ISNULL(@pmTarifaCom,-1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') 
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%') AND IdMarca LIKE ISNULL(@pmIdMarca,'%') 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Fcr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDoc,Factura,IdCia,1,Fecha,FechaVence,IdVend,TarifaCom,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,IdConcepto,'0',Cantidad,'0',IdUsuario,'0',0,IdLocEnv,'0',Modalidad,TipPed,Pedido,IdCiaPed,Anulado,Observacion,DetallePago,pVehiculo
 FROM Trn_Facturas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%') AND TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_FcrDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdLinea VARCHAR(8)=Null
,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,F.TipDoc,F.Factura,F.IdCia,Item,F.Fecha,FechaVence,K.IdVend,Comision,IdCliente,IdAgencia,VrPrecio*Salidas,VrDctoSal,VrIvaSal,VrRetencion+VrReteICA,VrFletes,VrCargos,VrOtrDcto
,F.IdConcepto,K.IdProducto,Salidas,'0',F.IdUsuario,CdOperario,ComisnOper,CdLocal,CdSzona,Modalidad,TipPed,Pedido,IdCiaPed,Anulado,Observacion,Descripcion,K.Referencia
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  F.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND CdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND (Comision>ISNULL(@pmTarifaCom,-1) OR ComisnOper>ISNULL(@pmTarifaCom,-1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') 
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%') AND IdMarca LIKE ISNULL(@pmIdMarca,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Ncr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,A.TipRec,A.Recibo,A.IdCia,Item,A.Fecha,A.FecPago,A.IdVend,Comision,A.IdCliente,A.IdAgencia,VrBaseCms,0,VrImpuesto,VrRetencion+VrReteICA,VrSancion,VrOtros,VrDescto
,IdConcepto,CdProducto,Cantidad,'0',CdCajero,'0',0,IdLocal,'0',Modalidad,A.TipDoc,Factura,IdCiaFac,0,Observacion,Detalle,Referencia
 FROM  Trn_VencAbonos AS A INNER JOIN Trn_Notas AS N ON A.TipRec=N.TipDoc AND A.Recibo=N.NumNota  AND  A.IdCia=N.IdCia
 WHERE TipRec='NCR'  AND  A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND A.IdVend LIKE ISNULL(@pmIdVend,'%') AND A.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND A.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND A.Comision>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Ndb @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDoc,NumNota,IdCia,1,Fecha,FechaVence,IdVend,TarifaCom,IdCliente,IdAgencia,VrSubTotal,0,VrImpuesto,VrRetencion+VrReteICA,VrSancion,VrOtros,0
,IdConcepto,CdProducto,Cantidad,'0',CdCajero,'0',0,IdLocal,'0',Modalidad,TipDcm,Documento,IdCiaDcm,0,Observacion,NumCheque,Referencia
 FROM  Trn_Notas 
 WHERE TipDoc='NDB'  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Odb @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,A.TipRec,A.Recibo,A.IdCia,Item,A.Fecha,A.FecPago,A.IdVend,Comision,A.IdCliente,A.IdAgencia,CASE WHEN VrBaseCms<0 THEN VrBaseCms*-1 ELSE VrBaseCms END,0,VrReteIVA,VrRetencion+VrReteICA,VrPagosMas,VrOtros,VrDescto
,IdConcepto,'0',0,'0',IdCajero,'0',0,IdLocal,'0',Modalidad,A.TipDoc,Factura,IdCiaFac,0,Observacion,Detalle,Referencia
 FROM  Trn_VencAbonos AS A INNER JOIN Trn_DevRec AS R ON A.TipRec=R.TipDev AND A.Recibo=R.Devolucion AND   A.IdCia=R.IdCia
 WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND A.IdVend LIKE ISNULL(@pmIdVend,'%') AND A.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND A.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND A.Comision>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Pla @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipDoc,Planilla,IdCia,1,Fecha,FecCorte,IdVend,TarifaCom,NitTercero,'0',VrSubTotal+VrConTotal+VrCreTotal,VrConDcto+VrCreDcto,VrConImp+VrCreImp,VrSobretasa+VrImpGlobal,VrOtros
,VrSobrantes,VrFaltantes,IdConcepto,'0',Cantidad,NitRef,IdCajero,'0',0,'0','0',Modalidad,'COR',NumCorte,'00',Anulado,Observacion,'',Referencia
 FROM Trn_EdsCortes
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND  TarifaCom>ISNULL(@pmTarifaCom,-1)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_PlaDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmTarifaCom DECIMAL(14,4),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
 SELECT @pmtmEst,C.TipDoc,Planilla,C.IdCia,Item,C.Fecha,FecCorte,K.IdVend,Comision,NitTercero,'0',VrPrecio*Salidas,VrDctoSal,VrIvaSal,VrSobretasa+VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes
,C.IdConcepto,K.IdProducto,Salidas,NitRef,IdCajero,CdOperario,ComisnOper,CdLocal,CdSzona,Modalidad,'COR',NumCorte,'00',Anulado,Observacion,Descripcion,K.Referencia
 FROM Trn_EdsCortes AS C INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.Planilla=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND CdLocal LIKE ISNULL(@pmIdLocEnv,'%') 
 AND (Comision>ISNULL(@pmTarifaCom,-1) OR ComisnOper>ISNULL(@pmTarifaCom,-1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') 
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%') AND IdMarca LIKE ISNULL(@pmIdMarca,'%') 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comisiones_Rec @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmTarifaCom DECIMAL(14,4)
 AS 
 INSERT INTO tm_Comisiones (tmEst,TipDoc,Numero,IdCia,Item,Fecha,FecVence,IdVend,TarifaCom,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos
,IdConcepto,CdProducto,Cantidad,nClieCon,IdCajero,CdOperario,ComisnOper,IdLocal,CdSzona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,Observacion,Detalles,Referencia)
SELECT @pmtmEst,TipRec,A.Recibo,A.IdCia,Item,A.Fecha,A.FecPago,A.IdVend,Comision,A.IdCliente,A.IdAgencia,VrBaseCms,0,VrReteIVA,VrRetencion+VrReteICA,VrPagosMas,VrOtros,VrDescto
,IdConcepto,'0',0,'0',IdCajero,'0',0,IdLocal,'0',Modalidad,A.TipDoc,Factura,IdCiaFac,Anulado,Observacion,Detalle,Referencia
 FROM  Trn_VencAbonos AS A INNER JOIN Trn_Recibos AS R ON A.TipRec=R.TipDoc AND A.Recibo=R.Recibo AND   A.IdCia=R.IdCia
 WHERE A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND A.IdVend LIKE ISNULL(@pmIdVend,'%') AND A.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND A.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdLocal LIKE ISNULL(@pmIdLocEnv,'%') AND A.Comision>ISNULL(@pmTarifaCom,-1)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comp @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdCuenta VARCHAR(16),@pmtmDetalle VARCHAR(250),@pmtmVrDebito MONEY,@pmtmVrCredito MONEY,@pmtmIdTercero VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16)
,@pmtmVrBase MONEY,@pmtmTarifaBase DECIMAL(10,4),@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCiaDoc CHAR(2),@pmtmCodConce VARCHAR(4),@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15),@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmFecVence SMALLDATETIME,@pmtmCodCta VARCHAR(4)
,@pmtmNumCheque VARCHAR(20),@pmtmIntegrado BIT,@pmtmTipoAplica CHAR(1),@pmtmConsolida BIT,@pmtmCodCargo VARCHAR(4),@pmtmNitOtros VARCHAR(16),@pmtmCodSubgpo VARCHAR(8),@pmtmCiuOrigen VARCHAR(8),@pmtmCodAgncia VARCHAR(16),@pmtmVehPropio BIT,@pmtmReferncia VARCHAR(50),@pmtmTipCom VARCHAR(3)
,@pmtmComprobante INT,@pmtmIdCia CHAR(2),@pmtmNombre VARCHAR(100),@pmtmCcosto VARCHAR(100),@pmtmSubcc VARCHAR(100),@pmtmTercero VARCHAR(150),@pmtmTipDocRef VARCHAR(3),@pmtmDocRef INT,@pmtmIdCiaRef CHAR(2),@pmtmNitDoc VARCHAR(16)
 AS 
 INSERT INTO tm_Comp (tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo
,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmTipCom,tmComprobante,tmIdCia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdCuenta,@pmtmDetalle,@pmtmVrDebito,@pmtmVrCredito,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmVrBase,@pmtmTarifaBase,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCiaDoc,@pmtmCodConce,@pmtmTipFac,@pmtmFactura,@pmtmIdCiaFac,@pmtmItemFac,@pmtmFecVence
,@pmtmCodCta,@pmtmNumCheque,@pmtmIntegrado,@pmtmTipoAplica,@pmtmConsolida,@pmtmCodCargo,@pmtmNitOtros,@pmtmCodSubgpo,@pmtmCiuOrigen,@pmtmCodAgncia,@pmtmVehPropio,@pmtmReferncia,@pmtmTipCom,@pmtmComprobante,@pmtmIdCia,@pmtmNombre,@pmtmCcosto,@pmtmSubcc,@pmtmTercero
,@pmtmTipDocRef,@pmtmDocRef,@pmtmIdCiaRef,@pmtmNitDoc)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comp_Caj @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_Comp (tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodAgncia,tmVehPropio,tmReferncia,tmComprobante,tmNombre,tmCcosto,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef)
SELECT  @pmtmNumero,Item,D.IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos,VrBase,TarifaBase,TipDoc,Recibo,IdCia,IdConcepto,TipFac,Factura,IdCiaFac,ItemFac,FecVence
,CodCta,NumCheque,1,TipoAplica,0,IdConcepto,NitOtros,CodAgencia,0,Referncia,0,SUBSTRING(NomCuenta,1,100),SUBSTRING(CCosto,1,100),SUBSTRING(RazonSocial,1,150),TipDocRef,DocRef,IdCiaRef
 FROM Trn_CompCajaDet AS D  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero
 LEFT JOIN CentroCosto AS O ON D.CdCCosto=O.IdCCosto
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Comp_Sel @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Comp (tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce
,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo
,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmTipCom,tmComprobante,tmIdCia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc)
 SELECT @pmtmNumero,Item,D.IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,IdVehiculo,D.IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia
,TipCom,Comprobante,IdCia,SUBSTRING(NomCuenta,1,100),SUBSTRING(CCosto,1,100),'',SUBSTRING(RazonSocial,1,150),TipDocRef,DocRef,IdCiaRef,NitDoc
 FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 LEFT JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 LEFT JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Compau @pmtmNumero VARCHAR(5),@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCia CHAR(2)
 AS 
 INSERT INTO tm_Compau (tmNumero,tmTipCom,tmComprobante,tmIdCia,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos
,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica
,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc)
 SELECT tmNumero,@pmtmTipCom,@pmtmComprobante,@pmtmIdCia,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos
,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica
,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CompauAbr @pmtmNumero VARCHAR(5),@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCia CHAR(2)
 AS 
 INSERT INTO  tm_Comp (tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos
,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica
,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
,tmTipCom,tmComprobante,tmIdCia)
SELECT tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos
,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica
,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
,tmTipCom,tmComprobante,tmIdCia
FROM  tm_Compau 
WHERE  tmNumero=@pmtmNumero AND tmTipCom=@pmtmTipCom AND tmComprobante=@pmtmComprobante  AND tmIdCia=@pmtmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Contratos @pmtmEst CHAR(2),@pmtmItem INT,@pmtmContrato INT,@pmtmIdCia CHAR(2)
,@pmtmTipoProd VARCHAR(20),@pmtmCdProducto VARCHAR(16),@pmtmPeriodo VARCHAR(6),@pmtmPresptoMes DECIMAL(14,4),@pmtmPresptoAnno DECIMAL(14,4),@pmtmCantNeto DECIMAL(14,4),@pmtmValorNeto MONEY
 AS 
 INSERT INTO tm_Contratos (tmEst,tmItem,tmContrato,tmIdCia,tmTipoProd,tmCdProducto,tmPeriodo,tmPresptoMes,tmPresptoAnno,tmCantNeto,tmValorNeto)
 VALUES (@pmtmEst,@pmtmItem,@pmtmContrato,@pmtmIdCia,@pmtmTipoProd,@pmtmCdProducto,@pmtmPeriodo
,@pmtmPresptoMes,@pmtmPresptoAnno,@pmtmCantNeto,@pmtmValorNeto)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Cuotas @pmtmEst CHAR(2),@pmtmPrestamoID INT,@pmtmIdCia CHAR(2),@pmtmNumCuota INT,@pmtmFecCausa SMALLDATETIME, @pmtmFecVence SMALLDATETIME, @pmtmValorCapital MONEY
, @pmtmValorInteres MONEY,@pmtmValorCuota MONEY,@pmtmSaldo MONEY, @pmtmValorCausado MONEY, @pmtmValorAbonado MONEY,@pmtmFecPago SMALLDATETIME, @pmtmTipRec VARCHAR(3), @pmtmRecibo INT, @pmtmIdCiaRec CHAR(2)
AS
 INSERT INTO tm_Cuotas (tmEst, tmPrestamoID, tmIdCia, tmNumCuota, tmFecCausa, tmFecVence, tmValorCapital, tmValorInteres,tmValorCuota, tmSaldo, tmValorCausado, tmValorAbonado,tmFecPago, tmTipRec, tmRecibo, tmIdCiaRec)
 VALUES ( @pmtmEst,@pmtmPrestamoID,@pmtmIdCia,@pmtmNumCuota,@pmtmFecCausa, @pmtmFecVence, @pmtmValorCapital, @pmtmValorInteres,@pmtmValorCuota,@pmtmSaldo, @pmtmValorCausado, @pmtmValorAbonado
,@pmtmFecPago, @pmtmTipRec, @pmtmRecibo, @pmtmIdCiaRec)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxCobrar @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmFecVence SMALLDATETIME,@pmtmVrFactura MONEY,@pmtmTarifDcto DECIMAL(14,4)
,@pmtmTipoDcto CHAR(1),@pmtmTipoBase VARCHAR(10),@pmtmFecLmtDcto SMALLDATETIME,@pmtmReferencia VARCHAR(50),@pmtmDetalle VARCHAR(150)
,@pmtmTipRef VARCHAR(3),@pmtmDocRef INT,@pmtmIdCiaRef CHAR(2),@pmtmTotAbono MONEY,@pmtmFecPlazo SMALLDATETIME
 AS 
 INSERT INTO tm_CxCobrar (tmNumero,tmItem,tmFecVence,tmVrFactura,tmTarifDcto,tmTipoDcto,tmTipoBase,tmFecLmtDcto,tmReferencia,tmDetalle,tmTipRef,tmDocRef,tmIdCiaRef,tmTotAbono,tmFecPlazo)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmFecVence,@pmtmVrFactura,@pmtmTarifDcto,@pmtmTipoDcto,@pmtmTipoBase,@pmtmFecLmtDcto,@pmtmReferencia
,@pmtmDetalle,@pmtmTipRef,@pmtmDocRef,@pmtmIdCiaRef,@pmtmTotAbono,@pmtmFecPlazo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxCobrar_Sel @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_CxCobrar (tmNumero,tmItem,tmFecVence,tmVrFactura,tmTarifDcto,tmTipoDcto,tmTipoBase,tmFecLmtDcto,tmReferencia,tmDetalle,tmTipRef,tmDocRef,tmIdCiaRef,tmTotAbono,tmFecPlazo)
 SELECT @pmtmNumero,Item,FechaVence,VrFactura,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,Referencia,Detalle,TipRef,DocRef,IdCiaRef,0,FecPlazoCal
 FROM Trn_Vencimientos 
 WHERE TipDoc=@pmTipDoc  AND Factura=@pmFactura  AND  IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxPagar @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProveedor VARCHAR(16),@pmtmFecVence SMALLDATETIME,@pmtmIdCuenta VARCHAR(16),@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT
,@pmtmIdCiaDoc CHAR(2),@pmtmVrFactura MONEY,@pmtmItemCom INT,@pmtmReferencia VARCHAR(50),@pmtmDetalle VARCHAR(100),@pmtmpVehiculo VARCHAR(10),@pmtmVehPropio BIT,@pmtmTipRef VARCHAR(3),@pmtmDocRef INT
,@pmtmIdCiaRef CHAR(2),@pmtmProveedor VARCHAR(250)
 AS 
 INSERT INTO tm_CxPagar (tmNumero,tmItem,tmIdProveedor,tmFecVence,tmIdCuenta,tmTipDoc,tmDocumento,tmIdCiaDoc,tmVrFactura,tmItemCom,tmReferencia,tmDetalle,tmpVehiculo,tmVehPropio,tmTipRef,tmDocRef,tmIdCiaRef,tmProveedor)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProveedor,@pmtmFecVence,@pmtmIdCuenta,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCiaDoc,@pmtmVrFactura,@pmtmItemCom,@pmtmReferencia,@pmtmDetalle,@pmtmpVehiculo
,@pmtmVehPropio,@pmtmTipRef,@pmtmDocRef,@pmtmIdCiaRef,@pmtmProveedor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxPagar_Doc  @pmTipFac VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProveedor VARCHAR(16),@pmFactura VARCHAR(15),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_CxPagar (tmNumero,tmItem,tmIdProveedor,tmFecVence,tmIdCuenta,tmTipDoc,tmDocumento,tmIdCiaDoc,tmVrFactura,tmItemCom,tmReferencia,tmDetalle
,tmpVehiculo,tmVehPropio,tmTipRef,tmDocRef,tmIdCiaRef,tmProveedor)
 SELECT @pmtmNumero,Item,IdProveedor,FecVence,IdCuenta,TipDoc,Documento,IdCiaDoc,VrFactura,ItemCom,Referencia,Detalle
,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,RazonSocial
 FROM Trn_ComFactura AS F INNER JOIN Terceros AS T ON F.IdProveedor=T.IdTercero
 WHERE TipFac=@pmTipFac AND TipDoc=@pmTipFac AND Documento=@pmDocumento AND  IdCia=@pmIdCia AND IdCiaDoc=@pmIdCia
 AND ItemCom<=0 AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND Factura LIKE ISNULL(@pmFactura,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxPagar_Sel @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCia CHAR(2)
,@pmIdProveedor VARCHAR(16),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_CxPagar (tmNumero,tmItem,tmIdProveedor,tmFecVence,tmIdCuenta,tmTipDoc,tmDocumento,tmIdCiaDoc,tmVrFactura,tmItemCom,tmReferencia,tmDetalle
,tmpVehiculo,tmVehPropio,tmTipRef,tmDocRef,tmIdCiaRef,tmProveedor)
 SELECT @pmtmNumero,Item,IdProveedor,FecVence,IdCuenta,TipDoc,Documento,IdCiaDoc,VrFactura,ItemCom,Referencia,Detalle
,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,RazonSocial
 FROM Trn_ComFactura AS F INNER JOIN Terceros AS T ON F.IdProveedor=T.IdTercero
 WHERE TipFac=@pmTipFac  AND Factura=@pmFactura  AND IdCia=@pmIdCia AND IdProveedor=@pmIdProveedor
 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxcAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmFactura INT,@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmVrAbono MONEY,@pmtmVrDescto MONEY
,@pmtmVrBaseCms MONEY,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmComision DECIMAL(14,4),@pmtmDetalle VARCHAR(150),@pmtmTipoAplica VARCHAR(10)
,@pmtmCodTarDct VARCHAR(4),@pmtmCodTarCom VARCHAR(4),@pmtmTipRec VARCHAR(3),@pmtmRecibo INT,@pmtmCiaRec CHAR(2),@pmtmItemRec INT,@pmtmFecPago SMALLDATETIME
 AS 
 INSERT INTO tm_CxcAbonos (tmNumero,tmItem,tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmVrDescto,tmVrBaseCms,tmIdCliente,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmTipoAplica
,tmCodTarDct,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmFactura,@pmtmIdCiaFac,@pmtmItemFac,@pmtmVrAbono,@pmtmVrDescto,@pmtmVrBaseCms,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmComision
,@pmtmDetalle,@pmtmTipoAplica,@pmtmCodTarDct,@pmtmCodTarCom,@pmtmTipRec,@pmtmRecibo,@pmtmCiaRec,@pmtmItemRec,@pmtmFecPago)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxcAbonos_Sel @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_CxcAbonos (tmNumero,tmItem,tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmVrDescto,tmVrBaseCms,tmIdCliente
,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmTipoAplica,tmCodTarDct,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago)
 SELECT @pmtmNumero,Item,TipDoc,Factura,IdCiaFac,ItemFac,VrAbono,VrDescto,VrBaseCms,IdCliente,IdAgencia
,IdVend,Comision,Detalle,TipoAplica,'','',TipRec,Recibo,IdCia,Item AS ItemRec,FecPago
 FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxpAplica @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProveedor VARCHAR(16),@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15)
,@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmVrAbono MONEY,@pmtmIdCuenta VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmVehPropio BIT,@pmtmTipDev VARCHAR(3)
,@pmtmNumDev INT,@pmtmDetAbono VARCHAR(100),@pmtmProveedor VARCHAR(250)
 AS 
 INSERT INTO tm_CxpAplica (tmNumero,tmItem,tmIdProveedor,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmIdCuenta,tmpVehiculo,tmVehPropio,tmTipDev,tmNumDev,tmDetAbono,tmProveedor)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProveedor,@pmtmTipFac,@pmtmFactura,@pmtmIdCiaFac,@pmtmItemFac,@pmtmVrAbono,@pmtmIdCuenta,@pmtmpVehiculo,@pmtmVehPropio
,@pmtmTipDev,@pmtmNumDev,@pmtmDetAbono,@pmtmProveedor)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxpAplica_Doc @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmFactura VARCHAR(15)=Null,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_CxpAplica (tmNumero,tmItem,tmIdProveedor,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmIdCuenta,tmpVehiculo,tmVehPropio,tmTipDev,tmNumDev,tmDetAbono,tmProveedor)
 SELECT @pmtmNumero,Item,IdProveedor,TipFac,Factura,IdCiaFac,ItemFac,VrAbono,IdCuenta,pVehiculo,VehPropio,TipDev,NumDev,DetAbono,RazonSocial
 FROM Trn_EgrFactura AS E INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero
 WHERE  TipCom=@pmTipCom AND Comprobante=@pmComprobante  AND IdCia=@pmIdCia
 AND TipDev=@pmTipCom AND NumDev=@pmComprobante AND EsEgreso=0
 AND Factura LIKE ISNULL(@pmFactura,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_CxpAplica_Sel @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_CxpAplica (tmNumero,tmItem,tmIdProveedor,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmIdCuenta,tmpVehiculo,tmVehPropio,tmTipDev,tmNumDev,tmDetAbono,tmProveedor)
 SELECT @pmtmNumero,Item,IdProveedor,TipFac,Factura,IdCiaFac,ItemFac,VrAbono,IdCuenta,pVehiculo,VehPropio,TipDev,NumDev,DetAbono,RazonSocial
 FROM Trn_EgrFactura AS E INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero
 WHERE  TipCom=@pmTipCom AND Comprobante=@pmComprobante  AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_DetCue @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmConcepto VARCHAR(10),@pmtmIdCuenta VARCHAR(16),@pmtmValor MONEY,@pmtmDetalle VARCHAR(150),@pmtmIdTercero VARCHAR(16)
,@pmtmCodAgncia VARCHAR(16),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmVrBase MONEY,@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15),@pmtmIdCiaFac CHAR(2)
,@pmtmItemFac INT,@pmtmFecVence SMALLDATETIME,@pmtmCheque VARCHAR(20),@pmtmCiuOrigen VARCHAR(8),@pmtmReferencia VARCHAR(50)
 AS 
 INSERT INTO tm_DetCue (tmNumero,tmItem,tmConcepto,tmIdCuenta,tmValor,tmDetalle,tmIdTercero,tmCodAgncia,tmCdCCosto,tmCdSubCos,tmpVehiculo,tmVrBase,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCheque,tmCiuOrigen,tmReferencia)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmConcepto,@pmtmIdCuenta,@pmtmValor,@pmtmDetalle,@pmtmIdTercero,@pmtmCodAgncia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmpVehiculo,@pmtmVrBase,@pmtmTipFac,@pmtmFactura,@pmtmIdCiaFac
,@pmtmItemFac,@pmtmFecVence,@pmtmCheque,@pmtmCiuOrigen,@pmtmReferencia)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_DetCue_Pto @pmtmNumero VARCHAR(5),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO  tm_DetCue (tmNumero,tmItem,tmConcepto,tmIdCuenta,tmValor,tmDetalle,tmIdTercero,tmCodAgncia,tmCdCCosto,tmCdSubCos
,tmpVehiculo,tmVrBase,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCheque,tmCiuOrigen,tmReferencia)
 SELECT @pmtmNumero,Item,'0',IdCuenta,VrPresupuesto,'PRESUPUESTO','0','0',IdCCosto,IdSubCos
,'0',TotalDebitos-TotalCreditos,'','','00',0,FecUpdate,'','',CdUsuario
 FROM Trn_PresDetalle
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_DetCue_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmConcepto VARCHAR(10)
 AS 
 INSERT INTO  tm_DetCue (tmNumero,tmItem,tmConcepto,tmIdCuenta,tmValor,tmDetalle,tmIdTercero,tmCodAgncia,tmCdCCosto,tmCdSubCos
,tmpVehiculo,tmVrBase,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCheque,tmCiuOrigen,tmReferencia)
 SELECT @pmtmNumero,Item,Concepto,IdCuenta,Valor,Detalle,IdTercero,CodAgncia,CdCCosto,CdSubCos,pVehiculo,VrBase,TipFac,Factura,IdCiaFac,ItemFac
,FecVence,Cheque,CiuOrigen,Referencia
 FROM Trn_DetCuentas
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND Concepto LIKE ISNULL(@pmConcepto,'%')
 ORDER BY  Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Che @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,R.TipDoc,R.Recibo,R.IdCia,R.Fecha,'0',IdCliente,IdAgencia,VrForma,0,0,0,0,0,0,0,0,0,0,0,0,VrForma AS ValCheque,0,VrForma AS ValorOtros,0,0,0,'0',0,0,TipForma,Numero,IdBanco
,Detalle,CASE CdCta WHEN Null THEN '0' WHEN '' THEN '0' ELSE CdCta END,FecPago,CdVend,0,'0',R.IdUsuario,IdLocal,Referncia1,Referncia2,Beneficiario,'CHEQUES','',C.TipDoc,Documento,IdCiaDoc,'0',0,'00',R.Anulado,NumDev,R.FecDev,Observacion,TimeSys
 FROM Trn_RecCheques AS R INNER JOIN Trn_Cheques AS C ON R.TipDoc=C.TipRec AND R.Recibo=C.Recibo AND R.IdCia=C.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Cns @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,C.TipDoc,C.Consignacion,C.IdCia,C.Fecha,IdConcepto,NitCliente,CdAgencia,VrPagado,VrCambio,0,0,0,0,0,0,0,0,0,0,0,VrPagado-VrCambio,CASE IdForma WHEN 'EFE' THEN VrPagado-VrCambio ELSE 0 END
,0,0,0,0,'0',0,EnEfectivo,IdForma,NumForma,IdBanco,Detalle,CASE CdCta WHEN NULL THEN '0' WHEN '' THEN '0' ELSE CdCta  END,FecForma,NitDeptante,0,'0',IdCajero,CdLocal,Referncia1,P.Referncia2,Beneficiario,'CONSIGNA','',TipRef,DocRef,IdCiaRef
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Consigna AS C INNER JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND C.Consignacion=P.Documento AND C.IdCia=P.IdCia
 WHERE  C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dfc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrSobretasa,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrFletes
,D.VrCargos,D.VrOtrDcto,D.VrImpGlobal,D.VrOtros,D.VrCostos,D.VrNeto,0,0,0,0,D.CantPuntos,'0',D.Cantidad,MulPlazos,IdForma,'','0'
,DetallePago,'0',F.FecPedido,D.IdVend,D.TarifaCom,'0',D.IdUsuario,D.IdLocEnv,ModdDev,'','',D.Modalidad,pVehiculo,TipPed,D.Pedido,D.IdCiaPed,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Factura,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevFcr AS D INNER JOIN  Trn_Facturas AS F ON D.TipDoc=F.TipDoc  AND D.Factura=F.Factura  AND D.IdCiaDoc=F.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_DfcDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,KarValor,KarDcto,KarIva,KarSobTasa,KarReteFte,KarIca,D.VrReteIVA,D.VrFletes
,D.VrCargos,D.VrOtrDcto,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarIva-KarDcto+KarImpGlobal+KarSobTasa+KarSoldicom,0,0,0,0,D.CantPuntos,IdProducto,KarCantidad,MulPlazos,IdForma,'','0'
,DetallePago,'0',F.FecPedido,D.IdVend,D.TarifaCom,'0',D.IdUsuario,D.IdLocEnv,ModdDev,'','',D.Modalidad,pVehiculo,TipPed,D.Pedido,D.IdCiaPed,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Factura,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevFcr AS D INNER JOIN  Trn_Facturas AS F ON D.TipDoc=F.TipDoc  AND D.Factura=F.Factura  AND D.IdCiaDoc=F.IdCia
 INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Entradas) AS KarCantidad,SUM(VrPrecio*Entradas) AS KarValor
,SUM(VrIvaEnt) AS KarIva,SUM(VrDctoEnt) AS KarDcto,SUM(VrCostoEnt) AS KarCosto,SUM(VrIcaEnt) AS KarIca,SUM(VrReteEnt) AS KarReteFte
,SUM(Sobretasa*Entradas) AS KarSobTasa,SUM(Soldicom*Entradas) AS KarSoldicom,SUM(ImpGlobal*Entradas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc IN ('DFC','DF1','DF2','DF3','DF4','DF5') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON D.TipDev=K.KarTipoDoc  AND D.Devolucion=K.KarNumDoc  AND D.IdCia=K.KarIdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dfo @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.NitCliente,D.CdAgencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,0,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrFletes
,D.VrCargos,D.VrOtrDcto,0,0,D.VrCostos,D.VrNeto,D.VrEfectivo,D.VrPagosOtr,0,0,D.CantPuntos,'0',D.Cantidad,D.EnEfectivo,F.CdForma,F.NumForma,F.CdBanco
,SUBSTRING(F.DetallePago,1,150),'0',D.FecPedido,D.IdVend,D.TarifaCom,D.nClieCon,D.IdCajero,D.IdLocal,Referncia1,Referncia2,'',D.Modalidad,'','PED',D.Pedido,D.IdCiaPed,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Factura,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevFco AS D INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_DfoDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.NitCliente,D.CdAgencia,KarValor,KarDcto,KarIva,0,KarReteFte,KarIca,D.VrReteIVA,D.VrFletes
,D.VrCargos,D.VrOtrDcto,0,0,KarCosto,D.VrNeto,D.VrEfectivo,D.VrPagosOtr,0,0,D.CantPuntos,IdProducto,KarCantidad,D.EnEfectivo,F.CdForma,F.NumForma,F.CdBanco
,SUBSTRING(F.DetallePago,1,150),'0',D.FecPedido,D.IdVend,D.TarifaCom,D.nClieCon,D.IdCajero,D.IdLocal,Referncia1,Referncia2,'',D.Modalidad,'','PED',D.Pedido,D.IdCiaPed,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Factura,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevFco AS D INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Entradas) AS KarCantidad,SUM(VrPrecio*Entradas) AS KarValor
,SUM(VrIvaEnt) AS KarIva,SUM(VrDctoEnt) AS KarDcto,SUM(VrCostoEnt) AS KarCosto,SUM(VrIcaEnt) AS KarIca,SUM(VrReteEnt) AS KarReteFte
 FROM  Trn_Kardex  WHERE TipDoc IN ('DFO','DO1','DO2','DO3','DO4','DO5') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON D.TipDev=K.KarTipoDoc  AND D.Devolucion=K.KarNumDoc  AND D.IdCia=K.KarIdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dnp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,0,0,VrSobretasa,0,0,0,0
,0,0,VrImpGlobal,VrOtros,VrCostos,VrNeto,VrNeto AS VrTotalEfe,0,0,0,0,'0',Cantidad,1,'EFE','','0'
,'','0',Null,IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','','NOTACALIB','','0',0,'00',TipCom,Comprobante,IdCiaCom,0,nNota,FecDoc,Observacion,TimeSys
 FROM Trn_DevPas
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_DnpDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',KarValor,KarDcto,KarIva,KarSobTasa,0,0,0,0
,0,0,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarSobTasa+KarIva+KarImpGlobal+KarSoldicom,0,0,0,0,0,IdProducto,KarCantidad,1,'EFE','','0'
,'','0',Null,IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','','NOTACALIB','','0',0,'00',TipCom,Comprobante,IdCiaCom,0,nNota,FecDoc,Observacion,TimeSys
 FROM Trn_DevPas  AS  D  INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Salidas) AS KarCantidad,SUM(VrPrecio*Salidas) AS KarValor
,SUM(VrIvaSal) AS KarIva,SUM(VrDctoSal) AS KarDcto,SUM(VrCostoSal) AS KarCosto,SUM(VrIcaSal) AS KarIca,SUM(VrReteSal) AS KarReteFte
,SUM(Sobretasa*Salidas) AS KarSobTasa,SUM(Soldicom*Salidas) AS KarSoldicom,SUM(ImpGlobal*Salidas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc='DNP'  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON D.TipDev=K.KarTipoDoc  AND D.Devolucion=K.KarNumDoc  AND D.IdCia=K.KarIdCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Drc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,C.IdCia,C.Fecha,IdConcepto,IdTercero,CdAgencia,VrPagado,VrCambio,0,0,0,0,0,VrTotal
,VrTotDebito,VrTotCredito,0,0,0,VrPagado-VrCambio,CASE IdForma WHEN 'EFE' THEN VrPagado-VrCambio ELSE 0 END,0,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,IdBanco
,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,NitCliente,0,'0',IdCajero,CdLocal,Referncia1,Referncia2,Beneficiario
,CASE C.TipDoc WHEN 'RCO' THEN 'RECIBOS' ELSE 'EGRCAJA' END,'',TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,0,Recibo,FecDoc,Observacion,TimeSys
 FROM Trn_DevCaj AS C INNER JOIN Trn_Pagos AS P ON C.TipDev=P.TipDoc AND C.Devolucion=P.Documento AND C.IdCia=P.IdCia
 WHERE  C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dvc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,C.IdCia,C.Fecha,IdConcepto,NitCliente,CdAgencia,VrPagado,VrCambio,0,0,0,0,0,0,0,0,0,0,0,VrPagado-VrCambio,CASE IdForma WHEN 'EFE' THEN VrPagado-VrCambio ELSE 0 END
,0,0,0,0,'0',0,EnEfectivo,IdForma,NumForma,IdBanco,Detalle,CASE CdCta WHEN NULL THEN '0' WHEN '' THEN '0' ELSE CdCta  END,FecForma,'0',0,'0',IdCajero,CdLocal,Referncia1,P.Referncia2,Beneficiario,ModdDev,'',TipRef,DocRef,IdCiaRef
,TipCom,Comprobante,IdCiaCom,0,Consignacion,FecDoc,Observacion,TimeSys
 FROM Trn_DevCns AS C INNER JOIN Trn_Pagos AS P ON C.TipDev=P.TipDoc AND C.Devolucion=P.Documento AND C.IdCia=P.IdCia
 WHERE  C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dve @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdProv,'0',D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrSobretasa,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrFletes
,D.VrCargos,D.VrOtrDcto,D.VrImpGlobal,D.VrOtros,0,D.VrNeto,0,0,0,0,0,'0',D.Cantidad,D.CxPagar,'EFE',D.Factura,'0'
,ModdDev,'0',FechaFac,CASE D.NitFletes WHEN Null THEN '0' WHEN '' THEN '0' ELSE D.NitFletes END,0,'0',D.IdUsuario,'0',D.Factura AS FacReferencia,D.DocEquiv,'',D.Modalidad,'',TipOdc,OCompra,IdCiaOdc
,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Entrada,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevEnt AS D INNER JOIN  Trn_Entradas AS E ON D.TipDoc=E.TipDoc AND  D.Entrada=E.Entrada AND  D.IdCiaDoc=E.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_DveDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdProv,'0',KarValor,KarDcto,KarIva,KarSobTasa,KarReteFte,KarIca,D.VrReteIVA,D.VrFletes
,D.VrCargos,D.VrOtrDcto,KarImpGlobal,KarSoldicom,KarCosto,KarValor-KarDcto+KarIva+KarSobTasa+KarImpGlobal+KarSoldicom,0,0,0,0,0,IdProducto,KarCantidad,D.CxPagar,'EFE',D.Factura,'0'
,ModdDev,'0',FechaFac,CASE D.NitFletes WHEN Null THEN '0' WHEN '' THEN '0' ELSE D.NitFletes END,0,'0',D.IdUsuario,'0',D.Factura AS FacReferencia,D.DocEquiv,'',D.Modalidad,'',TipOdc,OCompra,IdCiaOdc
,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Entrada,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevEnt AS D INNER JOIN  Trn_Entradas AS E ON D.TipDoc=E.TipDoc AND  D.Entrada=E.Entrada AND  D.IdCiaDoc=E.IdCia
 INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Salidas) AS KarCantidad,SUM(VrPrecio*Salidas) AS KarValor
,SUM(VrIvaSal) AS KarIva,SUM(VrDctoSal) AS KarDcto,SUM(VrCostoSal) AS KarCosto,SUM(VrIcaSal) AS KarIca,SUM(VrReteSal) AS KarReteFte
,SUM(Sobretasa*Salidas) AS KarSobTasa,SUM(Soldicom*Salidas) AS KarSoldicom,SUM(ImpGlobal*Salidas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc IN ('DVE','DEI') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON D.TipDev=K.KarTipoDoc  AND D.Devolucion=K.KarNumDoc  AND D.IdCia=K.KarIdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dvp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal+VrCreTotal+VrConTotal,VrConDcto+VrCreDcto,VrCreImp+VrConImp,VrSobretasa,0,0,0,VrSubTotal AS TotalComb
,VrSobrantes,VrFaltantes,VrImpGlobal,VrOtros,VrCostos+VrConCos+VrCreCos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrGastComb+VrGastOtros,0,'0',Cantidad,0,'EFE','','0'
,'','0',FecCorte,IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'','',Modalidad,'','COR',NumCorte,'00',TipCom,Comprobante,IdCiaCom,0,Planilla,FecDoc,Observacion,TimeSys
 FROM Trn_DevPla
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_DvpDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',KarValor,KarDcto,KarIva,KarSobTasa,0,0,0,0
,VrSobrantes,VrFaltantes,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarIva+KarSobTasa+KarImpGlobal+KarSoldicom,VrEfectivo,VrPagosOtr,VrCreditos,VrGastComb+VrGastOtros,0,IdProducto,KarCantidad,0,'EFE','','0'
,'','0',FecCorte,IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'','',Modalidad,'','COR',NumCorte,'00',TipCom,Comprobante,IdCiaCom,0,Planilla,FecDoc,Observacion,TimeSys
 FROM Trn_DevPla  AS D  INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Entradas) AS KarCantidad,SUM(VrPrecio*Entradas) AS KarValor
,SUM(VrIvaEnt) AS KarIva,SUM(VrDctoEnt) AS KarDcto,SUM(VrCostoEnt) AS KarCosto,SUM(VrIcaEnt) AS KarIca,SUM(VrReteEnt) AS KarReteFte
,SUM(Sobretasa*Entradas) AS KarSobTasa,SUM(Soldicom*Entradas) AS KarSoldicom,SUM(ImpGlobal*Entradas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc='DVP' AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON D.TipDev=K.KarTipoDoc  AND D.Devolucion=K.KarNumDoc  AND D.IdCia=K.KarIdCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Dvs @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,Fecha,IdConcepto,IdTercero,CdAgencia,VrSubTotal,0,VrImpuesto,0,0,0,0,0
,0,0,0,0,VrCostos,VrNeto,0,0,0,0,0,'0',Cantidad,0,'EFE','','0'
,'','0',Null,NitCliente,0,'0',IdUsuario,'0',ModRequis,ModdDev,'',Modalidad,pVehiculo,TipOrd,NumOrden,IdCiaOrd,TipCom,Comprobante,IdCiaCom,0,Salida,FecDoc,Observacion,TimeSys
 FROM Trn_DevSal
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_DvsDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,Fecha,IdConcepto,IdTercero,CdAgencia,KarValor,KarDcto,KarIva,0,0,0,0,0
,0,0,0,0,KarCosto,KarValor+KarIva,0,0,0,0,0,IdProducto,KarCantidad,0,'EFE','','0'
,'','0',Null,NitCliente,0,'0',IdUsuario,'0',ModRequis,ModdDev,'',Modalidad,pVehiculo,TipOrd,NumOrden,IdCiaOrd,TipCom,Comprobante,IdCiaCom,0,Salida,FecDoc,Observacion,TimeSys
 FROM Trn_DevSal  AS D INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Entradas) AS KarCantidad,SUM(VrPrecio*Entradas) AS KarValor
,SUM(VrIvaEnt) AS KarIva,SUM(VrDctoEnt) AS KarDcto,SUM(VrCostoEnt) AS KarCosto
 FROM  Trn_Kardex  WHERE TipDoc IN ('DVS','DSI') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON D.TipDev=K.KarTipoDoc  AND D.Devolucion=K.KarNumDoc  AND D.IdCia=K.KarIdCia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Egr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
 AS 
INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipCom,Comprobante,IdCia,Fecha,'0',IdTercero,'0',VrTotal,0,0,0,0,0,0,0,0,0,0,0,0,VrTotal AS ValNeto,CASE EnEfectivo WHEN 1 THEN VrTotal ELSE 0 END
,0,0,0,0,'0',0,EnEfectivo,CASE EnEfectivo WHEN 1 THEN  'EFE' ELSE 'CHE' END,NumCheque,IdBanco
,'',C.IdCta,FecCheque,CASE CedCondtor WHEN NULL THEN '0' WHEN '' THEN '0' ELSE CedCondtor END,0,'0',IdUsuario,'0','','',Beneficiario,'EGRESO',pVehiculo,TipDoc,Documento,IdCiaDoc,CASE WHEN TipEgr IS NULL OR LEN(TipEgr)=0 THEN '0' ELSE TipEgr END
 ,NEgreso,'00',Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Comprobantes AS C LEFT JOIN CtasCorrientes AS CT ON C.IdCta=CT.IdCta
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsEgreso<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Ent @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Entrada,IdCia,Fecha,IdConcepto,IdProv,'0',VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrImpGlobal,VrOtros,0,VrNeto,0,0,0,0,0,'0',Cantidad,CxPagar,'EFE',Factura,'0'
,'','0',FechaFac,CASE NitFletes WHEN Null THEN '0' WHEN '' THEN '0' ELSE NitFletes END,0,'0',IdUsuario,'0',Factura AS FacReferencia,DocEquiv,'',Modalidad,'',TipOdc,OCompra,IdCiaOdc,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Entradas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_EntDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Entrada,IdCia,Fecha,IdConcepto,IdProv,'0',KarValor,KarDcto,KarIva,KarSobTasa,KarReteFte,KarIca,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarIva-KarDcto,0,0,0,0,0,IdProducto,KarCantidad,CxPagar,'EFE',Factura,'0'
,'','0',FechaFac,CASE NitFletes WHEN Null THEN '0' WHEN '' THEN '0' ELSE NitFletes END,0,'0',IdUsuario,'0',Factura AS FacReferencia,DocEquiv,'',Modalidad,'',TipOdc,OCompra,IdCiaOdc,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Entradas AS E  INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Entradas) AS KarCantidad,SUM(VrUnitario*Entradas) AS KarValor
,SUM(VrIvaEnt) AS KarIva,SUM(VrDctoEnt) AS KarDcto,SUM(VrCostoEnt) AS KarCosto,SUM(VrIcaEnt) AS KarIca,SUM(VrReteEnt) AS KarReteFte
,SUM(Sobretasa*Entradas) AS KarSobTasa,SUM(Soldicom*Entradas) AS KarSoldicom,SUM(ImpGlobal*Entradas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc IN ('ENT','COM') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON E.TipDoc=K.KarTipoDoc  AND E.Entrada=K.KarNumDoc  AND E.IdCia=K.KarIdCia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Fco @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Factura,IdCia,Fecha,IdConcepto,NitCliente,CdAgencia,VrSubTotal,VrDescuento,VrImpuesto,0,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,0,0,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,0,0,CantPuntos,'0',Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,SUBSTRING(DetallePago,1,150),'0',FecPedido,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referncia1,Referncia2,'',Modalidad,'',TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Factcon
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_FcoDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Factura,IdCia,Fecha,IdConcepto,NitCliente,CdAgencia,KarValor,KarDcto,KarIva,0,KarReteFte,KarIca,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,0,0,KarCosto,VrNeto,VrEfectivo,VrPagosOtr,0,0,CantPuntos,IdProducto,KarCantidad,EnEfectivo,CdForma,NumForma,CdBanco
,SUBSTRING(DetallePago,1,150),'0',FecPedido,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referncia1,Referncia2,'',Modalidad,'',TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Factcon AS F INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Salidas) AS KarCantidad,SUM(VrPrecio*Salidas) AS KarValor
,SUM(VrIvaSal) AS KarIva,SUM(VrDctoSal) AS KarDcto,SUM(VrCostoSal) AS KarCosto,SUM(VrIcaSal) AS KarIca,SUM(VrReteSal) AS KarReteFte
 FROM  Trn_Kardex  WHERE TipDoc IN ('FCO','FO1','FO2','FO3','FO4','FO5') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON F.TipDoc=K.KarTipoDoc  AND F.Factura=K.KarNumDoc  AND F.IdCia=K.KarIdCia
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Fcr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrImpGlobal,VrOtros,VrCostos,VrNeto,0,0,0,0,CantPuntos,'0',Cantidad,MulPlazos,IdForma,'','0'
,DetallePago,'0',FecPedido,IdVend,TarifaCom,'0',IdUsuario,IdLocEnv,'','','',Modalidad,pVehiculo,TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Facturas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_FcrDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,KarValor,KarDcto,KarIva,KarSobTasa,KarReteFte,KarIca,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarIva-KarDcto+KarSobTasa+KarSoldicom+KarImpGlobal,0,0,0,0,CantPuntos,IdProducto,KarCantidad,MulPlazos,IdForma,'','0'
,DetallePago,'0',FecPedido,IdVend,TarifaCom,'0',IdUsuario,IdLocEnv,'','','',Modalidad,pVehiculo,TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Facturas  AS F INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Salidas) AS KarCantidad,SUM(VrPrecio*Salidas) AS KarValor
,SUM(VrIvaSal) AS KarIva,SUM(VrDctoSal) AS KarDcto,SUM(VrCostoSal) AS KarCosto,SUM(VrIcaSal) AS KarIca,SUM(VrReteSal) AS KarReteFte
,SUM(Sobretasa*Salidas) AS KarSobTasa,SUM(Soldicom*Salidas) AS KarSoldicom,SUM(ImpGlobal*Salidas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc IN ('FCR','FC1','FC2','FC3','FC4','FC5') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON F.TipDoc=K.KarTipoDoc  AND F.Factura=K.KarNumDoc  AND F.IdCia=K.KarIdCia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Not @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,0,VrImpuesto,0,VrRetencion,VrReteICA,VrReteIVA,VrSancion
,0,0,0,0,VrCosto,VrNeto,VrOtros AS VlrEfectivo,0,0,0,0,CdProducto,Cantidad,PrestCaja,CdForma,NumCheque,CdBanco
,'','0',FecDcm,IdVend,TarifaCom,'0',CdCajero,IdLocal,Referencia,'','',Modalidad,pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,0,0,Null,Observacion,TimeSys
 FROM Trn_Notas
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND CdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_NotBus @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,ValorTotal,0,ValorIva,0,ValorReteFte,ValorReteIca,0,ValorSancion
,0,0,0,0,ValorCosto,ValorNeto,0,0,0,0,0,'0',0,PrestCaja,'0',NumCheque,IdBanco
,'','0',FecVence,IdVendedor,Comision,'0',IdUsuario,'0',Referencia,'','','BUSES',IdVehiculo,TipDcm,Documento,IdCiaDoc,TipCom,Comprobante,IdCiaCom,0,0,Null,Observacion,TimeSys
 FROM Trn_NotasBuses
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Diario_NotPre @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,0,VrImpuesto,0,VrRetencion,VrReteICA,VrReteIVA,VrSancion
,0,0,0,0,0,VrNeto,VrOtros AS VlrEfectivo,0,0,0,0,'0',0,0,CdForma,NumCheque,CdBanco
,'','0',FecDcm,IdVend,TarifaCom,'0',IdUsuario,'0',Referencia,'','',Modalidad,pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,0,0,Null,Observacion,TimeSys
 FROM Trn_FinNotas
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Od1 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,D.ValorTotal,D.ValorDcto,0,0,D.ValorReteFte,D.ValorReteIca,D.ValorReteIva,D.ValorPagMas
,D.ValorOtros,D.ValorOtrosDct,0,D.ValorAhorro,0,D.ValorNeto,CASE D.EnEfectivo WHEN 1 THEN D.ValorNeto ELSE 0 END,CASE D.EnEfectivo WHEN 0 THEN D.ValorNeto ELSE 0 END,0,0,0,'0',0,D.EnEfectivo,'EFE',D.NumCheque,D.IdBanco
,'',R.IdCta,Null,D.IdVendedor,D.Comision,'0',D.IdUsuario,'0',R.Referencia,'','',D.Modalidad,D.IdVehiculo,TipCar,Cartulina,IdCiaCar,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Recibo,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Od1Det @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,R.IdCia,R.Fecha,IdConcepto,IdCliente,IdAgencia,VrPagado,VrCambio,0,0,ValorReteFte,ValorReteIca,ValorReteIva,ValorPagMas
,ValorOtros,ValorOtrosDct,0,ValorAhorro,0,VrPagado-VrCambio,CASE IdForma WHEN 'EFE' THEN VrPagado-VrCambio ELSE 0 END, CASE IdForma WHEN 'EFE' THEN 0 ELSE  VrPagado-VrCambio END
,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,P.IdBanco,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,IdVendedor,Comision,'0',R.IdUsuario,CdLocal,Referncia1,Referncia2,Beneficiario
,Modalidad,IdVehiculo,R.TipDoc,Recibo,IdCiaDoc,TipCom,Comprobante,IdCiaCom,0,Recibo,FecDoc,Observacion,TimeSys
 FROM Trn_DevRecBuses AS R INNER JOIN Trn_Pagos AS P ON R.TipDev=P.TipDoc AND R.Devolucion=P.Documento AND R.IdCia=P.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Diario_Od2 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,D.VrSubTotal,0,0,0,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrPagosMas
,D.VrOtros,D.VrOtrDcto,0,0,0,D.VrNeto,D.VrEfectivo,0,0,0,0,'0',0,D.EnEfectivo,CdForma,NumForma,CdBanco
,SUBSTRING(DetallePago,1,150),CdCta,D.FecPago,D.IdVend,D.TarifaCom,'0',D.IdUsuario,'0',D.Referencia,'','','RC2',R.pVehiculo,R.TipDcm,R.Documento,R.IdCiaDcm,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Recibo,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_FinDevRec AS D INNER JOIN Trn_FinRecibos AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Diario_Od2Det @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,R.IdCia,R.Fecha,IdConcepto,IdCliente,IdAgencia,VrPagado,VrCambio,0,0,VrRetencion,VrReteICA,VrReteIVA,VrPagosMas
,VrOtros,VrOtrDcto,0,0,0,VrPagado-VrCambio,VrEfectivo,0,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,IdBanco
,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,IdVend,TarifaCom,'0',R.IdUsuario,CdLocal,Referncia1,Referncia2,Beneficiario,'RC2','0',TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,0,Recibo,FecDoc,Observacion,TimeSys
 FROM Trn_FinDevRec AS R INNER JOIN Trn_Pagos AS P ON R.TipDev=P.TipDoc AND R.Devolucion=P.Documento AND R.IdCia=P.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Odb @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.IdCliente,D.IdAgencia,D.VrSubTotal,D.VrDescuento,0,0,D.VrRetencion,D.VrReteICA,D.VrReteIVA,D.VrPagosMas
,D.VrOtros,D.VrOtrDcto,0,D.VrAhorro,0,D.VrNeto,D.VrEfectivo,0,0,0,0,'0',0,D.EnEfectivo,CdForma,NumForma,CdBanco
,SUBSTRING(DetallePago,1,150),CdCta,D.FecPago,D.IdVend,D.TarifaCom,'0',D.IdCajero,D.IdLocal,D.Referencia,'','',D.Modalidad,D.pVehiculo,D.TipDcm,D.Documento,D.IdCiaDcm,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Recibo,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevRec AS D INNER JOIN Trn_Recibos AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 WHERE  D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_OdbDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDev,Devolucion,R.IdCia,R.Fecha,IdConcepto,IdCliente,IdAgencia,VrPagado,VrCambio,0,0,VrRetencion,VrReteICA,VrReteIVA,VrPagosMas
,VrOtros,VrOtrDcto,0,VrAhorro,0,VrPagado-VrCambio,VrEfectivo,0,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,IdBanco
,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,IdVend,TarifaCom,'0',IdCajero,CdLocal,Referncia1,Referncia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,0,Recibo,FecDoc,Observacion,TimeSys
 FROM Trn_DevRec AS R INNER JOIN Trn_Pagos AS P ON R.TipDev=P.TipDoc AND R.Devolucion=P.Documento AND R.IdCia=P.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Pas @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,nNota,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,0,0,VrSobretasa,0,0,0,0
,0,0,VrImpGlobal,VrOtros,VrCostos,VrNeto,VrNeto AS VrTotalEfe,0,0,0,0,'0',Cantidad,1,'EFE','','0'
,'','0',Null,IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','','NOTACALIB','','0',0,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCalibra
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_PasDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,nNota,IdCia,Fecha,IdConcepto,NitTercero,'0',KarValor,KarDcto,KarIva,KarSobTasa,0,0,0,0
,0,0,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarIva+KarSobTasa+KarSoldicom+KarImpGlobal,0,0,0,0,0,IdProducto,KarCantidad,1,'EFE','','0'
,'','0',Null,IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','','NOTACALIB','','0',0,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCalibra AS N INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Entradas) AS KarCantidad,SUM(VrPrecio*Entradas) AS KarValor
,SUM(VrIvaEnt) AS KarIva,SUM(VrDctoEnt) AS KarDcto,SUM(VrCostoEnt) AS KarCosto,SUM(VrIcaEnt) AS KarIca,SUM(VrReteEnt) AS KarReteFte
,SUM(Sobretasa*Entradas) AS KarSobTasa,SUM(Soldicom*Entradas) AS KarSoldicom,SUM(ImpGlobal*Entradas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc='PAS' AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON N.TipDoc=K.KarTipoDoc  AND N.nNota=K.KarNumDoc  AND N.IdCia=K.KarIdCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Pla @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal+VrCreTotal+VrConTotal,VrConDcto+VrCreDcto,VrCreImp+VrConImp,VrSobretasa,0,0,0,VrSubTotal AS VrCombustible
,VrSobrantes,VrFaltantes,VrImpGlobal,VrOtros,VrCostos+VrCreCos+VrConCos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrGastComb+VrGastOtros,0,'0',Cantidad,0,'EFE','','0'
,'','0',FecCorte,IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'','',Modalidad,'','COR',NumCorte,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCortes
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_PlaDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,'0',KarValor,KarDcto,KarIva,KarSobTasa,0,0,0,0
,VrSobrantes,VrFaltantes,KarImpGlobal,KarSoldicom,KarCosto,KarValor+KarIva+KarSobTasa+KarImpGlobal+KarSoldicom,VrEfectivo,VrPagosOtr,VrCreditos,VrGastComb+VrGastOtros,0,IdProducto,KarCantidad,0,'EFE','','0'
,'','0',FecCorte,IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'','',Modalidad,'','COR',NumCorte,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCortes  F INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Salidas) AS KarCantidad,SUM(VrPrecio*Salidas) AS KarValor
,SUM(VrIvaSal) AS KarIva,SUM(VrDctoSal) AS KarDcto,SUM(VrCostoSal) AS KarCosto,SUM(VrIcaSal) AS KarIca,SUM(VrReteSal) AS KarReteFte
,SUM(Sobretasa*Salidas) AS KarSobTasa,SUM(Soldicom*Salidas) AS KarSoldicom,SUM(ImpGlobal*Salidas) AS KarImpGlobal
 FROM  Trn_Kardex  WHERE TipDoc='PLA' AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON F.TipDoc=K.KarTipoDoc  AND F.Planilla=K.KarNumDoc  AND F.IdCia=K.KarIdCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Rc1 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc, Recibo, IdCia, Fecha, IdConcepto, IdCliente, IdAgencia,ValorTotal,ValorDcto,0,0,ValorReteFte,ValorReteIca,ValorReteIva,ValorPagMas,ValorOtros,ValorOtrosDct
,0,TotalAhorro,0,ValorNeto,CASE EnEfectivo WHEN 1 THEN ValorNeto ELSE 0 END,CASE EnEfectivo WHEN 0 THEN ValorNeto ELSE 0 END,0,0,0,'0',0,EnEfectivo,CASE EnEfectivo WHEN 1 THEN  'EFE' ELSE 'CHE' END
,NumCheque,IdBanco,'',IdCta,Null,IdVendedor,Comision,'0',IdUsuario,'0',Referencia,'','',Modalidad,IdVehiculo,TipCar, Cartulina, IdCiaCar,TipCom, Comprobante, IdCiaCom, Anulado, NumDev, FecDev,Observacion,TimeSys
 FROM Trn_RecBuses
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Rc1Det @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,R.TipDoc,Recibo,R.IdCia,R.Fecha,IdConcepto,IdCliente,IdAgencia,VrPagado,VrCambio,0,0,ValorReteFte,ValorReteIca,ValorReteIva,ValorPagMas
,ValorOtros,ValorOtrosDct,0,TotalAhorro,0,VrPagado-VrCambio,CASE EnEfectivo WHEN 1 THEN VrPagado-VrCambio ELSE 0 END,CASE EnEfectivo WHEN 0 THEN VrPagado-VrCambio ELSE 0 END,0,0,0,'0',0,EnEfectivo
,IdForma,NumForma,P.IdBanco,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,IdVendedor,Comision,'0',R.IdUsuario,CdLocal,Referncia1,Referncia2,Beneficiario,Modalidad,IdVehiculo,TipCar,Cartulina,IdCiaCar
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_RecBuses AS R INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdUsuario LIKE ISNULL(@pmIdCajero,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Diario_Rc2 @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,0,0,0,VrRetencion,VrReteICA,VrReteIVA,VrPagosMas
,VrOtros,VrOtrDcto,0,0,0,VrNeto,VrEfectivo,0,0,0,0,'0',0,EnEfectivo,CdForma,NumForma,CdBanco
,SUBSTRING(DetallePago,1,150),CdCta,FecPago,IdVend,TarifaCom,'0',IdUsuario,'0',Referencia,'','','RC2',pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_FinRecibos
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Diario_Rc2Det @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,R.TipDoc,Recibo,R.IdCia,R.Fecha,IdConcepto,IdCliente,IdAgencia,VrPagado,VrCambio,0,0,VrRetencion,VrReteICA,VrReteIVA,VrPagosMas
,VrOtros,VrOtrDcto,0,0,0,VrPagado-VrCambio,VrEfectivo,0,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,IdBanco
,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,IdVend,TarifaCom,'0',R.IdUsuario,CdLocal,Referncia1,Referncia2,Beneficiario,'RC2',pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_FinRecibos AS R INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Rco @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,C.TipDoc,Recibo,C.IdCia,C.Fecha,IdConcepto,IdTercero,P.CdAgencia,VrPagado,VrCambio,0,0,0,0,0,VrTotal
,VrTotDebito,VrTotCredito,0,0,0,VrPagado-VrCambio,CASE IdForma WHEN 'EFE' THEN VrPagado-VrCambio ELSE 0 END,0,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,IdBanco
,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,NitCliente,0,'0',IdCajero,CdLocal,Referncia1,Referncia2,Beneficiario,CASE C.TipDoc WHEN 'RCO' THEN 'RECIBOS' ELSE 'EGRCAJA' END,'',TipRef,DocRef,IdCiaRef
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_CompCaja AS C INNER JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND C.Recibo=P.Documento AND C.IdCia=P.IdCia
 WHERE  C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Rec @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,0,0,VrRetencion,VrReteICA,VrReteIVA,VrPagosMas
,VrOtros,VrOtrDcto,0,VrAhorro,0,VrNeto,VrEfectivo,0,0,0,0,'0',0,EnEfectivo,CdForma,NumForma,CdBanco
,SUBSTRING(DetallePago,1,150),CdCta,FecPago,IdVend,TarifaCom,'0',IdCajero,IdLocal,Referencia,'','',Modalidad,pVehiculo,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Recibos
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_RecDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,R.TipDoc,Recibo,R.IdCia,R.Fecha,IdConcepto,IdCliente,IdAgencia,VrPagado,VrCambio,0,0,VrRetencion,VrReteICA,VrReteIVA,VrPagosMas
,VrOtros,VrOtrDcto,0,VrAhorro,0,VrPagado-VrCambio,VrEfectivo,0,0,0,0,'0',0,EnEfectivo,IdForma,P.NumForma,IdBanco
,Detalle,CASE P.CdCta WHEN NULL THEN '0'  WHEN '' THEN '0' ELSE P.CdCta END,FecForma,IdVend,TarifaCom,'0',IdCajero,CdLocal,Referncia1,Referncia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Recibos AS R INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 WHERE  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_Sal @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Salida,IdCia,Fecha,IdConcepto,IdTercero,CdAgencia,VrSubTotal,0,VrImpuesto,0,0,0,0,0
,0,0,0,0,VrCostos,VrNeto,0,0,0,0,0,'0',Cantidad,0,'EFE','','0'
,SUBSTRING(DirEntrega,1,150),'0',Null,CdConductor,0,'0',IdUsuario,IdLocEnt,ModRequis,'','',Modalidad,pVehiculo,TipOrd,NumOrden,IdCiaOrd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Salidas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Diario_SalDet @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 INSERT INTO tm_Diario (tmEst,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros
,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,Cantidad,EnEfectivo,CdForma,NumForma,CdBanco
,DetallePagos,CdCta,FecCheque,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Beneficiario,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
 SELECT @pmtmEst,TipDoc,Salida,IdCia,Fecha,IdConcepto,IdTercero,CdAgencia,KarValor,KarDcto,KarIva,0,0,0,0,0
,0,0,0,0,KarCosto,KarValor+KarIva,0,0,0,0,0,IdProducto,KarCantidad,0,'EFE','','0'
,SUBSTRING(DirEntrega,1,150),'0',Null,CdConductor,0,'0',IdUsuario,IdLocEnt,ModRequis,'','',Modalidad,pVehiculo,TipOrd,NumOrden,IdCiaOrd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Salidas  AS S INNER JOIN (SELECT TipDoc AS KarTipoDoc,Documento AS KarNumDoc,IdCia AS KarIdCia,IdProducto,SUM(Salidas) AS KarCantidad,SUM(VrPrecio*Salidas) AS KarValor
,SUM(VrIvaSal) AS KarIva,SUM(VrDctoSal) AS KarDcto,SUM(VrCostoSal) AS KarCosto
 FROM  Trn_Kardex  WHERE TipDoc IN ('SAL','SAI') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY TipDoc,Documento,IdCia,IdProducto) AS K ON S.TipDoc=K.KarTipoDoc  AND S.Salida=K.KarNumDoc  AND S.IdCia=K.KarIdCia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_DifIngreso @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdDiferido VARCHAR(30),@pmtmFecha SMALLDATETIME,@pmtmPeriodos INT,@pmtmVrBase MONEY
,@pmtmMejoras MONEY,@pmtmDisminucion MONEY,@pmtmAjustes MONEY,@pmtmAmortiza MONEY,@pmtmAmortizaAju MONEY,@pmtmAjuInfAmtza MONEY,@pmtmMesesAcum INT
,@pmtmCorrMonDif MONEY,@pmtmMesesCorr INT,@pmtmFecUltAmtza SMALLDATETIME,@pmtmConcepto VARCHAR(150)
 AS 
 INSERT INTO tm_DifIngreso (tmNumero,tmItem,tmIdDiferido,tmFecha,tmPeriodos,tmVrBase,tmMejoras,tmDisminucion,tmAjustes,tmAmortiza,tmAmortizaAju,tmAjuInfAmtza
,tmMesesAcum,tmCorrMonDif,tmMesesCorr,tmFecUltAmtza,tmConcepto)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdDiferido,@pmtmFecha,@pmtmPeriodos,@pmtmVrBase,@pmtmMejoras,@pmtmDisminucion,@pmtmAjustes
,@pmtmAmortiza,@pmtmAmortizaAju,@pmtmAjuInfAmtza,@pmtmMesesAcum,@pmtmCorrMonDif,@pmtmMesesCorr,@pmtmFecUltAmtza,@pmtmConcepto)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_DifIngreso_Sel @pmtmNumero VARCHAR(5),@pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_DifIngreso (tmNumero,tmItem,tmIdDiferido,tmFecha,tmPeriodos,tmVrBase,tmMejoras,tmDisminucion,tmAjustes,tmAmortiza,tmAmortizaAju,tmAjuInfAmtza
,tmMesesAcum,tmCorrMonDif,tmMesesCorr,tmFecUltAmtza,tmConcepto)
 SELECT  @pmtmNumero,Item,IdDiferido,FecActivacion,NumPeriodos,VrBase,VrMejoras,VrDisminucion,VrAjuInfAcum,VrAmortizaHis
,VrAmortizaAju,VrAjuInfAmtza,NumMesesAmtza,VrCorrMonDif,NumMesesCorr,FecUltAmtza,Concepto
 FROM Trn_AfDifIngreso
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EVEstTip @pmtmEst CHAR(2)
AS 
 INSERT INTO tm_EVEst (tmEst,tmIdTipo,tmTotalProducido,tmMaxProducido,tmMinProducido,tmTotalRecrridos,tmTotalPasaj,tmTotalDias,tmTotalCausac,tmGalones,tmValorComb,tmCantVeh)
 SELECT  tmEst,CASE CdTarifa  WHEN NULL THEN '0' WHEN '' THEN '0' ELSE CdTarifa END
,SUM(tmTotalProducido),MAX(tmMaxProducido),MIN(tmMinProducido),SUM(tmTotalRecrridos),SUM(tmTotalPasaj),SUM(tmTotalDias),SUM(tmTotalCausac),SUM(tmGalones)
,SUM(tmValorComb),COUNT(tmIdVehiculo)
 FROM tm_ExtVeh AS EV INNER JOIN Vehiculos AS V ON EV.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst
 GROUP BY tmEst,CdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EVcau @pmtmEst CHAR(2),@pmtmTipo VARCHAR(3),@pmtmDocmnto INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmFecha SMALLDATETIME,@pmtmGrupo VARCHAR(3),@pmtmIdCargo VARCHAR(4),@pmtmIdConcepto VARCHAR(4),@pmtmValor MONEY,@pmtmValorDev MONEY,@pmtmVrGastos MONEY,@pmtmIdCliente VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmVehPropio BIT,@pmtmIdConductor VARCHAR(16)
,@pmtmModalidad VARCHAR(10),@pmtmValorTotal MONEY,@pmtmGalones DECIMAL(14,4),@pmtmVrComb MONEY,@pmtmDetalle VARCHAR(250),@pmtmNumCheque VARCHAR(20),@pmtmReferencia VARCHAR(50),@pmtmIdClase VARCHAR(4),@pmtmAnulado BIT,@pmtmNumDev INT,@pmtmFecDev SMALLDATETIME,@pmtmTipDcm VARCHAR(3),@pmtmDocDev INT,@pmtmIdCiaDcm CHAR(2),@pmtmFecDcm SMALLDATETIME,@pmtmIdEstado VARCHAR(4),@pmtmIdUsuario VARCHAR(11),@pmtmTimeSys SMALLDATETIME
 AS 
 INSERT INTO tm_EVcau (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmIdCargo,tmIdConcepto,tmValor,tmValorDev,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmModalidad,tmValorTotal,tmGalones,tmVrComb,tmDetalle,tmNumCheque,tmReferencia,tmIdClase,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmVrGastos,tmTimeSys)
 VALUES (@pmtmEst,@pmtmTipo,@pmtmDocmnto,@pmtmIdCia,@pmtmItem,@pmtmFecha,@pmtmGrupo,@pmtmIdCargo,@pmtmIdConcepto,@pmtmValor,@pmtmValorDev,@pmtmIdCliente,@pmtmIdVehiculo,@pmtmVehPropio,@pmtmIdConductor,@pmtmModalidad,@pmtmValorTotal,@pmtmGalones,@pmtmVrComb,@pmtmDetalle,@pmtmNumCheque,@pmtmReferencia,@pmtmIdClase,@pmtmAnulado,@pmtmNumDev,@pmtmFecDev
,@pmtmTipDcm,@pmtmDocDev,@pmtmIdCiaDcm,@pmtmFecDcm,@pmtmIdEstado,@pmtmIdUsuario,@pmtmVrGastos,@pmtmTimeSys)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EVcauCte @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS
--INSERTA CAUSACIONES CIERRE CTE
 INSERT INTO tm_EVcau (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmIdCargo,tmIdConcepto,tmValor,tmValorDev,tmVrGastos,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmModalidad,tmValorTotal,tmGalones,tmVrComb,tmDetalle,tmNumCheque,tmReferencia,tmIdClase,tmAnulado
,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,'DVO',Recibo,IdCia,Item+2000,Fecha,TipRec,IdCargo,'0',0,ValorAbono,(ValorAbono*-1),R.IdCliente,R.IdVehiculo,VehPropio,IdVendedor,'CCTE',ValorAbono,0,0,Detalle,'0','OTROS',IdClase,0
,0,Null,TipDoc,Documento,IdCiaDoc,Null,'0001',R.IdUsuario,R.TimeSys
 FROM  Trn_RecFactura  AS R  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo 
 WHERE  TipRec='OTR' AND  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EVcauDFV @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS
 INSERT INTO tm_EVcau (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmIdCargo,tmIdConcepto,tmValor,tmValorDev,tmVrGastos,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmModalidad,tmValorTotal,tmGalones,tmVrComb,tmDetalle,tmNumCheque,tmReferencia,tmIdClase,tmAnulado
,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,TipDev, Devolucion, D.IdCia,Item+1000,Fecha,VC.TipDoc,IdCargo,D.IdConcepto,0,ValorFactura,(ValorFactura*-1),VC.IdCliente,VC.IdVehiculo,VC.VehPropio,VC.IdVendedor,D.TipDoc,VrSubTotal,0,0,D.Observacion,VC.Factura,VC.Referencia,VC.IdClase,0,0,Null,D.TipDoc,D.Factura,IdCiaDoc, FecDoc,D.IdEstado,D.IdUsuario,TimeSys
 FROM   Trn_DevFvc  AS D  INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Factura=VC.Documento AND D.IdCiaDoc=VC.IdCia
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo 
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia like ISNULL(@pmIdCia,'%%')
 AND VC.IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND VC.IdCliente like ISNULL(@pmIdCliente,'%')
 AND IdGrupo like ISNULL(@pmIdGrupo,'%') AND IdAdmon like ISNULL(@pmIdAdmon,'%') AND VC.IdClase like ISNULL(@pmIdClase,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EVcauDvo @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS
--INSERTA DEV CAUSACIONES
 INSERT INTO tm_EVcau (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmIdCargo,tmIdConcepto,tmValor,tmValorDev,tmVrGastos,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmModalidad,tmValorTotal,tmGalones,tmVrComb,tmDetalle,tmNumCheque,tmReferencia,tmIdClase,tmAnulado
,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,TipDev, Devolucion, IdCia,1,Fecha,TipDev,'0',IdConcepto,0,ValorNeto,(ValorNeto*-1),D.IdPropietario,D.IdVehiculo,D.VehPropio,D.IdConductor,TipDoc,ValorTotal,0,0,D.Observacion,Factura,'',TipDoc,0,0,Null,TipDoc, Documento, IdCiaDoc, FecDoc,D.IdEstado,D.IdUsuario,TimeSys
 FROM   Trn_DevCausacion  AS D INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo 
 WHERE TipDev IN ('DVO','DVA') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia like ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND D.IdPropietario like ISNULL(@pmIdCliente,'%')
 AND IdGrupo like ISNULL(@pmIdGrupo,'%') AND IdAdmon like ISNULL(@pmIdAdmon,'%') AND TipDoc like ISNULL(@pmIdClase,'%')
 ORDER BY  IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EVcauNcr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS
--INSERTA notas credito
 INSERT INTO tm_EVcau (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmIdCargo,tmIdConcepto,tmValor,tmValorDev,tmVrGastos,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmModalidad,tmValorTotal,tmGalones,tmVrComb,tmDetalle,tmNumCheque,tmReferencia,tmIdClase,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,TipDoc,NumNota, IdCia,1,Fecha,TipDoc,'0',IdConcepto,0,ValorNeto,0,IdCliente,N.IdVehiculo, N.VehPropio,N.IdConductor,'NC1',ValorTotal,0,0,N.Observacion,NumCheque,Referencia,N.IdClase,0,0,Null,TipDcm, Documento, IdCiaDoc,Null,N.IdEstado,N.IdUsuario,TimeSys
 FROM  Trn_NotasBuses  AS N INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo 
 WHERE TipDoc='NC1'  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND N.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND N.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY  IdCia,NumNota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EVrecCAR @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
--INSERTA RECIBOS DE CARTULINAS
 INSERT INTO tm_EVrec (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmValor,tmValorDev,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdConcepto,tmModalidad,tmTipCar,tmCartulina,tmIdCiaCar,tmFechaRec,tmCentInicial
,tmCentFinal,tmNPasajeros,tmRecorridos,tmValorTotal,tmValorOtros,tmTotalProducido,tmTotalGastos,tmTotalAhorro,tmTotalOtros,tmTotalVales,tmTotalAbono,tmValorPasaje,tmDetalle,tmNumCheque,tmReferencia,tmIdBanco,tmIdClase,tmIdRuta,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,R.TipDoc, Recibo, R.IdCia,1,R.Fecha,'RC1',ValorNeto,0,IdCliente,R.IdVehiculo,R.VehPropio,R.IdConductor,IdConcepto,Modalidad,TipCar, R.Cartulina, IdCiaCar,FechaRec,R.CentInicial,R.CentFinal, R.NPasajeros, R.Recorridos,ValorTotal,R.ValorOtros,TotalProducido,TotalGastos,TotalAhorro,TotalOtros, TotalVales, TotalAbono,R.ValorPasaje,R.Observacion
,NumCheque,Referencia,IdBanco,R.IdClase,R.IdRuta, R.Anulado, R.NumDev, R.FecDev,TipCom,Comprobante,IdCiaCom,Null,R.IdEstado,R.IdUsuario,R.TimeSys
 FROM  Trn_RecBuses  AS R INNER JOIN Trn_Despachos AS D ON R.TipCar=D.TipDoc AND R.Cartulina=D.Cartulina AND R.IdCiaCar=D.IdCia
 INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo 
 WHERE Modalidad='CARTULINA' AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND  (R.Anulado=ISNULL(@pmAnulado,0) or R.Anulado=ISNULL(@pmAnulado,1))
 ORDER BY  R.IdCia,Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EVrecODB @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS 
--INSERTA RECIBOS DE CARTULINAS
 INSERT INTO tm_EVrec (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmValor,tmValorDev,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdConcepto,tmModalidad,tmTipCar,tmCartulina,tmIdCiaCar,tmFechaRec,tmCentInicial
,tmCentFinal,tmNPasajeros,tmRecorridos,tmValorTotal,tmValorOtros,tmTotalProducido,tmTotalGastos,tmTotalAhorro,tmTotalOtros,tmTotalVales,tmTotalAbono,tmValorPasaje,tmDetalle,tmNumCheque,tmReferencia,tmIdBanco,tmIdClase,tmIdRuta
,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,TipDev, Devolucion,D.IdCia,1,D.Fecha,'RC1',0,D.ValorNeto,D.IdCliente,D.IdVehiculo,D.VehPropio,D.IdConductor,D.IdConcepto,D.Modalidad,TipCar,C.Cartulina,IdCiaCar,FechaRec,R.CentInicial,R.CentFinal, R.NPasajeros,R.Recorridos,D.ValorTotal
,D.ValorOtros,TotalProducido,TotalGastos,TotalAhorro,TotalOtros, TotalVales, TotalAbono,R.ValorPasaje,D.Observacion,D.NumCheque,Referencia,D.IdBanco,D.IdClase,R.IdRuta,D.EnEfectivo,0,Null,D.TipDoc, D.Recibo, IdCiaDoc, FecDoc
,D.IdEstado,D.IdUsuario,D.TimeSys
 FROM  Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses  AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo 
 LEFT JOIN  Trn_Despachos AS C ON R.TipCar=C.TipDoc AND R.Cartulina=C.Cartulina AND R.IdCiaCar=C.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY  D.IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EVrecOTR @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
--INSERTA RECIBOS DE NO CARTULINAS
 INSERT INTO tm_EVrec (tmEst,tmTipo,tmDocmnto,tmIdCia,tmItem,tmFecha,tmGrupo,tmValor,tmValorDev,tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdConcepto,tmModalidad,tmTipCar,tmCartulina,tmIdCiaCar,tmFechaRec,tmCentInicial
,tmCentFinal,tmNPasajeros,tmRecorridos,tmValorTotal,tmValorOtros,tmTotalProducido,tmTotalGastos,tmTotalAhorro,tmTotalOtros,tmTotalVales,tmTotalAbono,tmValorPasaje,tmDetalle,tmNumCheque,tmReferencia,tmIdBanco,tmIdClase,tmIdRuta,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,tmTimeSys)
 SELECT @pmtmEst,R.TipDoc, Recibo, IdCia,2,Fecha,'ROC',ValorNeto,0,IdCliente,R.IdVehiculo,R.VehPropio,R.IdConductor,IdConcepto,Modalidad,TipCar, Cartulina, IdCiaCar,Null,R.CentInicial,R.CentFinal,NPasajeros, Recorridos,ValorTotal,ValorOtros,TotalProducido,TotalGastos,TotalAhorro,TotalOtros, TotalVales, TotalAbono,ValorPasaje,R.Observacion
,NumCheque,Referencia,IdBanco,R.IdClase,IdRuta, Anulado, NumDev, FecDev,TipCom,Comprobante,IdCiaCom,Null,R.IdEstado,R.IdUsuario,TimeSys
 FROM  Trn_RecBuses  AS R  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo 
 WHERE Modalidad<>'CARTULINA' AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY  IdCia,Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EdsFormas @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmCredito BIT,@pmtmTipoRango VARCHAR(3),@pmtmIdRango INT,@pmtmIdForma VARCHAR(4)
,@pmtmObservacion VARCHAR(150),@pmtmEsCaja BIT,@pmtmIdBanco VARCHAR(4),@pmtmNForma VARCHAR(20),@pmtmFecForma SMALLDATETIME,@pmtmCtaForma VARCHAR(30)
,@pmtmBeneficiario VARCHAR(150),@pmtmNumSerie VARCHAR(5),@pmtmNumForma INT,@pmtmVrTotal MONEY,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16)
,@pmtmIdCuenta VARCHAR(16),@pmtmpVehiculo VARCHAR(10),@pmtmnVehiculo VARCHAR(10),@pmtmIdVend VARCHAR(16),@pmtmCdProducto VARCHAR(16)
,@pmtmCantidad DECIMAL(14,4),@pmtmReferencia VARCHAR(50),@pmtmReferncia2 VARCHAR(50),@pmtmCantPuntos DECIMAL(14,4),@pmtmCdLocal VARCHAR(8)
,@pmtmNumAutoriza VARCHAR(30),@pmtmVrDenom MONEY,@pmtmCantDenm INT,@pmtmTipDenom CHAR(1),@pmtmCdCta VARCHAR(4),@pmtmTipAplica VARCHAR(10),@pmtmIdUsuario VARCHAR(11)
,@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16)
 AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmCredito,@pmtmTipoRango,@pmtmIdRango,@pmtmIdForma,@pmtmObservacion,@pmtmEsCaja,@pmtmIdBanco,@pmtmNForma,@pmtmFecForma,@pmtmCtaForma
,@pmtmBeneficiario,@pmtmNumSerie,@pmtmNumForma,@pmtmVrTotal,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdCuenta,@pmtmpVehiculo,@pmtmnVehiculo,@pmtmIdVend,@pmtmCdProducto
,@pmtmCantidad,@pmtmReferencia,@pmtmReferncia2,@pmtmCantPuntos,@pmtmCdLocal,@pmtmNumAutoriza,@pmtmVrDenom,@pmtmCantDenm,@pmtmTipDenom,@pmtmCdCta,@pmtmTipAplica,@pmtmIdUsuario,@pmtmCdCCosto,@pmtmCdSubCos)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EdsFormas_Cre  @pmtmNumero VARCHAR(5),@pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos)
 SELECT @pmtmNumero,Item,1,TipoRango,IdRango,IdForma,Observacion,0,'0','0',Fecha,'0','',NumSerie,NumForma,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo
,IdVend,CdProducto,Cantidad,Referencia,'',CantPuntos,'0','',0,0,'B','0','',IdCajero,CdCCosto,CdSubCos
 FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EdsFormas_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_EdsFormas (tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma
,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm
,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos)
 SELECT @pmtmNumero,Item,0,'0',0,IdForma,Detalle,EsCaja,IdBanco,NumForma,FecForma,CtaForma,Beneficiario,'0',0
,VrPagado,NitCliente,CdAgencia,'0','0','0','0','0',0,Referncia1,Referncia2,0,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,CdCta,'',IdUsuario,'0','0'
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstClientes @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdCia CHAR(2),@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmFecUltFcr SMALLDATETIME,@pmtmPPlazoFcr DECIMAL(14,4),@pmtmMayorFcr MONEY
,@pmtmMenorFcr MONEY,@pmtmFecUltFco SMALLDATETIME,@pmtmPPlazoFco DECIMAL(14,4),@pmtmMayorFco MONEY,@pmtmMenorFco MONEY,@pmtmVentaBruto MONEY,@pmtmVrDevolucion MONEY,@pmtmVrDescuentos MONEY,@pmtmCostoBruto MONEY
,@pmtmCostoDev MONEY,@pmtmVrIva MONEY,@pmtmFecUltPago SMALLDATETIME,@pmtmPromPago DECIMAL(14,4),@pmtmSaldoMora MONEY,@pmtmFecIniMora SMALLDATETIME,@pmtmDiasEnMora INT,@pmtmChequesDev INT,@pmtmSaldoActual MONEY
,@pmtmSaldoAnterior MONEY,@pmtmVrFacturas MONEY,@pmtmVrDevFacturas MONEY,@pmtmVrRecibos MONEY,@pmtmVrDevRecibos MONEY,@pmtmVrNotasDeb MONEY,@pmtmVrNotasCre MONEY,@pmtmVrCheques MONEY
 AS 
 INSERT INTO tm_EstClientes (tmEst,tmItem,tmIdCia,tmIdCliente,tmIdAgencia,tmFecUltFcr,tmPPlazoFcr,tmMayorFcr,tmMenorFcr,tmFecUltFco,tmPPlazoFco,tmMayorFco,tmMenorFco,tmVentaBruto,tmVrDevolucion,tmVrDescuentos,tmCostoBruto,tmCostoDev,tmVrIva
,tmFecUltPago,tmPromPago,tmSaldoMora,tmFecIniMora,tmDiasEnMora,tmChequesDev,tmSaldoActual,tmSaldoAnterior,tmVrFacturas,tmVrDevFacturas,tmVrRecibos,tmVrDevRecibos,tmVrNotasDeb,tmVrNotasCre,tmVrCheques)
 VALUES (@pmtmEst,@pmtmItem,@pmtmIdCia,@pmtmIdCliente,@pmtmIdAgencia,@pmtmFecUltFcr,@pmtmPPlazoFcr,@pmtmMayorFcr,@pmtmMenorFcr,@pmtmFecUltFco,@pmtmPPlazoFco,@pmtmMayorFco,@pmtmMenorFco,@pmtmVentaBruto,@pmtmVrDevolucion
,@pmtmVrDescuentos,@pmtmCostoBruto,@pmtmCostoDev,@pmtmVrIva,@pmtmFecUltPago,@pmtmPromPago,@pmtmSaldoMora,@pmtmFecIniMora,@pmtmDiasEnMora,@pmtmChequesDev,@pmtmSaldoActual,@pmtmSaldoAnterior,@pmtmVrFacturas
,@pmtmVrDevFacturas,@pmtmVrRecibos,@pmtmVrDevRecibos,@pmtmVrNotasDeb,@pmtmVrNotasCre,@pmtmVrCheques)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paInstm_EstCueCte @pmtmEst CHAR(2), @pmtmTipDoc VARCHAR(3), @pmtmDocumento INT, @pmtmIdCia CHAR(2), @pmtmItem INT, @pmtmIndTip INT, @pmtmFecha SMALLDATETIME, @pmtmFecVence SMALLDATETIME, @pmtmValorDebito MONEY, @pmtmValorCredito MONEY, @pmtmValorAbono MONEY
, @pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16), @pmtmIdVehiculo VARCHAR(10), @pmtmVehPropio BIT, @pmtmIdVendedor VARCHAR(16), @pmtmComision DECIMAL(10,4), @pmtmIdCargo VARCHAR(4), @pmtmIdClase VARCHAR(4), @pmtmIdConcepto VARCHAR(4), @pmtmFecUltPago SMALLDATETIME, @pmtmFactura VARCHAR(15), @pmtmReferencia VARCHAR(50)
,@pmtmDetalle VARCHAR(250), @pmtmTipCar VARCHAR(3), @pmtmCartulina INT, @pmtmIdCiaCar CHAR(2), @pmtmModalidad VARCHAR(10), @pmtmCentInicial INT, @pmtmCentFinal INT, @pmtmNPasajeros INT, @pmtmRecorridos INT, @pmtmValorRecibido MONEY, @pmtmTotalGastos MONEY,@pmtmTotalVales MONEY
, @pmtmTotalAhorro MONEY, @pmtmTotalOtros MONEY, @pmtmTotalAbono MONEY, @pmtmNumCheque VARCHAR(20), @pmtmIdBanco VARCHAR(4), @pmtmAnulado BIT, @pmtmNumDev INT, @pmtmFecDev SMALLDATETIME, @pmtmIntMora BIT, @pmtmCxPagar BIT, @pmtmTipDcm VARCHAR(3), @pmtmDocDev INT, @pmtmIdCiaDcm CHAR(2)
, @pmtmFecDcm SMALLDATETIME, @pmtmIdEstado VARCHAR(4), @pmtmIdUsuario VARCHAR(11), @pmtmTimeSys SMALLDATETIME, @pmtmSaldoAnt MONEY, @pmtmSaldoAntCla MONEY, @pmtmSaldoAntCia MONEY, @pmtmSaldoAntDia MONEY,@pmtmNuevoSaldo MONEY,@pmtmSalAntPro MONEY,@pmtmSalAntProCla MONEY,@pmtmSalAntProCia MONEY
AS
 INSERT INTO tm_EstCueCte (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente, 
                     tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmFactura, tmReferencia, 
                    tmDetalle, tmTipCar, tmCartulina, tmIdCiaCar, tmModalidad, tmCentInicial, tmCentFinal, tmNPasajeros, tmRecorridos, tmValorRecibido, tmTotalGastos, 
                   tmTotalVales, tmTotalAhorro, tmTotalOtros, tmTotalAbono, tmNumCheque, tmIdBanco, tmAnulado, tmNumDev, tmFecDev, tmIntMora, tmCxPagar, 
                  tmTipDcm, tmDocDev, tmIdCiaDcm, tmFecDcm, tmIdEstado, tmIdUsuario, tmTimeSys, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo,tmSalAntPro,tmSalAntProCla,tmSalAntProCia)
 VALUES (@pmtmEst, @pmtmTipDoc, @pmtmDocumento, @pmtmIdCia, @pmtmItem, @pmtmIndTip, @pmtmFecha, @pmtmFecVence, @pmtmValorDebito, @pmtmValorCredito, @pmtmValorAbono
, @pmtmIdCliente,@pmtmIdAgencia, @pmtmIdVehiculo, @pmtmVehPropio, @pmtmIdVendedor, @pmtmComision, @pmtmIdCargo, @pmtmIdClase, @pmtmIdConcepto, @pmtmFecUltPago, @pmtmFactura, @pmtmReferencia
,@pmtmDetalle, @pmtmTipCar, @pmtmCartulina, @pmtmIdCiaCar, @pmtmModalidad, @pmtmCentInicial, @pmtmCentFinal, @pmtmNPasajeros, @pmtmRecorridos, @pmtmValorRecibido, @pmtmTotalGastos,@pmtmTotalVales
, @pmtmTotalAhorro, @pmtmTotalOtros, @pmtmTotalAbono, @pmtmNumCheque, @pmtmIdBanco, @pmtmAnulado, @pmtmNumDev, @pmtmFecDev, @pmtmIntMora, @pmtmCxPagar, @pmtmTipDcm, @pmtmDocDev, @pmtmIdCiaDcm
, @pmtmFecDcm, @pmtmIdEstado, @pmtmIdUsuario, @pmtmTimeSys, @pmtmSaldoAnt, @pmtmSaldoAntCla, @pmtmSaldoAntCia, @pmtmSaldoAntDia,@pmtmNuevoSaldo,@pmtmSalAntPro,@pmtmSalAntProCla,@pmtmSalAntProCia)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstCueCte_Cc @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
  INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
 ,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmFactura, tmReferencia,tmDetalle, tmCartulina
 ,tmIdUsuario,tmIdEstado,tmModalidad,tmCxPagar, tmTimeSys, tmAnulado, tmNumDev, tmFecDev,tmIdBanco, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,R.TipRec,R.Recibo,R.IdCia,R.Item+10000,@pmtmIndTip,R.Fecha,V.FecVence,0,R.ValorAbono,0,R.IdCliente
,R.IdAgencia,R.IdVehiculo,R.VehPropio,R.IdVendedor,R.Comision,R.IdCargo,R.IdClase,V.IdConcepto,FecPago,V.Factura,V.Referencia,R.Detalle,V.NotaInt
,R.IdUsuario,'0001','CCTE',0,R.TimeSys,0,0,Null,'0',0,0,0,0,0
 FROM Trn_RecFactura AS R INNER JOIN Trn_CausacVence AS V ON R.TipDoc =V.TipDoc  AND R.Documento=V.Documento  AND R.IdCiaDoc=V.IdCia AND R.ItemDoc=V.Item
 INNER JOIN Vehiculos AS VH ON R.IdVehiculo=VH.IdVehiculo 
 WHERE R.TipRec='OTR'  AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND R.IdCia like ISNULL(@pmIdCia,'%%')
 AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND VH.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstCueCte_Dfv @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
AS
 INSERT INTO tm_EstCueCte (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
,tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdClase,tmIdCargo,tmIdBanco,tmIdAgencia,tmIdConcepto, tmDetalle, tmTipDcm, tmDocDev, tmIdCiaDcm, tmFecDcm, tmIdEstado
, tmIdUsuario, tmTimeSys, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,TipDev, Devolucion,D.IdCia,Item,@pmtmIndTip,Fecha,FecVence,0,ValorFactura,0,D.IdCliente,VC.IdVehiculo,VC.VehPropio,VC.IdVendedor,0,VC.IdClase,IdCargo,'0','0',VC.IdConcepto
,D.Observacion,D.TipDoc, D.Factura, IdCiaDoc, FecDoc,D.IdEstado,D.IdUsuario,TimeSys,0,0,0,0,0
 FROM    Trn_DevFvc AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Factura=VC.Documento AND D.IdCiaDoc=VC.IdCia
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo 
 WHERE  TipDev='DFV'  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND VC.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY  D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstCueCte_Dvo @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null,@pmTipDev VARCHAR(3)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
AS
 INSERT INTO tm_EstCueCte (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
,tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdClase,tmIdCargo,tmIdBanco,tmIdAgencia,tmIdConcepto, tmDetalle, tmTipDcm, tmDocDev, tmIdCiaDcm, tmFecDcm, tmIdEstado
, tmIdUsuario, tmTimeSys, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,TipDev, Devolucion,D.IdCia,Item,@pmtmIndTip,Fecha,FecVence,0,ValorFactura,0,IdCliente,VC.IdVehiculo,VC.VehPropio,VC.IdVendedor,0,VC.IdClase,IdCargo,'0','0',VC.IdConcepto
,D.Observacion,D.TipDoc, D.Documento, IdCiaDoc, FecDoc,D.IdEstado,D.IdUsuario,TimeSys,0,0,0,0,0
 FROM    Trn_DevCausacion AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Documento=VC.Documento AND D.IdCiaDoc=VC.IdCia
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo 
 WHERE  TipDev IN ('DVO','DVA')  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.TipDoc LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND TipDev LIKE ISNULL(@pmTipDev,'%')
 ORDER BY  D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstCueCte_Fvc @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
  INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
 ,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmFactura, tmReferencia,tmDetalle, tmCartulina
 ,tmIdUsuario,tmIdEstado,tmModalidad,tmCxPagar, tmTimeSys, tmAnulado, tmNumDev, tmFecDev,tmIdBanco, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia, Item,@pmtmIndTip,FecEmision, V.FecVence,ValorFactura,0,ValorAbono,V.IdCliente, V.IdAgencia, V.IdVehiculo,V.VehPropio, IdVendedor, Comision
 ,IdCargo, V.IdClase, V.IdConcepto,FecUltPago,V.Factura, V.Referencia,Detalle,NotaInt,C.IdUsuario,C.IdEstado,'FVC',0,TimeSys,Anulado,NumDev, FecDev,'0',0,0,0,0,0
 FROM  Trn_CausacVence AS V INNER JOIN Trn_CausaVales AS C ON V.TipDoc=C.TipDoc AND V.Documento=C.Documento AND V.IdCia=C.IdCia
 INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
 WHERE V.TipDoc='FVC'  AND FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND V.IdCia like ISNULL(@pmIdCia,'%%')
 AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') 
 ORDER BY V.TipDoc, V.IdCia,V.Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EstCueCte_Ncr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
 INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto,   tmReferencia
,tmDetalle, tmModalidad,tmNumCheque, tmIdBanco, tmIntMora,  tmTipDcm, tmDocDev, tmIdCiaDcm,  tmIdEstado
, tmIdUsuario, tmTimeSys, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst, TipDoc, NumNota, IdCia,1,@pmtmIndTip, Fecha,FecVence,0,ValorNeto,ValorAplicado,IdCliente, IdAgencia, N.IdVehiculo, N.VehPropio,N.IdConductor,Comision
,IdCargo,N.IdClase,IdConcepto,Referencia,N.Observacion,'NC1',NumCheque, IdBanco,IntMora,TipDcm, Documento, IdCiaDoc,N.IdEstado,N.IdUsuario,TimeSys,0,0,0,0,0
 FROM  Trn_NotasBuses  AS N INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo 
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia like ISNULL(@pmIdCia,'%%')
 AND N.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND N.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY  IdCia,NumNota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EstCueCte_Ndb  @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
 INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
 ,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmFactura, tmReferencia,tmDetalle, tmCartulina,tmNumCheque, tmIdBanco
 ,tmIntMora,tmIdUsuario,tmIdEstado,tmModalidad, tmTimeSys,tmTipDcm, tmDocDev, tmIdCiaDcm,tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia, Item,@pmtmIndTip,FecEmision, V.FecVence,ValorFactura,0,ValorAbono,V.IdCliente, V.IdAgencia, V.IdVehiculo,V.VehPropio, V.IdVendedor, V.Comision
 ,V.IdCargo, V.IdClase, V.IdConcepto,FecUltPago,V.Factura, V.Referencia,Detalle,NotaInt,NumCheque,IdBanco,IntMora,N.IdUsuario,N.IdEstado,N.TipDoc,TimeSys,TipDcm,N.Documento,IdCiaDoc,0,0,0,0,0
 FROM  Trn_CausacVence AS V INNER JOIN Trn_NotasBuses AS N ON V.TipDoc=N.TipDoc  AND V.Documento=N.NumNota AND V.IdCia=N.IdCia
 INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
 WHERE V.TipDoc=@pmTipDoc AND FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND V.IdCia like ISNULL(@pmIdCia,'%%')
 AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY V.TipDoc, V.IdCia,V.Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EstCueCte_Odb  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
 INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto,  tmReferencia
,tmDetalle, tmTipCar, tmCartulina, tmIdCiaCar, tmModalidad, tmCentInicial, tmCentFinal, tmNPasajeros, tmRecorridos, tmValorRecibido, tmTotalGastos,tmTotalVales, tmTotalAhorro
, tmTotalOtros, tmTotalAbono, tmNumCheque, tmIdBanco, tmCxPagar, tmTipDcm, tmDocDev, tmIdCiaDcm, tmFecDcm, tmIdEstado
, tmIdUsuario, tmTimeSys, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
SELECT @pmtmEst,TipDev, Devolucion, D.IdCia,1,@pmtmIndTip,D.Fecha,D.Fecha AS FecVce,D.ValorNeto,0, D.ValorAplicado,D.IdCliente, D.IdAgencia, D.IdVehiculo, D.VehPropio,D.IdConductor,D.Comision,'0',D.IdClase
,D.IdConcepto,Referencia,D.Observacion,TipCar, Cartulina, IdCiaCar,D.Modalidad,R.CentInicial,R.CentFinal, NPasajeros, Recorridos,D.ValorRecibido,TotalGastos,TotalVales,TotalAhorro
,TotalOtros,TotalAbono, D.NumCheque, D.IdBanco,D.EnEfectivo, D.TipDoc, D.Recibo, IdCiaDoc, FecDoc,R.IdEstado,R.IdUsuario,D.TimeSys,0,0,0,0,0
 FROM  Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses  AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo 
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY  D.IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EstCueCte_Pre @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
 --estado cuenta -prestamos
  INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
 ,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmFactura, tmReferencia,tmDetalle, tmCartulina
 ,tmIdUsuario,tmIdEstado,tmModalidad,tmCxPagar, tmTimeSys, tmAnulado, tmNumDev, tmFecDev,tmIdBanco, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia, Item,@pmtmIndTip,FecEmision, V.FecVence,ValorFactura,0,ValorAbono,IdCliente, IdAgencia, V.IdVehiculo,V.VehPropio, IdVendedor, Comision
 ,IdCargo, V.IdClase, V.IdConcepto,FecUltPago,V.Factura, V.Referencia,Detalle,NotaInt,P.IdUsuario,P.IdEstado,TipoCuota,CxPagar,TimeSys,Anulado,NumDev, FecDev,'0',0,0,0,0,0
 FROM  Trn_CausacVence AS V INNER JOIN Trn_Prestamos AS P ON V.TipDoc=P.TipDoc AND V.Documento=P.PrestamoID AND V.IdCia=P.IdCia
 INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
 WHERE V.TipDoc='PRE'  AND FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND V.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 ORDER BY V.TipDoc, V.IdCia,V.Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_EstCueCte_Rec  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
 INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
 ,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmReferencia
 ,tmDetalle, tmTipCar, tmCartulina, tmIdCiaCar, tmModalidad, tmCentInicial, tmCentFinal, tmNPasajeros, tmRecorridos, tmValorRecibido, tmTotalGastos,tmTotalVales, tmTotalAhorro
 , tmTotalOtros, tmTotalAbono, tmNumCheque, tmIdBanco, tmAnulado, tmNumDev, tmFecDev, tmCxPagar,tmIdEstado
 , tmIdUsuario, tmTimeSys, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,TipDoc, Recibo, IdCia, 1,@pmtmIndTip,Fecha,Fecha AS FecVce,0,ValorNeto,ValorAplicado,IdCliente, IdAgencia,R.IdVehiculo,R.VehPropio,R.IdConductor,Comision,'0'
,R.IdClase,IdConcepto,Referencia,R.Observacion,TipCar, Cartulina, IdCiaCar,Modalidad,R.CentInicial,R.CentFinal, NPasajeros, Recorridos,ValorRecibido,TotalGastos,TotalVales,TotalAhorro
,TotalOtros,TotalAbono, NumCheque, IdBanco, Anulado, NumDev, FecDev,EnEfectivo,R.IdEstado,R.IdUsuario,TimeSys,0,0,0,0,0
 FROM  Trn_RecBuses  AS R INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY  IdCia,Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstCueCte_Vce @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmtmEst CHAR(2),@pmtmIndTip INT,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS
  INSERT INTO tm_EstCueCte  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmIndTip, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmValorAbono, tmIdCliente
 ,tmIdAgencia, tmIdVehiculo, tmVehPropio, tmIdVendedor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmFactura, tmReferencia,tmDetalle, tmCartulina
 ,tmIdUsuario,tmIdEstado,tmModalidad,tmCxPagar, tmTimeSys, tmAnulado, tmNumDev, tmFecDev,tmIdBanco, tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo)
 SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia, Item,@pmtmIndTip,FecEmision, V.FecVence,ValorFactura,0,ValorAbono,IdCliente, IdAgencia, V.IdVehiculo,V.VehPropio, IdVendedor, Comision
 ,IdCargo, V.IdClase, V.IdConcepto,FecUltPago,V.Factura, V.Referencia,Detalle,NotaInt,C.IdUsuario,C.IdEstado,Modalidad,CxPagar,TimeSys,Anulado,NumDev, FecDev,'0',0,0,0,0,0
 FROM  Trn_CausacVence AS V INNER JOIN Trn_Causacion AS C ON V.TipDoc=C.TipDoc AND V.Documento=C.Documento AND V.IdCia=C.IdCia
 INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
 WHERE V.TipDoc IN ('ADM','OTR')  AND FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND V.IdCia like ISNULL(@pmIdCia,'%%')
 AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 ORDER BY V.TipDoc, V.IdCia,V.Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_EstadoGyp @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT,@pmtmIdCia CHAR(2),@pmtmIdTercero VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16)
,@pmtmCodAgncia VARCHAR(16),@pmtmTipo VARCHAR(4),@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 INSERT INTO tm_EstadoGyp (tmEst,tmIdCuenta,tmItem,tmIdCia,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmCodAgncia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2)
 VALUES (@pmtmEst,@pmtmIdCuenta,@pmtmItem,@pmtmIdCia,@pmtmIdTercero,@pmtmIdVehiculo,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmCodAgncia,@pmtmTipo,@pmtmSaldoAnterior,@pmtmTotalDebitos,@pmtmTotalCreditos,@pmtmNuevoSaldo
,@pmtmSalAnt2,@pmtmDebitos2,@pmtmCreditos2,@pmtmSaldo2)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_ExtVeh @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdConductor VARCHAR(16),@pmtmVehPropio BIT,@pmtmTotaDebito MONEY,@pmtmTotaCredito MONEY
,@pmtmSaldoAnt MONEY,@pmtmSaldoAntCla MONEY,@pmtmSalAntPro MONEY,@pmtmSalAntProCla MONEY,@pmtmTotalProducido MONEY,@pmtmMaxProducido MONEY,@pmtmMinProducido MONEY,@pmtmTotalRecrridos INT,@pmtmTotalPasaj INT
,@pmtmTotalDias INT,@pmtmTotalCausac MONEY,@pmtmGalones DECIMAL(14,4),@pmtmValorComb MONEY
 AS 
 INSERT INTO tm_ExtVeh (tmEst,tmIdPropietario,tmIdVehiculo,tmIdConductor,tmVehPropio,tmTotaDebito,tmTotaCredito,tmSaldoAnt,tmSaldoAntCla,tmSalAntPro,tmSalAntProCla,tmTotalProducido,tmMaxProducido,tmMinProducido,tmTotalRecrridos,tmTotalPasaj,tmTotalDias,tmTotalCausac,tmGalones,tmValorComb)
 VALUES (@pmtmEst,@pmtmIdPropietario,@pmtmIdVehiculo,@pmtmIdConductor,@pmtmVehPropio,@pmtmTotaDebito,@pmtmTotaCredito,@pmtmSaldoAnt,@pmtmSaldoAntCla,@pmtmSalAntPro,@pmtmSalAntProCla,@pmtmTotalProducido,@pmtmMaxProducido,@pmtmMinProducido,@pmtmTotalRecrridos
,@pmtmTotalPasaj,@pmtmTotalDias,@pmtmTotalCausac,@pmtmGalones,@pmtmValorComb)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_ExtVehRec @pmtmEst CHAR(2)
AS
 INSERT INTO tm_ExtVeh (tmEst,tmIdPropietario,tmIdVehiculo,tmIdConductor,tmVehPropio,tmTotaDebito,tmTotaCredito,tmTotalProducido,tmMaxProducido,tmMinProducido,tmTotalRecrridos,tmTotalPasaj)
 SELECT tmEst,tmIdCliente,tmIdVehiculo,IdConductor,CASE TipoAfil WHEN 'PROPIO' THEN 1 ELSE 0 END,SUM(tmValorDev),SUM(tmValor),SUM(tmTotalProducido),MAX(tmTotalProducido),MIN(tmTotalProducido),SUM(tmRecorridos),SUM(tmNPasajeros)
 FROM  tm_EVrec AS TR INNER JOIN Vehiculos AS V ON TR.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst
 GROUP BY  tmEst,tmIdCliente,tmIdVehiculo,IdConductor,CASE TipoAfil WHEN 'PROPIO' THEN 1 ELSE 0 END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ForItems @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmCantidad DECIMAL(14,4),@pmtmVrPrecio MONEY
,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifaDct DECIMAL(14,4),@pmtmVrDcto MONEY,@pmtmVrBruto MONEY,@pmtmDescripcion VARCHAR(150),@pmtmServcios BIT,@pmtmListaPrec CHAR(1)
 AS 
 INSERT INTO tm_ForItems (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCantidad,tmVrPrecio,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmVrBruto,tmDescripcion,tmServcios,tmListaPrec)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmCantidad,@pmtmVrPrecio,@pmtmTarifaIva,@pmtmVrIva,@pmtmTarifaDct,@pmtmVrDcto,@pmtmVrBruto,@pmtmDescripcion,@pmtmServcios,@pmtmListaPrec)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ForItems_Sel @pmtmNumero VARCHAR(5),@pmTipoRango VARCHAR(3),@pmIdRango INT
,@pmNumSerie VARCHAR(5),@pmNumForma INT
 AS 
 INSERT INTO tm_ForItems (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCantidad,tmVrPrecio,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmVrBruto,tmDescripcion,tmServcios,tmListaPrec)
 SELECT @pmtmNumero,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,VrPrecio,Descripcion,Servicios,ListaPrec
 FROM Trn_EdsForItems
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioDfc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDev,Devolucion,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto+VrSobretasa,VrRetencion,VrReteICA,VrReteIVA
,VrFletes,VrCargos,VrOtrDcto,VrImpGlobal,VrOtros,VrCostos,VrNeto,0,VrNeto,0,BaseImp,0,0,'0','','0','',IdVend,TarifaCom,'0',IdUsuario,IdLocEnv,'CREDITO','',Modalidad,'PED',Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,0,Factura,FecDoc,Observacion,TimeSys
 FROM Trn_DevFcr
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioDfo @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDev,Devolucion,D.IdCia,D.Fecha,D.IdConcepto,D.NitCliente,D.CdAgencia,D.VrSubTotal,D.VrDescuento,D.VrImpuesto,D.VrRetencion,D.VrReteICA,D.VrReteIVA
,D.VrFletes,D.VrCargos,D.VrOtrDcto,0,0,D.VrCostos,D.VrNeto,D.VrEfectivo,D.VrPagosOtr,0,D.BaseImp,TarifaIva,D.EnEfectivo,F.CdForma,F.NumForma
,F.CdBanco,SUBSTRING(F.DetallePago,1,150),D.IdVend,D.TarifaCom,D.nClieCon,D.IdCajero,D.IdLocal,Referncia1,Referncia2,D.Modalidad,'PED',D.Pedido,D.IdCiaPed,D.TipCom,D.Comprobante,D.IdCiaCom,0,D.Factura,FecDoc,D.Observacion,D.TimeSys
 FROM Trn_DevFco AS D INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioDnp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,0,VrSobretasa,0,0,0,0,0,0,VrImpGlobal,VrOtros,VrCostos,VrNeto,VrNeto,0,0,VrSubTotal,0,1,'EFE',''
,'0','',IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','NOTACALIB','0',0,'00',TipCom,Comprobante,IdCiaCom,0,nNota,FecDoc,Observacion,TimeSys
 FROM Trn_DevPas
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioDvp @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDev,Devolucion,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,VrConDcto+VrCreDcto,VrSobretasa,0,0,0,VrCreTotal+VrConTotal+VrCreImp+VrConImp
,VrSobrantes,VrFaltantes,VrImpGlobal,VrOtros,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos+VrGastComb+VrGastOtros,VrSubTotal,0,0,'EFE',''
,'0','',IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'',Modalidad,'COR',NumCorte,'00',TipCom,Comprobante,IdCiaCom,0,Planilla,FecDoc,Observacion,TimeSys
 FROM Trn_DevPla
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioFco @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDoc,Factura,IdCia,Fecha,IdConcepto,NitCliente,CdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA
,VrFletes,VrCargos,VrOtrDcto,0,0,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,0,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,SUBSTRING(DetallePago,1,150),IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referncia1,Referncia2,Modalidad,TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Factcon
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioFcr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,2,TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto+VrSobretasa,VrRetencion,VrReteICA,VrReteIVA
,VrFletes,VrCargos,VrOtrDcto,VrImpGlobal,VrOtros,VrCostos,VrNeto,0,VrNeto,0,BaseImp,TarifaIva,0,IdForma,''
,'0',SUBSTRING(DetallePago,1,150),IdVend,TarifaCom,'0',IdUsuario,IdLocEnv,'CREDITO','',Modalidad,TipPed,Pedido,IdCiaPed,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_Facturas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioPas @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDoc,nNota,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,0,VrSobretasa,0,0,0,0,0,0,VrImpGlobal,VrOtros,VrCostos,VrNeto,VrNeto,0,0,VrSubTotal,0,1,'EFE',''
,'0','',IdVend,TarifaCom,'0',IdCajero,'0',Referencia,'','NOTACALIB','0',0,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCalibra
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_InfDiarioPla @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCajero VARCHAR(11)=Null
 AS 
 INSERT INTO tm_InfDiario (tmEst,tmItem,TipDoc,Numero,IdCia,Fecha,IdConcepto,IdTercero,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA
,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,BaseImp,TarifaIva,EnEfectivo,CdForma,NumForma
,CdBanco,DetallePagos,IdVend,TarifaCom,nClieCon,IdCajero,IdLocal,Referencia,Referencia2,Modalidad,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys)
SELECT @pmtmEst,1,TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,'0',VrSubTotal,VrConDcto+VrCreDcto,VrSobretasa,0,0,0,VrCreTotal+VrConTotal+VrCreImp+VrConImp
,VrSobrantes,VrFaltantes,VrImpGlobal,VrOtros,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos+VrGastComb+VrGastOtros,VrSubTotal,0,0,'EFE',''
,'0','',IdVend,TarifaCom,NitRef,IdCajero,'0',Referencia,'',Modalidad,'COR',NumCorte,'00',TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,TimeSys
 FROM Trn_EdsCortes
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Ingresos @pmtmEst CHAR(2),@pmtmItem INT,@pmtmNMes VARCHAR(6),@pmtmIdEmpleado VARCHAR(16),@pmtmIdConcepto VARCHAR(4),@pmtmDetalle VARCHAR(100),@pmtmVrBase MONEY,@pmtmCantDevg DECIMAL(10,4),@pmtmCantDed DECIMAL(10,4),@pmtmVrDevngado MONEY,@pmtmVrDeducido MONEY,@pmtmTarifa DECIMAL(10,4),@pmtmDiasBase INT,@pmtmDiasNoRem INT
,@pmtmFecInicial SMALLDATETIME,@pmtmFecFinal SMALLDATETIME,@pmtmNContrato INT,@pmtmpVehiculo VARCHAR(10),@pmtmIdDep VARCHAR(4),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmIdInstala VARCHAR(4),@pmtmVrSalario MONEY,@pmtmCodFondo VARCHAR(8),@pmtmClaseLiq VARCHAR(10),@pmtmOrigenLiq VARCHAR(10),@pmtmBaseRet BIT
,@pmtmEsPstamo BIT,@pmtmEsRetiro BIT,@pmtmNPrestamo INT
 AS 
 INSERT INTO tm_Ingresos (tmEst,tmItem,tmNMes,tmIdEmpleado,tmIdConcepto,tmDetalle,tmVrBase,tmCantDevg,tmCantDed,tmVrDevngado,tmVrDeducido,tmTarifa,tmDiasBase,tmDiasNoRem,tmFecInicial,tmFecFinal,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmIdInstala,tmVrSalario,tmCodFondo,tmClaseLiq,tmOrigenLiq,tmBaseRet,tmEsPstamo,tmEsRetiro,tmNPrestamo)
 VALUES (@pmtmEst,@pmtmItem,@pmtmNMes,@pmtmIdEmpleado,@pmtmIdConcepto,@pmtmDetalle,@pmtmVrBase,@pmtmCantDevg,@pmtmCantDed,@pmtmVrDevngado,@pmtmVrDeducido,@pmtmTarifa,@pmtmDiasBase,@pmtmDiasNoRem,@pmtmFecInicial,@pmtmFecFinal,@pmtmNContrato,@pmtmpVehiculo,@pmtmIdDep,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdNom,@pmtmIdInstala
,@pmtmVrSalario,@pmtmCodFondo,@pmtmClaseLiq,@pmtmOrigenLiq,@pmtmBaseRet,@pmtmEsPstamo,@pmtmEsRetiro,@pmtmNPrestamo)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_IntAhorro @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdVehiculo VARCHAR(10),@pmtmIdTercero VARCHAR(16),@pmtmSaldoBase MONEY
,@pmtmTasa DECIMAL(14,4),@pmtmValor MONEY,@pmtmNmes INT,@pmtmVehPropio BIT,@pmtmIdConductor VARCHAR(16),@pmtmIdPropietario VARCHAR(16)
, @pmtmConductor VARCHAR(100),@pmtmTercero VARCHAR(100),@pmtmRetencion MONEY,@pmtmVrAbono MONEY
 AS
 INSERT INTO tm_IntAhorro (tmEst, tmItem, tmIdVehiculo, tmIdTercero, tmSaldoBase, tmTasa, tmValor, tmNmes, tmVehPropio, tmIdConductor, tmIdPropietario, tmConductor,tmTercero,tmRetencion,tmVrAbono)
 VALUES (@pmtmEst, @pmtmItem, @pmtmIdVehiculo, @pmtmIdTercero, @pmtmSaldoBase, @pmtmTasa, @pmtmValor, @pmtmNmes, @pmtmVehPropio, @pmtmIdConductor
, @pmtmIdPropietario, @pmtmConductor,@pmtmTercero,@pmtmRetencion,@pmtmVrAbono)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_IntNomina @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdVehiculo VARCHAR(10),@pmtmIdEmpleado VARCHAR(16)
,@pmtmIdConductor VARCHAR(16),@pmtmIdPropietario VARCHAR(16),@pmtmVehPropio BIT,@pmtmConductor VARCHAR(100),@pmtmTercero VARCHAR(100)
,@pmtmCcosto VARCHAR(16),@pmtmValorAbono MONEY
 AS 
 INSERT INTO tm_IntNomina (tmEst,tmItem,tmIdVehiculo,tmIdEmpleado,tmIdConductor,tmIdPropietario,tmVehPropio,tmConductor,tmTercero,tmCcosto,tmValorAbono)
 VALUES (@pmtmEst,@pmtmItem,@pmtmIdVehiculo,@pmtmIdEmpleado,@pmtmIdConductor,@pmtmIdPropietario,@pmtmVehPropio,@pmtmConductor,@pmtmTercero
,@pmtmCcosto,@pmtmValorAbono)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_KarUbi @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16)
,@pmtmIdBodega VARCHAR(4),@pmtmIdUbic VARCHAR(10),@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4)
,@pmtmLinea INT,@pmtmCdNueBod VARCHAR(4),@pmtmCdNueUbic VARCHAR(10)
 AS 
 INSERT INTO tm_KarUbi (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmIdUbic,tmEntradas,tmSalidas,tmLinea,tmCdNueBod,tmCdNueUbic)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmIdUbic,@pmtmEntradas,@pmtmSalidas,@pmtmLinea,@pmtmCdNueBod,@pmtmCdNueUbic)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_KarUbi_Dvn @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
 AS 
 INSERT INTO tm_KarUbi (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmIdUbic,tmEntradas,tmSalidas,tmLinea)
 SELECT @pmtmNumero,Item,IdProducto,IdBodega,IdUbic,Salidas,Entradas,Linea
 FROM Trn_KarUbic
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_KarUbi_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
 AS 
 INSERT INTO tm_KarUbi (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmIdUbic,tmEntradas,tmSalidas,tmLinea)
 SELECT @pmtmNumero,Item,IdProducto,IdBodega,IdUbic,Entradas,Salidas,Linea
 FROM Trn_KarUbic
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%') 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmCdTanque VARCHAR(4),@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmIdUnd VARCHAR(4),@pmtmVrUnitario MONEY,@pmtmVrPrecio MONEY,@pmtmVrCostProm MONEY,@pmtmVrProm MONEY
,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifaDct DECIMAL(14,4),@pmtmVrDcto MONEY,@pmtmTarifaRet DECIMAL(14,4),@pmtmVrRete MONEY,@pmtmTarifaIca DECIMAL(14,4),@pmtmVrIca MONEY,@pmtmVrBruto MONEY,@pmtmCdUbic VARCHAR(10),@pmtmNumLote VARCHAR(30),@pmtmFechLote SMALLDATETIME,@pmtmFecVceLote SMALLDATETIME
,@pmtmIdTercero VARCHAR(16),@pmtmCdAgencia VARCHAR(16),@pmtmCdCCosto VARCHAR(16),@pmtmCdSubCos VARCHAR(16),@pmtmCdLocal VARCHAR(8),@pmtmCdSzona VARCHAR(4),@pmtmpVehiculo VARCHAR(10),@pmtmIdVend VARCHAR(16),@pmtmComision DECIMAL(14,4),@pmtmCdOperario VARCHAR(16),@pmtmComisnOper DECIMAL(14,4),@pmtmReferencia VARCHAR(50)
,@pmtmDescripcion VARCHAR(250),@pmtmComptmntos VARCHAR(50),@pmtmCdProdEquiv VARCHAR(16),@pmtmCdMngra VARCHAR(10),@pmtmNumInicial DECIMAL(14,4),@pmtmNumFinal DECIMAL(14,4),@pmtmSobretasa DECIMAL(14,4),@pmtmTasaNac DECIMAL(14,4),@pmtmTasaDep DECIMAL(14,4),@pmtmTasaMun DECIMAL(14,4),@pmtmSoldicom DECIMAL(14,4)
,@pmtmImpGlobal DECIMAL(14,4),@pmtmOtroImpto DECIMAL(14,4),@pmtmUnidades DECIMAL(14,4),@pmtmServcios BIT,@pmtmNoVentas INT,@pmtmEsCombo BIT,@pmtmEsProdBase INT,@pmtmItemCbo INT,@pmtmCodTarDct VARCHAR(4),@pmtmCodTarIva VARCHAR(4),@pmtmCodTarIca VARCHAR(4),@pmtmCodTarRet VARCHAR(4),@pmtmCodTarCom VARCHAR(4)
,@pmtmCodTarCmc VARCHAR(4),@pmtmCdSubgrupo VARCHAR(8),@pmtmListaPrec CHAR(1),@pmtmVrBase MONEY,@pmtmCdMoneda VARCHAR(5),@pmtmVrTasaCamb MONEY,@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmTipRem VARCHAR(3),@pmtmRemision INT,@pmtmIdCiaRem CHAR(2),@pmtmReferencia2 VARCHAR(50),@pmtmFecOrden SMALLDATETIME
,@pmtmgalsbruto DECIMAL(14,4),@pmtmgalsneto DECIMAL(14,4),@pmtmTemperatura DECIMAL(14,4),@pmtmUmTemp VARCHAR(3),@pmtmDensidad DECIMAL(14,4),@pmtmRec_Costo DECIMAL(14,4)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad,tmRec_Costo)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmCdTanque,@pmtmEntradas,@pmtmSalidas,@pmtmIdUnd,@pmtmVrUnitario,@pmtmVrPrecio,@pmtmVrCostProm,@pmtmVrProm,@pmtmTarifaIva,@pmtmVrIva,@pmtmTarifaDct,@pmtmVrDcto,@pmtmTarifaRet,@pmtmVrRete,@pmtmTarifaIca,@pmtmVrIca,@pmtmVrBruto,@pmtmCdUbic,@pmtmNumLote,@pmtmFechLote,@pmtmFecVceLote
,@pmtmIdTercero,@pmtmCdAgencia,@pmtmCdCCosto,@pmtmCdSubCos,@pmtmCdLocal,@pmtmCdSzona,@pmtmpVehiculo,@pmtmIdVend,@pmtmComision,@pmtmCdOperario,@pmtmComisnOper,@pmtmReferencia,@pmtmDescripcion,@pmtmComptmntos,@pmtmCdProdEquiv,@pmtmCdMngra,@pmtmNumInicial,@pmtmNumFinal,@pmtmSobretasa,@pmtmTasaNac,@pmtmTasaDep,@pmtmTasaMun,@pmtmSoldicom
,@pmtmImpGlobal,@pmtmOtroImpto,@pmtmUnidades,@pmtmServcios,@pmtmNoVentas,@pmtmEsCombo,@pmtmEsProdBase,@pmtmItemCbo,@pmtmCodTarDct,@pmtmCodTarIva,@pmtmCodTarIca,@pmtmCodTarRet,@pmtmCodTarCom,@pmtmCodTarCmc,@pmtmCdSubgrupo,@pmtmListaPrec,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia
,@pmtmVrBase,@pmtmCdMoneda,@pmtmVrTasaCamb,@pmtmTipRem,@pmtmRemision,@pmtmIdCiaRem,@pmtmReferencia2,@pmtmFecOrden,@pmtmgalsbruto,@pmtmgalsneto,@pmtmTemperatura,@pmtmUmTemp,@pmtmDensidad,@pmtmRec_Costo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_Nts @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad,tmRec_Costo)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Entradas,Salidas,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipDoc,Documento,IdCia,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,0,0,0,'',0,Rec_Costo
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND EsProdBase=0 AND Salidas>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_Sel @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad,tmRec_Costo)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Entradas,Salidas,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipOrd,NumOrden,IdCiaOrd,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,Rec_Costo
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND EsProdBase=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_SelDev @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad,tmRec_Costo)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Salidas,Entradas,K.IdUnd,VrUnitario,VrPrecio,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipOrd,NumOrden,IdCiaOrd,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,Rec_Costo
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia  AND  EsProdBase<=1
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Kdex_SelDve @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Kdex (tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete
,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto
,tmUnidades,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmCdSubgrupo
,tmListaPrec,tmTipDoc,tmDocumento,tmIdCia,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad,tmRec_Costo)
SELECT @pmtmNumero,Item,K.IdProducto,K.IdBodega,CdTanque,Salidas,Entradas,K.IdUnd,CASE WHEN NumInicial>0 THEN VrUnitario-NumInicial ELSE VrUnitario END,CASE WHEN NumInicial>0 THEN VrPrecio-NumInicial ELSE VrPrecio END
,VrCostProm,0,TarifaIva,VrIvaEnt+VrIvaSal,TarifaDct,VrDctoEnt+VrDctoSal,TarifaRet,VrReteEnt+VrReteSal
,TarifaIca,VrIcaEnt+VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,FechLote,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona,pVehiculo,IdVend
,Comision,CdOperario,ComisnOper,K.Referencia,Descripcion,Comptmntos,CdProdEquiv,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,CodTarDct,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,IdSubgrupo,ListaPrec,TipOrd,NumOrden,IdCiaOrd,VrBase,CdMoneda,VrTasaCamb
,'REM',Remision,IdCiaRem,Referencia2,FecOrden,galsbruto,galsneto,Temperatura,UmTemp,Densidad,Rec_Costo
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_LibOf @pmtmEst CHAR(2),@pmtmTipCom VARCHAR(3),@pmtmItem INT,@pmtmIdCuenta VARCHAR(16),@pmtmIdTercero VARCHAR(16)
,@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdCia CHAR(2),@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmSaldoAnterior MONEY
,@pmtmSanNit MONEY,@pmtmSanNitcc MONEY,@pmtmSanNitCcSub MONEY
 AS 
 INSERT INTO tm_LibOf (tmEst,tmTipCom,tmItem,tmIdCuenta,tmIdTercero,tmIdCCosto,tmIdSubCos,tmIdCia,tmTotalDebitos,tmTotalCreditos,tmSaldoAnterior,tmSanNit,tmSanNitcc,tmSanNitCcSub)
 VALUES (@pmtmEst,@pmtmTipCom,@pmtmItem,@pmtmIdCuenta,@pmtmIdTercero,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdCia,@pmtmTotalDebitos,@pmtmTotalCreditos
,@pmtmSaldoAnterior,@pmtmSanNit,@pmtmSanNitcc,@pmtmSanNitCcSub)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Liquida @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdEmpleado VARCHAR(16),@pmtmIdConcepto VARCHAR(4),@pmtmDetalle VARCHAR(100),@pmtmFecUltLiq SMALLDATETIME,@pmtmFecInicial SMALLDATETIME,@pmtmFecFinal SMALLDATETIME,@pmtmVrBase MONEY,@pmtmCantidad INT,@pmtmVrLiquida MONEY,@pmtmVrDeducido MONEY,@pmtmFecIniPmd SMALLDATETIME,@pmtmFecFinPmd SMALLDATETIME,@pmtmDiasBase INT
,@pmtmDiasNoRem INT,@pmtmTarifa DECIMAL(10,4),@pmtmNPrestamo INT,@pmtmNCuota INT,@pmtmLogPmdio VARCHAR(600),@pmtmNContrato INT,@pmtmpVehiculo VARCHAR(10),@pmtmIdDep VARCHAR(4),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmVrSalario MONEY,@pmtmVrParcial MONEY
 AS 
 INSERT INTO tm_Liquida (tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmDetalle,tmFecUltLiq,tmFecInicial,tmFecFinal,tmVrBase,tmCantidad,tmVrLiquida,tmVrDeducido,tmFecIniPmd,tmFecFinPmd,tmDiasBase,tmDiasNoRem,tmTarifa,tmNPrestamo,tmNCuota,tmLogPmdio,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmVrSalario,tmVrParcial)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdEmpleado,@pmtmIdConcepto,@pmtmDetalle,@pmtmFecUltLiq,@pmtmFecInicial,@pmtmFecFinal,@pmtmVrBase,@pmtmCantidad,@pmtmVrLiquida,@pmtmVrDeducido,@pmtmFecIniPmd,@pmtmFecFinPmd,@pmtmDiasBase,@pmtmDiasNoRem,@pmtmTarifa,@pmtmNPrestamo,@pmtmNCuota,@pmtmLogPmdio
,@pmtmNContrato,@pmtmpVehiculo,@pmtmIdDep,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdNom,@pmtmVrSalario,@pmtmVrParcial)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Liquida_Sel @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmtmNumero VARCHAR(5),@pmIdEmpleado VARCHAR(16)=Null
 AS 
 INSERT INTO tm_Liquida (tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmDetalle,tmFecUltLiq,tmFecInicial,tmFecFinal,tmVrBase,tmCantidad,tmVrLiquida,tmVrDeducido,tmFecIniPmd,tmFecFinPmd
,tmDiasBase,tmDiasNoRem,tmTarifa,tmNPrestamo,tmNCuota,tmLogPmdio,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmVrSalario,tmVrParcial)
 SELECT @pmtmNumero,Item,IdEmpleado,IdConcepto,Detalle,FecUltLiq,FecInicial,FecFinal,VrBase,Cantidad,VrLiquida,VrDeducido,FecIniPmd,FecFinPmd
 ,DiasBase,DiasNoRem,Tarifa,NPrestamo,NCuota,LogPmdio,NContrato,pVehiculo,IdDep,IdCCosto,IdSubCos,IdNom,VrSalario,VrParcial
 FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ListaPrec @pmtmEst CHAR(2),@pmIdProducto VARCHAR(16),@pmMargen1 DECIMAL(14,4),@pmMargen2 DECIMAL(14,4),@pmMargen3 DECIMAL(14,4),@pmMargen4 DECIMAL(14,4),@pmMargen5 DECIMAL(14,4),@pmTarifDcto1 DECIMAL(14,4)
,@pmTarifDcto2 DECIMAL(14,4),@pmTarifDcto3 DECIMAL(14,4),@pmTarifDcto4 DECIMAL(14,4),@pmTarifDcto5 DECIMAL(14,4),@pmTrmDia1 MONEY,@pmTrmDia2 MONEY,@pmTrmDia3 MONEY,@pmTrmDia4 MONEY,@pmTrmDia5 MONEY,@pmIdCia CHAR(2)
,@pmCostoAnt MONEY,@pmCostoUlt MONEY,@pmCostoPmd MONEY,@pmExistenciaCia DECIMAL(14,4),@pmKarTipDoc VARCHAR(3),@pmKarEntradas DECIMAL(14,4),@pmKarSalidas DECIMAL(14,4),@pmKarCostoEnt MONEY,@pmKarCostoSal MONEY
,@pmCiaPrecio1 MONEY,@pmCiaPrecio2 MONEY,@pmCiaPrecio3 MONEY,@pmCiaPrecio4 MONEY,@pmCiaPrecio5 MONEY,@pmCiaTarifIva DECIMAL(14,4),@pmCiaIvaInc VARCHAR(10)
 AS 
 INSERT INTO tm_ListaPrec (tmEst,IdProducto,Margen1,Margen2,Margen3,Margen4,Margen5,TarifDcto1,TarifDcto2,TarifDcto3,TarifDcto4,TarifDcto5,TrmDia1,TrmDia2,TrmDia3,TrmDia4,TrmDia5,IdCia,CostoAnt,CostoUlt,CostoPmd,ExistenciaCia,KarTipDoc,KarEntradas,KarSalidas,KarCostoEnt,KarCostoSal
,CiaPrecio1,CiaPrecio2,CiaPrecio3,CiaPrecio4,CiaPrecio5,CiaTarifIva,CiaIvaInc)
 VALUES (@pmtmEst,@pmIdProducto,@pmMargen1,@pmMargen2,@pmMargen3,@pmMargen4,@pmMargen5,@pmTarifDcto1,@pmTarifDcto2,@pmTarifDcto3,@pmTarifDcto4,@pmTarifDcto5,@pmTrmDia1,@pmTrmDia2,@pmTrmDia3,@pmTrmDia4,@pmTrmDia5,@pmIdCia
,@pmCostoAnt,@pmCostoUlt,@pmCostoPmd,@pmExistenciaCia,@pmKarTipDoc,@pmKarEntradas,@pmKarSalidas,@pmKarCostoEnt,@pmKarCostoSal
,@pmCiaPrecio1,@pmCiaPrecio2,@pmCiaPrecio3,@pmCiaPrecio4,@pmCiaPrecio5,@pmCiaTarifIva,@pmCiaIvaInc)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Lot @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmLinea INT,@pmtmNumLote VARCHAR(30)
,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmCdUbic VARCHAR(10),@pmtmEntradas DECIMAL(14,4)
,@pmtmSalidas DECIMAL(14,4),@pmtmFecha SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmCdNueUbic VARCHAR(10)
 AS 
 INSERT INTO tm_Lot (tmNumero,tmItem,tmLinea,tmNumLote,tmIdProducto,tmIdBodega,tmCdUbic,tmEntradas,tmSalidas,tmFecha,tmFecVence,tmCdNueUbic)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmLinea,@pmtmNumLote,@pmtmIdProducto,@pmtmIdBodega,@pmtmCdUbic,@pmtmEntradas
,@pmtmSalidas,@pmtmFecha,@pmtmFecVence,@pmtmCdNueUbic)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Lot_DVN @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)=Null,@pmLinea INT=Null
 AS 
 INSERT INTO tm_Lot (tmNumero,tmItem,tmLinea,tmNumLote,tmIdProducto,tmIdBodega,tmCdUbic,tmEntradas,tmSalidas,tmFecha,tmFecVence)
 SELECT @pmtmNumero,Item,Linea,NumLote,IdProducto,IdBodega,CdUbic,Salidas,Entradas,Fecha,FecVence
 FROM Trn_LotMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Linea>=ISNULL(@pmLinea,0)  AND Linea<=ISNULL(@pmLinea,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Lot_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)=Null,@pmLinea INT=Null
 AS 
 INSERT INTO tm_Lot (tmNumero,tmItem,tmLinea,tmNumLote,tmIdProducto,tmIdBodega,tmCdUbic,tmEntradas,tmSalidas,tmFecha,tmFecVence)
 SELECT @pmtmNumero,Item,Linea,NumLote,IdProducto,IdBodega,CdUbic,Entradas,Salidas,Fecha,FecVence
 FROM Trn_LotMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Linea>=ISNULL(@pmLinea,0)  AND Linea<=ISNULL(@pmLinea,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes @pmtmEst CHAR(2),@pmtmTipDoc VARCHAR(3),@pmtmNumero INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmFecha SMALLDATETIME,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmCargos MONEY,@pmtmAbonos MONEY,@pmtmTimeSys SMALLDATETIME,@pmtmFecVence SMALLDATETIME
,@pmtmCdConcepto VARCHAR(4),@pmtmModalidad VARCHAR(10),@pmtmDetalle VARCHAR(250),@pmtmReferencia VARCHAR(50),@pmtmTipRef VARCHAR(3),@pmtmDocRef INT,@pmtmIdCiaRef CHAR(2),@pmtmCdLocal VARCHAR(8),@pmtmNumForma VARCHAR(20),@pmtmCdBanco VARCHAR(4),@pmtmBeneficiario VARCHAR(150),@pmtmCdForma VARCHAR(4),@pmtmEnEfectivo BIT,@pmtmTotalAbono MONEY
,@pmtmVrAfavor MONEY,@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCiaCom CHAR(2),@pmtmAnulado BIT,@pmtmNumDev INT,@pmtmFecDev SMALLDATETIME,@pmtmIdUsuario VARCHAR(11),@pmtmSanClie MONEY,@pmtmSanClieCia MONEY,@pmtmSanAge MONEY,@pmtmSanAgeCia MONEY
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev
,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 VALUES (@pmtmEst,@pmtmTipDoc,@pmtmNumero,@pmtmIdCia,@pmtmItem,@pmtmFecha,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmCargos,@pmtmAbonos,@pmtmTimeSys,@pmtmFecVence,@pmtmCdConcepto,@pmtmModalidad,@pmtmDetalle,@pmtmReferencia,@pmtmTipRef,@pmtmDocRef,@pmtmIdCiaRef,@pmtmCdLocal,@pmtmNumForma,@pmtmCdBanco,@pmtmBeneficiario,@pmtmCdForma
,@pmtmEnEfectivo,@pmtmTotalAbono,@pmtmVrAfavor,@pmtmTipCom,@pmtmComprobante,@pmtmIdCiaCom,@pmtmAnulado,@pmtmNumDev,@pmtmFecDev,@pmtmIdUsuario,@pmtmSanClie,@pmtmSanClieCia,@pmtmSanAge,@pmtmSanAgeCia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes_Dfc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,0,VrNeto,TimeSys,FecDoc,IdConcepto,Modalidad,Observacion,ModdDev
,TipDoc,Factura,IdCiaDoc,IdLocEnv,'','0','','0',0,0,0,TipCom,Comprobante,IdCiaCom,0,0,Fecha,IdUsuario,0,0,0,0
 FROM Trn_DevFcr
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes_Fcr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,V.TipDoc,V.Factura,V.IdCia,Item,V.Fecha,V.IdCliente,V.IdAgencia,V.IdVend,VrFactura,0,TimeSys,V.FechaVence,V.IdConcepto,Modalidad,Detalle,Referencia
,TipRef,DocRef,IdCiaRef,IdLocEnv,CAST(Pedido AS VARCHAR(20)),'0',NomContac,'0',MulPlazos,VrAbonado,0,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,IdUsuario,0,0,0,0
 FROM Trn_Vencimientos AS V INNER JOIN Trn_Facturas AS F ON V.TipDoc=F.TipDoc  AND V.Factura=F.Factura  AND V.IdCia=F.IdCia
 WHERE V.TipDoc<>'NDB' AND V.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND V.IdCia LIKE ISNULL(@pmIdCia,'%%') AND V.IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes_Ncr @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDoc,NumNota,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,0,VrNeto,TimeSys,FechaVence,IdConcepto,Modalidad,Observacion,Referencia
,TipDcm,Documento,IdCiaDcm,IdLocal,NumCheque,CdBanco,'',CdForma,PrestCaja,0,VrAnticipo,TipCom,Comprobante,IdCiaCom,0,0,Null,IdUsuario,0,0,0,0
 FROM Trn_Notas
 WHERE TipDoc='NCR' AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes_Ndb @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,V.TipDoc,V.Factura,V.IdCia,Item,V.Fecha,V.IdCliente,V.IdAgencia,V.IdVend,VrFactura,0,TimeSys,V.FechaVence,V.IdConcepto,Modalidad,Detalle,V.Referencia
,TipRef,DocRef,IdCiaRef,IdLocal,NumCheque,CdBanco,N.Referencia,CdForma,MulPlazos,VrAbonado,VrSancion,TipCom,Comprobante,IdCiaCom,0,Documento,Null,IdUsuario,0,0,0,0
 FROM Trn_Vencimientos AS V INNER JOIN Trn_Notas AS N ON V.TipDoc=N.TipDoc  AND V.Factura=N.NumNota  AND V.IdCia=N.IdCia
 WHERE V.TipDoc='NDB' AND V.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND V.IdCia LIKE ISNULL(@pmIdCia,'%%') AND V.IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes_Odb @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,Documento,Fecha,IdCliente,IdAgencia,IdVend,VrNeto,0,TimeSys,FecPago,IdConcepto,Modalidad,Observacion,Referencia
,TipDoc,Recibo,IdCiaDoc,IdLocal,'','0',pVehiculo,'0',EnEfectivo,0,VrAnticipo,TipCom,Comprobante,IdCiaCom,0,0,FecDoc,IdUsuario,0,0,0,0
 FROM Trn_DevRec
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovClientes_Rec @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_MovClientes (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmTimeSys,tmFecVence,tmCdConcepto,tmModalidad,tmDetalle,tmReferencia
,tmTipRef,tmDocRef,tmIdCiaRef,tmCdLocal,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDoc,Recibo,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,0,VrNeto,TimeSys,FecPago,IdConcepto,Modalidad,Observacion,Referencia
,TipDcm,Documento,IdCiaDcm,IdLocal,NumForma,CdBanco,pVehiculo,CdForma,EnEfectivo,0,VrAnticipo,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,IdUsuario,0,0,0,0
 FROM Trn_Recibos
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovCue @pmtmEst CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmVrBase MONEY,@pmTarifaBase DECIMAL(14,4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmCodConce VARCHAR(4),@pmNitDoc VARCHAR(16),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME,@pmCodCta VARCHAR(4)
,@pmNumCheque VARCHAR(20),@pmIntegrado BIT,@pmTipoAplica CHAR(1),@pmConsolida BIT,@pmCodCargo VARCHAR(4),@pmNitOtros VARCHAR(16),@pmCodSubgpo VARCHAR(8),@pmCiuOrigen VARCHAR(8),@pmCodAgncia VARCHAR(16),@pmVehPropio BIT,@pmReferncia VARCHAR(50),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
,@pmVrSanCue MONEY,@pmVrSanCueCia MONEY,@pmVrSanNit MONEY,@pmVrSanNitCia MONEY,@pmVrSanVeh MONEY,@pmVrSanVehCia MONEY,@pmVrSanCc MONEY,@pmVrSanCcCia MONEY,@pmVrSanCcSub MONEY,@pmVrSanCcSubCia MONEY,@pmVrSanNitCc MONEY,@pmVrSanNitCcCia MONEY,@pmVrSanNitCcSub MONEY,@pmVrSanNitCcSubCia MONEY
,@pmVrSanNitAge MONEY,@pmVrSanNitAgeCia MONEY,@pmVrSanNitAgeCc MONEY,@pmVrSanNitAgeCcCia MONEY,@pmVrSanNitAgeCcSub MONEY,@pmVrSanNitAgeCcSubCia MONEY,@pmVrSanNitVeh MONEY,@pmVrSanNitVehCia MONEY,@pmVrSanNitVehCc MONEY,@pmVrSanNitVehCcCia MONEY,@pmVrSanVehCc MONEY,@pmVrSanVehCcCia MONEY
,@pmVrSanVehCcSub MONEY,@pmVrSanVehCcSubCia MONEY
 AS 
 INSERT INTO tm_MovCue (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio
,Referncia,TipDocRef,DocRef,IdCiaRef,VrSanCue,VrSanCueCia,VrSanNit,VrSanNitCia,VrSanVeh,VrSanVehCia,VrSanCc,VrSanCcCia,VrSanCcSub,VrSanCcSubCia,VrSanNitCc,VrSanNitCcCia,VrSanNitCcSub,VrSanNitCcSubCia,VrSanNitAge,VrSanNitAgeCia,VrSanNitAgeCc,VrSanNitAgeCcCia,VrSanNitAgeCcSub,VrSanNitAgeCcSubCia,VrSanNitVeh,VrSanNitVehCia,VrSanNitVehCc
,VrSanNitVehCcCia,VrSanVehCc,VrSanVehCcCia,VrSanVehCcSub,VrSanVehCcSubCia)
 VALUES (@pmtmEst,@pmTipCom,@pmComprobante,@pmIdCia,@pmItem,@pmFecha,@pmIdCuenta,@pmDetalle,@pmVrDebito,@pmVrCredito,@pmIdTercero,@pmIdVehiculo,@pmIdCCosto,@pmIdSubCos,@pmVrBase,@pmTarifaBase,@pmTipDoc,@pmDocumento,@pmIdCiaDoc,@pmCodConce,@pmNitDoc,@pmTipFac,@pmFactura,@pmIdCiaFac,@pmItemFac,@pmFecVence
,@pmCodCta,@pmNumCheque,@pmIntegrado,@pmTipoAplica,@pmConsolida,@pmCodCargo,@pmNitOtros,@pmCodSubgpo,@pmCiuOrigen,@pmCodAgncia,@pmVehPropio,@pmReferncia,@pmTipDocRef,@pmDocRef,@pmIdCiaRef,@pmVrSanCue,@pmVrSanCueCia,@pmVrSanNit,@pmVrSanNitCia,@pmVrSanVeh,@pmVrSanVehCia,@pmVrSanCc,@pmVrSanCcCia
,@pmVrSanCcSub,@pmVrSanCcSubCia,@pmVrSanNitCc,@pmVrSanNitCcCia,@pmVrSanNitCcSub,@pmVrSanNitCcSubCia,@pmVrSanNitAge,@pmVrSanNitAgeCia,@pmVrSanNitAgeCc,@pmVrSanNitAgeCcCia,@pmVrSanNitAgeCcSub,@pmVrSanNitAgeCcSubCia,@pmVrSanNitVeh,@pmVrSanNitVehCia,@pmVrSanNitVehCc,@pmVrSanNitVehCcCia,@pmVrSanVehCc,@pmVrSanVehCcCia,@pmVrSanVehCcSub,@pmVrSanVehCcSubCia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovCue_Acc @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdSubCos VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null
,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null
 AS 
 IF @pmCueIni IS NULL
  INSERT INTO tm_MovCue (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef)
  SELECT  @pmtmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef
  FROM Trn_ComDetalle AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsAccnista<>0 AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (Integrado=ISNULL(@pmIntegrado,0) or  Integrado=ISNULL(@pmIntegrado,1))
ELSE
  INSERT INTO tm_MovCue (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef)
  SELECT  @pmtmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,D.IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef
  FROM Trn_ComDetalle AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta BETWEEN @pmCueIni AND @pmCueFin AND EsAccnista<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (Integrado=ISNULL(@pmIntegrado,0) or  Integrado=ISNULL(@pmIntegrado,1))
  
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovCue_Sel @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdSubCos VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null
,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null
 AS 
 IF @pmCueIni IS NULL
  INSERT INTO tm_MovCue (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef)
  SELECT  @pmtmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef
  FROM Trn_ComDetalle
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (Integrado=ISNULL(@pmIntegrado,0) or  Integrado=ISNULL(@pmIntegrado,1))
ELSE
  INSERT INTO tm_MovCue (tmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
 ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef)
  SELECT  @pmtmEst,TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,NitDoc,TipFac,Factura
  ,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef
  FROM Trn_ComDetalle
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCuenta BETWEEN @pmCueIni AND @pmCueFin
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
 AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND  (Integrado=ISNULL(@pmIntegrado,0) or  Integrado=ISNULL(@pmIntegrado,1))
   

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovInvent @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmIdCia CHAR(2),@pmtmTipDoc VARCHAR(3),@pmtmIdConcepto VARCHAR(4),@pmtmIdCliente VARCHAR(16),@pmtmCdAgencia VARCHAR(16)
,@pmtmIdVend VARCHAR(16),@pmtmCdLocal VARCHAR(8),@pmtmCdSubzona VARCHAR(4),@pmtmFecha SMALLDATETIME,@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmCostoEnt MONEY,@pmtmCostoSal MONEY,@pmtmValorEnt MONEY,@pmtmValorSal MONEY
,@pmtmDctosEnt MONEY,@pmtmDctosSal MONEY,@pmtmIvaEnt MONEY,@pmtmIvaSal MONEY,@pmtmOtrosImpEnt MONEY,@pmtmOtrosImpSal MONEY,@pmtmUnidVolumen DECIMAL(14,4),@pmtmEntradas2 DECIMAL(14,4),@pmtmSalidas2 DECIMAL(14,4),@pmtmCostoEnt2 MONEY
,@pmtmCostoSal2 MONEY,@pmtmValorEnt2 MONEY,@pmtmValorSal2 MONEY,@pmtmDctosEnt2 MONEY,@pmtmDctosSal2 MONEY,@pmtmIvaEnt2 MONEY,@pmtmIvaSal2 MONEY,@pmtmOtrosImpEnt2 MONEY,@pmtmOtrosImpSal2 MONEY,@pmtmUnidVolumen2 DECIMAL(14,4)
 AS 
 INSERT INTO tm_MovInvent (tmEst,tmItem,tmIdProducto,tmIdBodega,tmIdCia,tmTipDoc,tmIdConcepto,tmIdCliente,tmCdAgencia,tmIdVend,tmCdLocal,tmCdSubzona,tmFecha,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal,tmValorEnt,tmValorSal,tmDctosEnt,tmDctosSal,tmIvaEnt,tmIvaSal
,tmOtrosImpEnt,tmOtrosImpSal,tmUnidVolumen,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmOtrosImpEnt2,tmOtrosImpSal2,tmUnidVolumen2)
 VALUES (@pmtmEst,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmIdCia,@pmtmTipDoc,@pmtmIdConcepto,@pmtmIdCliente,@pmtmCdAgencia,@pmtmIdVend,@pmtmCdLocal,@pmtmCdSubzona,@pmtmFecha,@pmtmEntradas,@pmtmSalidas,@pmtmCostoEnt,@pmtmCostoSal
,@pmtmValorEnt,@pmtmValorSal,@pmtmDctosEnt,@pmtmDctosSal,@pmtmIvaEnt,@pmtmIvaSal,@pmtmOtrosImpEnt,@pmtmOtrosImpSal,@pmtmUnidVolumen,@pmtmEntradas2,@pmtmSalidas2,@pmtmCostoEnt2,@pmtmCostoSal2,@pmtmValorEnt2,@pmtmValorSal2,@pmtmDctosEnt2
,@pmtmDctosSal2,@pmtmIvaEnt2,@pmtmIvaSal2,@pmtmOtrosImpEnt2,@pmtmOtrosImpSal2,@pmtmUnidVolumen2)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovTanques @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdTanque VARCHAR(4),@pmtmItem INT,@pmtmIdCia CHAR(2),@pmtmFecha SMALLDATETIME
,@pmtmSaldoAnt DECIMAL(14,4),@pmtmCostoAnt MONEY,@pmtmEntradas DECIMAL(14,4),@pmtmCostoEnt MONEY,@pmtmDevEnt DECIMAL(14,4),@pmtmCostoDve MONEY,@pmtmSalidas DECIMAL(14,4)
,@pmtmCostoSal MONEY,@pmtmDevSalidas DECIMAL(14,4),@pmtmCostoDvs MONEY,@pmtmFacturas DECIMAL(14,4),@pmtmCostoFac MONEY,@pmtmDevFact DECIMAL(14,4),@pmtmCostoDfc MONEY
,@pmtmAjusteSob DECIMAL(14,4),@pmtmCostoAjs MONEY,@pmtmAjusteFal DECIMAL(14,4),@pmtmCostoAjf MONEY,@pmtmPlanillas DECIMAL(14,4),@pmtmCostoPla MONEY,@pmtmDevPlanillas DECIMAL(14,4)
,@pmtmCostoDvp MONEY,@pmtmNotasCalib DECIMAL(14,4),@pmtmCostoNot MONEY,@pmtmTraslados DECIMAL(14,4),@pmtmCostoTra MONEY,@pmtmOtros DECIMAL(14,4),@pmtmCostoOtr MONEY
,@pmtmNuevoSaldo DECIMAL(14,4),@pmtmCostoNsal MONEY,@pmtmCantFisico DECIMAL(14,4),@pmtmCantAgua DECIMAL(14,4),@pmtmDiferencia DECIMAL(14,4),@pmtmSaldoAntCia DECIMAL(14,4)
,@pmtmCostoSanCia MONEY,@pmtmSaldoAntTan DECIMAL(14,4),@pmtmCostoSanTan MONEY,@pmtmSaldoAntPro DECIMAL(14,4),@pmtmCostoSanPro MONEY,@pmtmSaldoAntDia DECIMAL(14,4),@pmtmCostoAntDia MONEY
 AS 
 INSERT INTO tm_MovTanques (tmEst,tmIdProducto,tmIdTanque,tmItem,tmIdCia,tmFecha,tmSaldoAnt,tmCostoAnt,tmEntradas,tmCostoEnt,tmDevEnt,tmCostoDve,tmSalidas,tmCostoSal,tmDevSalidas,tmCostoDvs
,tmFacturas,tmCostoFac,tmDevFact,tmCostoDfc,tmAjusteSob,tmCostoAjs,tmAjusteFal,tmCostoAjf,tmPlanillas,tmCostoPla,tmDevPlanillas,tmCostoDvp,tmNotasCalib,tmCostoNot,tmTraslados,tmCostoTra,tmOtros
,tmCostoOtr,tmNuevoSaldo,tmCostoNsal,tmCantFisico,tmCantAgua,tmDiferencia,tmSaldoAntCia,tmCostoSanCia,tmSaldoAntTan,tmCostoSanTan,tmSaldoAntPro,tmCostoSanPro,tmSaldoAntDia,tmCostoAntDia)
 VALUES (@pmtmEst,@pmtmIdProducto,@pmtmIdTanque,@pmtmItem,@pmtmIdCia,@pmtmFecha,@pmtmSaldoAnt,@pmtmCostoAnt,@pmtmEntradas,@pmtmCostoEnt,@pmtmDevEnt,@pmtmCostoDve
,@pmtmSalidas,@pmtmCostoSal,@pmtmDevSalidas,@pmtmCostoDvs,@pmtmFacturas,@pmtmCostoFac,@pmtmDevFact,@pmtmCostoDfc,@pmtmAjusteSob,@pmtmCostoAjs,@pmtmAjusteFal
,@pmtmCostoAjf,@pmtmPlanillas,@pmtmCostoPla,@pmtmDevPlanillas,@pmtmCostoDvp,@pmtmNotasCalib,@pmtmCostoNot,@pmtmTraslados,@pmtmCostoTra,@pmtmOtros,@pmtmCostoOtr
,@pmtmNuevoSaldo,@pmtmCostoNsal,@pmtmCantFisico,@pmtmCantAgua,@pmtmDiferencia,@pmtmSaldoAntCia,@pmtmCostoSanCia,@pmtmSaldoAntTan,@pmtmCostoSanTan,@pmtmSaldoAntPro,@pmtmCostoSanPro,@pmtmSaldoAntDia,@pmtmCostoAntDia)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_MovVentas @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmIdCia CHAR(2),@pmtmTipDoc VARCHAR(3),@pmtmIdConcepto VARCHAR(4),@pmtmIdCliente VARCHAR(16)
,@pmtmCdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmCdLocal VARCHAR(8),@pmtmCdSubzona VARCHAR(4),@pmtmFecha SMALLDATETIME,@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmCostoEnt MONEY
,@pmtmCostoSal MONEY,@pmtmValorEnt MONEY,@pmtmValorSal MONEY,@pmtmDctosEnt MONEY,@pmtmDctosSal MONEY,@pmtmIvaEnt MONEY,@pmtmIvaSal MONEY,@pmtmImpGlobalEnt MONEY,@pmtmImpGlobalSal MONEY
,@pmtmOtrosEnt MONEY,@pmtmOtrosSal MONEY,@pmtmUnidVolumen DECIMAL(14,4),@pmtmEntradas2 DECIMAL(14,4),@pmtmSalidas2 DECIMAL(14,4),@pmtmCostoEnt2 MONEY,@pmtmCostoSal2 MONEY,@pmtmValorEnt2 MONEY
,@pmtmValorSal2 MONEY,@pmtmDctosEnt2 MONEY,@pmtmDctosSal2 MONEY,@pmtmIvaEnt2 MONEY,@pmtmIvaSal2 MONEY,@pmtmImpGlobalEnt2 MONEY,@pmtmImpGlobalSal2 MONEY,@pmtmOtrosEnt2 MONEY,@pmtmOtrosSal2 MONEY,@pmtmUnidVolumen2 DECIMAL(14,4)
 AS 
 INSERT INTO tm_MovVentas (tmEst,tmItem,tmIdProducto,tmIdBodega,tmIdCia,tmTipDoc,tmIdConcepto,tmIdCliente,tmCdAgencia,tmIdVend,tmCdLocal,tmCdSubzona,tmFecha,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal,tmValorEnt,tmValorSal,tmDctosEnt,tmDctosSal
,tmIvaEnt,tmIvaSal,tmImpGlobalEnt,tmImpGlobalSal,tmOtrosEnt,tmOtrosSal,tmUnidVolumen,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmImpGlobalEnt2,tmImpGlobalSal2,tmOtrosEnt2,tmOtrosSal2,tmUnidVolumen2)
 VALUES (@pmtmEst,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmIdCia,@pmtmTipDoc,@pmtmIdConcepto,@pmtmIdCliente,@pmtmCdAgencia,@pmtmIdVend,@pmtmCdLocal,@pmtmCdSubzona,@pmtmFecha,@pmtmEntradas,@pmtmSalidas,@pmtmCostoEnt,@pmtmCostoSal
,@pmtmValorEnt,@pmtmValorSal,@pmtmDctosEnt,@pmtmDctosSal,@pmtmIvaEnt,@pmtmIvaSal,@pmtmImpGlobalEnt,@pmtmImpGlobalSal,@pmtmOtrosEnt,@pmtmOtrosSal,@pmtmUnidVolumen,@pmtmEntradas2,@pmtmSalidas2,@pmtmCostoEnt2,@pmtmCostoSal2,@pmtmValorEnt2
,@pmtmValorSal2,@pmtmDctosEnt2,@pmtmDctosSal2,@pmtmIvaEnt2,@pmtmIvaSal2,@pmtmImpGlobalEnt2,@pmtmImpGlobalSal2,@pmtmOtrosEnt2,@pmtmOtrosSal2,@pmtmUnidVolumen2)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_NivCue @pmtmIdCuenta VARCHAR(16),@pmtmNivel1 VARCHAR(100)
,@pmtmNivel2 VARCHAR(100),@pmtmNivel4 VARCHAR(100),@pmtmNivel6 VARCHAR(100)
,@pmtmNivel9 VARCHAR(100),@pmtmNivel12 VARCHAR(100)
 AS 
 INSERT INTO tm_NivCue (tmIdCuenta,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12)
 VALUES (@pmtmIdCuenta,@pmtmNivel1,@pmtmNivel2,@pmtmNivel4,@pmtmNivel6,@pmtmNivel9,@pmtmNivel12)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_NomHoras @pmtmIdEmpleado VARCHAR(16),@pmtmNContrato INT,@pmtmHorDiurno DECIMAL(14,4),@pmtmHorNocturno DECIMAL(14,4),@pmtmHorExtraDia DECIMAL(14,4),@pmtmHorExtraNoc DECIMAL(14,4),@pmtmHorRecNoc DECIMAL(14,4),@pmtmHorDomDia DECIMAL(14,4)
,@pmtmHorDomNoc DECIMAL(14,4),@pmtmHorExtDom DECIMAL(14,4),@pmtmHorExtDomNoc DECIMAL(14,4),@pmtmVrHoras1 DECIMAL(14,4),@pmtmVrHoras2 DECIMAL(14,4),@pmtmVrHoras3 DECIMAL(14,4),@pmtmVrHoras4 DECIMAL(14,4),@pmtmIdArea VARCHAR(4),@pmtmIdDep VARCHAR(4)
,@pmtmIdInstala VARCHAR(4),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmVrSalario MONEY,@pmtmVrATrans MONEY,@pmtmpVehiculo VARCHAR(10),@pmtmTipoLiquida CHAR(1)
 AS 
 INSERT INTO tm_NomHoras (tmIdEmpleado,tmNContrato,tmHorDiurno,tmHorNocturno,tmHorExtraDia,tmHorExtraNoc,tmHorRecNoc,tmHorDomDia,tmHorDomNoc,tmHorExtDom,tmHorExtDomNoc,tmVrHoras1,tmVrHoras2,tmVrHoras3,tmVrHoras4,tmIdArea,tmIdDep,tmIdInstala,tmIdCCosto,tmIdSubCos
,tmIdNom,tmVrSalario,tmVrATrans,tmpVehiculo,tmTipoLiquida)
 VALUES (@pmtmIdEmpleado,@pmtmNContrato,@pmtmHorDiurno,@pmtmHorNocturno,@pmtmHorExtraDia,@pmtmHorExtraNoc,@pmtmHorRecNoc,@pmtmHorDomDia,@pmtmHorDomNoc,@pmtmHorExtDom,@pmtmHorExtDomNoc,@pmtmVrHoras1,@pmtmVrHoras2,@pmtmVrHoras3
,@pmtmVrHoras4,@pmtmIdArea,@pmtmIdDep,@pmtmIdInstala,@pmtmIdCCosto,@pmtmIdSubCos,@pmtmIdNom,@pmtmVrSalario,@pmtmVrATrans,@pmtmpVehiculo,@pmtmTipoLiquida)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_NomRes @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmIdConcepto VARCHAR(4),@pmtmItem INT,@pmtmNContrato INT
,@pmtmFecInicial SMALLDATETIME,@pmtmFecFinal SMALLDATETIME,@pmtmIdPeriodo VARCHAR(8),@pmtmIdArea VARCHAR(4),@pmtmIdDep VARCHAR(4),@pmtmIdInstala VARCHAR(4)
,@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmVrSalario MONEY,@pmtmVrATrans MONEY,@pmtmpVehiculo VARCHAR(10)
,@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCiaCom CHAR(2),@pmtmObservacion VARCHAR(250),@pmtmEsRetiro BIT,@pmtmNLiqRet INT
,@pmtmTipoLiq CHAR(1),@pmtmDetalle VARCHAR(100),@pmtmCantDevg DECIMAL(14,4),@pmtmCantDed DECIMAL(14,4),@pmtmVrUnitario MONEY,@pmtmVrTotDevg MONEY,@pmtmVrTotDed MONEY
,@pmtmUnidad VARCHAR(10),@pmtmClaseLiq VARCHAR(10),@pmtmVrOrigen MONEY,@pmtmTarifa DECIMAL(14,4),@pmtmDiasCalc DECIMAL(14,4),@pmtmDiasNov DECIMAL(14,4)
,@pmtmCodFondo VARCHAR(8),@pmtmNPrestamo INT,@pmtmNCuota INT,@pmtmVrBaseLiq MONEY
 AS 
 INSERT INTO tm_NomRes (tmNumero,tmIdEmpleado,tmIdConcepto,tmItem,tmNContrato,tmFecInicial,tmFecFinal,tmIdPeriodo,tmIdArea,tmIdDep,tmIdInstala,tmIdCCosto,tmIdSubCos,tmIdNom,tmVrSalario
,tmVrATrans,tmpVehiculo,tmTipCom,tmComprobante,tmIdCiaCom,tmObservacion,tmEsRetiro,tmNLiqRet,tmTipoLiq,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad
,tmClaseLiq,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmVrBaseLiq)
 VALUES (@pmtmNumero,@pmtmIdEmpleado,@pmtmIdConcepto,@pmtmItem,@pmtmNContrato,@pmtmFecInicial,@pmtmFecFinal,@pmtmIdPeriodo,@pmtmIdArea,@pmtmIdDep,@pmtmIdInstala,@pmtmIdCCosto
,@pmtmIdSubCos,@pmtmIdNom,@pmtmVrSalario,@pmtmVrATrans,@pmtmpVehiculo,@pmtmTipCom,@pmtmComprobante,@pmtmIdCiaCom,@pmtmObservacion,@pmtmEsRetiro,@pmtmNLiqRet,@pmtmTipoLiq
,@pmtmDetalle,@pmtmCantDevg,@pmtmCantDed,@pmtmVrUnitario,@pmtmVrTotDevg,@pmtmVrTotDed,@pmtmUnidad,@pmtmClaseLiq,@pmtmVrOrigen,@pmtmTarifa,@pmtmDiasCalc,@pmtmDiasNov,@pmtmCodFondo,@pmtmNPrestamo,@pmtmNCuota,@pmtmVrBaseLiq)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Nomina @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT,@pmtmIdConcepto VARCHAR(4),@pmtmClaseCon VARCHAR(10),@pmtmDetalle VARCHAR(100),@pmtmCantDevg DECIMAL(10,4),@pmtmCantDed DECIMAL(10,4),@pmtmVrUnitario MONEY,@pmtmVrTotDevg MONEY,@pmtmVrTotDed MONEY
,@pmtmUnidad VARCHAR(10),@pmtmClaseLiq VARCHAR(10),@pmtmVrOrigen MONEY,@pmtmTarifa DECIMAL(10,4),@pmtmDiasCalc DECIMAL(10,4),@pmtmDiasNov DECIMAL(10,4),@pmtmCodFondo VARCHAR(8),@pmtmNPrestamo INT,@pmtmNCuota INT,@pmtmOrigCargue VARCHAR(10),@pmtmVrBaseLiq MONEY,@pmtmNContto INT,@pmtmFecIni SMALLDATETIME
,@pmtmFecFin SMALLDATETIME,@pmtmIdPeriodo VARCHAR(8),@pmtmFecUltLiq SMALLDATETIME,@pmtmFecIniPmd SMALLDATETIME,@pmtmFecFinPmd SMALLDATETIME,@pmtmLogPmdio VARCHAR(600),@pmtmVrPension MONEY,@pmtmVrSalud MONEY
 AS 
 INSERT INTO tm_Nomina (tmNumero,tmIdEmpleado,tmItem,tmIdConcepto,tmClaseCon,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad,tmClaseLiq,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmOrigCargue,tmVrBaseLiq,tmNContto,tmFecIni,tmFecFin,tmIdPeriodo,tmFecUltLiq,tmFecIniPmd,tmFecFinPmd,tmLogPmdio,tmVrPension,tmVrSalud)
 VALUES (@pmtmNumero,@pmtmIdEmpleado,@pmtmItem,@pmtmIdConcepto,@pmtmClaseCon,@pmtmDetalle,@pmtmCantDevg,@pmtmCantDed,@pmtmVrUnitario,@pmtmVrTotDevg,@pmtmVrTotDed,@pmtmUnidad,@pmtmClaseLiq,@pmtmVrOrigen,@pmtmTarifa
,@pmtmDiasCalc,@pmtmDiasNov,@pmtmCodFondo,@pmtmNPrestamo,@pmtmNCuota,@pmtmOrigCargue,@pmtmVrBaseLiq,@pmtmNContto,@pmtmFecIni,@pmtmFecFin,@pmtmIdPeriodo,@pmtmFecUltLiq,@pmtmFecIniPmd,@pmtmFecFinPmd,@pmtmLogPmdio,@pmtmVrPension,@pmtmVrSalud)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Nomina_Liq  @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Nomina (tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmClaseCon,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad
,tmClaseLiq,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmOrigCargue,tmVrBaseLiq,tmNContto,tmFecIni,tmFecFin,tmIdPeriodo,tmFecUltLiq
,tmFecIniPmd,tmFecFinPmd,tmLogPmdio,tmVrPension,tmVrSalud)
 SELECT @pmtmNumero,Item,D.IdEmpleado,D.IdConcepto,ClaseCon,Detalle
,'CntDev'= CASE  WHEN VrDeducido<=0 THEN Cantidad ELSE 0 END
,'CntDed'= CASE  WHEN VrDeducido>0 THEN Cantidad ELSE 0  END
,VrUnitario,VrDevngado,VrDeducido,Unidad,ClaseLiq,VrBase,D.Tarifa,DiasBase,D.DiasNoRem,CodFondo
,NPrestamo,NCuota,'LIQUDACION',VrBaseLiq,NContrato,FecDesde,FecHasta,IdPeriodo,FecUltLiq,FecIniPmd,FecFinPmd,LogPmdio,0,0
 FROM Trn_NomRetDet AS D INNER JOIN Trn_NomRet AS R ON D.IdEmpleado=R.IdEmpleado AND D.Numero=R.Numero
 INNER JOIN  NomConceptos AS C ON D.IdConcepto=C.IdConcepto
 WHERE D.IdEmpleado=@pmIdEmpleado AND D.Numero=@pmNumero  AND  ClaseLiq NOT IN  ('APORTE','PROVISION')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Nomina_Sel @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Nomina (tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmClaseCon,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad
,tmClaseLiq,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmOrigCargue,tmVrBaseLiq,tmNContto,tmFecIni,tmFecFin,tmIdPeriodo,tmVrPension,tmVrSalud)
 SELECT @pmtmNumero,Item,D.IdEmpleado,D.IdConcepto,ClaseCon,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,D.Unidad,ClaseLiq,VrOrigen,D.Tarifa,DiasCalc
,DiasNov,CodFondo,NPrestamo,NCuota,OrigCargue,VrBaseLiq,NContrato,FecInicial,FecFinal,D.IdPeriodo,0,0
 FROM Trn_NomDetalle  AS D INNER JOIN NomConceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Trn_Nomina AS N ON D.IdPeriodo=N.IdPeriodo AND D.KeyRegistro=N.KeyRegistro
 WHERE D.IdPeriodo=@pmIdPeriodo AND D.KeyRegistro=@pmKeyRegistro 
 AND  ClaseLiq  IN  ('NOMINA','NOVEDAD')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Nomina_Vac  @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Nomina (tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmClaseCon,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad
,tmClaseLiq,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmOrigCargue,tmVrBaseLiq,tmNContto,tmFecIni,tmFecFin,tmIdPeriodo,tmVrPension,tmVrSalud)
 SELECT @pmtmNumero,Item,IdEmpleado,D.IdConcepto,ClaseCon,Detalle,0,Cantidad,0,VrDevgado,VrDeducido,Unidad,'NOMINA',VrBaseLiq,D.Tarifa,0,0,'',NPrestamo,NCuota,'VAC',VrCertfcado
,0,getdate(),getdate(),IdPeriodo,VrPension,VrSalud
 FROM Trn_NomVacDet  AS D INNER JOIN NomConceptos AS C ON D.IdConcepto=C.IdConcepto
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_OccComp @pmtmTipDoc VARCHAR(3),@pmtmOCargue INT,@pmtmIdCia CHAR(2)
,@pmtmNumComp INT,@pmtmIdProducto VARCHAR(16),@pmtmCantidad DECIMAL(14,4),@pmtmDescProd VARCHAR(50)
,@pmtmComp2 INT,@pmtmIdProd2 VARCHAR(16),@pmtmDescProd2 VARCHAR(50),@pmtmCantidad2 DECIMAL(14,4)
,@pmtmComp3 INT,@pmtmIdProd3 VARCHAR(16),@pmtmDescProd3 VARCHAR(50),@pmtmCantidad3 DECIMAL(14,4)
,@pmtmComp4 INT,@pmtmIdProd4 VARCHAR(16),@pmtmDescProd4 VARCHAR(50),@pmtmCantidad4 DECIMAL(14,4)
,@pmtmComp5 INT,@pmtmIdProd5 VARCHAR(16),@pmtmDescProd5 VARCHAR(50),@pmtmCantidad5 DECIMAL(14,4)
 AS 
 INSERT INTO tm_OccComp (tmTipDoc,tmOCargue,tmIdCia,tmNumComp,tmIdProducto,tmCantidad,tmDescProd,tmComp2,tmIdProd2,tmDescProd2,tmCantidad2
,tmComp3,tmIdProd3,tmDescProd3,tmCantidad3,tmComp4,tmIdProd4,tmDescProd4,tmCantidad4,tmComp5,tmIdProd5,tmDescProd5,tmCantidad5)
 VALUES (@pmtmTipDoc,@pmtmOCargue,@pmtmIdCia,@pmtmNumComp,@pmtmIdProducto,@pmtmCantidad,@pmtmDescProd,@pmtmComp2,@pmtmIdProd2
,@pmtmDescProd2,@pmtmCantidad2,@pmtmComp3,@pmtmIdProd3,@pmtmDescProd3,@pmtmCantidad3,@pmtmComp4,@pmtmIdProd4,@pmtmDescProd4
,@pmtmCantidad4,@pmtmComp5,@pmtmIdProd5,@pmtmDescProd5,@pmtmCantidad5)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Pagos @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdForma VARCHAR(4),@pmtmDetalle VARCHAR(150),@pmtmVrPagado MONEY,@pmtmVrCambio MONEY
,@pmtmEsCaja BIT,@pmtmIdBanco VARCHAR(4),@pmtmNitCliente VARCHAR(16),@pmtmCdAgencia VARCHAR(16),@pmtmNumForma VARCHAR(20),@pmtmFecForma SMALLDATETIME,@pmtmCtaForma VARCHAR(30)
,@pmtmBeneficiario VARCHAR(150),@pmtmReferncia1 VARCHAR(50),@pmtmReferncia2 VARCHAR(50),@pmtmCdLocal VARCHAR(8),@pmtmNumAutoriza VARCHAR(30),@pmtmVrDenom MONEY,@pmtmCantidad INT
,@pmtmTipDenom CHAR(1),@pmtmTipRef VARCHAR(3),@pmtmDocRef INT,@pmtmIdCiaRef CHAR(2),@pmtmItemDoc INT,@pmtmCdCta VARCHAR(4),@pmtmCliente VARCHAR(150)
,@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmTipAplica VARCHAR(10)
 AS 
 INSERT INTO tm_Pagos (tmNumero,tmItem,tmIdForma,tmDetalle,tmVrPagado,tmVrCambio,tmEsCaja,tmIdBanco,tmNitCliente,tmCdAgencia,tmNumForma,tmFecForma,tmCtaForma,tmBeneficiario,tmReferncia1,tmReferncia2
,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantidad,tmTipDenom,tmTipRef,tmDocRef,tmIdCiaRef,tmItemDoc,tmCdCta,tmCliente,tmTipDoc,tmDocumento,tmIdCia,tmTipAplica)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdForma,@pmtmDetalle,@pmtmVrPagado,@pmtmVrCambio,@pmtmEsCaja,@pmtmIdBanco,@pmtmNitCliente,@pmtmCdAgencia,@pmtmNumForma,@pmtmFecForma
,@pmtmCtaForma,@pmtmBeneficiario,@pmtmReferncia1,@pmtmReferncia2,@pmtmCdLocal,@pmtmNumAutoriza,@pmtmVrDenom,@pmtmCantidad,@pmtmTipDenom,@pmtmTipRef,@pmtmDocRef,@pmtmIdCiaRef
,@pmtmItemDoc,@pmtmCdCta,@pmtmCliente,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia,@pmtmTipAplica)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Pagos_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_Pagos (tmNumero,tmItem,tmIdForma,tmDetalle,tmVrPagado,tmVrCambio,tmEsCaja,tmIdBanco,tmNitCliente,tmCdAgencia,tmNumForma,tmFecForma,tmCtaForma,tmBeneficiario,tmReferncia1,tmReferncia2
,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantidad,tmTipDenom,tmTipRef,tmDocRef,tmIdCiaRef,tmItemDoc,tmCdCta,tmCliente,tmTipDoc,tmDocumento,tmIdCia)
 SELECT @pmtmNumero,Item,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2
,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,SUBSTRING(RazonSocial,1,150) AS Client,TipDoc,Documento,IdCia
 FROM Trn_Pagos AS P LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Plasueldos @pmtmIdPeriodo VARCHAR(8),@pmtmKeyRegistro VARCHAR(30),@pmtmDias DECIMAL(10,4),@pmtmVrBasico MONEY,@pmtmVrAuxtrans MONEY,@pmtmVrHextras MONEY,@pmtmVrRecargos MONEY
,@pmtmVrComision MONEY,@pmtmVrIncapac MONEY,@pmtmVrLicencia MONEY,@pmtmVrVacacnes MONEY,@pmtmVrOtrosDev MONEY,@pmtmVrDctoPen MONEY,@pmtmVrDctoSal MONEY,@pmtmVrDctoFsp MONEY,@pmtmVrRetencion MONEY
,@pmtmVrPrestmos MONEY,@pmtmVrSanciones MONEY,@pmtmVrOtrosDed MONEY,@pmtmObserv VARCHAR(1000),@pmtmVrVacLiquida MONEY,@pmtmVrVacPen MONEY,@pmtmVrVacSal MONEY,@pmtmVrVacFsp MONEY,@pmtmVrVacOtr MONEY
 AS 
 INSERT INTO tm_Plasueldos (tmIdPeriodo,tmKeyRegistro,tmDias,tmVrBasico,tmVrAuxtrans,tmVrHextras,tmVrRecargos,tmVrComision,tmVrIncapac,tmVrLicencia,tmVrVacacnes,tmVrOtrosDev,tmVrDctoPen,tmVrDctoSal,tmVrDctoFsp,tmVrRetencion
,tmVrPrestmos,tmVrSanciones,tmVrOtrosDed,tmObserv,tmVrVacLiquida,tmVrVacPen,tmVrVacSal,tmVrVacFsp,tmVrVacOtr)
 VALUES (@pmtmIdPeriodo,@pmtmKeyRegistro,@pmtmDias,@pmtmVrBasico,@pmtmVrAuxtrans,@pmtmVrHextras,@pmtmVrRecargos,@pmtmVrComision,@pmtmVrIncapac,@pmtmVrLicencia,@pmtmVrVacacnes,@pmtmVrOtrosDev,@pmtmVrDctoPen,@pmtmVrDctoSal,@pmtmVrDctoFsp
,@pmtmVrRetencion,@pmtmVrPrestmos,@pmtmVrSanciones,@pmtmVrOtrosDed,@pmtmObserv,@pmtmVrVacLiquida,@pmtmVrVacPen,@pmtmVrVacSal,@pmtmVrVacFsp,@pmtmVrVacOtr)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_PreAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmIdPrestamo INT,@pmtmIdCiaPre CHAR(2),@pmtmItemPre INT,@pmtmVrAbono MONEY,@pmtmVrBaseCms MONEY
,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmComision DECIMAL(14,4),@pmtmDetalle VARCHAR(150),@pmtmGalones DECIMAL(14,4),@pmtmTipoAplica INT,@pmtmCodTarCom VARCHAR(4),@pmtmTipRec VARCHAR(3),@pmtmRecibo INT
,@pmtmCiaRec CHAR(2),@pmtmItemRec INT,@pmtmFecPago SMALLDATETIME
 AS 
 INSERT INTO tm_PreAbonos (tmNumero,tmItem,tmTipDoc,tmIdPrestamo,tmIdCiaPre,tmItemPre,tmVrAbono,tmVrBaseCms,tmIdCliente,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmGalones,tmTipoAplica,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmIdPrestamo,@pmtmIdCiaPre,@pmtmItemPre,@pmtmVrAbono,@pmtmVrBaseCms,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmComision,@pmtmDetalle,@pmtmGalones,@pmtmTipoAplica
,@pmtmCodTarCom,@pmtmTipRec,@pmtmRecibo,@pmtmCiaRec,@pmtmItemRec,@pmtmFecPago)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_PreAbonos_Sel  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_PreAbonos (tmNumero,tmItem,tmTipDoc,tmIdPrestamo,tmIdCiaPre,tmItemPre,tmVrAbono,tmVrBaseCms,tmIdCliente,tmIdAgencia
,tmIdVend,tmComision,tmDetalle,tmGalones,tmTipoAplica,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago)
SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCiaPre,ItemPre,TotalAbono,VrBaseCms,IdCliente,IdAgencia
,IdVend,Comision,Detalle,CantGal,TipoAplica,'0',TipRec,Recibo,IdCia,Item,FecPago
 FROM Trn_FinAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreCartera @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT,@pmNumCuota INT,@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrTotal MONEY,@pmVrAbonado MONEY,@pmVrSaldo MONEY
,@pmTipoTasa VARCHAR(10),@pmDTF_EA DECIMAL(16,8),@pmTasaNom DECIMAL(16,8),@pmSpread_TA DECIMAL(16,8),@pmTasaEfe DECIMAL(16,8),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVend VARCHAR(16),@pmConcepto VARCHAR(10)
,@pmTipoCuota INT,@pmNumCausacion INT,@pmIdCiaCau CHAR(2),@pmDetalle VARCHAR(250),@pmIdConcepto VARCHAR(4),@pmFecUltPago SMALLDATETIME,@pmFecLiqMora SMALLDATETIME,@pmFechaCrea SMALLDATETIME,@pmCtaForma VARCHAR(30)
,@pmBeneficiario VARCHAR(150),@pmRefCheque VARCHAR(50),@pmNumCheque VARCHAR(20),@pmCdBanco VARCHAR(4)
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago
,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 VALUES (@pmtmEst,@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmItem,@pmNumCuota,@pmFecha,@pmFechaVence,@pmVrTotal,@pmVrAbonado,@pmVrSaldo,@pmTipoTasa,@pmDTF_EA,@pmTasaNom,@pmSpread_TA,@pmTasaEfe,@pmIdCliente,@pmIdAgencia
,@pmIdVend,@pmConcepto,@pmTipoCuota,@pmNumCausacion,@pmIdCiaCau,@pmDetalle,@pmIdConcepto,@pmFecUltPago,@pmFecLiqMora,@pmFechaCrea,@pmCtaForma,@pmBeneficiario,@pmRefCheque,@pmNumCheque,@pmCdBanco)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreCartera_Fac @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 SELECT @pmtmEst,C.TipDoc,C.IdPrestamo,C.IdCia,Item,NumCuota,C.Fecha,C.FechaVence,VrTotal,VrAbonado,VrTotal-VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,C.IdCliente,C.IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,'EFE','','FACTURAS','0','0'
 FROM Trn_FinCuotas  AS C INNER JOIN  Trn_FinFactint  AS F ON C.TipDoc=F.TipDoc  AND C.IdPrestamo=F.Factura AND C.IdCia=F.IdCia
 WHERE C.TipDoc='FIF' AND VrTotal>VrAbonado AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')   AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreCartera_Ndb @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 SELECT @pmtmEst,C.TipDoc,C.IdPrestamo,C.IdCia,Item,NumCuota,C.Fecha,C.FechaVence,VrTotal,VrAbonado,VrTotal-VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,C.IdCliente,C.IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CdForma,'',Referencia,NumCheque,CdBanco
 FROM Trn_FinCuotas  AS C INNER JOIN Trn_FinNotas AS N ON C.TipDoc=N.TipDoc  AND C.IdPrestamo=N.NumNota AND C.IdCia=N.IdCia
 WHERE C.TipDoc='ND2' AND VrTotal>VrAbonado AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')   AND N.IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreCartera_Pre @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 SELECT @pmtmEst,C.TipDoc,C.IdPrestamo,C.IdCia,Item,NumCuota,C.Fecha,FechaVence,VrTotal,VrAbonado,VrTotal-VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,C.IdCliente,C.IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,'0','','','','0'
 FROM Trn_FinCuotas  AS C  INNER JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc AND C.IdPrestamo=P.IdPrestamo AND C.IdCia=P.IdCia
 WHERE C.TipDoc='PR1' AND VrTotal>VrAbonado AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')   AND P.IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreCartera_Saf @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null
 AS 
 INSERT INTO tm_PreCartera (tmEst,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,VrSaldo,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,IdVend,Concepto,TipoCuota,NumCausacion,IdCiaCau,Detalle,IdConcepto,FecUltPago,FecLiqMora,FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco)
 SELECT @pmtmEst,'SAF',MAX(Recibo),IdCia,0,0,getdate(),getdate(),SUM(VrAFavor-VrOtrosDb),SUM(VrAbono),SUM((VrAFavor-VrOtrosDb)-VrAbono),'0',0,0,0,0
,IdCliente,IdAgencia,IdVend,'SAF',0,0,'00','SALDO A FAVOR','0',NULL,NULL,getdate(),'0','','','','0'
 FROM Trn_FinAfavor
 WHERE  (VrAFavor-VrOtrosDb)>VrAbono AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')   AND IdVend LIKE ISNULL(@pmIdVend,'%')
 GROUP BY IdCia,IdCliente,IdAgencia,IdVend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInstm_PreCuotas @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmPrestamo INT,@pmtmIdCiaPre CHAR(2),@pmtmItemPre INT,@pmtmCuota INT,@pmtmConcepto VARCHAR(10)
,@pmtmFecCausa SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmVrTotal MONEY,@pmtmTipoTasa VARCHAR(10),@pmtmDTF_EA DECIMAL(16,8),@pmtmTasaNom DECIMAL(16,8),@pmtmSpread DECIMAL(16,8)
,@pmtmTasaEfe DECIMAL(16,8),@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmEstado INT,@pmtmDetalle VARCHAR(250),@pmtmTotAbono MONEY,@pmtmLinCred VARCHAR(4)
,@pmtmFactura VARCHAR(20),@pmtmDiasLiq INT,@pmtmVrIntCorr MONEY,@pmtmVrSaldo MONEY,@pmtmUltCausac SMALLDATETIME,@pmtmVrAcumCap MONEY,@pmtmNitConv VARCHAR(16),@pmtmConvnio INT
 AS 
 INSERT INTO tm_PreCuotas (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCiaPre,tmItemPre,tmCuota,tmConcepto,tmFecCausa,tmFecVence,tmVrTotal,tmTipoTasa,tmDTF_EA,tmTasaNom,tmSpread,tmTasaEfe,tmIdCliente
,tmIdAgencia,tmIdVend,tmEstado,tmDetalle,tmTotAbono,tmLinCred,tmFactura,tmDiasLiq,tmVrIntCorr,tmVrSaldo,tmUltCausac,tmVrAcumCap,tmNitConv,tmConvnio)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmPrestamo,@pmtmIdCiaPre,@pmtmItemPre,@pmtmCuota,@pmtmConcepto,@pmtmFecCausa,@pmtmFecVence,@pmtmVrTotal,@pmtmTipoTasa,@pmtmDTF_EA,@pmtmTasaNom
,@pmtmSpread,@pmtmTasaEfe,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmEstado,@pmtmDetalle,@pmtmTotAbono,@pmtmLinCred,@pmtmFactura,@pmtmDiasLiq,@pmtmVrIntCorr,@pmtmVrSaldo,@pmtmUltCausac,@pmtmVrAcumCap,@pmtmNitConv,@pmtmConvnio)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreCuotas_Aza @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 INSERT INTO tm_PreCuotas (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCiaPre,tmItemPre,tmCuota,tmConcepto,tmFecCausa,tmFecVence,tmVrTotal,tmTipoTasa
,tmDTF_EA,tmTasaNom,tmSpread,tmTasaEfe,tmIdCliente,tmIdAgencia,tmIdVend,tmEstado,tmDetalle,tmTotAbono,tmLinCred,tmFactura
,tmDiasLiq,tmVrIntCorr,tmVrSaldo,tmUltCausac,tmVrAcumCap,tmNitConv,tmConvnio)
SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCia,Item,Item,'CAPITAL',FecCausac,FecVence,VrCuota,'VARIABLE',TasaEfeMes,TasaEfeDia,0,TasaEfeMes
,'0','0','0',0,'',0,'0','',NumDias,VrInteres,VrSaldo,Null,VrCapital,'0',0
 FROM Trn_FinAmtza
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreCuotas_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 INSERT INTO tm_PreCuotas (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCiaPre,tmItemPre,tmCuota,tmConcepto,tmFecCausa,tmFecVence,tmVrTotal,tmTipoTasa
,tmDTF_EA,tmTasaNom,tmSpread,tmTasaEfe,tmIdCliente,tmIdAgencia,tmIdVend,tmEstado,tmDetalle,tmTotAbono,tmLinCred,tmFactura,tmDiasLiq,tmVrIntCorr
,tmVrSaldo,tmUltCausac,tmVrAcumCap,tmNitConv,tmConvnio)
SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCia,Item,NumCuota,Concepto,Fecha,FechaVence,VrTotal,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente
,IdAgencia,'0',1,Detalle,VrAbonado,CodLinCred,NumFactura,DiasLiquida,VrInteresFact,0,FecUltCausac,VrAcumCapital,NitConvenio,Convenio
 FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreFactura @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdConc VARCHAR(4),@pmtmConcepto VARCHAR(250),@pmtmTipoConc VARCHAR(10),@pmtmCantidad DECIMAL(14,4),@pmtmVrUnitario MONEY
,@pmtmVrTotal MONEY,@pmtmTarifIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmTarifRet DECIMAL(14,4),@pmtmVrRetfte MONEY,@pmtmTarifIca DECIMAL(14,4),@pmtmVrRetica MONEY,@pmtmnPrestamo INT,@pmtmCdCiaPrest CHAR(2)
,@pmtmFecIniLiq SMALLDATETIME,@pmtmFecFinLiq SMALLDATETIME,@pmtmCantDias INT,@pmtmCdTarifIva VARCHAR(4),@pmtmCdTarifRet VARCHAR(4),@pmtmCdTarifIca VARCHAR(4),@pmtmTarifDct DECIMAL(14,4),@pmtmVrDcto MONEY
,@pmtmCdTarifDct VARCHAR(4)
 AS 
 INSERT INTO tm_PreFactura (tmNumero,tmItem,tmIdConc,tmConcepto,tmTipoConc,tmCantidad,tmVrUnitario,tmVrTotal,tmTarifIva,tmVrIva,tmTarifRet,tmVrRetfte,tmTarifIca,tmVrRetica,tmTarifDct,tmVrDcto,tmnPrestamo,tmCdCiaPrest,tmFecIniLiq,tmFecFinLiq
,tmCantDias,tmCdTarifIva,tmCdTarifRet,tmCdTarifIca,tmCdTarifDct )
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdConc,@pmtmConcepto,@pmtmTipoConc,@pmtmCantidad,@pmtmVrUnitario,@pmtmVrTotal,@pmtmTarifIva,@pmtmVrIva,@pmtmTarifRet,@pmtmVrRetfte,@pmtmTarifIca,@pmtmVrRetica
,@pmtmTarifDct,@pmtmVrDcto,@pmtmnPrestamo,@pmtmCdCiaPrest,@pmtmFecIniLiq,@pmtmFecFinLiq,@pmtmCantDias,@pmtmCdTarifIva,@pmtmCdTarifRet,@pmtmCdTarifIca,@pmtmCdTarifDct)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreFactura_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_PreFactura (tmNumero,tmItem,tmIdConc,tmConcepto,tmTipoConc,tmCantidad,tmVrUnitario,tmVrTotal,tmTarifIva,tmVrIva,tmTarifRet,tmVrRetfte,tmTarifIca,tmVrRetica,tmTarifDct,tmVrDcto,tmnPrestamo,tmCdCiaPrest,tmFecIniLiq,tmFecFinLiq
,tmCantDias,tmCdTarifIva,tmCdTarifRet,tmCdTarifIca,tmCdTarifDct)
 SELECT @pmtmNumero,Item,IdConcepto,Concepto,TipoReg,Cantidad,VrUnitario,VrTotal,TarifIva,VrIva,TarifRet,VrRetfte,TarifIca,VrRetica,TarifDcto,VrDcto,nPrestamo,CdCiaPrest,FecIniLiq,FecFinLiq,CantDias,CdTarifIva,CdTarifRet,CdTarifIca,CdTarifDct
 FROM Trn_FinFactDet
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreIntMora @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipDoc VARCHAR(3),@pmtmPrestamo INT,@pmtmIdCia CHAR(2),@pmtmItemPre INT,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16)
,@pmtmFecVence SMALLDATETIME,@pmtmVrTotal MONEY,@pmtmVrAbonado MONEY,@pmtmFecUltLiq SMALLDATETIME,@pmtmFecIniLiq SMALLDATETIME,@pmtmFecFinLiq SMALLDATETIME,@pmtmDiasLiq INT,@pmtmDiasGracia INT,@pmtmBaseLiq MONEY
,@pmtmTasaEA DECIMAL(16,8),@pmtmTasaEM DECIMAL(16,8),@pmtmTasaDia DECIMAL(16,8),@pmtmVrLiquida MONEY,@pmtmTarifaIva DECIMAL(14,4),@pmtmVrIva MONEY,@pmtmEstado INT,@pmtmDetalle VARCHAR(250),@pmtmLinCred VARCHAR(4),@pmtmNCuota INT
 AS 
 INSERT INTO tm_PreIntMora (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCia,tmItemPre,tmIdCliente,tmIdAgencia,tmIdVend,tmFecVence,tmVrTotal,tmVrAbonado,tmFecUltLiq,tmFecIniLiq,tmFecFinLiq,tmDiasLiq,tmDiasGracia,tmBaseLiq,tmTasaEA,tmTasaEM,tmTasaDia
,tmVrLiquida,tmTarifaIva,tmVrIva,tmEstado,tmDetalle,tmLinCred,tmNCuota)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipDoc,@pmtmPrestamo,@pmtmIdCia,@pmtmItemPre,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmFecVence,@pmtmVrTotal,@pmtmVrAbonado,@pmtmFecUltLiq,@pmtmFecIniLiq,@pmtmFecFinLiq
,@pmtmDiasLiq,@pmtmDiasGracia,@pmtmBaseLiq,@pmtmTasaEA,@pmtmTasaEM,@pmtmTasaDia,@pmtmVrLiquida,@pmtmTarifaIva,@pmtmVrIva,@pmtmEstado,@pmtmDetalle,@pmtmLinCred,@pmtmNCuota)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreIntMora_Sel @pmtmNumero VARCHAR(5),@pmTipoProc VARCHAR(3),@pmNumProc INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_PreIntMora (tmNumero,tmItem,tmTipDoc,tmPrestamo,tmIdCia,tmItemPre,tmIdCliente,tmIdAgencia,tmIdVend,tmFecVence,tmVrTotal,tmVrAbonado
,tmFecUltLiq,tmFecIniLiq,tmFecFinLiq,tmDiasLiq,tmDiasGracia,tmBaseLiq,tmTasaEA,tmTasaEM,tmTasaDia,tmVrLiquida,tmTarifaIva,tmVrIva,tmEstado,tmDetalle,tmLinCred,tmNCuota)
 SELECT @pmtmNumero,Item,TipDoc,IdPrestamo,IdCiaPre,ItemPre,IdCliente,IdAgencia,IdVend,FecVence,VrTotal,VrAbonado,FecUltLiq,FecIniLiq,FecFinLiq,DiasLiquida
,DiasGracia,BaseLiquida,TasaEA,TasaEM,TasaDia,VrLiquida,TarifaIva,VrIvaInt,1,'',CdLinCred,NCuotaLiq
 FROM Trn_FinCauMora 
 WHERE TipoProc=@pmTipoProc AND  NumProc=@pmNumProc AND  IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paInstm_PreMora @pmtmTipDoc VARCHAR(3),@pmtmIdPrestamo INT,@pmtmIdCia CHAR(2)
,@pmtmVrCuota MONEY,@pmtmDescCuota VARCHAR(100),@pmtmVrMora MONEY,@pmtmDiasMora INT
 AS 
 INSERT INTO tm_PreMora (tmTipDoc,tmIdPrestamo,tmIdCia,tmVrCuota,tmDescCuota,tmVrMora,tmDiasMora)
 VALUES (@pmtmTipDoc,@pmtmIdPrestamo,@pmtmIdCia,@pmtmVrCuota,@pmtmDescCuota,@pmtmVrMora,@pmtmDiasMora)



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreMovi @pmtmEst CHAR(2),@pmtmTipDoc VARCHAR(3),@pmtmNumero INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmFecha SMALLDATETIME,@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmCargos MONEY,@pmtmAbonos MONEY,@pmtmNumCuota INT,@pmtmTimeSys SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmCdConcepto VARCHAR(4),@pmtmConcepto VARCHAR(10)
,@pmtmDetalle VARCHAR(250),@pmtmReferencia VARCHAR(50),@pmtmTipoTasa VARCHAR(10),@pmtmDTF_EA DECIMAL(16,8),@pmtmTasaNom DECIMAL(16,8),@pmtmPuntos DECIMAL(16,8),@pmtmTasaEfe DECIMAL(16,8),@pmtmTipRef VARCHAR(3),@pmtmDocRef INT,@pmtmIdCiaRef CHAR(2),@pmtmNumForma VARCHAR(20),@pmtmCdBanco VARCHAR(4),@pmtmBeneficiario VARCHAR(150),@pmtmCdForma VARCHAR(4),@pmtmEnEfectivo BIT,@pmtmTotalAbono MONEY
,@pmtmVrAfavor MONEY,@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCiaCom CHAR(2),@pmtmAnulado BIT,@pmtmNumDev INT,@pmtmFecDev SMALLDATETIME,@pmtmIdUsuario VARCHAR(11),@pmtmSanClie MONEY,@pmtmSanClieCia MONEY,@pmtmSanAge MONEY,@pmtmSanAgeCia MONEY
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario
,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 VALUES (@pmtmEst,@pmtmTipDoc,@pmtmNumero,@pmtmIdCia,@pmtmItem,@pmtmFecha,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdVend,@pmtmCargos,@pmtmAbonos,@pmtmNumCuota,@pmtmTimeSys,@pmtmFecVence,@pmtmCdConcepto,@pmtmConcepto,@pmtmDetalle,@pmtmReferencia,@pmtmTipoTasa,@pmtmDTF_EA,@pmtmTasaNom,@pmtmPuntos,@pmtmTasaEfe,@pmtmTipRef,@pmtmDocRef,@pmtmIdCiaRef,@pmtmNumForma,@pmtmCdBanco
,@pmtmBeneficiario,@pmtmCdForma,@pmtmEnEfectivo,@pmtmTotalAbono,@pmtmVrAfavor,@pmtmTipCom,@pmtmComprobante,@pmtmIdCiaCom,@pmtmAnulado,@pmtmNumDev,@pmtmFecDev,@pmtmIdUsuario,@pmtmSanClie,@pmtmSanClieCia,@pmtmSanAge,@pmtmSanAgeCia)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Abo  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,'INI',Recibo,A.IdCia,A.Item,A.Fecha,A.IdCliente,A.IdAgencia,IdVend,0,TotalAbono,NumCuota,TimeSys,FecPago,'0',Concepto,A.Detalle,'ABONO SALDO INICIAL'
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,A.TipDoc,A.IdPrestamo,A.IdCiaPre,'0','0','','0',0,VrAbonado,0,'0',0,'00',0,0,Null,'ADMIN',0,0,0,0
 FROM Trn_FinAbonos AS A INNER JOIN Trn_FinCuotas AS C ON A.TipDoc=C.TipDoc AND  A.IdPrestamo=C.IdPrestamo AND  A.IdCiaPre=C.IdCia AND  A.ItemPre=C.Item
 WHERE A.TipRec='PR1' AND A.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND A.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND A.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Dfa  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,0,VrNeto,0,TimeSys,Fecha,IdConcepto,'',Observacion,'','0',0,0,0,0,TipDoc,Factura,IdCiaDoc
,'','0','','',0,0,0,TipCom,Comprobante,IdCiaCom,0,0,Fecha,IdUsuario,0,0,0,0
 FROM Trn_FinDevFac
  WHERE Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Fac  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,C.TipDoc,C.IdPrestamo,C.IdCia,Item,C.Fecha,C.IdCliente,C.IdAgencia,IdVend,VrTotal,0,NumCuota,TimeSys,C.FechaVence,IdConcepto,Concepto,Detalle,'FACTURAS'
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,'CAU',NumCausacion,IdCiaCau,'0','0','0','0',0,VrAbonado,0,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,IdUsuario,0,0,0,0
 FROM Trn_FinCuotas AS C INNER JOIN Trn_FinFactint  AS F ON C.TipDoc=F.TipDoc  AND C.IdPrestamo=F.Factura  AND C.IdCia=F.IdCia
 WHERE C.TipDoc='FIF' AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND C.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Ndb  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDoc,NumNota,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,CASE TipDoc WHEN 'ND2' THEN VrNeto ELSE 0 END,CASE TipDoc WHEN 'NC2' THEN VrNeto ELSE 0 END,0,TimeSys,FechaVence,IdConcepto
,Modalidad,Observacion,Referencia,'0',0,0,0,0,TipDcm,Documento,IdCiaDcm,NumCheque,CdBanco,'',CdForma,0,0,VrAnticipo,TipCom,Comprobante,IdCiaCom,Anulado,0,Null,IdUsuario,0,0,0,0
 FROM Trn_FinNotas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente  LIKE ISNULL(@pmIdCliente ,'%') AND  IdAgencia  LIKE ISNULL(@pmIdAgencia ,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Odb  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDev,Devolucion,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,VrNeto,0,0,TimeSys,FecDoc,IdConcepto,ModdDev,Observacion,Referencia,'0',0,0,0,0,TipDoc,Recibo,IdCiaDoc,'0','0','','0',EnEfectivo,0,VrAnticipo
,TipCom,Comprobante,IdCiaCom,0,0,Null,IdUsuario,0,0,0,0
 FROM Trn_FinDevRec
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente  LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Pre  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,C.TipDoc,C.IdPrestamo,C.IdCia,Item,C.Fecha,C.IdCliente,C.IdAgencia,IdVend,VrTotal,0,NumCuota,TimeSys,FechaVence,IdConcepto,Concepto,Detalle,Causacion
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,'CAU',NumCausacion,IdCiaCau,NumCredito,'0',TipoGarantia,'0',0,VrAbonado,0,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,IdUsuario,0,0,0,0
 FROM Trn_FinCuotas AS C INNER JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc  AND C.IdPrestamo=P.IdPrestamo  AND C.IdCia=P.IdCia
 WHERE C.TipDoc='PR1' AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND C.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PreMovi_Rec  @pmtmEst CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 INSERT INTO tm_PreMovi (tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmFecha,tmIdCliente,tmIdAgencia,tmIdVend,tmCargos,tmAbonos,tmNumCuota,tmTimeSys,tmFecVence,tmCdConcepto,tmConcepto,tmDetalle,tmReferencia
,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmTipRef,tmDocRef,tmIdCiaRef,tmNumForma,tmCdBanco,tmBeneficiario,tmCdForma,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia)
 SELECT @pmtmEst,TipDoc,Recibo,IdCia,0,Fecha,IdCliente,IdAgencia,IdVend,0,VrNeto,0,TimeSys,FecPago,IdConcepto,pVehiculo,Observacion,Referencia,'0',0,0,0,0,TipDcm,Documento,IdCiaDcm
,NumForma,CdBanco,'',CdForma,EnEfectivo,0,VrAnticipo,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,IdUsuario,0,0,0,0
 FROM Trn_FinRecibos
  WHERE Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreVence @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmNumCuota INT,@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME
,@pmNumDias INT,@pmVrCuota MONEY,@pmVrCapital MONEY,@pmVrInteres MONEY,@pmVrIntMora MONEY,@pmVrAbonoCap MONEY,@pmVrAbonoInt MONEY,@pmVrSaldo MONEY
,@pmTasaEfeMes DECIMAL(16,8),@pmTasaEfeDia DECIMAL(16,8),@pmTipoCuota VARCHAR(10)
 AS 
 INSERT INTO tm_PreVence (tmEst,TipDoc,IdPrestamo,IdCia,NumCuota,Fecha,FechaVence,NumDias,VrCuota,VrCapital,VrInteres,VrIntMora,VrAbonoCap,VrAbonoInt,VrSaldo,TasaEfeMes,TasaEfeDia,TipoCuota)
 VALUES (@pmtmEst,@pmTipDoc,@pmIdPrestamo,@pmIdCia,@pmNumCuota,@pmFecha,@pmFechaVence,@pmNumDias,@pmVrCuota,@pmVrCapital,@pmVrInteres,@pmVrIntMora
,@pmVrAbonoCap,@pmVrAbonoInt,@pmVrSaldo,@pmTasaEfeMes,@pmTasaEfeDia,@pmTipoCuota)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_PreVenceAmza @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 INSERT INTO tm_PreVence (tmEst,TipDoc,IdPrestamo,IdCia,NumCuota,Fecha,FechaVence,NumDias,VrCuota,VrCapital,VrInteres,VrIntMora
,VrAbonoCap,VrAbonoInt,VrSaldo,TasaEfeMes,TasaEfeDia,TipoCuota)
 SELECT @pmtmEst,TipDoc,IdPrestamo,IdCia,Item,FecCausac,FecVence,NumDias,VrCuota,VrCapital,VrInteres,0
,0,0,VrSaldo,TasaEfeMes,TasaEfeDia,'PORCAUSAR'
 FROM Trn_FinAmtza 
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paInstm_PresPagos_Sel @pmtmNumero VARCHAR(5),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 INSERT INTO tm_PresPagos (tmNumero,tmItem,tmNumForma,tmFecha,tmFecPago,tmVrCuota,tmCdBanco,tmCtaCheque,tmDetalle)
 SELECT @pmtmNumero,Item, Numero, Fecha, FecPago, VrCuota,  CdBanco, CtaCheque, Detalle
 FROM Trn_FinPagos
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInstm_RecAplica @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipoDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCiaDoc CHAR(2),@pmtmItemDoc INT,@pmtmIdClase VARCHAR(4),@pmtmIdCargo VARCHAR(4),@pmtmValor MONEY,@pmtmIdVehiculo VARCHAR(10),@pmtmVehPropio BIT,@pmtmIdConductor VARCHAR(16),@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmReferencia VARCHAR(20),@pmtmFactura VARCHAR(15),@pmtmDetalle VARCHAR(100),@pmtmComision DECIMAL(14,4),@pmtmDescuento MONEY,@pmtmConductor VARCHAR(100),@pmtmTipRec VARCHAR(3),@pmtmRecibo INT,@pmtmCiaRec CHAR(2),@pmtmFecRec SMALLDATETIME,@pmtmItemRec INT,@pmtmAplAnt BIT
 AS 
 INSERT INTO tm_RecAplica (tmNumero,tmItem,tmTipoDoc,tmDocumento,tmIdCiaDoc,tmItemDoc,tmIdClase,tmIdCargo,tmValor,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdCliente,tmIdAgencia,tmReferencia,tmFactura,tmDetalle,tmComision,tmDescuento,tmConductor,tmTipRec,tmRecibo,tmCiaRec,tmFecRec,tmItemRec,tmAplAnt)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipoDoc,@pmtmDocumento,@pmtmIdCiaDoc,@pmtmItemDoc,@pmtmIdClase,@pmtmIdCargo,@pmtmValor,@pmtmIdVehiculo,@pmtmVehPropio,@pmtmIdConductor,@pmtmIdCliente,@pmtmIdAgencia,@pmtmReferencia,@pmtmFactura,@pmtmDetalle,@pmtmComision,@pmtmDescuento,@pmtmConductor,@pmtmTipRec,@pmtmRecibo,@pmtmCiaRec,@pmtmFecRec,@pmtmItemRec,@pmtmAplAnt)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ReqDetalle @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmCdProducto VARCHAR(16),@pmtmDescripcion VARCHAR(250)
,@pmtmIdSubgrupo VARCHAR(8),@pmtmCdBodega VARCHAR(4),@pmtmCantidad DECIMAL(14,4),@pmtmIdUnd VARCHAR(4),@pmtmVrUnitario MONEY
,@pmtmTipOrd VARCHAR(3),@pmtmNumOrden INT,@pmtmIdCiaOrd CHAR(2),@pmtmTipSal VARCHAR(3),@pmtmNSalida INT,@pmtmIdCiaSal CHAR(2),@pmtmFechaSal SMALLDATETIME
 AS 
 INSERT INTO tm_ReqDetalle (tmNumero,tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad,tmIdUnd,tmVrUnitario,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmCdProducto,@pmtmDescripcion,@pmtmIdSubgrupo,@pmtmCdBodega,@pmtmCantidad,@pmtmIdUnd,@pmtmVrUnitario
,@pmtmTipOrd,@pmtmNumOrden,@pmtmIdCiaOrd,@pmtmTipSal,@pmtmNSalida,@pmtmIdCiaSal,@pmtmFechaSal)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ReqDetalle_Sel @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_ReqDetalle (tmNumero,tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad
,tmIdUnd,tmVrUnitario,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal)
 SELECT  @pmtmNumero,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,Cantidad,IdUnd,VrUnitario,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal
 FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Saldos @pmtmEst CHAR(2),@pmtmIdCia CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdTercero VARCHAR(16),@pmtmCdSubgrupo VARCHAR(8),@pmtmSanCue MONEY
,@pmtmDebitos MONEY,@pmtmCreditos MONEY,@pmtmCostoAnt MONEY,@pmtmCostoEnt MONEY,@pmtmCostoSal MONEY,@pmtmSaldoAnt DECIMAL(14,4),@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4)
 AS 
 INSERT INTO tm_Saldos (tmEst,tmIdCia,tmIdCuenta,tmIdTercero,tmCdSubgrupo,tmSanCue,tmDebitos,tmCreditos,tmCostoAnt,tmCostoEnt,tmCostoSal,tmSaldoAnt,tmEntradas,tmSalidas)
 VALUES (@pmtmEst,@pmtmIdCia,@pmtmIdCuenta,@pmtmIdTercero,@pmtmCdSubgrupo,@pmtmSanCue,@pmtmDebitos,@pmtmCreditos,@pmtmCostoAnt,@pmtmCostoEnt,@pmtmCostoSal,@pmtmSaldoAnt,@pmtmEntradas,@pmtmSalidas)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Saldos_Kar @pmtmEst CHAR(2),@pmDetCia BIT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmTipoRef VARCHAR(10)=Null,@pmIdSubgrupo VARCHAR(8)=Null
 AS 
 IF @pmDetCia=1 
 BEGIN 
  INSERT INTO tm_Saldos (tmEst,tmIdCia,tmIdCuenta,tmIdTercero,tmCdSubgrupo,tmSanCue,tmDebitos,tmCreditos,tmCostoAnt,tmCostoEnt,tmCostoSal,tmSaldoAnt,tmEntradas,tmSalidas)
  SELECT @pmtmEst,IdCia,IdCuenta,'0',P.IdSubgrupo,0,0,0,0,SUM(VrCostoEnt),SUM(VrCostoSal),0,SUM(Entradas),SUM(Salidas)
  FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
  LEFT JOIN (SELECT IdSubgrupo,IdCuenta  FROM SubgruposCue WHERE IdClase='0001') AS SC ON P.IdSubgrupo=SC.IdSubgrupo
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
  AND TipoRef<>'SERVICIO'  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')
  GROUP BY IdCia,IdCuenta,P.IdSubgrupo 
 END
ELSE
 BEGIN 
  INSERT INTO tm_Saldos (tmEst,tmIdCia,tmIdCuenta,tmIdTercero,tmCdSubgrupo,tmSanCue,tmDebitos,tmCreditos,tmCostoAnt,tmCostoEnt,tmCostoSal,tmSaldoAnt,tmEntradas,tmSalidas)
  SELECT @pmtmEst,'00',IdCuenta,'0',P.IdSubgrupo,0,0,0,0,SUM(VrCostoEnt),SUM(VrCostoSal),0,SUM(Entradas),SUM(Salidas)
  FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
  LEFT JOIN (SELECT IdSubgrupo,IdCuenta  FROM SubgruposCue WHERE IdClase='0001') AS SC ON P.IdSubgrupo=SC.IdSubgrupo
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
  AND TipoRef<>'SERVICIO'  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipoRef LIKE ISNULL(@pmTipoRef,'%')
  AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')
  GROUP BY  IdCuenta,P.IdSubgrupo 
 END
 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Semanas @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmVehiculo VARCHAR(10)
,@pmtmSemana INT,@pmtmVrTotal MONEY,@pmtmVrPagado MONEY,@pmtmDescripcion VARCHAR(150)
 AS 
 INSERT INTO tm_Semanas (tmNumero,tmItem,tmVehiculo,tmSemana,tmVrTotal,tmVrPagado,tmDescripcion)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmVehiculo,@pmtmSemana,@pmtmVrTotal,@pmtmVrPagado,@pmtmDescripcion)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Semanas_Sel  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmtmNumero VARCHAR(5)
 AS 
 INSERT INTO tm_Semanas (tmNumero,tmItem,tmVehiculo,tmSemana,tmVrTotal,tmVrPagado,tmDescripcion)
 SELECT  @pmtmNumero,Item, IdVehiculo, nSemana
,0,CASE WHEN VrAbono>0  THEN VrAbono ELSE VrAbono*-1 END,Descripcion
 FROM   Trn_FcAbonos
 WHERE  TipRec=@pmTipRec  AND Recibo=@pmRecibo  AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Ser @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmNumSerial VARCHAR(30),@pmtmIdCia CHAR(2),@pmtmIdBodega VARCHAR(4),@pmtmCdUbic VARCHAR(10)
,@pmtmIdEstado VARCHAR(4),@pmtmLinea INT,@pmtmDetalle VARCHAR(250),@pmtmReferencia VARCHAR(50),@pmtmRef2 VARCHAR(50),@pmtmFecRef1 SMALLDATETIME,@pmtmFecRef2 SMALLDATETIME,@pmtmUbicacion VARCHAR(50)
,@pmtmIdBodNue VARCHAR(4),@pmtmUbicNue VARCHAR(10),@pmtmNueEstado VARCHAR(4),@pmtmInactivo BIT,@pmtmTipMovi VARCHAR(3)
 AS 
 INSERT INTO tm_Ser (tmNumero,tmItem,tmIdProducto,tmNumSerial,tmIdCia,tmIdBodega,tmCdUbic,tmIdEstado,tmLinea,tmDetalle,tmReferencia,tmRef2,tmFecRef1,tmFecRef2,tmUbicacion,tmIdBodNue,tmUbicNue,tmNueEstado,tmInactivo,tmTipMovi)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmIdProducto,@pmtmNumSerial,@pmtmIdCia,@pmtmIdBodega,@pmtmCdUbic,@pmtmIdEstado,@pmtmLinea,@pmtmDetalle,@pmtmReferencia,@pmtmRef2,@pmtmFecRef1,@pmtmFecRef2
,@pmtmUbicacion,@pmtmIdBodNue,@pmtmUbicNue,@pmtmNueEstado,@pmtmInactivo,@pmtmTipMovi)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Ser_Dvn @pmtmNumero VARCHAR(5), @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
 AS 
 INSERT INTO tm_Ser (tmNumero,tmItem,tmIdProducto,tmNumSerial,tmIdCia,tmIdBodega,tmCdUbic,tmIdEstado,tmLinea,tmDetalle,tmReferencia,tmRef2
,tmFecRef1,tmFecRef2,tmUbicacion,tmIdBodNue,tmUbicNue,tmNueEstado,tmInactivo,tmTipMovi)
 SELECT  @pmtmNumero,Item,M.IdProducto,M.NumSerial,M.IdCia,ISNULL(S.IdBodega,M.IdBodega),S.CdUbic,ISNULL(IdEstado,'0001'),Linea,M.Observacion,Referencia,Referncia2
,FecRef1,FecRef2,Ubicacion,M.IdBodega,M.CdUbic,ISNULL(IdEstado,'0001') ,ISNULL(Inactivo,0)
,CASE Movimiento WHEN 'ENT' THEN 'SAL' ELSE 'ENT'  END AS Movmiento
 FROM Trn_SerMov AS M  LEFT JOIN Trn_Seriales AS S ON M.IdProducto=S.IdProducto AND M.NumSerial=S.NumSerial AND M.IdCia=S.IdCia
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND M.IdCia=@pmIdCia 
 AND M.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND M.IdBodega LIKE ISNULL(@pmIdBodega,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Ser_Sel @pmtmNumero VARCHAR(5), @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmMovimiento VARCHAR(3)=Null
 AS 
 INSERT INTO tm_Ser (tmNumero,tmItem,tmIdProducto,tmNumSerial,tmIdCia,tmIdBodega,tmCdUbic,tmIdEstado,tmLinea,tmDetalle,tmReferencia,tmRef2
,tmFecRef1,tmFecRef2,tmUbicacion,tmIdBodNue,tmUbicNue,tmNueEstado,tmInactivo,tmTipMovi)
 SELECT  @pmtmNumero,Item,M.IdProducto,M.NumSerial,M.IdCia,ISNULL(S.IdBodega,M.IdBodega),S.CdUbic,ISNULL(IdEstado,'0001'),Linea,M.Observacion,Referencia,Referncia2
,FecRef1,FecRef2,Ubicacion,M.IdBodega,M.CdUbic,ISNULL(IdEstado,'0001') ,ISNULL(Inactivo,0),ISNULL(@pmMovimiento,Movimiento)
 FROM Trn_SerMov AS M  LEFT JOIN Trn_Seriales AS S ON M.IdProducto=S.IdProducto AND M.NumSerial=S.NumSerial AND M.IdCia=S.IdCia
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND M.IdCia=@pmIdCia 
 AND M.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND M.IdBodega LIKE ISNULL(@pmIdBodega,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Sobtasas @pmtmIdDec VARCHAR(4),@pmtmNumDeclara INT,@pmtmAcpmCant DECIMAL(14,4)
,@pmtmAcpmBase MONEY,@pmtmAcpmValor MONEY,@pmtmAcpmTarif DECIMAL(14,4),@pmtmAcpmPrecio MONEY
,@pmtmAcpiCant DECIMAL(14,4),@pmtmAcpiBase MONEY,@pmtmAcpiValor MONEY,@pmtmAcpiTarif DECIMAL(14,4)
,@pmtmAcpiPrecio MONEY,@pmtmGcorCant DECIMAL(14,4),@pmtmGcorBase MONEY,@pmtmGcorValor MONEY
,@pmtmGcorTarif DECIMAL(14,4),@pmtmGcorPrecio MONEY,@pmtmGextCant DECIMAL(14,4),@pmtmGextBase MONEY
,@pmtmGextValor MONEY,@pmtmGextTarif DECIMAL(14,4),@pmtmGextPrecio MONEY,@pmtmGimpCant DECIMAL(14,4)
,@pmtmGimpBase MONEY,@pmtmGimpValor MONEY,@pmtmGimpTarif DECIMAL(14,4),@pmtmGimpPrecio MONEY
,@pmtmGcoxCant DECIMAL(14,4),@pmtmGcoxBase MONEY,@pmtmGcoxValor MONEY,@pmtmGcoxTarif DECIMAL(14,4)
,@pmtmGcoxPrecio MONEY,@pmtmGeoxCant DECIMAL(14,4),@pmtmGeoxBase MONEY,@pmtmGeoxValor MONEY,@pmtmGeoxTarif DECIMAL(14,4),@pmtmGeoxPrecio MONEY
,@pmtmAczfCant DECIMAL(14,4),@pmtmAczfBase MONEY,@pmtmAczfValor MONEY,@pmtmAczfTarif DECIMAL(14,4) ,@pmtmAczfPrecio MONEY
,@pmtmGnzfCant DECIMAL(14,4),@pmtmGnzfBase MONEY,@pmtmGnzfValor MONEY,@pmtmGnzfTarif DECIMAL(14,4),@pmtmGnzfPrecio MONEY
 AS 
 INSERT INTO tm_Sobtasas (tmIdDec,tmNumDeclara,tmAcpmCant,tmAcpmBase,tmAcpmValor,tmAcpmTarif,tmAcpmPrecio,tmAcpiCant,tmAcpiBase,tmAcpiValor
,tmAcpiTarif,tmAcpiPrecio,tmGcorCant,tmGcorBase,tmGcorValor,tmGcorTarif,tmGcorPrecio,tmGextCant,tmGextBase,tmGextValor,tmGextTarif,tmGextPrecio
,tmGimpCant,tmGimpBase,tmGimpValor,tmGimpTarif,tmGimpPrecio,tmGcoxCant,tmGcoxBase,tmGcoxValor,tmGcoxTarif,tmGcoxPrecio,tmGeoxCant
,tmGeoxBase,tmGeoxValor,tmGeoxTarif,tmGeoxPrecio,tmAczfCant,tmAczfBase,tmAczfValor,tmAczfTarif,tmAczfPrecio,tmGnzfCant,tmGnzfBase,tmGnzfValor,tmGnzfTarif,tmGnzfPrecio)
 VALUES (@pmtmIdDec,@pmtmNumDeclara,@pmtmAcpmCant,@pmtmAcpmBase,@pmtmAcpmValor,@pmtmAcpmTarif,@pmtmAcpmPrecio,@pmtmAcpiCant
,@pmtmAcpiBase,@pmtmAcpiValor,@pmtmAcpiTarif,@pmtmAcpiPrecio,@pmtmGcorCant,@pmtmGcorBase,@pmtmGcorValor,@pmtmGcorTarif
,@pmtmGcorPrecio,@pmtmGextCant,@pmtmGextBase,@pmtmGextValor,@pmtmGextTarif,@pmtmGextPrecio,@pmtmGimpCant,@pmtmGimpBase
,@pmtmGimpValor,@pmtmGimpTarif,@pmtmGimpPrecio,@pmtmGcoxCant,@pmtmGcoxBase,@pmtmGcoxValor,@pmtmGcoxTarif,@pmtmGcoxPrecio
,@pmtmGeoxCant,@pmtmGeoxBase,@pmtmGeoxValor,@pmtmGeoxTarif,@pmtmGeoxPrecio,@pmtmAczfCant,@pmtmAczfBase,@pmtmAczfValor,@pmtmAczfTarif 
,@pmtmAczfPrecio,@pmtmGnzfCant,@pmtmGnzfBase,@pmtmGnzfValor,@pmtmGnzfTarif,@pmtmGnzfPrecio)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_Tanqueos @pmtmNumero VARCHAR(5),@pmtmItem INT, @pmtmFecha SMALLDATETIME, @pmtmDocumento VARCHAR(10), @pmtmIdTipoCom VARCHAR(4), @pmtmCantidad DECIMAL(14,4)
, @pmtmValorUnitario MONEY,@pmtmIdEstacion VARCHAR(4), @pmtmEstacion VARCHAR(50), @pmtmTipoCom  VARCHAR(50)
AS
 INSERT INTO  tm_Tanqueos ( tmNumero, tmItem, tmFecha, tmDocumento, tmIdTipoCom, tmCantidad, tmValorUnitario,tmIdEstacion, tmEstacion, tmTipoCom )
 VALUES (@pmtmNumero, @pmtmItem, @pmtmFecha, @pmtmDocumento, @pmtmIdTipoCom, @pmtmCantidad, @pmtmValorUnitario,@pmtmIdEstacion, @pmtmEstacion, @pmtmTipoCom)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ValesCred @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipo VARCHAR(3),@pmtmRango INT,@pmtmNumSerie VARCHAR(5),@pmtmNumVale INT,@pmtmFecha SMALLDATETIME,@pmtmHoraServ VARCHAR(10),@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdCuenta VARCHAR(16)
,@pmtmNumMovil VARCHAR(10),@pmtmCentCosto VARCHAR(50),@pmtmUnidades DECIMAL(14,4),@pmtmHoraRec DECIMAL(14,4),@pmtmHoraEspera DECIMAL(14,4),@pmtmFactUnid BIT,@pmtmFactRutas BIT,@pmtmFactHoras BIT,@pmtmVrPago MONEY,@pmtmVrCobro MONEY,@pmtmPlacaVeh VARCHAR(10),@pmtmCdConductor VARCHAR(16)
,@pmtmConductor VARCHAR(150),@pmtmCantidad INT,@pmtmUsuarios VARCHAR(1000),@pmtmObservacion VARCHAR(1000),@pmtmNumAuto BIT,@pmtmRutas VARCHAR(150),@pmtmRadica INT,@pmtmCiaRad CHAR(2),@pmtmItemRad INT
 AS 
 INSERT INTO tm_ValesCred (tmNumero,tmItem,tmTipo,tmRango,tmNumSerie,tmNumVale,tmFecha,tmHoraServ,tmIdCliente,tmIdAgencia,tmIdCuenta,tmNumMovil,tmCentCosto,tmUnidades,tmHoraRec,tmHoraEspera,tmFactUnid,tmFactRutas,tmFactHoras,tmVrPago,tmVrCobro,tmPlacaVeh
,tmCdConductor,tmConductor,tmCantidad,tmUsuarios,tmObservacion,tmNumAuto,tmRutas,tmRadica,tmCiaRad,tmItemRad)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipo,@pmtmRango,@pmtmNumSerie,@pmtmNumVale,@pmtmFecha,@pmtmHoraServ,@pmtmIdCliente,@pmtmIdAgencia,@pmtmIdCuenta,@pmtmNumMovil,@pmtmCentCosto,@pmtmUnidades,@pmtmHoraRec,@pmtmHoraEspera,@pmtmFactUnid,@pmtmFactRutas,@pmtmFactHoras,@pmtmVrPago,@pmtmVrCobro
,@pmtmPlacaVeh,@pmtmCdConductor,@pmtmConductor,@pmtmCantidad,@pmtmUsuarios,@pmtmObservacion,@pmtmNumAuto,@pmtmRutas,@pmtmRadica,@pmtmCiaRad,@pmtmItemRad)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ValesCred_Dev  @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
,@pmNumDev INT,@pmtmNumero VARCHAR(5)
 AS
 INSERT INTO tm_ValesCred (tmNumero,tmItem,tmTipo,tmRango,tmNumSerie,tmNumVale,tmFecha,tmHoraServ,tmIdCliente,tmIdAgencia,tmIdCuenta,tmNumMovil,tmCentCosto,tmUnidades,tmHoraRec
,tmHoraEspera,tmFactUnid,tmFactRutas,tmFactHoras,tmVrPago,tmVrCobro,tmPlacaVeh,tmCdConductor,tmConductor,tmCantidad,tmUsuarios,tmObservacion,tmNumAuto,tmRutas,tmRadica,tmCiaRad,tmItemRad)
 SELECT @pmtmNumero,Item,TipoRango,IdRango,NumSerie,NumForma,Fecha,HoraServ,IdCliente,IdAgencia,IdCuenta,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera
,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,PlacaVeh,CdConductor,NomConductor,CantPasaj,Usuarios,Observacion,0,'',nRadica,IdCia,Item
 FROM Trn_Vales
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia AND Anulado<>0 AND NumDev=@pmNumDev
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ValesCred_Sel  @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
,@pmtmNumero VARCHAR(5)
 AS
 INSERT INTO tm_ValesCred (tmNumero,tmItem,tmTipo,tmRango,tmNumSerie,tmNumVale,tmFecha,tmHoraServ,tmIdCliente,tmIdAgencia,tmIdCuenta,tmNumMovil,tmCentCosto,tmUnidades,tmHoraRec
,tmHoraEspera,tmFactUnid,tmFactRutas,tmFactHoras,tmVrPago,tmVrCobro,tmPlacaVeh,tmCdConductor,tmConductor,tmCantidad,tmUsuarios,tmObservacion,tmNumAuto,tmRutas,tmRadica,tmCiaRad,tmItemRad)
 SELECT @pmtmNumero,Item,TipoRango,IdRango,NumSerie,NumForma,Fecha,HoraServ,IdCliente,IdAgencia,IdCuenta,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera
,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,PlacaVeh,CdConductor,NomConductor,CantPasaj,Usuarios,Observacion,0,'',nRadica,IdCia,Item
 FROM Trn_Vales
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_Ventas @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmIdCia CHAR(2),@pmtmTipDoc VARCHAR(3)
,@pmtmIdConcepto VARCHAR(4),@pmtmIdCliente VARCHAR(16),@pmtmCdAgencia VARCHAR(16),@pmtmIdVend VARCHAR(16),@pmtmCdLocal VARCHAR(8),@pmtmCdSubzona VARCHAR(4),@pmtmPeriodo VARCHAR(6)
 AS 
 INSERT INTO tm_Ventas (tmEst,tmItem,tmIdProducto,tmIdBodega,tmIdCia,tmTipDoc,tmIdConcepto,tmIdCliente,tmCdAgencia,tmIdVend,tmCdLocal,tmCdSubzona,tmPeriodo)
 VALUES (@pmtmEst,@pmtmItem,@pmtmIdProducto,@pmtmIdBodega,@pmtmIdCia,@pmtmTipDoc,@pmtmIdConcepto,@pmtmIdCliente,@pmtmCdAgencia,@pmtmIdVend
,@pmtmCdLocal,@pmtmCdSubzona,@pmtmPeriodo)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_VentasCom @pmtmEst CHAR(2),@pmtmItem INT,@pmtmUnidades DECIMAL(14,4),@pmtmVentaNeto MONEY,@pmtmCostoNeto MONEY,@pmtmImpuestos MONEY,@pmtmImpGlobal MONEY
,@pmtmSoldicom MONEY,@pmtmVolumen DECIMAL(14,4),@pmtmUnidades2 DECIMAL(14,4),@pmtmVentaNeto2 MONEY,@pmtmCostoNeto2 MONEY,@pmtmImpuestos2 MONEY,@pmtmImpGlobal2 MONEY
,@pmtmSoldicom2 MONEY,@pmtmVolumen2 DECIMAL(14,4)
 AS 
 INSERT INTO tm_VentasCom (tmEst,tmItem,tmUnidades,tmVentaNeto,tmCostoNeto,tmImpuestos,tmImpGlobal,tmSoldicom,tmVolumen,tmUnidades2,tmVentaNeto2,tmCostoNeto2,tmImpuestos2,tmImpGlobal2,tmSoldicom2,tmVolumen2)
 VALUES (@pmtmEst,@pmtmItem,@pmtmUnidades,@pmtmVentaNeto,@pmtmCostoNeto,@pmtmImpuestos,@pmtmImpGlobal,@pmtmSoldicom,@pmtmVolumen,@pmtmUnidades2,@pmtmVentaNeto2,@pmtmCostoNeto2
,@pmtmImpuestos2,@pmtmImpGlobal2,@pmtmSoldicom2,@pmtmVolumen2)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_VentasDet @pmtmEst CHAR(2),@pmtmItem INT,@pmtmEntradas DECIMAL(14,4),@pmtmSalidas DECIMAL(14,4),@pmtmVtaBrutoEnt MONEY,@pmtmVtaBrutoSal MONEY,@pmtmCostoEnt MONEY,@pmtmCostoSal MONEY
,@pmtmDctosEnt MONEY,@pmtmDctosSal MONEY,@pmtmIvaEnt MONEY,@pmtmIvaSal MONEY,@pmtmReteFteEnt MONEY,@pmtmReteFteSal MONEY,@pmtmReteIcaEnt MONEY,@pmtmReteIcaSal MONEY,@pmtmSobtasaEnt MONEY
,@pmtmSobtasaSal MONEY,@pmtmImpGlobalEnt MONEY,@pmtmImpGlobalSal MONEY,@pmtmSoldicomEnt MONEY,@pmtmSoldicomSal MONEY,@pmtmUnidVolumen DECIMAL(14,4),@pmtmComision DECIMAL(14,4)
 AS 
 INSERT INTO tm_VentasDet (tmEst,tmItem,tmEntradas,tmSalidas,tmVtaBrutoEnt,tmVtaBrutoSal,tmCostoEnt,tmCostoSal,tmDctosEnt,tmDctosSal,tmIvaEnt,tmIvaSal,tmReteFteEnt,tmReteFteSal,tmReteIcaEnt,tmReteIcaSal,tmSobtasaEnt,tmSobtasaSal
,tmImpGlobalEnt,tmImpGlobalSal,tmSoldicomEnt,tmSoldicomSal,tmUnidVolumen,tmComision)
 VALUES (@pmtmEst,@pmtmItem,@pmtmEntradas,@pmtmSalidas,@pmtmVtaBrutoEnt,@pmtmVtaBrutoSal,@pmtmCostoEnt,@pmtmCostoSal,@pmtmDctosEnt,@pmtmDctosSal,@pmtmIvaEnt,@pmtmIvaSal,@pmtmReteFteEnt,@pmtmReteFteSal
,@pmtmReteIcaEnt,@pmtmReteIcaSal,@pmtmSobtasaEnt,@pmtmSobtasaSal,@pmtmImpGlobalEnt,@pmtmImpGlobalSal,@pmtmSoldicomEnt,@pmtmSoldicomSal,@pmtmUnidVolumen,@pmtmComision)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_VentasRes @pmtmEst CHAR(2),@pmtmItem INT,@pmtmEnero DECIMAL(14,4),@pmtmFebrero DECIMAL(14,4),@pmtmMarzo DECIMAL(14,4)
,@pmtmAbril DECIMAL(14,4),@pmtmMayo DECIMAL(14,4),@pmtmJunio DECIMAL(14,4),@pmtmJulio DECIMAL(14,4),@pmtmAgosto DECIMAL(14,4)
,@pmtmSeptiembre DECIMAL(14,4),@pmtmOctubre DECIMAL(14,4),@pmtmNoviembre DECIMAL(14,4),@pmtmDiciembre DECIMAL(14,4)
 AS 
 INSERT INTO tm_VentasRes (tmEst,tmItem,tmEnero,tmFebrero,tmMarzo,tmAbril,tmMayo,tmJunio,tmJulio,tmAgosto,tmSeptiembre,tmOctubre,tmNoviembre,tmDiciembre)
 VALUES (@pmtmEst,@pmtmItem,@pmtmEnero,@pmtmFebrero,@pmtmMarzo,@pmtmAbril,@pmtmMayo,@pmtmJunio,@pmtmJulio,@pmtmAgosto
,@pmtmSeptiembre,@pmtmOctubre,@pmtmNoviembre,@pmtmDiciembre)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_ccCupones @pmtmEst CHAR(2),@pmtmItem INT,@pmtmTipoReg INT,@pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmItemDoc INT,@pmtmFecEmision SMALLDATETIME,@pmtmFecVence SMALLDATETIME,@pmtmValorFactura MONEY,@pmtmValorAbono MONEY
,@pmtmIdPropietario VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmIdConductor VARCHAR(16),@pmtmIdCargo VARCHAR(4),@pmtmIdClase VARCHAR(4),@pmtmFactura VARCHAR(15),@pmtmReferencia VARCHAR(50),@pmtmDetalle VARCHAR(100),@pmtmDescuento MONEY
 AS 
 INSERT INTO tm_ccCupones (tmEst,tmItem,tmTipoReg,tmTipDoc,tmDocumento,tmIdCia,tmItemDoc,tmFecEmision,tmFecVence,tmValorFactura,tmValorAbono,tmIdPropietario,tmIdVehiculo,tmIdConductor,tmIdCargo,tmIdClase,tmFactura,tmReferencia,tmDetalle,tmDescuento)
 VALUES (@pmtmEst,@pmtmItem,@pmtmTipoReg,@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia,@pmtmItemDoc,@pmtmFecEmision,@pmtmFecVence,@pmtmValorFactura,@pmtmValorAbono,@pmtmIdPropietario,@pmtmIdVehiculo,@pmtmIdConductor,@pmtmIdCargo,@pmtmIdClase
,@pmtmFactura,@pmtmReferencia,@pmtmDetalle,@pmtmDescuento)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_cheq @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipForma VARCHAR(3),@pmtmNumForma VARCHAR(20),@pmtmIdBanco VARCHAR(4),@pmtmFecha SMALLDATETIME,@pmtmFecPago SMALLDATETIME
,@pmtmVrForma MONEY,@pmtmCtaForma VARCHAR(30),@pmtmBeneficiario VARCHAR(150),@pmtmReferncia1 VARCHAR(50),@pmtmReferncia2 VARCHAR(50),@pmtmDetalle VARCHAR(150),@pmtmNitCliente VARCHAR(16),@pmtmCdAgencia VARCHAR(16)
,@pmtmCdVend VARCHAR(16),@pmtmIdLocal VARCHAR(8),@pmtmCdCta VARCHAR(4)
 AS 
 INSERT INTO tm_cheq (tmNumero,tmItem,tmTipForma,tmNumForma,tmIdBanco,tmFecha,tmFecPago,tmVrForma,tmCtaForma,tmBeneficiario,tmReferncia1,tmReferncia2,tmDetalle,tmNitCliente,tmCdAgencia,tmCdVend,tmIdLocal,tmCdCta)
 VALUES (@pmtmNumero,@pmtmItem,@pmtmTipForma,@pmtmNumForma,@pmtmIdBanco,@pmtmFecha,@pmtmFecPago,@pmtmVrForma,@pmtmCtaForma,@pmtmBeneficiario,@pmtmReferncia1,@pmtmReferncia2,@pmtmDetalle
,@pmtmNitCliente,@pmtmCdAgencia,@pmtmCdVend,@pmtmIdLocal,@pmtmCdCta)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paInstm_vce @pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmValorFactura MONEY,@pmtmValorAbono MONEY,@pmtmIdVehiculo VARCHAR(10),@pmtmFecUltPago SMALLDATETIME
 AS 
 INSERT INTO tm_vce (tmTipDoc,tmDocumento,tmIdCia,tmItem,tmValorFactura,tmValorAbono,tmIdVehiculo,tmFecUltPago)
 VALUES (@pmtmTipDoc,@pmtmDocumento,@pmtmIdCia,@pmtmItem,@pmtmValorFactura,@pmtmValorAbono,@pmtmIdVehiculo,@pmtmFecUltPago)


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paInstm_vence @pmtmEst CHAR(2), @pmtmTipDoc VARCHAR(3), @pmtmDocumento INT, @pmtmIdCia CHAR(2), @pmtmItem INT, @pmtmFecEmision SMALLDATETIME, @pmtmFecVence SMALLDATETIME, @pmtmValorFactura MONEY
, @pmtmValorAbono MONEY, @pmtmIdPropietario VARCHAR(16), @pmtmIdVehiculo VARCHAR(10),@pmtmIdAgencia VARCHAR(16), @pmtmVehPropio BIT, @pmtmIdConductor VARCHAR(16), @pmtmComision DECIMAL(14,4), @pmtmIdCargo VARCHAR(4), @pmtmIdClase VARCHAR(4)
, @pmtmIdConcepto VARCHAR(4), @pmtmFecUltPago SMALLDATETIME, @pmtmNotaInt INT, @pmtmFecUltLiquida SMALLDATETIME, @pmtmFactura VARCHAR(15),@pmtmReferencia VARCHAR(50), @pmtmDetalle VARCHAR(100), @pmtmObservacion VARCHAR(250)
, @pmtmModalidad VARCHAR(10), @pmtmIdEstado VARCHAR(4), @pmtmIdUsuario VARCHAR(11), @pmtmTipCom VARCHAR(3), @pmtmComprobante INT, @pmtmIdCiaCom CHAR(2), @pmtmTipDcm VARCHAR(3), @pmtmDocmto INT,@pmtmIdCiaDcm CHAR(2), @pmtmTimeSys SMALLDATETIME
, @pmtmVencido MONEY, @pmtmPorVencer MONEY, @pmtmVence1 MONEY, @pmtmVence2 MONEY, @pmtmVence3 MONEY, @pmtmVence4 MONEY
AS
 INSERT INTO tm_vence (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono, tmIdPropietario, tmIdVehiculo, 
                      tmIdAgencia, tmVehPropio, tmIdConductor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmNotaInt, tmFecUltLiquida, tmFactura, 
                      tmReferencia, tmDetalle, tmObservacion, tmModalidad, tmIdEstado, tmIdUsuario, tmTipCom, tmComprobante, tmIdCiaCom, tmTipDcm, tmDocmto, 
                      tmIdCiaDcm, tmTimeSys, tmVencido, tmPorVencer, tmVence1, tmVence2, tmVence3, tmVence4)
 VALUES (@pmtmEst, @pmtmTipDoc, @pmtmDocumento, @pmtmIdCia, @pmtmItem, @pmtmFecEmision, @pmtmFecVence, @pmtmValorFactura, @pmtmValorAbono, @pmtmIdPropietario
, @pmtmIdVehiculo,@pmtmIdAgencia, @pmtmVehPropio, @pmtmIdConductor, @pmtmComision, @pmtmIdCargo, @pmtmIdClase, @pmtmIdConcepto, @pmtmFecUltPago, @pmtmNotaInt, @pmtmFecUltLiquida, @pmtmFactura
,@pmtmReferencia, @pmtmDetalle, @pmtmObservacion, @pmtmModalidad, @pmtmIdEstado, @pmtmIdUsuario, @pmtmTipCom, @pmtmComprobante, @pmtmIdCiaCom, @pmtmTipDcm, @pmtmDocmto
,@pmtmIdCiaDcm, @pmtmTimeSys, @pmtmVencido, @pmtmPorVencer, @pmtmVence1, @pmtmVence2, @pmtmVence3, @pmtmVence4)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_vence_Adm @pmtmEst CHAR(2),@pmSaldo DECIMAL(14,2),@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS
  IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   INSERT INTO tm_vence  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono, tmIdPropietario
 , tmIdVehiculo,tmIdAgencia, tmVehPropio, tmIdConductor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmNotaInt, tmFecUltLiquida, tmFactura
 ,tmReferencia, tmDetalle, tmObservacion, tmModalidad, tmIdEstado, tmIdUsuario, tmTipCom, tmComprobante, tmIdCiaCom, tmTipDcm, tmDocmto
 ,tmIdCiaDcm, tmTimeSys, tmVencido, tmPorVencer, tmVence1, tmVence2, tmVence3, tmVence4)
  SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia,Item,FecEmision,V.FecVence,ValorFactura,ValorAbono,IdCliente, V.IdVehiculo,IdAgencia,V.VehPropio, IdVendedor
 ,Comision ,IdCargo, V.IdClase, V.IdConcepto,FecUltPago,NotaInt,FecUltLiquida,V.Factura, V.Referencia,Detalle,C.Observacion,Modalidad,C.IdEstado,C.IdUsuario,TipCom, Comprobante, IdCiaCom
 ,TipRec,Recibo,IdCiaRec,TimeSys,0,0,0,0,0,0
  FROM Trn_CausacVence AS V INNER JOIN Trn_Causacion AS C ON V.TipDoc=C.TipDoc AND V.Documento=C.Documento AND V.IdCia=C.IdCia
  INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
  WHERE (ValorFactura-ValorAbono)>@pmSaldo  AND  V.TipDoc IN ('ADM','OTR')  AND FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND V.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
  AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.TipDoc LIKE ISNULL(@pmTipDoc,'%')
  ORDER BY V.TipDoc, V.IdCia,V.Documento
ELSE
   INSERT INTO tm_vence  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono, tmIdPropietario
 , tmIdVehiculo,tmIdAgencia, tmVehPropio, tmIdConductor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmNotaInt, tmFecUltLiquida, tmFactura
 ,tmReferencia, tmDetalle, tmObservacion, tmModalidad, tmIdEstado, tmIdUsuario, tmTipCom, tmComprobante, tmIdCiaCom, tmTipDcm, tmDocmto
 ,tmIdCiaDcm, tmTimeSys, tmVencido, tmPorVencer, tmVence1, tmVence2, tmVence3, tmVence4)
  SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia,Item,FecEmision,V.FecVence,ValorFactura,ValorAbono,IdCliente, V.IdVehiculo,IdAgencia,V.VehPropio, IdVendedor
 ,Comision ,IdCargo, V.IdClase, V.IdConcepto,FecUltPago,NotaInt,FecUltLiquida,V.Factura, V.Referencia,Detalle,C.Observacion,Modalidad,C.IdEstado,C.IdUsuario,TipCom, Comprobante, IdCiaCom
 ,TipRec,Recibo,IdCiaRec,TimeSys,0,0,0,0,0,0
  FROM Trn_CausacVence AS V INNER JOIN Trn_Causacion AS C ON V.TipDoc=C.TipDoc AND V.Documento=C.Documento AND V.IdCia=C.IdCia
  INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
  WHERE (ValorFactura-ValorAbono)>@pmSaldo  AND  V.TipDoc IN ('ADM','OTR')   AND V.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
  AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.TipDoc LIKE ISNULL(@pmTipDoc,'%')
  ORDER BY V.TipDoc, V.IdCia,V.Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_vence_Fvc @pmtmEst CHAR(2), @pmSaldo DECIMAL(14,2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS
  INSERT INTO tm_vence  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono, tmIdPropietario
 , tmIdVehiculo,tmIdAgencia, tmVehPropio, tmIdConductor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmNotaInt, tmFecUltLiquida, tmFactura
 ,tmReferencia, tmDetalle, tmObservacion, tmModalidad, tmIdEstado, tmIdUsuario, tmTipCom, tmComprobante, tmIdCiaCom, tmTipDcm, tmDocmto
 ,tmIdCiaDcm, tmTimeSys, tmVencido, tmPorVencer, tmVence1, tmVence2, tmVence3, tmVence4)
  SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia,Item,FecEmision,V.FecVence,ValorFactura,ValorAbono,V.IdCliente, V.IdVehiculo,V.IdAgencia,V.VehPropio, V.IdVendedor
 ,V.Comision ,V.IdCargo, V.IdClase, V.IdConcepto,FecUltPago,NotaInt,FecUltLiquida,V.Factura, V.Referencia,Detalle,F.Observacion,'FVC',F.IdEstado,F.IdUsuario,TipCom, Comprobante, IdCiaCom
 ,'FVC',0,'00',TimeSys,0,0,0,0,0,0
  FROM Trn_CausacVence AS V INNER JOIN Trn_CausaVales AS F ON V.TipDoc=F.TipDoc AND V.Documento=F.Documento AND V.IdCia=F.IdCia
  INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
  WHERE (ValorFactura-ValorAbono)>@pmSaldo  AND  V.TipDoc='FVC'
  AND (FecEmision>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecEmision<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
  AND V.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND V.IdClase LIKE ISNULL(@pmIdClase,'%') AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paInstm_vence_Not @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3), @pmSaldo DECIMAL(14,2),@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS
  INSERT INTO tm_vence  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono, tmIdPropietario
 , tmIdVehiculo,tmIdAgencia, tmVehPropio, tmIdConductor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmNotaInt, tmFecUltLiquida, tmFactura
 ,tmReferencia, tmDetalle, tmObservacion, tmModalidad, tmIdEstado, tmIdUsuario, tmTipCom, tmComprobante, tmIdCiaCom, tmTipDcm, tmDocmto
 ,tmIdCiaDcm, tmTimeSys, tmVencido, tmPorVencer, tmVence1, tmVence2, tmVence3, tmVence4)
  SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia,Item,FecEmision,V.FecVence,ValorFactura,ValorAbono,V.IdCliente, V.IdVehiculo,V.IdAgencia,V.VehPropio, V.IdVendedor
 ,V.Comision ,V.IdCargo, V.IdClase, V.IdConcepto,FecUltPago,NotaInt,FecUltLiquida,V.Factura, V.Referencia,Detalle,N.Observacion,'ND1',N.IdEstado,N.IdUsuario,TipCom, Comprobante, IdCiaCom
 ,TipDcm,N.Documento,IdCiaDoc,TimeSys,0,0,0,0,0,0
  FROM Trn_CausacVence AS V INNER JOIN Trn_NotasBuses AS N ON V.TipDoc=N.TipDoc AND V.Documento=N.NumNota AND V.IdCia=N.IdCia
  INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
  WHERE (ValorFactura-ValorAbono)>@pmSaldo  AND  V.TipDoc=@pmTipDoc
  AND (FecEmision>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecEmision<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
  AND V.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND V.IdClase LIKE ISNULL(@pmIdClase,'%') AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paInstm_vence_Pre @pmtmEst CHAR(2),@pmSaldo DECIMAL(14,2),@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS
   INSERT INTO tm_vence  (tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono, tmIdPropietario
 , tmIdVehiculo,tmIdAgencia, tmVehPropio, tmIdConductor, tmComision, tmIdCargo, tmIdClase, tmIdConcepto, tmFecUltPago, tmNotaInt, tmFecUltLiquida, tmFactura
 ,tmReferencia, tmDetalle, tmObservacion, tmModalidad, tmIdEstado, tmIdUsuario, tmTipCom, tmComprobante, tmIdCiaCom, tmTipDcm, tmDocmto
 ,tmIdCiaDcm, tmTimeSys, tmVencido, tmPorVencer, tmVence1, tmVence2, tmVence3, tmVence4)
  SELECT @pmtmEst,V.TipDoc, V.Documento, V.IdCia,Item,FecEmision,V.FecVence,ValorFactura,ValorAbono,IdCliente, V.IdVehiculo,IdAgencia,V.VehPropio, IdVendedor
 ,Comision ,IdCargo, V.IdClase, V.IdConcepto,FecUltPago,NotaInt,FecUltLiquida,V.Factura, V.Referencia,Detalle,P.Observacion,TipoCuota,P.IdEstado,P.IdUsuario,TipCom, Comprobante, IdCiaCom
 ,TipRec,Recibo,IdCiaRec,TimeSys,0,0,0,0,0,0
  FROM Trn_CausacVence AS V INNER JOIN Trn_Prestamos AS P ON V.TipDoc=P.TipDoc AND V.Documento=P.PrestamoID AND V.IdCia=P.IdCia
  INNER JOIN Vehiculos AS VH ON V.IdVehiculo=VH.IdVehiculo 
  WHERE (ValorFactura-ValorAbono)>@pmSaldo  AND  V.TipDoc='PRE' 
  AND FecEmision BETWEEN ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME))  AND ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME))
  AND V.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND V.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdClase LIKE ISNULL(@pmIdClase,'%')
  AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.TipDoc LIKE ISNULL(@pmTipDoc,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivos @pmIdActivo VARCHAR(30)
 AS 
 SELECT IdActivo,NomActivo,NumSerie,CodInvent,CodBarras,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,IdCia,IdProv,FecCompra,FecActivacion,NumOrden
,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep,VrMejoras,VrDisminucion,VidaUtil,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo
,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,IdEstado,FecRetiro,IdCausal,VrVenta,FecCreacion,IdUsuario
 FROM Activos
 WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivosBar @pmIdActivo VARCHAR(30),@pmCodBarras VARCHAR(30)
 AS 
 SELECT IdActivo,NomActivo,NumSerie,CodInvent,CodBarras,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,IdCia,IdProv,FecCompra,FecActivacion,NumOrden
,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep,VrMejoras,VrDisminucion,VidaUtil,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo
,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,IdEstado,FecRetiro,IdCausal,VrVenta,FecCreacion,IdUsuario
 FROM Activos
 WHERE IdActivo=@pmIdActivo OR (CodBarras=@pmCodBarras AND IdEstado<>'9999')
 ORDER BY  IdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivosDep @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT IdActivo,NomActivo,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,IdCia,IdProv,FecCompra,FecActivacion
,VrBaseDep,VrMejoras,VrDisminucion,VidaUtil,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo
,FecUltDep,IdEstado,FecRetiro
 FROM Activos
 WHERE FecActivacion<=@pmFechaFin 
 AND (IdEstado<>'9999'  OR (IdEstado='9999' AND FecRetiro>=@pmFechaIni))
 ORDER BY IdActivo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivosHis @pmIdActivo VARCHAR(30)=Null
 AS 
 SELECT A.IdActivo AS CdActivo,NomActivo,NumSerie,CodInvent,CodBarras,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CC.CCosto AS ActCentroCosto
,A.IdSubCos AS CdSubcentro,SC.SubCosto AS ActSubCentro,A.IdInstala AS CdInstala,I.Instlacion AS ActInstalacion,A.IdDep AS CdDep,DEP.Dependencia AS NomDependencia
,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,CI.Compania AS NomCia,IdProv,T.RazonSocial AS Proveedor,FecCompra,FecActivacion,NumOrden,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep
,A.VrMejoras AS TotMejoras,A.VrDisminucion AS TotDisminucion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep
,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS ActEstado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,FecCreacion
--datos de depreciacion
,D.Numero AS NumDeprecia,Fecha,D.IdUsuario AS CdUsuario,Usuario,Item,VrCostoIni,DP.VrMejoras AS ValMejoras,DP.VrDisminucion AS ValDisminuc,VrAcuAjustes,VrDepreciaHis,VrDepreciaMes,VrAjustesInf,VrAjustesDep,VrDepreciaAcum
,VrAjuDepAcum,TarifaAju,DP.IdCia AS DetCodCia,CN.Compania AS DetCompania,CdCCosto,CCD.CCosto AS DetCentroCosto,CdSubCos,SCD.SubCosto AS DetSubCentro,CdInstala,INS.Instlacion AS DetInstacion,CdDep,DE.Dependencia AS DetDependencia
,NitRespons,TRD.RazonSocial AS DetNomResponsable,FecIniDep,MesesVida,MetodoDeprec
 FROM Activos AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DEP ON A.IdDep=DEP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS T ON A.IdProv=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN  Trn_AfDetalle AS DP ON A.IdActivo=DP.IdActivo  LEFT JOIN Trn_AfDeprecia  AS D ON  DP.Numero=D.Numero
 LEFT JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 LEFT JOIN Companias AS CN ON DP.IdCia=CN.IdCia
 LEFT JOIN CentroCosto AS CCD ON DP.CdCCosto=CCD.IdCCosto LEFT JOIN Instalaciones AS INS ON DP.CdInstala=INS.IdInstala
 LEFT JOIN Dependencias AS DE ON DP.CdDep=DE.IdDep LEFT JOIN Terceros AS TRD ON DP.NitRespons=TRD.IdTercero
 LEFT JOIN SubCentros AS SCD ON DP.CdSubCos=SCD.IdSubCos
 WHERE A.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 ORDER BY NomActivo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivosLta @pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdInstala VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
,@pmNumEntrada INT=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT A.IdActivo AS CdActivo,NomActivo,NumSerie,CodInvent,CodBarras,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto,A.IdInstala AS CdInstala,Instlacion,A.IdDep AS CdDep,Dependencia
,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,Compania,IdProv,T.RazonSocial AS Proveedor,FecCompra,FecActivacion,NumOrden,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep
,VrMejoras,VrDisminucion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep
,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,FecCreacion,A.IdUsuario AS IdUsuari,Usuario
--datos de anexo
,IdCuenta,IdCueDep,IdCueGasto,IdCueAjuste,IdCueCorr,IdCueAjuDep,IdCueCorrAD
--datos del proveedor
,T.Codigo AS TercCodigo,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigla,T.Direccion AS TercDirecc,T.IdLocal AS CdLocalidad,Localidad,LC.IdDep AS Cd_Depto,Departamento,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM Activos AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS T ON A.IdProv=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal INNER JOIN Departamentos AS D ON LC.IdDep=D.IdDep
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN Activos_Anexo AS AX ON A.IdActivo=AX.IdActivo
 WHERE A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND A.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND  A.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND A.IdDep LIKE ISNULL(@pmIdDep,'%') AND IdRespons LIKE ISNULL(@pmIdRespons,'%')
 AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%')
 AND (FecActivacion>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecActivacion<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (NumEntrada>=ISNULL(@pmNumEntrada,-1)  AND NumEntrada<=ISNULL(@pmNumEntrada,2147483647))
 AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY NomActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivosMar 
 AS 
 SELECT Marca,COUNT(IdActivo)
 FROM Activos
 WHERE IdEstado<>'9999'
 GROUP BY  Marca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivos_Anexo @pmIdActivo VARCHAR(30)
 AS 
 SELECT IdActivo,IdCuenta,IdCueDep,IdCueGasto,IdCueAjuste,IdCueCorr,IdCueAjuDep,IdCueCorrAD,NumPoliza,NitCiaAseg
,FecPoliza,FecVigencia,VrAsegurado,VrCuotaMes,VendPoliza,TeleVend,DiasGarantia,ComentGarantia,Pignorado
,FecIniPignor,FecFinPignor,GarantPignor,Mantenimiento,NumContrato,FecIniCont,FecFinCont,Periodicidad,FecUltMnmto
,NitEmpMnmto,ComentMnmto,DesUbicacion,Comentarios,ComentRetiro,DatosTecnicos
 FROM Activos_Anexo
 WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryActivos_Cr @pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT IdActivo,NomActivo,NumSerie,CodInvent,CodBarras,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto,A.IdInstala AS CdInstala,Instlacion,A.IdDep AS CdDep,Dependencia
,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,Compania,IdProv,T.RazonSocial AS Proveedor,FecCompra,FecActivacion,NumOrden,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep
,VrMejoras,VrDisminucion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable,CueEnGrupo,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep
,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,FecCreacion,A.IdUsuario AS IdUsuari,Usuario
--datos del proveedor
,T.Codigo AS TercCodigo,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigla,T.Direccion AS TercDirecc,T.IdLocal AS CdLocalidad,Localidad,LC.IdDep AS Cd_Depto,Departamento,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM Activos AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS T ON A.IdProv=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal INNER JOIN Departamentos AS D ON LC.IdDep=D.IdDep
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 WHERE A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (FecActivacion>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecActivacion<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY IdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCaja @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 SELECT FechaDia,NumCorte,IdCia,IdUsuario,SaldoAnt,Facturas,DevFacturas,Planillas,DevPlanillas,Recibos,DevRecibos,IngCaja,DevIngCaja
,EgrCaja,DevEgrCaja,Consignac,DevConsig,NotasCalibra,DevCalibra,OtrosIng,OtrosEgr
 FROM AcuCaja
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 ORDER BY IdCia,IdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryAcuCajaBusesSan @pmFecha SMALLDATETIME,@pmIdUsuario VARCHAR(11),@pmIdCia CHAR(2)

AS
 SELECT SUM(SaldoAnterior) AS STotSan,SUM(TotalDebitos) AS STotDeb,SUM(TotalCreditos) AS STotCre,SUM(NuevoSaldo) AS STotSal
 FROM   AcuCajaBuses 
 WHERE  Fecha=@pmFecha AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')  AND IdCia LIKE ISNULL(@pmIdCia,'%%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryAcuCajaBusesTot @pmFechaDia SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT  SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS TotalSaldo
 FROM AcuCajaBuses
 WHERE Fecha=@pmFechaDia  AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCajaTot @pmFechaDia SMALLDATETIME,@pmNumCorte INT
,@pmIdCia CHAR(2)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT  SUM(SaldoAnt+Facturas+Planillas+Recibos+IngCaja+DevEgrCaja+DevConsig+DevCalibra+OtrosIng) AS TotalDebitos
,SUM(DevFacturas+DevPlanillas+DevRecibos+DevIngCaja+EgrCaja+Consignac+NotasCalibra+OtrosEgr) AS TotalCreditos
 FROM AcuCaja
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCaja_Cr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdUsuario VARCHAR(11)=Null,@pmIdGrupo INT=Null
 AS 
 SELECT A.IdUsuario AS CdUsuario,Usuario,A.IdCia AS CdCia,Compania,FechaDia,SaldoAnt
,Facturas,DevFacturas,Planillas,DevPlanillas,Recibos,DevRecibos,IngCaja,DevIngCaja
,EgrCaja,DevEgrCaja,Consignac,DevConsig,NotasCalibra,DevCalibra,OtrosIng,OtrosEgr
,SaldoAnt+(SaldoAnt+Facturas+Planillas+Recibos+IngCaja+DevEgrCaja+DevConsig+DevCalibra+OtrosIng)
-(DevFacturas+DevPlanillas+DevRecibos+DevIngCaja+EgrCaja+Consignac+NotasCalibra+OtrosEgr) AS NuevoSaldo
,IdEstacion,IdGrupo,U.IdCargo AS CdCargo,Cargo,NumCorte
 FROM AcuCaja AS A INNER JOIN  adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia
 LEFT JOIN  Cargos AS C ON U.IdCargo=C.IdCargo
 WHERE FechaDia BETWEEN @pmFechaIni AND @pmFechaFin
 AND A.IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND IdGrupo<=ISNULL(@pmIdGrupo,9999)
 ORDER BY Usuario,A.IdCia,FechaDia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuClientes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
 AS 
 SELECT nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Facturas,DevFacturas,Recibos,DevRecibos,NotasDeb,NotasCre
 FROM AcuClientes
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuClientesCia @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT IdCliente,IdVend,IdCia,SUM((SaldoAnt+Facturas+DevRecibos+NotasDeb)-(DevFacturas+Recibos+NotasCre)) AS NuevoSaldo
 FROM AcuClientes AS A LEFT JOIN TercCliente AS TC ON A.IdCliente=TC.IdClie
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND ISNULL(IdVend,'0') LIKE ISNULL(@pmIdVend,'%')
 GROUP BY IdCliente,IdVend,IdCia
 HAVING SUM((SaldoAnt+Facturas+DevRecibos+NotasDeb)-(DevFacturas+Recibos+NotasCre))<>0
 ORDER BY IdCliente
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuClientesSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT  nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Facturas,DevFacturas,Recibos,DevRecibos
,NotasDeb,NotasCre,((SaldoAnt+Facturas+DevRecibos+NotasDeb)-(DevFacturas+Recibos+NotasCre)) AS NuevoSaldo
 FROM AcuClientes
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCliente,IdAgencia,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuClientesSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT IdCliente,IdVend,IdAgencia,IdCia,SUM((SaldoAnt+Facturas+DevRecibos+NotasDeb)-(DevFacturas+Recibos+NotasCre)) AS NuevoSaldo
 FROM AcuClientes AS A LEFT JOIN TercCliente AS TC ON A.IdCliente=TC.IdClie
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND ISNULL(IdVend,'0') LIKE ISNULL(@pmIdVend,'%')
 GROUP BY IdCliente,IdVend,IdAgencia,IdCia
 HAVING SUM((SaldoAnt+Facturas+DevRecibos+NotasDeb)-(DevFacturas+Recibos+NotasCre))<>0
 ORDER BY IdCliente,IdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentas @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
 AS 
 SELECT nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos
 FROM AcuCuentas
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasAge @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmTipoAcum CHAR(2),@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos
 FROM AcuCuentasAge
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCuenta=@pmIdCuenta
 AND TipoAcum=@pmTipoAcum  AND IdTercero LIKE ISNULL(@pmIdTercero,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasAgeSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,A.IdCuenta AS CdCuenta,IdTercero,CodAgncia,TipoAcum,SaldoAnterior
,TotalDebitos,TotalCreditos,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasAge AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes  AND Tercero<>0
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 ORDER BY A.IdCuenta,IdCia,IdTercero,CodAgncia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasAgeSalTip @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmTipoAcum CHAR(2)=Null
 AS 
 SELECT IdCia,A.IdCuenta AS CdCuenta,IdTercero,CodAgncia,SUM(SaldoAnterior) AS tSaldoAnterior
,SUM(TotalDebitos) AS tTotalDebitos,SUM(TotalCreditos) AS tTotalCreditos
,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
 FROM AcuCuentasAge AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes  AND Tercero<>0 AND TipoAcum=@pmTipoAcum
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 GROUP BY  IdCia,A.IdCuenta,IdTercero,CodAgncia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasAuxItm @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(nReg) AS MXREG
 FROM AcuCuentasAux
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasBG @pmNiv INT,@pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmCue8 VARCHAR(1)=Null,@pmCue9 VARCHAR(1)=Null
AS
  SELECT SUBSTRING(A.IdCuenta,1,@pmNiv) AS CdCuenta,IdCia,SUM(SaldoAnterior) AS tSaldoAnterior,SUM(TotalDebitos) AS tTotalDebitos
 ,SUM(TotalCreditos) AS tTotalCreditos,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
  FROM  AcuCuentas AS A INNER JOIN  Puc AS P ON A.IdCuenta=P.IdCuenta
  WHERE nAnno=@pmnAnno AND nMes BETWEEN @pmnMesIni AND @pmnMesFin
  AND Movimiento<>0 AND SUBSTRING(A.IdCuenta,1,1) IN ('1','2','3',@pmCue8,@pmCue9)
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasCc @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
 AS 
 SELECT nAnno,nMes,IdCia,IdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior,TotalDebitos,TotalCreditos
 FROM AcuCuentasCc
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCuenta=@pmIdCuenta
 AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasCcSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,A.IdCuenta AS CdCuenta,IdCCosto,IdSubCos,TipoAcum,SaldoAnterior
,TotalDebitos,TotalCreditos,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasCc AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND (CentroCosto<>0 OR SubCentro<>0)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 ORDER BY A.IdCuenta,IdCia,IdCCosto,IdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasCcSalc @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT IdCia,A.IdCuenta AS CdCuenta,IdCCosto,SUM(SaldoAnterior) AS tSaldoAnterior
,SUM(TotalDebitos) AS tTotalDebitos,SUM(TotalCreditos) AS tTotalCreditos
,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
 FROM AcuCuentasCc AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND CentroCosto<>0 AND TipoAcum='CC'
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 GROUP BY  IdCia,A.IdCuenta,IdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasCcSalcs @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT IdCia,A.IdCuenta AS CdCuenta,IdCCosto,IdSubCos,SUM(SaldoAnterior) AS tSaldoAnterior
,SUM(TotalDebitos) AS tTotalDebitos,SUM(TotalCreditos) AS tTotalCreditos
,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
 FROM AcuCuentasCc AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND CentroCosto<>0 AND SubCentro<>0  AND TipoAcum='CS'
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 GROUP BY  IdCia,A.IdCuenta,IdCCosto,IdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasGP @pmInc7 CHAR(1),@pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
AS
  SELECT IdCia,SUM(SaldoAnterior) AS tSaldoAnterior,SUM(TotalDebitos) AS tTotalDebitos,SUM(TotalCreditos) AS tTotalCreditos
  ,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
  FROM  AcuCuentas AS A INNER JOIN  Puc AS P ON A.IdCuenta=P.IdCuenta
  WHERE nAnno=@pmnAnno AND nMes BETWEEN @pmnMesIni AND @pmnMesFin
  AND Movimiento<>0 AND SUBSTRING(A.IdCuenta,1,1) IN ('4','5','6',@pmInc7)
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  GROUP BY  IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasMay @pmnAnno INT,@pmnMes INT,@pmNiv INT,@pmIdCia CHAR(2)=Null
 AS 
 SELECT  SUBSTRING(A.IdCuenta,1,@pmNiv) AS CdCuenta,NomCuenta,Naturaleza
,SUM(SaldoAnterior) AS tSaldoAnterior,SUM(TotalDebitos) AS tTotalDebitos ,SUM(TotalCreditos) AS tTotalCreditos
,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
 FROM AcuCuentas AS A INNER JOIN Puc AS P ON SUBSTRING(A.IdCuenta,1,@pmNiv)=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),NomCuenta,Naturaleza

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasMayCia @pmnAnno INT,@pmnMes INT,@pmNiv INT
,@pmIdCia CHAR(2)=Null
 AS 
 SELECT  SUBSTRING(A.IdCuenta,1,@pmNiv) AS CdCuenta,NomCuenta,Naturaleza,A.IdCia AS CdCia,Compania
,SUM(SaldoAnterior) AS tSaldoAnterior,SUM(TotalDebitos) AS tTotalDebitos ,SUM(TotalCreditos) AS tTotalCreditos
,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS NuevoSaldo
 FROM AcuCuentas AS A INNER JOIN Puc AS P ON SUBSTRING(A.IdCuenta,1,@pmNiv)=P.IdCuenta
 INNER JOIN Companias AS C ON A.IdCia=C.IdCia
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes  AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY SUBSTRING(A.IdCuenta,1,@pmNiv),NomCuenta,Naturaleza,A.IdCia,Compania
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasNit @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmIdTercero VARCHAR(16)
 AS 
 SELECT nAnno,nMes,IdCia,IdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos
 FROM AcuCuentasNit
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia
 AND IdCuenta=@pmIdCuenta AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasNitSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,A.IdCuenta AS CdCuenta,IdTercero,SaldoAnterior,TotalDebitos,TotalCreditos
,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasNit AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND Tercero<>0  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 ORDER BY A.IdCuenta,IdCia,IdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdCuenta,SaldoAnterior,TotalDebitos,TotalCreditos
,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentas
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 ORDER BY IdCuenta,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasVeh @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos
 FROM AcuCuentasVeh
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia
 AND IdCuenta=@pmIdCuenta AND IdVehiculo like ISNULL(@pmIdVehiculo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuCuentasVehSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,A.IdCuenta AS CdCuenta,IdVehiculo,SaldoAnterior,TotalDebitos,TotalCreditos
,SaldoAnterior+TotalDebitos-TotalCreditos AS NuevoSaldo
 FROM AcuCuentasVeh AS A INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND Vehiculo<>0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 ORDER BY A.IdCuenta,IdCia,IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvLotes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmNumLote VARCHAR(30)
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdBodega,NumLote,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal
 FROM AcuInvLotes
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdProducto=@pmIdProducto
 AND IdBodega=@pmIdBodega AND NumLote=@pmNumLote

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvLotesSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdBodega,NumLote,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS NuevoCosto
 FROM AcuInvLotes
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY IdProducto,IdBodega,NumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvLotes_Cr @pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmNumLote VARCHAR(30)=Null
 AS 
 SELECT  A.IdProducto AS CdProducto,DescripProd,A.IdCia AS CdCia,Compania,A.IdBodega AS CdBodega,Bodega
,NumLote,nAnno,nMes,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS CostoNuevoSaldo
--datos del producto
,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
,P.IdUnd AS CdUnid,Unidad,ExtciaMin,ExtciaMax,FecUltcom,FecUltVta,Lotes
 FROM AcuInvLotes AS A INNER JOIN ProdMcias  AS P ON A.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON A.IdBodega=B.IdBodega  INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 WHERE nAnno=@pmnAnno AND nMes BETWEEN ISNULL(@pmnMesIni,0) AND ISNULL(@pmnMesFin,2147483647)
 AND A.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND NumLote LIKE ISNULL(@pmNumLote,'%')
 ORDER BY DescripProd,nMes,A.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvTanques @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16),@pmIdTanque VARCHAR(4)
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdTanque,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal
 FROM AcuInvTanques
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdProducto=@pmIdProducto 
AND IdTanque=@pmIdTanque

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvTanquesGru @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT IdTanque,SUM(SaldoAnt) AS tSaldoAnt,SUM(Entradas) AS tEntradas,SUM(Salidas) AS tSalidas
,SUM(SaldoAnt+Entradas-Salidas) AS NuevoSaldo
,SUM(CostoAnt) AS tCostoAnt,SUM(CostoEnt) AS tCostoEnt,SUM(CostoSal) AS tCostoSal
,SUM(CostoAnt+CostoEnt-CostoSal) AS NuevoCosto
 FROM AcuInvTanques
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY IdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvTanquesSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdTanque,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS NuevoCosto
 FROM AcuInvTanques
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY IdProducto,IdTanque

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvTanques_Cr @pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdTanque VARCHAR(4)=Null
 AS 
 SELECT A.IdProducto AS CdProducto,DescripProd,A.IdCia AS CdCia,Compania,A.IdTanque AS CdTanque,Descripcion
,nAnno,nMes,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS CostoNuevoSaldo
--información del tanque
,CapTanq,NivAgua,CapNeta,SaldoActual,TnqGas,TnqUnido,TnqVirtual,NColor
--información de productos
,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
,P.IdUnd AS CdUnid,Unidad,ExtciaMin,ExtciaMax,FecUltcom,FecUltVta
 FROM AcuInvTanques AS A INNER JOIN ProdMcias  AS P ON A.IdProducto=P.IdProducto
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia  INNER JOIN Tanques AS T ON A.IdTanque=T.IdTanque
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 WHERE  nAnno=@pmnAnno AND nMes BETWEEN ISNULL(@pmnMesIni,0) AND ISNULL(@pmnMesFin,2147483647)
 AND A.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdTanque LIKE ISNULL(@pmIdTanque,'%')
 ORDER BY DescripProd,nMes,A.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvUbica @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16),@pmIdUbic VARCHAR(10)
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdUbic,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal
 FROM AcuInvUbica
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia 
AND IdProducto=@pmIdProducto AND IdUbic=@pmIdUbic

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInvUbicaSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdUbic,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS NuevoCosto
 FROM AcuInvUbica
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY IdProducto,IdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInventario @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4)
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdBodega,SaldoAnt,Entradas,Salidas,CostoAnt,CostoEnt,CostoSal
 FROM AcuInventario
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInventarioAju @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
 AS 
 SELECT A.IdProducto AS CdProducto,P.IdBodega AS CdBodega,VrCostPmd,DescripProd,Tanques,SUM(SaldoAnt+Entradas-Salidas) AS NuevoSaldo
,SUM(CostoAnt+CostoEnt-CostoSal) AS NuevoCosto
 FROM AcuInventario AS A INNER JOIN ProdMcias AS P ON A.IdProducto=P.IdProducto
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia
 AND NoAjustes=0 AND TipoRef<>'SERVICIO'
 GROUP BY A.IdProducto,P.IdBodega,VrCostPmd,DescripProd,Tanques
 HAVING SUM(SaldoAnt+Entradas-Salidas)<>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInventarioSal @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT nAnno,nMes,IdCia,IdProducto,IdBodega,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS NuevoCosto
 FROM AcuInventario
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY IdProducto,IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInventarioSanp @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
,@pmIdBodega VARCHAR(4)=Null,@pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmTanques BIT=Null
 AS 
 SELECT A.IdProducto AS CdProducto,DescripProd,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea
,SUM(SaldoAnt+Entradas-Salidas) AS NuevoSaldo,SUM(CostoAnt+CostoEnt-CostoSal) AS CostoTotal
 FROM AcuInventario AS A INNER JOIN ProdMcias  AS PM ON A.IdProducto=PM.IdProducto
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND A.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 AND IdMarca LIKE ISNULL(@pmIdMarca,'%')
 AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
 GROUP BY  A.IdProducto,DescripProd,TipoRef,PM.IdSubgrupo,Subgrupo,S.IdGrupo,Grupo,G.IdLinea,Linea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuInventario_Cr @pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
 AS 
 SELECT A.IdProducto AS CdProducto,DescripProd,A.IdCia AS CdCia,Compania,A.IdBodega AS CdBodega,Bodega
,nAnno,nMes,SaldoAnt,Entradas,Salidas,SaldoAnt+Entradas-Salidas AS NuevoSaldo
,CostoAnt,CostoEnt,CostoSal,CostoAnt+CostoEnt-CostoSal AS CostoNuevoSaldo
--datos del producto
,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
,P.IdUnd AS CdUnid,Unidad,ExtciaMin,ExtciaMax,FecUltcom,FecUltVta
 FROM AcuInventario AS A INNER JOIN ProdMcias  AS P ON A.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON A.IdBodega=B.IdBodega  INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 WHERE  nAnno=@pmnAnno AND nMes BETWEEN ISNULL(@pmnMesIni,0) AND ISNULL(@pmnMesFin,2147483647)
 AND A.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 ORDER BY DescripProd,nMes,A.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAcuPrestamos @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
 AS 
 SELECT nAnno,nMes,IdCia,IdCliente,IdAgencia,SaldoAnt,Prestamos,DevPrestamos,Recibos,DevRecibos
,NotasDeb,NotasCre,Facturas,DevFacturas
 FROM AcuPrestamos
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia 
AND IdCliente=@pmIdCliente AND IdAgencia=@pmIdAgencia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryAcuPropietarios  @pmnMes INT, @pmnAnno INT,@pmIdPropietario VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4), @pmIdCia CHAR(2)
AS
 SELECT IdReg,IdPropietario,IdVehiculo,IdClase, IdCia, nMes, nAnno, SaldoAnterior, TotalDebitos, TotalCreditos
 FROM    AcuPropietarios
 WHERE nMes=@pmnMes AND nAnno=@pmnAnno AND IdPropietario=@pmIdPropietario AND IdVehiculo=@pmIdVehiculo
 AND IdClase=@pmIdClase  AND IdCia=@pmIdCia
 ORDER BY IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryAcuPropietariosCla @pmnAnno INT,@pmnMes INT,@pmIdPropietario VARCHAR(16)=Null
,@pmIdClase VARCHAR(4)=Null, @pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT  P.IdPropietario AS NitPptario,P.IdVehiculo AS PlacaVeh,P.IdClase AS CodClase,V.IdPropietario AS VehNitppt,IdConductor,CASE TipoAfil WHEN 'PROPIO' THEN 1 ELSE 0 END AS VehPropio
,SUM(SaldoAnterior) AS STotSan,SUM(TotalDebitos) AS STotDeb,SUM(TotalCreditos) AS STotCre,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS STotSal
 FROM  AcuPropietarios AS P INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
 WHERE  nAnno=@pmnAnno AND nMes=@pmnMes  AND P.IdPropietario LIKE ISNULL(@pmIdPropietario, '%')
 AND P.IdClase LIKE ISNULL(@pmIdClase,'%') AND IdCia LIKE ISNULL(@pmIdCia ,'%%')
 AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 GROUP BY P.IdPropietario,P.IdVehiculo,P.IdClase,V.IdPropietario,IdConductor,CASE TipoAfil WHEN 'PROPIO' THEN 1 ELSE 0 END
 HAVING SUM(SaldoAnterior+TotalDebitos-TotalCreditos)<>0
 ORDER BY P.IdPropietario,P.IdVehiculo,P.IdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryAcuPropietariosSal @pmnAnno INT,@pmnMes INT=Null,@pmIdPropietario VARCHAR(16)=Null, @pmIdClase VARCHAR(4)=Null
, @pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT  IdPropietario,IdVehiculo,IdClase, IdCia, nMes, SUM(SaldoAnterior) AS STotSan,SUM(TotalDebitos) AS STotDeb,SUM(TotalCreditos) AS STotCre
,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS STotSal
 FROM    AcuPropietarios
 WHERE  nAnno=@pmnAnno AND IdPropietario LIKE ISNULL(@pmIdPropietario, '%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND IdClase LIKE ISNULL(@pmIdClase,'%') AND IdCia LIKE ISNULL(@pmIdCia ,'%%')
 AND (nMes>=ISNULL(@pmnMes,0)  AND nMes<=ISNULL(@pmnMes,2147483647))
 GROUP BY IdPropietario,IdVehiculo,IdClase,IdCia,nMes

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryAcuPropietariosSalCue @pmnAnno INT,@pmnMes INT
,@pmSaldoActual MONEY,@pmIdAdmon VARCHAR(4)
AS
 SELECT V.IdPropietario AS NitPropietario,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS StotSal
 FROM  AcuPropietarios AS A INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND V.IdAdmon<>@pmIdAdmon AND V.TipoAfil<>'PROPIO'
 GROUP BY V.IdPropietario
 HAVING  SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos)<=@pmSaldoActual
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryAcuPropietariosSalPro @pmnAnno INT,@pmnMes INT, @pmIdPropietario VARCHAR(16)
,@pmIdAdmon VARCHAR(4)
AS
 SELECT V.IdVehiculo AS PlacaVeh,V.IdPropietario AS NitPropietario,NumVeh,IdAdmon,TipoAfil
,IdConductor,RazonSocial,IdGrupo,SUM(A.SaldoAnterior+A.TotalDebitos-A.TotalCreditos) AS StotSal
 FROM  AcuPropietarios AS A INNER JOIN Vehiculos AS V ON A.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS T ON V.IdConductor=T.IdTercero
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND V.IdPropietario=@pmIdPropietario
 AND V.IdAdmon<>@pmIdAdmon AND V.TipoAfil<>'PROPIO'
 GROUP BY  V.IdVehiculo,V.IdPropietario,NumVeh,IdAdmon,TipoAfil,IdConductor,RazonSocial,IdGrupo
 ORDER BY  V.IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryAcuPropietariosVeh @pmnAnno INT,@pmnMes INT,@pmIdPropietario VARCHAR(16)=Null
,@pmIdClase VARCHAR(4)=Null, @pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT  P.IdVehiculo AS PlacaVeh,P.IdClase AS CodClase,V.IdPropietario AS VehNitppt,IdConductor,CASE TipoAfil WHEN 'PROPIO' THEN 1 ELSE 0 END AS VehPropio
,SUM(SaldoAnterior) AS STotSan,SUM(TotalDebitos) AS STotDeb,SUM(TotalCreditos) AS STotCre,SUM(SaldoAnterior+TotalDebitos-TotalCreditos) AS STotSal
 FROM  AcuPropietarios AS P INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
 WHERE  nAnno=@pmnAnno AND nMes=@pmnMes  AND P.IdPropietario LIKE ISNULL(@pmIdPropietario, '%')
 AND P.IdClase LIKE ISNULL(@pmIdClase,'%') AND IdCia LIKE ISNULL(@pmIdCia ,'%%')
 AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 GROUP BY P.IdVehiculo,P.IdClase,V.IdPropietario,IdConductor,CASE TipoAfil WHEN 'PROPIO' THEN 1 ELSE 0 END
 HAVING SUM(SaldoAnterior+TotalDebitos-TotalCreditos)<>0
 ORDER BY P.IdVehiculo,P.IdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfAmortiza @pmNumero INT
 AS 
 SELECT Numero,Fecha,Estado,IdUsuario,FechaCrea,IdCia
 FROM Trn_AfAmortiza
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfAmortizaLta  @pmNumeroIni INT,@pmNumeroFin INT
 AS 
 SELECT AMZ.Numero AS NumRegistro,Fecha,AMZ.Estado AS EstadoDoc,AMZ.IdUsuario AS CdUsuario,Usuario,FechaCrea,AMZ.IdCia AS CdCia,CI.Compania AS NomCia
,Item,D.IdDiferido AS CdDiferido,NomDiferido, D.ValorTotal AS ValorInicial, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminucion, VrAcuAjustes, VrAmortizaHis, VrAmortizaMes, VrAjustesInf
, VrAjustesAmtza,VrAmtzaHisAcum, VrAmortizaAcum, VrAjuAmtzaAcum, VrCorrMonAcum, VrBaseCorrMon, VrCorrMonDif, TarifaAju
,D.IdCia AS DetIdCia,CID.Compania AS DetNomCia,CdCCosto,CCosto,CdSubCos,SubCosto, CdDep, Dependencia,NitRespons,RazonSocial,FecInicioAmtza, NPeriodos
--datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS DifCdCentro,A.IdSubCos AS DifCdSubcentro
,A.IdDep AS DifCdDep,DesUbicacion,IdRespons,A.IdCia AS DifIdCia,FecInicio,A.ValorTotal AS DifValTotal,A.VrAdiciones AS DifValAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS EstadoDif
,Comentarios,A.FecCreacion AS DifFecCrea,VentaGal,CuotaGal,VrInversion,NumPlaca,CodInstala,Instlacion
 FROM Trn_AfAmortiza AS AMZ INNER JOIN Trn_AfAmtDetalle AS D ON AMZ.Numero=D.Numero
 INNER JOIN Companias AS CI ON AMZ.IdCia=CI.IdCia  INNER JOIN adm_Usuarios AS U ON AMZ.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido  INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif  INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN Companias AS CID ON D.IdCia=CID.IdCia 
 INNER JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto 
 INNER JOIN Dependencias AS DP ON D.CdDep=DP.IdDep INNER JOIN Terceros AS T ON D.NitRespons=T.IdTercero
 LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos  LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE  AMZ.Numero BETWEEN @pmNumeroIni  AND @pmNumeroFin
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfAmortizaRes @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdLinea VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdCia CHAR(2)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null
,@pmIdDiferido VARCHAR(30)
 AS 
 SELECT D.IdDiferido AS CdDiferido,NomDiferido,AMZ.Numero AS NumRegistro,Fecha,AMZ.Estado AS EstadoDoc,AMZ.IdUsuario AS CdUsuario,Usuario,FechaCrea,AMZ.IdCia AS CdCia,CI.Compania AS NomCia
,Item,D.ValorTotal AS ValorInicial, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminucion, VrAcuAjustes, VrAmortizaHis, VrAmortizaMes, VrAjustesInf
, VrAjustesAmtza,VrAmtzaHisAcum, VrAmortizaAcum, VrAjuAmtzaAcum, VrCorrMonAcum, VrBaseCorrMon, VrCorrMonDif, TarifaAju
,D.IdCia AS DetIdCia,CID.Compania AS DetNomCia,CdCCosto,CCD.CCosto AS NomCentroCosto,CdSubCos,SCD.SubCosto AS NomSubcentro, CdDep, DPD.Dependencia AS NomDepend
,NitRespons,TRD.RazonSocial AS DetResponsable,FecInicioAmtza, NPeriodos,VentaGal,CuotaGal,VrInversion
--datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro
,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro,A.IdDep AS CdDep,DP.Dependencia AS DifDependencia,DesUbicacion
,IdRespons,T.RazonSocial AS NomResponsable,A.IdCia AS CdCia,CIA.Compania AS DifNomCia,FecInicio,A.ValorTotal AS DifValTotal,A.VrAdiciones AS DifAdiciones,A.VrDisminucion AS DifDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS DifEstado
,FecRetiro,A.IdCausal AS CdCausal,Causal,ComentRetiro,Comentarios,NumPlaca,CodInstala,Instlacion,FecCreacion,A.IdUsuario AS IdUsuari
--DATOS DEL DIFERIDO
 FROM Trn_AfAmtDetalle AS D INNER JOIN Trn_AfAmortiza AS  AMZ  ON D.Numero=AMZ.Numero  INNER JOIN Companias AS CI ON AMZ.IdCia=CI.IdCia
 INNER JOIN adm_Usuarios AS U ON AMZ.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos  AS A ON D.IdDiferido=A.IdDiferido  INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN Companias AS CIA ON A.IdCia=CIA.IdCia 
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado   INNER JOIN Companias AS CID ON D.IdCia=CID.IdCia 
 INNER JOIN CentroCosto AS CCD ON D.CdCCosto=CCD.IdCCosto 
 INNER JOIN Dependencias AS DPD ON D.CdDep=DPD.IdDep INNER JOIN Terceros AS TRD ON D.NitRespons=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN SubCentros AS SCD ON D.CdSubCos=SCD.IdSubCos   LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE Fecha BETWEEN  @pmFechaIni  AND @pmFechaFin AND AMZ.Estado<>0 AND D.IdDiferido LIKE ISNULL(@pmIdDiferido ,'%')
 AND AMZ.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND  G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')  
 AND D.CdDep LIKE ISNULL(@pmIdDep,'%')  AND D.NitRespons LIKE ISNULL(@pmIdRespons,'%')  AND D.CdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY AMZ.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfCausales @pmIdCausal VARCHAR(4)
 AS 
 IF  @pmIdCausal IS NULL
 SELECT IdCausal,Causal
 FROM AfCausales
 WHERE Inactivo=0
 ORDER BY IdCausal
 ELSE
 SELECT IdCausal,Causal,Inactivo
 FROM AfCausales
 WHERE IdCausal=@pmIdCausal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfClasesCue @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,ClaseCue,Concepto
 FROM AfClasesCue
 WHERE  Inactivo=0
 ORDER BY IdClase
 ELSE
 SELECT IdClase,ClaseCue,Concepto,Inactivo
 FROM AfClasesCue
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDeprecia @pmNumero INT
 AS 
 SELECT Numero,Fecha,Estado,IdUsuario,FechaCrea,IdCia
 FROM Trn_AfDeprecia
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDepreciaLta @pmNumeroIni INT,@pmNumeroFin INT
 AS 
 SELECT D.Numero AS NumDeprecia,Fecha,D.Estado AS EstadoDoc,D.IdUsuario AS CdUsuario,Usuario,FechaCrea,D.IdCia AS CdCia
,Item,DP.IdActivo AS CdActivo,NomActivo,VrCostoIni,DP.VrMejoras AS ValMejoras,DP.VrDisminucion AS ValDisminuc,VrAcuAjustes,VrDepreciaHis,VrDepreciaMes,VrAjustesInf,VrAjustesDep,VrDepreciaAcum
,VrAjuDepAcum,TarifaAju,DP.IdCia AS DetCodCia,Compania,CdCCosto,CCosto,CdSubCos,SubCosto,CdInstala,Instlacion,CdDep,Dependencia,NitRespons,RazonSocial,FecIniDep,MesesVida,MetodoDeprec
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,Marca,Modelo,NumPlaca,Referencia,A.IdEstado AS CdEstado,ED.Estado AS NomEstado
,FecActivacion,FecFinDep,Ajustable,Contable
 FROM Trn_AfDeprecia AS D INNER JOIN  Trn_AfDetalle AS DP ON D.Numero=DP.Numero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON DP.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado INNER JOIN Companias AS CN ON DP.IdCia=CN.IdCia
 INNER JOIN CentroCosto AS CC ON DP.CdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON DP.CdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DE ON DP.CdDep=DE.IdDep INNER JOIN Terceros AS TR ON DP.NitRespons=TR.IdTercero
 LEFT JOIN SubCentros AS SC ON DP.CdSubCos=SC.IdSubCos
 WHERE D.Numero BETWEEN @pmNumeroIni  AND  @pmNumeroFin
 ORDER BY D.Numero,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDepreciaRes  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdLinea VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdCia CHAR(2)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null
,@pmIdActivo VARCHAR(30)=Null
 AS 
 SELECT DP.IdActivo AS CdActivo,NomActivo,Fecha,D.Numero AS NumDeprecia,D.IdUsuario AS CdUsuario,Usuario,FechaCrea,D.IdCia AS DepCdCia
,Item,VrCostoIni,DP.VrMejoras AS ValMejoras,DP.VrDisminucion AS ValDisminuc,VrAcuAjustes,VrDepreciaHis,VrDepreciaMes,VrAjustesInf,VrAjustesDep,VrDepreciaAcum
,VrAjuDepAcum,TarifaAju,DP.IdCia AS DetCodCia,CN.Compania AS DetCompania,CdCCosto,CCD.CCosto AS DetCentroCosto,CdSubCos,SCD.SubCosto AS DetSubCentro,CdInstala,INS.Instlacion AS DetInstacion
,CdDep,DE.Dependencia AS DetDependencia,NitRespons,TRD.RazonSocial AS DetNomResponsable,FecIniDep,MesesVida,MetodoDeprec
--datos del activo
,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CC.CCosto AS ActCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS ActSubCentro,A.IdInstala AS CdInstala,I.Instlacion AS ActInstalacion
,A.IdDep AS CdDep,DEP.Dependencia AS NomDependencia,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,CI.Compania AS NomCia,IdProv,T.RazonSocial AS Proveedor
,FecCompra,FecActivacion,NumOrden,IdCiaOrden,NumEntrada,IdCiaEnt,VrCompra,VrComercial,VrBaseDep,A.VrMejoras AS TotMejoras,A.VrDisminucion AS TotDisminucion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep
,Ajustable,Contable,CueEnGrupo,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju,VrAjuInfDep,NumMesesDep,FecUltDep,Marca,Modelo,NumPlaca,NumDoc,Referencia,NumSerie,CodInvent,CodBarras
,A.IdEstado AS CdEstado,ED.Estado AS ActEstado,FecRetiro,A.IdCausal AS CdCausal,Causal,VrVenta,FecCreacion
 FROM  Trn_AfDetalle AS DP  INNER JOIN Trn_AfDeprecia  AS D ON  DP.Numero=D.Numero INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON DP.IdActivo=A.IdActivo  INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DEP ON A.IdDep=DEP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS T ON A.IdProv=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN Companias AS CN ON DP.IdCia=CN.IdCia
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN CentroCosto AS CCD ON DP.CdCCosto=CCD.IdCCosto LEFT JOIN Instalaciones AS INS ON DP.CdInstala=INS.IdInstala
 LEFT JOIN Dependencias AS DE ON DP.CdDep=DE.IdDep LEFT JOIN Terceros AS TRD ON DP.NitRespons=TRD.IdTercero
 LEFT JOIN SubCentros AS SCD ON DP.CdSubCos=SCD.IdSubCos
 WHERE  D.Fecha BETWEEN  @pmFechaIni  AND @pmFechaFin AND D.Estado<>0  AND DP.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 AND DP.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND  G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')  
 AND DP.CdInstala LIKE ISNULL(@pmIdInstala,'%') AND DP.CdDep LIKE ISNULL(@pmIdDep,'%')
 AND DP.NitRespons LIKE ISNULL(@pmIdRespons,'%')  AND DP.CdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 ORDER BY NomActivo,Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifIngresoCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDiferido VARCHAR(30)=Null
AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
, Item,D.IdDiferido AS CdDiferido,NomDiferido,FecActivacion, D.NumPeriodos AS NumMeses, VrBase, VrMejoras, D.VrDisminucion AS ValDisminuc, D.VrAjuInfAcum AS ValAjuInfAcum, D.VrAmortizaHis AS ValAmzaHis
, VrAmortizaAju,D.VrAjuInfAmtza AS ValAjuInflacAmza, NumMesesAmtza, VrCorrMonDif, NumMesesCorr, D.FecUltAmtza AS FecUltAmortza, Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,RazonSocial,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,NumPlaca,CodInstala,Instlacion,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifIngreso AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos   LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND D.IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY M.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifIngresoLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT
,@pmIdCia CHAR(2)
AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
, Item,D.IdDiferido AS CdDiferido,NomDiferido,FecActivacion, D.NumPeriodos AS NumMeses, VrBase, VrMejoras, D.VrDisminucion AS ValDisminuc, D.VrAjuInfAcum AS ValAjuInfAcum, D.VrAmortizaHis AS ValAmzaHis
, VrAmortizaAju,D.VrAjuInfAmtza AS ValAjuInflacAmza, NumMesesAmtza, VrCorrMonDif, NumMesesCorr, D.FecUltAmtza AS FecUltAmortza, Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,RazonSocial,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,NumPlaca,CodInstala,Instlacion,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifIngreso AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos  LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni  AND @pmNumeroFin  AND M.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifMovi @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Fecha,Anulado,Observacion,TipCom,Comprobante,IdCiaCom,FecCreacion,IdUsuario
 FROM Trn_AfDifMovi
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifRetiroCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDiferido VARCHAR(30)=Null
AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumRetiro,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,D.IdCausal AS CdCausal,Causal, VrCostoIni, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminuciones, VrAcuAjustes, VrAmortizaAcum, VrAjuAmtzaAcum, FechaRet, Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,RazonSocial,A.IdCia AS DifIdCia,FecInicio,ValorTotal,A.VrAdiciones AS DifVrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea,FecRetiro,ComentRetiro
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifRetiro AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN AfCausales AS CR ON D.IdCausal=CR.IdCausal
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos 
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND D.IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY M.Numero,NomDiferido

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifRetiroLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT
,@pmIdCia CHAR(2)
AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumRetiro,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,D.IdCausal AS CdCausal,Causal, VrCostoIni, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminuciones, VrAcuAjustes, VrAmortizaAcum, VrAjuAmtzaAcum, FechaRet, Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,RazonSocial,A.IdCia AS DifIdCia,FecInicio,ValorTotal,A.VrAdiciones AS DifVrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea,FecRetiro,ComentRetiro
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifRetiro AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN AfCausales AS CR ON D.IdCausal=CR.IdCausal
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos 
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni  AND @pmNumeroFin  AND M.IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifTraslado @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Item,IdDiferido,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,Ubicacion,CdCiaDest
,CdGrupoDest,CdCCostoDest,CdSubCosDest,CdDepDest,CdResponsDest,UbicacionDest,Concepto
 FROM Trn_AfDifTraslado
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifTrasladoCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDiferido VARCHAR(30)=Null
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumTraslado,M.IdCia AS CdCia,CI.Compania AS NomCia,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,D.IdGrupo AS CdGrupoOri,GOR.Grupo AS GrupoOrigen,D.IdCCosto AS CdCentroOrig,CCO.CCosto AS CentroOrigen,D.IdSubCos AS CdSubCosOrig,SCO.SubCosto AS SubCentroOrig
,D.IdDep AS CdDepOrig,DPO.Dependencia AS DepOrigen,D.IdRespons AS IdResponsOrig,TRO.RazonSocial AS ResponsOrigen,Ubicacion,CdCiaDest,CND.Compania  AS CiaDestino,CdGrupoDest,GDT.Grupo AS NomGrupoDestino,CdCCostoDest,CCD.CCosto AS CentroDestino
,CdSubCosDest,SCD.SubCosto AS SubCenDestino,CdDepDest,DPD.Dependencia AS DepDestino,CdResponsDest,TRD.RazonSocial AS NomRespDestino,UbicacionDest,Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,L.Linea AS LineaDif,A.IdGrupo AS CodGrupo,G.Grupo AS DifGrupo,A.IdCCosto AS CdCentro,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro
,A.IdDep AS CdDep,DP.Dependencia AS DependDif,DesUbicacion,A.IdRespons AS DifNitRespons,T.RazonSocial AS NombreRespons,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado,NumPlaca,CodInstala,Instlacion
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifTraslado AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN CentroCosto AS CCO ON D.IdCCosto=CCO.IdCCosto  INNER JOIN Dependencias AS DPO ON D.IdDep=DPO.IdDep INNER JOIN Terceros AS TRO ON D.IdRespons=TRO.IdTercero
 INNER JOIN AfGrupos AS  GOR ON D.IdGrupo=GOR.IdGrupo INNER JOIN AfLineas AS LOR ON GOR.IdLinea=LOR.IdLinea
 LEFT JOIN   Companias AS CND ON D.CdCiaDest=CND.IdCia LEFT JOIN AfGrupos AS GDT ON D.CdGrupoDest=GDT.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON D.CdCCostoDest=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON D.CdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON D.CdResponsDest=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN SubCentros AS SCO ON D.IdSubCos=SCO.IdSubCos
 LEFT JOIN SubCentros AS SCD ON D.CdSubCosDest=SCD.IdSubCos LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND D.IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY M.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifTrasladoLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumTraslado,M.IdCia AS CdCia,CI.Compania AS NomCia,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,D.IdGrupo AS CdGrupoOri,GOR.Grupo AS GrupoOrigen,D.IdCCosto AS CdCentroOrig,CCO.CCosto AS CentroOrigen,D.IdSubCos AS CdSubCosOrig,SCO.SubCosto AS SubCentroOrig
,D.IdDep AS CdDepOrig,DPO.Dependencia AS DepOrigen,D.IdRespons AS IdResponsOrig,TRO.RazonSocial AS ResponsOrigen,Ubicacion,CdCiaDest,CND.Compania  AS CiaDestino,CdGrupoDest,GDT.Grupo AS NomGrupoDestino,CdCCostoDest,CCD.CCosto AS CentroDestino
,CdSubCosDest,SCD.SubCosto AS SubCenDestino,CdDepDest,DPD.Dependencia AS DepDestino,CdResponsDest,TRD.RazonSocial AS NomRespDestino,UbicacionDest,Concepto
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,L.Linea AS LineaDif,A.IdGrupo AS CodGrupo,G.Grupo AS DifGrupo,A.IdCCosto AS CdCentro,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro
,A.IdDep AS CdDep,DP.Dependencia AS DependDif,DesUbicacion,A.IdRespons AS DifNitRespons,T.RazonSocial AS NombreRespons,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,NumPlaca,CodInstala,Instlacion,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifTraslado AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN CentroCosto AS CCO ON D.IdCCosto=CCO.IdCCosto  INNER JOIN Dependencias AS DPO ON D.IdDep=DPO.IdDep INNER JOIN Terceros AS TRO ON D.IdRespons=TRO.IdTercero
 INNER JOIN AfGrupos AS  GOR ON D.IdGrupo=GOR.IdGrupo INNER JOIN AfLineas AS LOR ON GOR.IdLinea=LOR.IdLinea
 LEFT JOIN   Companias AS CND ON D.CdCiaDest=CND.IdCia LEFT JOIN AfGrupos AS GDT ON D.CdGrupoDest=GDT.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON D.CdCCostoDest=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON D.CdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON D.CdResponsDest=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN SubCentros AS SCO ON D.IdSubCos=SCO.IdSubCos
 LEFT JOIN SubCentros AS SCD ON D.CdSubCosDest=SCD.IdSubCos LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin  AND M.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifValoriza @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Item,IdDiferido,Concepto,Valor,NitProvee
 FROM Trn_AfDifValoriza
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifValorizaCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2),@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipDif VARCHAR(4)=Null,@pmIdDiferido VARCHAR(30)=Null
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NroValoriza,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,Concepto,Valor,NitProvee,PV.RazonSocial AS Proveedor
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,T.RazonSocial AS NomResponsable,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifValoriza AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN Terceros AS PV ON D.NitProvee=PV.IdTercero
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND D.IdDiferido LIKE ISNULL(@pmIdDiferido,'%')
 AND A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 ORDER BY M.Numero,NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfDifValorizaLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NroValoriza,M.IdCia AS CdCia,Compania,Fecha,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,D.IdDiferido AS CdDiferido,NomDiferido,Concepto,Valor,NitProvee,PV.RazonSocial AS Proveedor
--Datos del diferido
,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,T.RazonSocial AS Responsable,A.IdCia AS DifIdCia,FecInicio,ValorTotal,VrAdiciones,A.VrDisminucion AS DifValDisminuc,A.NumPeriodos AS DifNumMeses
,FecFinAmtza,Ajustable,Contable,A.VrAjuInfAcum AS DifAjusInfAcum,A.VrAmtzaAcuHis AS DifAmtzaAcuHis,VrAmtzaAcum,A.VrAjuInfAmtza AS DifAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,A.FecUltAmtza AS DifFecUltAmza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado
,Comentarios,A.FecCreacion AS DifFecCrea
 FROM Trn_AfDifMovi AS M INNER JOIN Trn_AfDifValoriza AS D ON M. IdMov=D.IdMov AND M.Numero=D.Numero AND M.IdCia=D.IdCia
 INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov  INNER JOIN Companias AS CI ON M.IdCia=CI.IdCia 
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Diferidos AS A ON D.IdDiferido=A.IdDiferido INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos  LEFT JOIN Terceros AS PV ON D.NitProvee=PV.IdTercero
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin AND  M.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfEstados @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
 SELECT IdEstado,Estado,NColor
 FROM AfEstados
 WHERE Inactivo=0
 ORDER BY IdEstado
 ELSE
 SELECT IdEstado,Estado,NColor,Inactivo
 FROM AfEstados
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfGrupos @pmIdGrupo VARCHAR(4)
 AS 
 SELECT IdGrupo,Grupo,IdLinea,VidaUtil,Inactivo
 FROM AfGrupos
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfGruposCue @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdGrupo,IdClase,IdCuenta
 FROM AfGruposCue
 WHERE IdGrupo=@pmIdGrupo AND IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfGruposCueLta @pmIdGrupo VARCHAR(4)=Null
 AS 
 SELECT GC.IdGrupo AS CdGrupo,Grupo,GC.IdClase AS CdClase,ClaseCue,GC.IdCuenta AS CdCuenta,NomCuenta
 FROM AfGruposCue AS GC INNER JOIN AfGrupos AS G ON GC.IdGrupo=G.IdGrupo
 INNER JOIN AfClasesCue AS C ON GC.IdClase=C.IdClase
 LEFT JOIN   Puc AS P ON GC.IdCuenta=P.IdCuenta
 WHERE G.Inactivo=0 AND GC.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY Grupo,GC.IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfGruposDso @pmIdLinea VARCHAR(4)=Null
 AS 
 SELECT IdGrupo,Grupo,Grupo+' '+IdGrupo+' ('+Linea+')' AS DsGru
 FROM AfGrupos AS G INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 WHERE G.Inactivo=0 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 ORDER BY G.IdLinea,Grupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfGruposLin @pmIdGrupo VARCHAR(4)
 AS 
 SELECT IdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,G.VidaUtil AS Vida_Util,G.Inactivo AS GruInactivo
 FROM AfGrupos AS G INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfGruposLta @pmIdLinea VARCHAR(4)=Null
 AS 
 SELECT IdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,G.VidaUtil AS Vida_Util
 FROM AfGrupos AS G INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 WHERE G.Inactivo=0 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 ORDER BY G.IdLinea,Grupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfLineas @pmIdLinea VARCHAR(4)
 AS 
 IF @pmIdLinea IS NULL
  SELECT IdLinea,Linea,VidaUtil,TarfDep
  FROM AfLineas
  WHERE Inactivo=0
  ORDER BY  IdLinea
 ELSE
  SELECT IdLinea,Linea,VidaUtil,TarfDep,Inactivo
  FROM AfLineas
  WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfLineasDso 
 AS 
 SELECT IdLinea,Linea,IdLinea+' '+Linea AS DsLin
 FROM AfLineas
 WHERE Inactivo=0 
 ORDER BY IdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMantnmto @pmNumero INT
 AS 
 SELECT Numero,IdCia,IdActivo,Fecha,NumContrato,NitEmpresa,NomEmpresa,FinContrato,IdUsuario,Diagnostico,Comentarios
 FROM Trn_AfMantnmto
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMantnmtoCr @pmNumeroIni INT,@pmNumeroFin INT
AS
 SELECT Numero,M.IdCia AS CdCia,CM.Compania AS CiaMnmto,Fecha,M.IdActivo AS CdActivo,NomActivo,M.NumContrato AS Num_Contrato,NitEmpresa,NomEmpresa
,FinContrato,Diagnostico,M.Comentarios,M.IdUsuario AS CdUsuario,Usuario
,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdInstala AS CdInstala,Instlacion,A.IdDep AS CdDep,Dependencia,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,CI.Compania AS CiaAfijo
,VrBaseDep,VrMejoras,VrDisminucion,FecActivacion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable
,A.IdEstado AS CdEstado,Estado,FecRetiro
,Mantenimiento,AX.NumContrato AS NContratoActual,FecIniCont,FecFinCont,Periodicidad,FecUltMnmto,NitEmpMnmto,ComentMnmto
 FROM Trn_AfMantnmto AS M INNER JOIN Activos AS A ON M.IdActivo=A.IdActivo INNER JOIN Companias AS CM ON M.IdCia=CM.IdCia
 INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario  INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 LEFT JOIN Activos_Anexo AS AX ON M.IdActivo=AX.IdActivo
 WHERE  Numero BETWEEN @pmNumeroIni  AND @pmNumeroFin
 ORDER BY  Numero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMantnmtoLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmNumeroIni INT,@pmNumeroFin INT,@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdActivo VARCHAR(30)=Null
AS
 SELECT Numero,M.IdCia AS CdCia,CM.Compania AS CiaMnmto,Fecha,M.IdActivo AS CdActivo,NomActivo,M.NumContrato AS Num_Contrato,NitEmpresa,NomEmpresa
,FinContrato,Diagnostico,M.Comentarios,M.IdUsuario AS CdUsuario,Usuario
,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdInstala AS CdInstala,Instlacion,A.IdDep AS CdDep,Dependencia,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,CI.Compania AS CiaAfijo
,VrBaseDep,VrMejoras,VrDisminucion,FecActivacion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable
,A.IdEstado AS CdEstado,Estado,FecRetiro
,Mantenimiento,AX.NumContrato AS NContratoActual,FecIniCont,FecFinCont,Periodicidad,FecUltMnmto,NitEmpMnmto,ComentMnmto
 FROM Trn_AfMantnmto AS M INNER JOIN Activos AS A ON M.IdActivo=A.IdActivo INNER JOIN Companias AS CM ON M.IdCia=CM.IdCia
 INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario  INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 LEFT JOIN Activos_Anexo AS AX ON M.IdActivo=AX.IdActivo
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND  A.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND M.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 AND (Numero>=ISNULL(@pmNumeroIni,-1)  AND Numero<=ISNULL(@pmNumeroFin,2147483647))
 ORDER BY  Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovIngreso @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT IdMov,Numero,IdCia,Item,IdActivo,FecActivacion,VrMejoras,VrDisminucion,VrAjuInfAcum,VrDepAcuHis,VrDepAcuAju
,VrAjuInfDep,NumMesesDep,FecUltDep,Concepto
 FROM Trn_AfMovIngreso
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovIngresoCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdActivo VARCHAR(30)=Null
AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
--datos del ingreso
,Item,I.IdActivo AS CdActivo,NomActivo,I.FecActivacion AS FecIngreso,VrBaseDep,I.VrMejoras AS ValMejoras,I.VrDisminucion AS ValDisminucion,I.VrAjuInfAcum AS ValAjuInf
,I.VrDepAcuHis AS ValDepHis,I.VrDepAcuAju AS ValDepAcumAju,I.VrAjuInfDep AS ValAjuInfDep,I.NumMesesDep AS NumMeses,I.FecUltDep AS FechaUltDep,Concepto
,A.VidaUtil AS MesesVidaUtil,FecFinDep,MetodoDep,Ajustable,Contable
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubCentro,SubCosto,A.IdInstala AS CdInstala,Instlacion
,A.IdDep AS CdDep,Dependencia,Marca,Modelo,NumPlaca
,IdRespons,TR.RazonSocial AS Responsable,IdProv,TP.RazonSocial AS Proveedor,FecCompra,NumOrden,NumEntrada,A.IdEstado AS CdEstado,Estado
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovIngreso AS I ON M.IdMov=I.IdMov AND M.Numero=I.Numero AND M.IdCia=I.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON I.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN Terceros AS TP ON A.IdProv=TP.IdTercero INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND I.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 ORDER BY M.Numero,NomActivo


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovIngresoLta @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
--datos del ingreso
,Item,I.IdActivo AS CdActivo,NomActivo,I.FecActivacion AS FecIngreso,VrBaseDep,I.VrMejoras AS ValMejoras,I.VrDisminucion AS ValDisminucion,I.VrAjuInfAcum AS ValAjuInf
,I.VrDepAcuHis AS ValDepHis,I.VrDepAcuAju AS ValDepAcumAju,I.VrAjuInfDep AS ValAjuInfDep,I.NumMesesDep AS NumMeses,I.FecUltDep AS FechaUltDep,Concepto
,A.VidaUtil AS MesesVidaUtil,FecFinDep,MetodoDep,Ajustable,Contable
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubCentro,SubCosto,A.IdInstala AS CdInstala,Instlacion
,A.IdDep AS CdDep,Dependencia,Marca,Modelo,NumPlaca
,IdRespons,TR.RazonSocial AS Responsable,IdProv,TP.RazonSocial AS Proveedor,FecCompra,NumOrden,NumEntrada,A.IdEstado AS CdEstado,Estado
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovIngreso AS I ON M.IdMov=I.IdMov AND M.Numero=I.Numero AND M.IdCia=I.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON I.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN Terceros AS TP ON A.IdProv=TP.IdTercero INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE M.IdMov=@pmIdMov AND M.Numero=@pmNumero AND M.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovRetiroCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdActivo VARCHAR(30)=Null
 AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumRetiro,M.IdCia AS CdCia,Compania,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,R.IdActivo AS CdActivo,NomActivo,R.IdCausal AS CdCausal,Causal,NitTercero, NomTercero, VrCostoIni, R.VrMejoras AS ValMejoras,R.VrDisminucion AS ValDisminuc, VrAcuAjustes, VrDepAcum
,VrAjuDepAcum, VrIngreso,FechaRet, IdCueDeb, IdCueIng, Concepto
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubCentro,SubCosto,A.IdInstala AS CdInstala,Instlacion
,A.IdDep AS CdDep,Dependencia,Marca,Modelo,NumPlaca,Referencia,IdRespons,TR.RazonSocial AS Responsable
,FecCompra,NumOrden,NumEntrada,FecActivacion,VrBaseDep,A.VrMejoras AS TotMejoras,A.VrDisminucion AS TotDisminuc,A.VidaUtil AS MesesVida,FecFinDep,MetodoDep,Ajustable,Contable,FecUltDep
,A.IdEstado AS CdEstado,Estado
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovRetiro AS R ON M.IdMov=R.IdMov AND M.Numero=R.Numero AND M.IdCia=R.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN AfCausales AS CR ON R.IdCausal=CR.IdCausal
 INNER JOIN Activos AS A ON R.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND R.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 ORDER BY M.Numero,NomActivo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovRetiroLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
 AS
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumRetiro,M.IdCia AS CdCia,Compania,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
,Item,R.IdActivo AS CdActivo,NomActivo,R.IdCausal AS CdCausal,Causal,NitTercero, NomTercero, VrCostoIni, R.VrMejoras AS ValMejoras,R.VrDisminucion AS ValDisminuc, VrAcuAjustes, VrDepAcum
,VrAjuDepAcum, VrIngreso,FechaRet, IdCueDeb, IdCueIng, Concepto
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubCentro,SubCosto,A.IdInstala AS CdInstala,Instlacion
,A.IdDep AS CdDep,Dependencia,Marca,Modelo,NumPlaca,Referencia,IdRespons,TR.RazonSocial AS Responsable
,FecCompra,NumOrden,NumEntrada,FecActivacion,VrBaseDep,A.VrMejoras AS TotMejoras,A.VrDisminucion AS TotDisminuc,A.VidaUtil AS MesesVida,FecFinDep,MetodoDep,Ajustable,Contable,FecUltDep
,A.IdEstado AS CdEstado,Estado
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovRetiro AS R ON M.IdMov=R.IdMov AND M.Numero=R.Numero AND M.IdCia=R.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario INNER JOIN AfCausales AS CR ON R.IdCausal=CR.IdCausal
 INNER JOIN Activos AS A ON R.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin AND M.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovTraslado @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Item,IdActivo,IdGrupo,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons,CdCiaDest
,CdGrupoDest,CdCCostoDest,CdSubCosDest,CdInstalaDest,CdDepDest,CdResponsDest,Concepto,Ubicacion,UbicacionDest
 FROM Trn_AfMovTraslado
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovTrasladoCr  @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdActivo VARCHAR(30)=Null
AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumTraslado,M.IdCia AS CdCia,CN.Compania AS CiaOrigen,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
--datos del traslado
,Item,T.IdActivo AS CdActivo,NomActivo,T.IdGrupo AS CdGrupoOri,GOR.Grupo AS GrupoOrigen,GOR.IdLinea AS CdLineaOri,LOR.Linea AS LineaOrigen,T.IdCCosto AS CdCentroOrigen,CCO.CCosto AS CentroOrigen
,T.IdSubCos AS CdSubCosOrig,SCO.SubCosto AS SubCentroOrig,T.IdInstala AS CdInsOrigen,ITO.Instlacion AS InsOrigen,T.IdDep AS CdDepOrig,DPO.Dependencia AS DepOrigen,T.IdRespons AS NitRespOrigen,TRO.RazonSocial AS ResponsOrigen
,CdCiaDest,CND.Compania  AS CiaDestino,CdGrupoDest,GDT.Grupo AS NomGrupoDestino,CdCCostoDest,CCD.CCosto AS CentroDestino,CdSubCosDest,SCD.SubCosto AS SubCenDestino,CdInstalaDest
,IDT.Instlacion AS InsDestino,CdDepDest,DPD.Dependencia AS DepDestino,CdResponsDest,TRD.RazonSocial AS NomRespDestino,Concepto,Ubicacion,UbicacionDest
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,G.Grupo AS ActGrupo,G.IdLinea AS CdLinea,L.Linea AS ActLinea,A.IdCCosto AS CdCentro,CC.CCosto AS ActivoCentro,A.IdSubCos AS CdSubCentro,SC.SubCosto AS ActSubcentro,A.IdInstala AS CdInstala,INS.Instlacion AS ActInstalacion
,A.IdDep AS CdDep,DP.Dependencia AS ActDepend,Marca,Modelo,NumPlaca,Referencia,FecActivacion,A.VidaUtil,FecFinDep,MetodoDep,FecUltDep
,A.IdRespons AS NitReponsb,TR.RazonSocial AS Responsable,A.IdEstado AS CdEstado,Estado
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovTraslado AS T ON M.IdMov=T.IdMov AND M.Numero=T.Numero AND M.IdCia=T.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON T.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN CentroCosto AS CCO ON T.IdCCosto=CCO.IdCCosto INNER JOIN Instalaciones AS ITO ON T.IdInstala=ITO.IdInstala
 INNER JOIN Dependencias AS DPO ON T.IdDep=DPO.IdDep INNER JOIN Terceros AS TRO ON T.IdRespons=TRO.IdTercero
 INNER JOIN AfGrupos AS  GOR ON T.IdGrupo=GOR.IdGrupo INNER JOIN AfLineas AS LOR ON GOR.IdLinea=LOR.IdLinea
 LEFT JOIN   Companias AS CND ON T.CdCiaDest=CND.IdCia LEFT JOIN AfGrupos AS GDT ON T.CdGrupoDest=GDT.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON T.CdCCostoDest=CCD.IdCCosto LEFT JOIN Instalaciones AS IDT ON T.CdInstalaDest=IDT.IdInstala
 LEFT JOIN Dependencias AS DPD ON T.CdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON T.CdResponsDest=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN SubCentros AS SCO ON T.IdSubCos=SCO.IdSubCos
 LEFT JOIN SubCentros AS SCD ON T.CdSubCosDest=SCD.IdSubCos
 WHERE M.IdMov=@pmIdMov AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND T.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 ORDER BY M.Numero,NomActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovTrasladoLta  @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,CN.Compania AS CiaOrigen,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario
--datos del traslado
,Item,T.IdActivo AS CdActivo,NomActivo,T.IdGrupo AS CdGrupoOri,GOR.Grupo AS GrupoOrigen,GOR.IdLinea AS CdLineaOri,LOR.Linea AS LineaOrigen,T.IdCCosto AS CdCentroOrigen,CCO.CCosto AS CentroOrigen
,T.IdSubCos AS CdSubCosOrig,SCO.SubCosto AS SubCentroOrig,T.IdInstala AS CdInsOrigen,ITO.Instlacion AS InsOrigen,T.IdDep AS CdDepOrig,DPO.Dependencia AS DepOrigen,T.IdRespons AS NitRespOrigen,TRO.RazonSocial AS ResponsOrigen
,CdCiaDest,CND.Compania  AS CiaDestino,CdGrupoDest,GDT.Grupo AS NomGrupoDestino,CdCCostoDest,CCD.CCosto AS CentroDestino,CdSubCosDest,SCD.SubCosto AS SubCenDestino,CdInstalaDest
,IDT.Instlacion AS InsDestino,CdDepDest,DPD.Dependencia AS DepDestino,CdResponsDest,TRD.RazonSocial AS NomRespDestino,Concepto,Ubicacion,UbicacionDest
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,G.Grupo AS ActGrupo,G.IdLinea AS CdLinea,L.Linea AS ActLinea,A.IdCCosto AS CdCentro,CC.CCosto AS ActivoCentro,A.IdSubCos AS CdSubCentro,SC.SubCosto AS ActSubcentro,A.IdInstala AS CdInstala,INS.Instlacion AS ActInstalacion
,A.IdDep AS CdDep,DP.Dependencia AS ActDepend,Marca,Modelo,NumPlaca,Referencia,FecActivacion,A.VidaUtil,FecFinDep,MetodoDep,FecUltDep
,A.IdRespons AS NitReponsb,TR.RazonSocial AS Responsable,A.IdEstado AS CdEstado,Estado
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovTraslado AS T ON M.IdMov=T.IdMov AND M.Numero=T.Numero AND M.IdCia=T.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON T.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN CentroCosto AS CCO ON T.IdCCosto=CCO.IdCCosto INNER JOIN Instalaciones AS ITO ON T.IdInstala=ITO.IdInstala
 INNER JOIN Dependencias AS DPO ON T.IdDep=DPO.IdDep INNER JOIN Terceros AS TRO ON T.IdRespons=TRO.IdTercero
 INNER JOIN AfGrupos AS  GOR ON T.IdGrupo=GOR.IdGrupo INNER JOIN AfLineas AS LOR ON GOR.IdLinea=LOR.IdLinea
 LEFT JOIN   Companias AS CND ON T.CdCiaDest=CND.IdCia LEFT JOIN AfGrupos AS GDT ON T.CdGrupoDest=GDT.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON T.CdCCostoDest=CCD.IdCCosto LEFT JOIN Instalaciones AS IDT ON T.CdInstalaDest=IDT.IdInstala
 LEFT JOIN Dependencias AS DPD ON T.CdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON T.CdResponsDest=TRD.IdTercero
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN SubCentros AS SCO ON T.IdSubCos=SCO.IdSubCos
 LEFT JOIN SubCentros AS SCD ON T.CdSubCosDest=SCD.IdSubCos
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin  AND M.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovValoriza @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Item,IdActivo,Concepto,Valor,NitProvee,FecAdquis,NumDocmto,DescripParte
 FROM Trn_AfMovValoriza
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovValorizaCr @pmIdMov VARCHAR(4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdActivo VARCHAR(30)=Null
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumValoriza,M.IdCia AS CdCia,Compania,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario,Item,V.IdActivo AS CdActivo,NomActivo,Concepto,Valor
,NitProvee,PV.RazonSocial AS Proveedor,FecAdquis,NumDocmto,DescripParte
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubCentro,SubCosto,A.IdInstala AS CdInstala,Instlacion
,A.IdDep AS CdDep,Dependencia,Marca,Modelo,NumPlaca,Referencia,IdRespons,TR.RazonSocial AS Responsable,A.IdEstado AS CdEstado,Estado
,FecActivacion,VrBaseDep,VrMejoras,VrDisminucion,A.VidaUtil AS MesesVida,FecFinDep,MetodoDep
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovValoriza AS V ON M.IdMov=V.IdMov AND M.Numero=V.Numero AND M.IdCia=V.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON V.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS PV ON  V.NitProvee=PV.IdTercero
 WHERE M.IdMov=@pmIdMov AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND V.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 ORDER BY M.Numero,NomActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovValorizaLta @pmIdMov VARCHAR(4),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT M.IdMov AS CdMov,TipoMov,M.Numero AS NumIngreso,M.IdCia AS CdCia,Compania,Fecha,Documento,Anulado,M.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
,M.FecCreacion AS FechaCrea,M.IdUsuario AS CdUsuario,Usuario,Item,V.IdActivo AS CdActivo,NomActivo,Concepto,Valor
,NitProvee,PV.RazonSocial AS Proveedor,FecAdquis,NumDocmto,DescripParte
--datos del activo
,NumSerie,A.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubCentro,SubCosto,A.IdInstala AS CdInstala,Instlacion
,A.IdDep AS CdDep,Dependencia,Marca,Modelo,NumPlaca,Referencia,IdRespons,TR.RazonSocial AS Responsable,A.IdEstado AS CdEstado,Estado
,FecActivacion,VrBaseDep,VrMejoras,VrDisminucion,A.VidaUtil AS MesesVida,FecFinDep,MetodoDep
 FROM Trn_AfMovimiento AS M  INNER JOIN Trn_AfMovValoriza AS V ON M.IdMov=V.IdMov AND M.Numero=V.Numero AND M.IdCia=V.IdCia
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia INNER JOIN AfTiposMov AS TM ON M.IdMov=TM.IdMov
 INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario
 INNER JOIN Activos AS A ON V.IdActivo=A.IdActivo INNER JOIN AfGrupos AS  G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS INS ON A.IdInstala=INS.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS PV ON V.NitProvee=PV.IdTercero
 WHERE M.IdMov=@pmIdMov AND M.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin AND M.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfMovimiento @pmIdMov VARCHAR(4),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdMov,Numero,IdCia,Fecha,Documento,Anulado,Observacion,TipCom,Comprobante,IdCiaCom,FecCreacion,IdUsuario
 FROM Trn_AfMovimiento
 WHERE IdMov=@pmIdMov AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfPignora @pmNumero INT
 AS 
 SELECT Numero,IdActivo,Fecha,TipoAccion,NomTercero,Comentarios,Garantia,IdUsuario
 FROM Trn_AfPignora
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfPignoraCr  @pmNumeroIni INT, @pmNumeroFin INT
 AS 
 SELECT Numero,Fecha,P.IdActivo AS CdActivo,NomActivo,TipoAccion,NomTercero,Comentarios,Garantia
,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdInstala AS CdInstala,Instlacion,A.IdDep AS CdDep,Dependencia,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,Compania
,VrBaseDep,VrMejoras,VrDisminucion,FecActivacion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable
,A.IdEstado AS CdEstado,Estado,FecRetiro,P.IdUsuario AS IdUsuari,Usuario
 FROM Trn_AfPignora AS P INNER JOIN Activos AS A ON P.IdActivo=A.IdActivo
 INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario  INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE  Numero BETWEEN @pmNumeroIni AND @pmNumeroFin 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfPignoraLta  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmNumeroIni INT,@pmNumeroFin INT,@pmTipoAccion VARCHAR(10)=Null, @pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdActivo VARCHAR(30)=Null
 AS 
 SELECT Numero,Fecha,P.IdActivo AS CdActivo,NomActivo,TipoAccion,NomTercero,Comentarios,Garantia
,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdInstala AS CdInstala,Instlacion,A.IdDep AS CdDep,Dependencia,IdRespons,TR.RazonSocial AS Responsable,A.IdCia AS CdCia,Compania
,VrBaseDep,VrMejoras,VrDisminucion,FecActivacion,A.VidaUtil AS Vida_util,FecFinDep,MetodoDep,Ajustable,Contable
,A.IdEstado AS CdEstado,Estado,FecRetiro,P.IdUsuario AS IdUsuari,Usuario
 FROM Trn_AfPignora AS P INNER JOIN Activos AS A ON P.IdActivo=A.IdActivo
 INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON A.IdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS TR ON A.IdRespons=TR.IdTercero
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario  INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND TipoAccion LIKE ISNULL(@pmTipoAccion,'%') AND  A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND  A.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND P.IdActivo LIKE ISNULL(@pmIdActivo,'%')
 AND (Numero>=ISNULL(@pmNumeroIni,-1)  AND Numero<=ISNULL(@pmNumeroFin,2147483647))
 ORDER BY  Numero


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfTiposDif @pmIdTipDif VARCHAR(4)
 AS 
 IF @pmIdTipDif IS NULL
 SELECT IdTipDif,TipoDiferido
 FROM AfTiposDif
 WHERE Inactivo=0
 ORDER BY IdTipDif
 ELSE
 SELECT IdTipDif,TipoDiferido,Inactivo
 FROM AfTiposDif
 WHERE IdTipDif=@pmIdTipDif

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfTiposMov @pmIdMov VARCHAR(4)
 AS 
 IF @pmIdMov IS NULL
  SELECT IdMov,TipoMov,Consecutivo
  FROM AfTiposMov
  WHERE Inactivo=0
  ORDER BY  IdMov
 ELSE
  SELECT IdMov,TipoMov,Consecutivo,Inactivo
  FROM AfTiposMov
  WHERE IdMov=@pmIdMov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAfTiposMovDso @pmTipo VARCHAR(3)
 AS 
 IF @pmTipo='AF'
  SELECT IdMov,TipoMov
  FROM AfTiposMov
  WHERE Inactivo=0 AND IdMov IN ('0001','0002','0003','0004','0005','0006','0007','0008','0009')
  ORDER BY  IdMov
 ELSE
  SELECT IdMov,TipoMov
  FROM AfTiposMov
  WHERE Inactivo=0 AND IdMov LIKE '2%'
  ORDER BY  IdMov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAgencias @pmIdAgencia VARCHAR(16)
 AS 
 SELECT IdAgencia,CodAgencia,IdClie,Agencia,DirAgncia,IdLocal,TelAgncia,FaxAgncia,NitCont,NomCont,emlCont
,CargoCont,IdSzona,IdPlazo,CdPlazoComb,IdForma,CdBandera,IdVend,CdCms,CdDct,IntMora,DiasGracia,FactFletes,FactSold
,VrCupoCre,VrSaldoAct,FecUpCupo,NContrato,CiaCont,FecIngreso,CodSicom,Referencia,Comentarios,CupoGalones
,IdEstado,Inactivo,TipoZona,FechaAdd,FechaUpdate,IdUsuario,CdTipAgenc,CdSubAgenc,CdGrupAgenc,CdSubGrupAgenc,CdFntePago,PlazoSobtasa,DocFinanc,VolContMes
,VolContTotal,VolViabMes,VolViabTotal,FecPriVenta,InvCtbObra,InvCtbCapital,InvCtbEfectivo,InvCtbImagen,InvCtbMntmto,FecDesemb,InvContObra,InvContCapital,InvContEfectivo
,InvContImagen,CrtlAprovInv,CrtlAprovImg,FecVigCond,TasaIntArranq
 FROM Agencias
 WHERE IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAgenciasCli @pmIdClie VARCHAR(16)
 AS 
 SELECT IdAgencia,CodAgencia,IdClie,RazonSocial,Codigo,Dv,Agencia,DirAgncia,A.IdLocal AS CdCiudad
,TelAgncia,FaxAgncia,NitCont,NomCont,emlCont,CargoCont,VrCupoCre,VrSaldoAct,Referencia,CodSicom
,IdSzona,IdPlazo,CdPlazoComb,IdForma,CdBandera,IdVend,CdCms,CdDct,IntMora,DiasGracia,FactFletes,FactSold
,FecUpCupo,NContrato,CiaCont,FecIngreso,CupoGalones,A.Inactivo AS Inactvo,A.IdEstado AS Id_Estado,Comentarios,TipoZona
,A.FechaAdd AS FecAdd,A.FechaUpdate AS FecUpd,A.IdUsuario AS IdUsuari
 FROM Agencias AS A INNER JOIN Terceros AS T ON A.IdClie=T.IdTercero
 WHERE A.Inactivo=0 AND IdClie=@pmIdClie
 ORDER BY IdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAgenciasCod @pmIdClie VARCHAR(16),@pmCodAgencia VARCHAR(16)
 AS 
 SELECT IdAgencia,CodAgencia,IdClie,Agencia,DirAgncia,IdLocal,TelAgncia,FaxAgncia,NitCont,NomCont,emlCont
,CargoCont,IdSzona,IdPlazo,CdPlazoComb,IdForma,CdBandera,IdVend,CdCms,CdDct,IntMora,DiasGracia,FactFletes,FactSold
,VrCupoCre,VrSaldoAct,FecUpCupo,NContrato,CiaCont,FecIngreso,CodSicom,Referencia,Comentarios,CupoGalones
,TipoZona,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
,CdTipAgenc,CdSubAgenc,CdGrupAgenc,CdSubGrupAgenc,CdFntePago,PlazoSobtasa,DocFinanc,VolContMes
,VolContTotal,VolViabMes,VolViabTotal,FecPriVenta,InvCtbObra,InvCtbCapital,InvCtbEfectivo,InvCtbImagen,InvCtbMntmto,FecDesemb,InvContObra,InvContCapital,InvContEfectivo
,InvContImagen,CrtlAprovInv,CrtlAprovImg,FecVigCond,TasaIntArranq
 FROM Agencias
 WHERE IdClie=@pmIdClie AND CodAgencia LIKE ISNULL(@pmCodAgencia,'%')
 ORDER BY IdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAgenciasId
 AS 
 SELECT COUNT(IdAgencia) AS CANTAGE
 FROM Agencias
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAgenciasLta @pmIdClie VARCHAR(16)=Null,@pmIdLocal VARCHAR(8)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdZona VARCHAR(4)=Null,@pmIdSzona VARCHAR(4)=Null,@pmIdEstrato VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdSector VARCHAR(8)=Null,@pmInactivo BIT=Null
 AS 
 SELECT A.IdClie AS NitCliente,RazonSocial,IdAgencia,CodAgencia,Agencia,DirAgncia,A.IdLocal AS CdCiuAgncia,L.Localidad AS CiudadAgncia
,L.IdDep AS CdDptoAgncia,D.Departamento AS DptoAgncia,TelAgncia,FaxAgncia,NitCont,NomCont,emlCont,CargoCont,VrCupoCre,VrSaldoAct,A.FecUpCupo AS FechaUpCupo,Referencia
,CodSicom,A.NContrato AS NroCont,CiaCont,A.FecIngreso AS Fec_Ingreso,NomCial,SiglaRaz,SZA.IdZona AS Age_CdZona,ZA.Zona AS Age_Zona,A.IdSzona AS Age_IdSubzona,SZA.Subzona AS Age_Subzona
,A.IdPlazo AS CdPlazo,Plazo,NVmto,A.CdPlazoComb AS CdPlazoComb,A.IdForma AS CdForma,FormaPago,A.IdVend AS NitVend,A.CdCms AS CodCms,A.CdDct AS CodDct,IntMora,A.DiasGracia AS Dias_gracia,FactFletes,A.FactSold AS FacturaSold
,A.CupoGalones AS CupoGals
,TipoId,Dv,Direccion,T.IdLocal AS CdCiuClie,LT.Localidad AS CiuClie,LT.IdDep AS CdDptoClie,DT.Departamento AS DptoClie
,NitRepLeg,NomRepLeg,NitContac,NomContac,C.IdGrupo AS CdGrupo,GrupoClie,SZ.IdZona AS CdZona,Z.Zona AS CliZona,C.IdSzona AS CdSubzona,SZ.Subzona AS CliSubZona
,A.CdBandera AS CodBandera,TipoBandera,C.IdEstrato AS CdEstrato,Estrato,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRgmen,Regimen,TipEnte,VrCupo,VrSaldo,Telefono,Fax
,TelMovil,e_mail,C.IdEstado AS CdEstado,Estado,C.Inactivo AS Clie_inactivo,A.IdEstado AS Age_Estado,A.Inactivo AS Age_inactivo,IniStgNom,A.FechaAdd AS FecAdd
,TipoZona,Comentarios,A.FechaUpdate AS FecUpd,A.IdUsuario AS IdUsuari,Usuario
--datos nuevos
,CdTipAgenc,CdSubAgenc,CdGrupAgenc,CdSubGrupAgenc,CdFntePago,PlazoSobtasa,DocFinanc,VolContMes
,VolContTotal,VolViabMes,VolViabTotal,FecPriVenta,InvCtbObra,InvCtbCapital,InvCtbEfectivo,InvCtbImagen,InvCtbMntmto,FecDesemb,InvContObra,InvContCapital,InvContEfectivo
,InvContImagen,CrtlAprovInv,CrtlAprovImg,FecVigCond,TasaIntArranq
 FROM Agencias AS A INNER JOIN Terceros AS T ON A.IdClie=T.IdTercero
 INNER JOIN TercCliente AS C ON A.IdClie=C.IdClie INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON A.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN Subzonas AS SZA ON A.IdSzona=SZA.IdSzona INNER JOIN Zonas AS ZA ON SZA.IdZona=ZA.IdZona
 INNER JOIN Localidades AS LT ON T.IdLocal=LT.IdLocal  INNER JOIN Departamentos AS DT ON LT.IdDep=DT.IdDep
 INNER JOIN GruposCli AS G ON C.IdGrupo=G.IdGrupo INNER JOIN Subzonas AS SZ ON C.IdSzona=SZ.IdSzona
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona INNER JOIN Estratos AS EC ON C.IdEstrato=EC.IdEstrato
 INNER JOIN SectoresEco AS S ON T.IdSector=S.IdSector INNER JOIN RegimenDian AS R ON T.IdRegimen=R.IdRegimen
 INNER JOIN EstadoTer AS E ON C.IdEstado=E.IdEstado INNER JOIN Plazos AS PZ ON A.IdPlazo=PZ.IdPlazo
 INNER JOIN Formaspago AS FP ON A.IdForma=FP.IdForma
 LEFT JOIN TiposBan AS TB ON A.CdBandera=TB.IdBandera
 WHERE A.Inactivo=0 AND A.IdClie LIKE ISNULL(@pmIdClie,'%') AND A.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND C.IdGrupo LIKE ISNULL(@pmIdGrupo ,'%') AND SZA.IdZona LIKE ISNULL(@pmIdZona,'%') AND A.IdSzona LIKE ISNULL(@pmIdSzona,'%')
 AND C.IdEstrato LIKE ISNULL(@pmIdEstrato,'%') AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND T.IdSector LIKE ISNULL(@pmIdSector,'%')
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY RazonSocial,Agencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAjuInv @pmTipDoc VARCHAR(3),@pmAjuste INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Ajuste,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,Cantidad,BaseAjuInf,TarifPaag,NumDcmto
,NumOrden,IdCCosto,IdSubCos,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado
,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_AjuInv
 WHERE TipDoc=@pmTipDoc AND Ajuste=@pmAjuste AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAjuInvLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmAjusteIni INT=Null,@pmAjusteFin INT=Null
,@pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Ajuste,IdCia,Fecha,A.IdConcepto AS CdConcepto,Concepto,A.IdTercero AS NitTercero,RazonSocial,VrSubTotal,Cantidad,BaseAjuInf,TarifPaag,NumDcmto
,NumOrden,A.IdCCosto AS CdCcosto,CCosto,IdSubCos,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado
,TimeSys,FecUpdate,IdCiaCrea,A.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_AjuInv AS A  INNER JOIN Terceros AS T ON A.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Ajuste BETWEEN ISNULL(@pmAjusteIni,0) AND ISNULL(@pmAjusteFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND A.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,Ajuste
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAjuInvOrd @pmTipDoc VARCHAR(3),@pmNumOrden INT,@pmIdCia CHAR(2)=Null
 AS 
 SELECT TipDoc,Ajuste,IdCia,Fecha,IdConcepto,NumOrden,NumDcmto
 FROM Trn_AjuInv
 WHERE TipDoc=@pmTipDoc AND NumOrden=@pmNumOrden AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY  IdCia,Ajuste
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAjuInvRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Ajuste,A.IdCia AS CdCia,Compania,Fecha,A.IdConcepto AS CdConcepto,Concepto,A.IdTercero AS NitTercero,RazonSocial
,VrSubTotal,Cantidad,BaseAjuInf,TarifPaag,NumDcmto,NumOrden,A.IdCCosto AS CdCcosto,CCosto,A.IdSubCos AS CdSubcosto,SubCosto
,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado
,TimeSys,A.FecUpdate AS Fec_Update,IdCiaCrea,A.IdUsuario AS IdUsuari,Usuario
,TipoId,Dv,Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
 FROM Trn_AjuInv AS A INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON A.IdTercero=T.IdTercero  INNER JOIN  Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND A.IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND A.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND A.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
AND  Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY A.IdCia,Ajuste

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAjuInv_Cr @pmTipDoc VARCHAR(3),@pmAjusteIni INT,@pmAjusteFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT A.TipDoc AS TDoc,TipoDoc,Ajuste,A.IdCia AS CdCia,Compania,A.Fecha AS FechaDoc,A.IdConcepto AS CdConcepto,Concepto,A.IdTercero AS NitTercero,RazonSocial
,VrSubTotal,Cantidad,BaseAjuInf,TarifPaag,NumDcmto,A.NumOrden AS NroOrden,A.IdCCosto AS CdCcosto,CC.CCosto AS CentCosto,A.IdSubCos AS CdSubcosto,SC.SubCosto AS SubcCosto
,Modalidad,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado
,A.TimeSys AS Time_Sys,A.FecUpdate AS Fec_Update,IdCiaCrea,A.IdUsuario AS IdUsuari,Usuario,Leyenda
,TipoId,Dv,Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
--datos del kardex
,Item,K.IdProducto AS CodProducto,DescripProd,DescripAbrv,K.IdBodega AS CdBodega,Bodega,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,KC.CCosto AS Kar_Ccosto,CdSubCos,KS.SubCosto AS Kar_Subcentro
,pVehiculo,K.Referencia AS Kar_Referencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,K.NumOrden AS Kar_NOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev
,NumDocDev,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,CodTarDct
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_AjuInv AS A  INNER JOIN Trn_Kardex AS K ON A.TipDoc=K.TipDoc AND A.Ajuste=K.Documento AND A.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON A.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON A.IdTercero=T.IdTercero  INNER JOIN  Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN TiposCom AS TC ON A.TipCom=TC.IdCom  
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE A.TipDoc=@pmTipDoc AND Ajuste BETWEEN @pmAjusteIni AND @pmAjusteFin  AND A.IdCia=@pmIdCia
 ORDER BY A.IdCia,Ajuste,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryAlmBodegas @pmIdBodega VARCHAR(4)
 AS 
 IF @pmIdBodega IS NULL
  SELECT IdBodega,Bodega,Direccion,TelBodega
  FROM AlmBodegas
  WHERE Inactivo=0
 ORDER BY IdBodega
 ELSE
 SELECT IdBodega,Bodega,Direccion,TelBodega,Inactivo
 FROM AlmBodegas
 WHERE IdBodega=@pmIdBodega

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAprobacion @pmTipDoc VARCHAR(3),@pmAprobacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Aprobacion,IdCia,Fecha,TipoPed,Pedido,IdCiaPed,FecPedido,IdCliente,IdAgencia,pVehiculo,VrCupoCred,VrSaldoAct
,ChequesDev,ClienteMora,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Aprobacion
 WHERE TipDoc=@pmTipDoc AND Aprobacion=@pmAprobacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAprobacionLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmAprobacionIni INT=Null,@pmAprobacionFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmPedido INT=Null
 AS 
 SELECT  Aprobacion,A.IdCia AS CdCia,Compania,Fecha,TipoPed,Pedido,IdCiaPed,FecPedido,IdCliente,RazonSocial,IdAgencia
,pVehiculo,VrCupoCred,VrSaldoAct,ChequesDev,ClienteMora,OrigenAdd,Anulado,FecDev,A.Observacion AS Observ
,A.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,A.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Aprobacion AS A INNER JOIN Terceros AS T ON A.IdCliente=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Aprobacion BETWEEN ISNULL(@pmAprobacionIni,0) AND ISNULL(@pmAprobacionFin,2147483647)
  AND A.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
  AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (Pedido>=ISNULL(@pmPedido,0)  AND Pedido<=ISNULL(@pmPedido,2147483647))
 ORDER BY A.IdCia,Aprobacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAprobacion_Cr @pmTipDoc VARCHAR(3),@pmAprobacionIni INT,@pmAprobacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Aprobacion,A.IdCia AS CdCia,Compania,Fecha,TipoPed,Pedido,IdCiaPed,FecPedido,IdCliente,RazonSocial,A.IdAgencia AS CdAgencia
,Agencia,pVehiculo,VrCupoCred,A.VrSaldoAct AS SaldoActual,ChequesDev,ClienteMora,OrigenAdd,Anulado,FecDev,A.Observacion AS Observ
,A.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,A.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Aprobacion AS A INNER JOIN Terceros AS T ON A.IdCliente=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia
 LEFT JOIN Agencias AS AG ON A.IdAgencia=AG.IdAgencia
 WHERE TipDoc=@pmTipDoc AND  Aprobacion BETWEEN  @pmAprobacionIni  AND @pmAprobacionFin
 AND A.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAreas @pmIdArea VARCHAR(4)
 AS 
  SELECT IdArea,Area,JefeArea,IdCCosto,Inactivo
  FROM Areas
  WHERE IdArea=@pmIdArea


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAreasLta
 AS 
  SELECT IdArea,Area,JefeArea,A.IdCCosto AS IdCcost,CCosto
  FROM Areas AS A INNER JOIN CentroCosto AS C ON A.IdCCosto=C.IdCCosto
  WHERE A.Inactivo=0
  ORDER BY Area
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAutoriza @pmTipAut VARCHAR(3),@pmAutoriza INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipAut,Autoriza,IdCia,Fecha,IdConcepto,NumAutoriza,IdUsuarioAut,IdEstacion,NitTercero,pVehiculo,TipoDoc,Documento
,IdCiaDoc,FechaDoc,DocRef,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Autoriza
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAutorizaDoc @pmTipoDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipAut,Autoriza,IdCia,Fecha,IdConcepto,NumAutoriza,IdUsuarioAut,IdEstacion,NitTercero,pVehiculo,TipoDoc,Documento
,IdCiaDoc,FechaDoc,DocRef,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Autoriza
 WHERE TipoDoc=@pmTipoDoc AND Documento=@pmDocumento AND IdCiaDoc LIKE ISNULL(@pmIdCiaDoc,'%%')
 ORDER BY IdCia,Autoriza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAutorizaLta  @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmAutorizaIni INT=Null,@pmAutorizaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdUsuarioAut VARCHAR(11)=Null,@pmNitTercero VARCHAR(16)=Null,@pmTipoDoc VARCHAR(3)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT Autoriza,IdCia,Fecha,A.IdConcepto AS CdConcepto,ConceptoAut,NumAutoriza,IdUsuarioAut,UA.Usuario AS UsuarioAtzdo,A.IdEstacion AS CdEstacion,NitTercero,RazonSocial,pVehiculo
,TipoDoc,Documento,IdCiaDoc,FechaDoc,DocRef,OrigenAdd,Anulado,FecDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado,A.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TimeSys,FecUpdate,IdCiaCrea
 FROM Trn_Autoriza AS A INNER JOIN Sys_Autoriza AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS T ON A.NitTercero=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS UA ON A.IdUsuarioAut=UA.IdUsuario 
 INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Autoriza BETWEEN ISNULL(@pmAutorizaIni,0) AND ISNULL(@pmAutorizaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') 
 AND IdUsuarioAut LIKE ISNULL(@pmIdUsuarioAut,'%') AND NitTercero LIKE ISNULL(@pmNitTercero,'%')
 AND TipoDoc LIKE ISNULL(@pmTipoDoc,'%')  AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND A.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Autoriza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAutorizaNum @pmNumAutoriza INT,@pmIdCia CHAR(2)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipAut,Autoriza,IdCia,Fecha,IdConcepto,NumAutoriza,IdUsuarioAut,IdEstacion,NitTercero,pVehiculo,TipoDoc,Documento
,IdCiaDoc,FechaDoc,DocRef,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Autoriza
 WHERE NumAutoriza=@pmNumAutoriza AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY Autoriza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryAutoriza_Cr @pmTipAut VARCHAR(3),@pmAutorizaIni INT,@pmAutorizaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipAut,TD.TipoDoc AS TipoDocAut,Autoriza,A.IdCia AS CodCia,Compania,Fecha,A.IdConcepto AS CdConcepto,ConceptoAut,NumAutoriza,IdUsuarioAut,UA.Usuario AS UsuarioAtzdo
,UA.IdGrupo AS NivelUsuario,UA.IdCargo AS CdCago,CA.Cargo AS CargoUsuario,A.IdEstacion AS CdEstacion,NitTercero,RazonSocial,pVehiculo,A.TipoDoc AS Tip_Doc,Documento,IdCiaDoc,FechaDoc,DocRef
,OrigenAdd,Anulado,FecDev,A.Observacion AS Observ,A.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,A.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
,U.IdGrupo AS UsuNivel,U.IdCargo AS UsuIdCargo,CU.Cargo AS UsuNomCargo,Leyenda
 FROM Trn_Autoriza AS A INNER JOIN Sys_Autoriza AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS T ON A.NitTercero=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia
 INNER JOIN adm_Usuarios AS UA ON A.IdUsuarioAut=UA.IdUsuario 
 INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario 
 INNER JOIN Sys_TiposDoc AS TD ON A.TipAut=TD.IdDoc
 LEFT JOIN Cargos AS CU ON U.IdCargo=CU.IdCargo
 LEFT JOIN Cargos AS CA ON UA.IdCargo=CA.IdCargo
 WHERE TipAut=@pmTipAut AND Autoriza BETWEEN @pmAutorizaIni AND @pmAutorizaFin AND A.IdCia=@pmIdCia
 ORDER BY A.IdCia,Autoriza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBancos @pmIdBanco VARCHAR(4)
 AS 
 IF @pmIdBanco IS NULL
  SELECT IdBanco,Banco,CodEntidad,B.IdClase AS CodClase,ClaseBanco,Direccion,Contacto,Comision
  FROM Bancos AS B INNER JOIN ClaseBan AS C ON B.IdClase=C.IdClase
  WHERE B.Inactivo=0
  ORDER BY IdBanco
 ELSE
  SELECT IdBanco,Banco,IdClase,CodEntidad,Direccion,Contacto,Comision,Inactivo
  FROM Bancos
  WHERE IdBanco=@pmIdBanco
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBancosDso
AS
 SELECT  IdBanco,Banco,IdBanco+'  '+ Banco AS DsBan  
 FROM   Bancos  
 WHERE Inactivo=0   
 ORDER BY  IdBanco
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBancosEnt @pmCodEntidad VARCHAR(20)
 AS 
  SELECT IdBanco,Banco,IdClase,CodEntidad,Direccion,Contacto,Comision,Inactivo
  FROM Bancos
  WHERE CodEntidad=@pmCodEntidad

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodUbic @pmIdUbic VARCHAR(10)
 AS 
 SELECT IdUbic,Ubicacion,IdBodega,Pasillo,Nivel,Profdad,DimAltura,DimAnch,DimLong,CapcVolm
,UndCapc,CapcPeso,UndPes,CdTmcia,CdMnjo,Notas,Inactivo
 FROM BodUbic
 WHERE IdUbic=@pmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodUbicCan @pmIdBodega VARCHAR(4)
 AS 
 SELECT COUNT(IdUbic) AS SCANT 
 FROM BodUbic
 WHERE  Inactivo=0 AND IdBodega=@pmIdBodega AND IdUbic<>'0'

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodUbicDso @pmIdBodega VARCHAR(4)=Null
 AS 
 SELECT IdUbic,Ubicacion,IdUbic+'  (calle: '+Pasillo+' nivel:'+Nivel+'  Prof:'+Profdad + ') ' + Ubicacion AS DsUbi
 FROM BodUbic
 WHERE (Inactivo=0  AND IdBodega LIKE ISNULL(@pmIdBodega,'%')) OR IdUbic='0'
 ORDER BY IdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodUbicLta @pmIdBodega VARCHAR(4)=Null,@pmPasillo VARCHAR(10)=Null,@pmNivel VARCHAR(10)=Null
,@pmProfdad VARCHAR(10)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdUbic,Ubicacion,U.IdBodega AS CdBodg,Bodega,Pasillo,Nivel,Profdad,DimAltura,DimAnch,DimLong,CapcVolm
,UndCapc,CapcPeso,UndPes,CdTmcia,TipoMcia,CdMnjo,ManejoMcia,Notas,U.Inactivo AS Inactiv,Descrip,LtaPre,TipoInv
 FROM BodUbic AS U INNER JOIN Bodegas AS B ON U.IdBodega=B.IdBodega
 LEFT JOIN  TiposMcia AS TM ON U.CdTmcia=TM.IdTmcia
 LEFT JOIN TiposMnjo AS TJ ON U.CdMnjo=TJ.IdMnjo
 WHERE U.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND Pasillo LIKE ISNULL(@pmPasillo,'%')
 AND Nivel LIKE ISNULL(@pmNivel,'%') AND Profdad LIKE ISNULL(@pmProfdad,'%')
 AND (U.Inactivo=ISNULL(@pmInactivo,0) or  U.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY U.IdBodega,IdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodegas @pmIdBodega VARCHAR(4)
 AS 
 SELECT IdBodega,Bodega,Descrip,LtaPre,TipoInv,Inactivo
 FROM Bodegas
 WHERE IdBodega=@pmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodegasDso @pmTipoInv VARCHAR(10)=Null,@pmIdCia CHAR(2)=Null
 AS 
 IF @pmIdCia IS NULL
  SELECT IdBodega,Bodega,Bodega+' '+IdBodega AS DsBod
  FROM Bodegas  WHERE  Inactivo=0 AND TipoInv LIKE ISNULL(@pmTipoInv,'%')
  ORDER BY Bodega 
 ELSE
  SELECT B.IdBodega  AS IdBodega,Bodega,Bodega+' '+B.IdBodega AS DsBod
  FROM Bodegas AS B LEFT JOIN BodegasCias AS C ON B.IdBodega=C.IdBodega
   WHERE  B.Inactivo=0 AND B.TipoInv LIKE ISNULL(@pmTipoInv,'%')
  AND (C.IdCia=@pmIdCia OR C.IdCia IS NULL)
  ORDER BY Bodega 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryBodegasLta @pmTipoInv VARCHAR(10)=Null
 AS 
 SELECT IdBodega,Bodega,Descrip,LtaPre,TipoInv
 FROM Bodegas
 WHERE  Inactivo=0 AND TipoInv LIKE ISNULL(@pmTipoInv,'%')
 ORDER BY IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryBusClaseCue @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL 
  SELECT IdClase,ClaseCuenta,Prioridad
  FROM BusClaseCue
  WHERE  Inactivo=0 
  ORDER BY ClaseCuenta
 ELSE
  SELECT IdClase,ClaseCuenta,Prioridad,Inactivo
  FROM BusClaseCue
  WHERE IdClase=@pmIdClase



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCalndario @pmFechaDia SMALLDATETIME
 AS 
 IF @pmFechaDia IS NULL
  SELECT FechaDia,TipoFtvo  FROM Calndario  ORDER BY FechaDia
 ELSE
  SELECT FechaDia,TipoFtvo
  FROM Calndario
  WHERE FechaDia=@pmFechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCalndarioMes @pmMes INT,@pmAnno INT
 AS 
 SELECT FechaDia,TipoFtvo  FROM Calndario 
 WHERE MONTH(FechaDia)=@pmMes AND YEAR(FechaDia)=@pmAnno
 ORDER BY FechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCargos @pmIdCargo VARCHAR(4)
 AS 
 IF @pmIdCargo IS NULL
   SELECT IdCargo,Cargo,Funciones
   FROM Cargos 
   WHERE  Inactivo=0
  ORDER BY Cargo
 ELSE
  SELECT IdCargo,Cargo,Funciones,Inactivo
  FROM Cargos 
  WHERE IdCargo=@pmIdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausaVales @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos
,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca,TarifaIva,TarifaRet,TarifaIca,CodTarIva,CodTarRet,CodTarIca,IdVend,TarifaCom,CodTarCom,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_CausaVales
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausaValesLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDocumentoIni INT=Null, @pmDocumentoFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Documento,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,Agencia
,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca,TarifaIva,TarifaRet,TarifaIca
,CodTarIva,CodTarRet,CodTarIca,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,OrigenAdd,F.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
 FROM Trn_CausaVales AS F  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario  INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TC ON F.TipCom=TC.IdCom
 WHERE Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND (Documento>=ISNULL(@pmDocumentoIni,0)  AND Documento<=ISNULL(@pmDocumentoFin,2147483647))
 AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY F.IdCia,Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausaVales_Cr @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT F.TipDoc AS CdTipDoc,TipoDoc,F.Documento AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha AS FechaFact,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS Id_agencia,Agencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca,F.TarifaIva AS TarifIva
,F.TarifaRet AS TarifRet,F.TarifaIca AS TarifIca,F.CodTarIva AS CdTarifIva,F.CodTarRet AS CdTarifRet,F.CodTarIca AS CdTarifIca,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,F.CodTarCom AS CdTarifCom
,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,F.TimeSys AS Fecha_Add,FecUpdate,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm
,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,VrCostoEnt,VrCostoSal,VrBruto,K.Referencia AS KarReferencia,Descripcion
,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarRemision,K.IdCiaRem AS KarCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Unidades,pVehiculo
,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,Referencia2,CodTarCmc
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_CausaVales AS F  INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Documento=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON F.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TCM ON F.TipCom=TCM.IdCom
 WHERE F.TipDoc=@pmTipDoc AND F.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND F.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY F.Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryCausacVence @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,FecEmision,FecVence,ValorFactura,ValorAbono,IdCliente,IdAgencia,IdVehiculo,VehPropio,IdVendedor,Comision
,IdCargo,IdClase,IdConcepto,FecUltPago,NotaInt,FecUltLiquida,Factura,Referencia,Detalle,TipRec,Recibo,IdCiaRec,ValorAtcpo,IdCuenta,CombCant,CombValor
 FROM Trn_CausacVence
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceAbb @pmIdVehiculo VARCHAR(10)
AS
 SELECT  TipDoc,Documento,IdCia,Item,ValorFactura,ValorAbono,FecEmision,FecVence,V.IdCargo AS IdCargo,V.IdClase AS CodClase
,ClaseCuenta,Prioridad,Detalle,TipoDcto,DctoPpago,OrdenAbono
 FROM  Trn_CausacVence AS V INNER JOIN BusClaseCue AS C ON V.IdClase=C.IdClase
 LEFT JOIN Fijos AS F ON V.IdCargo=F.IdCargo
 WHERE  IdVehiculo=@pmIdVehiculo AND ValorFactura>ValorAbono
 ORDER BY FecVence,OrdenAbono
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceApl @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
AS
 SELECT   V.TipDoc AS TipoDoc, V.Documento AS Document, V.IdCia AS CodCia, V.Item AS NV,FecEmision,FecVence, ValorFactura, V.ValorAbono AS TotalAbono
 , R.TipRec AS TipoRec, R.Recibo AS NumRecibo, R.IdCia AS CiaRec, R.Item AS ItemRec,Fecha , 
                     R.ValorAbono AS ValorAplicado, Devolucion, Descuento,V.IdCliente AS IdClient,V.IdVehiculo AS IdVehic
 FROM  Trn_CausacVence AS  V LEFT JOIN Trn_RecFactura R ON V.TipDoc = R.TipDoc AND V.Documento = R.Documento AND V.IdCia = R.IdCiaDoc AND V.Item = R.ItemDoc 
 WHERE   V.TipDoc =@pmTipDoc AND V.Documento=@pmDocumento AND  V.IdCia=@pmIdCia
 AND (V.Item>=ISNULL(@pmItem,0)  AND V.Item<=ISNULL(@pmItem,2147483647))
 ORDER BY V.Item, R.IdCia,R.TipRec,R.Recibo,R.Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceCli  @pmIdCliente VARCHAR(16)
AS
 IF @pmIdCliente IS NULL
  SELECT IdCliente,SUM(ValorFactura) AS STotFac,SUM(ValorAbono) AS StotAbo,SUM(ValorAtcpo) AS STotAnt
  FROM     Trn_CausacVence
  GROUP BY  IdCliente
 ELSE
  SELECT IdCliente,SUM(ValorFactura) AS STotFac,SUM(ValorAbono) AS StotAbo,SUM(ValorAtcpo) AS STotAnt
  FROM     Trn_CausacVence
  WHERE  IdCliente=@pmIdCliente
 GROUP BY  IdCliente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceEvo @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
--causaciones prestamos y ndb
AS
 SELECT TipDoc,IdCia,IdCliente,VC.IdVehiculo AS PlacaVeh,IdConductor,VC.IdClase AS CodClase,IdConcepto
,SUM(ValorFactura) AS STotal
 FROM  Trn_CausacVence AS VC  INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo
 WHERE TipDoc IN ('ND1','PRE','FVC') AND  FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND VC.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 GROUP BY TipDoc,IdCia,IdCliente,VC.IdVehiculo,IdConductor,VC.IdClase,IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceExt @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
,@pmAnulado BIT=Null
--causaciones solamente
AS
 SELECT VC.TipDoc AS TipoDcm,VC.IdCia AS CodCia,IdCliente,VC.IdVehiculo AS PlacaVeh,V.IdConductor AS IdCondtor,VC.IdClase AS CodClase,VC.IdConcepto AS CodCocepto,IdCargo
,SUM(ValorFactura) AS STotal,SUM(CombCant) AS STotGal,SUM(CombValor) AS SValComb
 FROM  Trn_CausacVence AS VC  INNER JOIN Trn_Causacion AS C ON VC.TipDoc=C.TipDoc  AND  VC.Documento=C.Documento AND  VC.IdCia=C.IdCia
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo
 WHERE VC.TipDoc IN ('ADM','OTR')  AND  FecEmision BETWEEN @pmFechaIni AND @pmFechaFin  AND VC.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND VC.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 GROUP BY VC.TipDoc,VC.IdCia,IdCliente,VC.IdVehiculo,V.IdConductor,VC.IdClase,VC.IdConcepto,IdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceLca @pmIdVehiculo VARCHAR(10)
AS
SELECT  TipDoc, Documento, IdCia, Item,V.IdCargo AS IdCarg,Concepto,Detalle,ValorFactura-ValorAbono AS ValorSaldo,ValorFactura,ValorAbono,FecEmision,FecVence, IdCliente,T.RazonSocial AS NomCliente,IdAgencia
, IdVehiculo,VehPropio,IdVendedor,C.RazonSocial AS Conductor,V.IdClase AS CodClase,ClaseCuenta,Factura, Referencia,TipRec, Recibo, IdCiaRec,ValorAtcpo,CombCant
 FROM   Trn_CausacVence AS V INNER JOIN Terceros AS T ON  V.IdCliente=T.IdTercero
 INNER JOIN  BusClaseCue AS U ON  V.IdClase=U.IdClase
 INNER JOIN Terceros AS C ON  V.IdVendedor=C.IdTercero
 INNER JOIN  Fijos AS F ON  V.IdCargo=F.IdCargo
 WHERE  ValorFactura>ValorAbono  AND  IdVehiculo=@pmIdVehiculo
ORDER BY Prioridad,FecVence
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmFechaAct SMALLDATETIME,@pmSaldo DECIMAL(14,4)
,@pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT  TipDoc,Documento, IdCia, Item, FecEmision, FecVence,DATEDIFF(Day,FecVence,@pmFechaAct) AS DiasMora,ValorFactura, ValorAbono,ValorFactura-ValorAbono AS ValorSaldo,IdCliente,T.RazonSocial AS NomCliente,IdVehiculo,VehPropio
 ,IdVendedor AS IdCondctor,C.RazonSocial AS NomConductor,V.IdCargo AS IdCarg,Concepto,V.IdClase AS CodClase, IdConcepto,Factura, Referencia, Detalle,FecUltPago, TipRec, Recibo, IdCiaRec,ValorAtcpo,NotaInt,FecUltLiquida,IdAgencia,Comision
,CombCant,CombValor
  FROM Trn_CausacVence AS V INNER JOIN Terceros AS T ON  V.IdCliente=T.IdTercero
  INNER JOIN Terceros AS C ON  V.IdVendedor=C.IdTercero
  INNER JOIN  Fijos AS F ON  V.IdCargo=F.IdCargo
  WHERE (ValorFactura-ValorAbono)>@pmSaldo 
  AND (FecVence>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecVence<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
  AND  IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
  AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  IdCia LIKE ISNULL(@pmIdCia ,'%%')  AND V.IdClase LIKE ISNULL(@pmIdClase ,'%')
 ORDER BY FecVence DESC,IdCia,TipDoc,Documento,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceMor @pmTipDoc VARCHAR(3),@pmIdClase VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdVehiculo VARCHAR(10)
AS
SELECT   TipDoc, Documento, IdCia, Item, FecEmision, FecVence, ValorFactura, ValorAbono, IdCliente, IdAgencia, IdVehiculo, IdCargo,IdClase, Factura, Referencia, Detalle
FROM     Trn_CausacVence
WHERE ValorFactura>ValorAbono AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND IdClase LIKE ISNULL(@pmIdClase,'%')
  AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
ORDER BY FecVence

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceNMes @pmAnno INT,@pmMes INT=Null,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdClase VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS --ACUMULADOS MENSUALES POR VEHICULO Y COMPAÑIA
 SELECT  IdCliente,IdVehiculo,IdCia,IdClase,Month(FecEmision) AS nMes,SUM(ValorFactura) AS StotFac,SUM(ValorAbono) AS StotAbo
 FROM   Trn_CausacVence
 WHERE Year(FecEmision)=@pmAnno AND  Month(FecEmision)=ISNULL(@pmMes, Month(FecEmision))
 AND IdCia like ISNULL(@pmIdCia,'%%') AND   TipDoc like ISNULL(@pmTipDoc,'%') AND IdCliente like ISNULL(@pmIdCliente,'%')
 AND IdClase like ISNULL(@pmIdClase,'%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 GROUP BY IdCliente,IdVehiculo,IdCia,IdClase,Month(FecEmision)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceTot @pmTipDoc VARCHAR(3), @pmDocumento INT, @pmIdCia CHAR(2)
AS
SELECT  IdCliente,IdVehiculo,SUM(ValorFactura) AS SValFac,SUM(ValorAbono) AS SValAbo
FROM     Trn_CausacVence
WHERE   TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia
GROUP BY IdCliente, IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceTpa @pmIdVehiculo VARCHAR(10)
AS
 IF @pmIdVehiculo IS NULL
  SELECT  SUM(ValorFactura) AS STotFac,SUM(ValorAbono) AS STotAbo,SUM(ValorAtcpo) AS STotAnt 
  FROM  Trn_CausacVence 
 ELSE
  SELECT  SUM(ValorFactura) AS STotFac,SUM(ValorAbono) AS STotAbo,SUM(ValorAtcpo) AS STotAnt 
  FROM  Trn_CausacVence 
  WHERE  IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceVFec @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdClase VARCHAR(4)=Null
AS 
 SELECT IdCliente,IdVehiculo,SUM(ValorFactura) AS StotFac,SUM(ValorAbono) AS StotAbo
 FROM   Trn_CausacVence
 WHERE FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND IdClase LIKE ISNULL(@pmIdClase,'%')
 GROUP BY IdCliente,IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVenceVeh  @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
IF @pmIdVehiculo IS NULL 
 SELECT V.IdVehiculo AS IdVehic,V.IdClase AS IdClas, IdCia, SUM(ValorFactura) AS STotFac,SUM(ValorAbono) AS StotAbo,SUM(ValorAtcpo) AS STotAnt
 FROM  Trn_CausacVence AS V INNER JOIN Vehiculos AS H ON V.IdVehiculo=H.IdVehiculo
 WHERE IdPropietario like ISNULL(@pmIdPropietario,'%') 
 GROUP BY  V.IdVehiculo,V.IdClase ,IdCia
ELSE --POR VEHICULO
 SELECT V.IdVehiculo AS IdVehic,V.IdClase AS IdClas, IdCia, SUM(ValorFactura) AS STotFac,SUM(ValorAbono) AS StotAbo,SUM(ValorAtcpo) AS STotAnt
 FROM     Trn_CausacVence AS V 
 WHERE V.IdVehiculo=@pmIdVehiculo
 GROUP BY  V.IdVehiculo,V.IdClase ,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVenceVp @pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT     TipDoc, Documento, IdCia, Item, FecEmision, FecVence, ValorFactura, ValorAbono, IdCliente, IdAgencia, IdVehiculo, VehPropio, IdVendedor
, Comision, IdCargo, V.IdClase AS CodClase,ClaseCuenta,Prioridad,IdConcepto,FecUltPago, NotaInt, FecUltLiquida, Factura, Referencia, Detalle, TipRec, Recibo, IdCiaRec,ValorAtcpo,IdCuenta,CombCant,CombValor
,ISNULL(tmValorAbono,0) AS TotaAbonos
 FROM  Trn_CausacVence AS V INNER JOIN BusClaseCue AS C ON V.IdClase=C.IdClase
 LEFT JOIN tm_vce AS TV ON V.TipDoc=TV.tmTipDoc AND V.Documento=TV.tmDocumento AND V.IdCia=TV.tmIdCia AND V.Item=TV.tmItem
 WHERE ValorFactura>(ISNULL(tmValorAbono,0)+0.5)  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND   IdCliente like ISNULL(@pmIdCliente,'%')  
 ORDER BY Prioridad,FecVence,TipDoc,Documento,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacVence_Cr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmSaldo DECIMAL(14,2),@pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdClase VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdVendedor VARCHAR(16)=Null,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT IdCliente,NP.RazonSocial AS Propietario,VC.IdVehiculo AS PlacaVeh,TipDoc,TipoDoc,Documento,VC.IdCia AS CodCia,Item, FecEmision, FecVence, ValorFactura, ValorAbono, ValorFactura-ValorAbono AS TotalSaldo
,VC.IdClase AS IdClas,ClaseCuenta,VC.IdCargo AS IdCarg,F.Concepto AS ConceptoCargo,Factura,Referencia,Detalle,VC.IdConcepto AS IdConcep,C.Concepto AS ConcepDoc,IdVendedor AS CedConductor
,NC.RazonSocial AS Conductor,Compania,FecUltPago,NotaInt,FecUltLiquida,TipRec, Recibo, IdCiaRec,ValorAtcpo,IdCuenta,VC.VehPropio AS VhPropio,CombCant,CombValor
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_CausacVence AS VC INNER JOIN Terceros AS NP ON VC.IdCliente=NP.IdTercero
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo INNER JOIN Sys_TiposDoc  AS TD ON VC.TipDoc=TD.IdDoc
 INNER JOIN Companias  AS CI ON VC.IdCia=CI.IdCia INNER JOIN BusClaseCue  AS CU ON VC.IdClase=CU.IdClase
 INNER JOIN Fijos AS F ON VC.IdCargo=F.IdCargo INNER JOIN Conceptos AS C ON VC.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS NC ON VC.IdVendedor=NC.IdTercero  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON VC.IdVendedor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE FecVence BETWEEN @pmFechaIni AND @pmFechaFin AND (ValorFactura-ValorAbono)>@pmSaldo AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  VC.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND VC.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND VC.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND VC.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND IdVendedor LIKE ISNULL(@pmIdVendedor,'%') AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY NP.RazonSocial,VC.IdVehiculo,TipDoc,VC.IdCia,Documento,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacVence_Cra @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmSaldo DECIMAL(14,2),@pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdClase VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdVendedor VARCHAR(16)=Null,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT VC.IdCliente AS NitCliente,NP.RazonSocial AS Propietario,VC.IdVehiculo AS PlacaVeh,VC.TipDoc AS CdTipDoc,TipoDoc,VC.Documento AS NumDoc,VC.IdCia AS CodCia,VC.Item AS NumItem, FecEmision, FecVence, ValorFactura, VC.ValorAbono AS TotalAbonos, ValorFactura-VC.ValorAbono AS TotalSaldo
,VC.IdClase AS IdClas,ClaseCuenta,VC.IdCargo AS IdCarg,F.Concepto AS ConceptoCargo,Factura,Referencia,VC.Detalle AS DetalleVence,VC.IdConcepto AS IdConcep,C.Concepto AS ConcepDoc,VC.IdVendedor AS CedConductor
,NC.RazonSocial AS Conductor,Compania,FecUltPago,NotaInt,FecUltLiquida,ValorAtcpo,IdCuenta,VC.VehPropio AS VhPropio,CombCant,CombValor
,R.TipRec AS TipDocApl, R.Recibo AS NumRecibo, R.IdCia AS IdCiaApl, R.Item AS ItmApl,Fecha,R.ValorAbono AS VrAbonado,Devolucion, R.Detalle AS DetalleApl,FecPago
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_CausacVence AS VC INNER JOIN Terceros AS NP ON VC.IdCliente=NP.IdTercero
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo INNER JOIN Sys_TiposDoc  AS TD ON VC.TipDoc=TD.IdDoc
 INNER JOIN Companias  AS CI ON VC.IdCia=CI.IdCia INNER JOIN BusClaseCue  AS CU ON VC.IdClase=CU.IdClase
 INNER JOIN Fijos AS F ON VC.IdCargo=F.IdCargo INNER JOIN Conceptos AS C ON VC.IdConcepto=C.IdConcepto
 INNER JOIN Terceros AS NC ON VC.IdVendedor=NC.IdTercero  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON VC.IdVendedor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN Trn_RecFactura AS R ON VC.TipDoc = R.TipDoc AND VC.Documento = R.Documento AND VC.IdCia = R.IdCiaDoc AND VC.Item = R.ItemDoc 
 WHERE FecVence BETWEEN @pmFechaIni AND @pmFechaFin AND (ValorFactura-VC.ValorAbono)>@pmSaldo AND VC.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  VC.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND VC.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND VC.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND VC.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND VC.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND VC.IdVendedor LIKE ISNULL(@pmIdVendedor,'%') AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY NP.RazonSocial,VC.IdVehiculo,VC.TipDoc,VC.IdCia,VC.Documento,VC.Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryCausacion @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Fecha,IdConcepto,Factura,IdProveedor,FecFactura,FecVence,Referencia,Modalidad,IdVehiculo,IdPropietario,IdConductor,VehPropio,ValorTotal,ValorDcto,ValorIva,ValorReteFte,ValorReteIva,ValorReteIca,ValorOtros,ValorFletes,ValorNeto,ValorCCobrar,BaseIva,BaseRet,TarifaIva,TarifaRet,TarifaIca,IdTarIva,IdTarRet,IdTarIca,MulPlazos,IdPlazo,CxPagar,CxcVarios,TipEgr,Egreso,IdCiaEgr,Anulado,NumDev,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,CierreCte,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Causacion
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionCie @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10)=Null,@pmIdCia CHAR(2)=Null
AS
 SELECT  TipDoc, Documento, IdCia, Fecha,Factura,Modalidad 
 FROM    Trn_Causacion 
 WHERE  TipDoc=@pmTipDoc AND CierreCte<>0 AND Anulado=0 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY  Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionCue @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
 SELECT VC.IdCargo AS CodCargo,Concepto,V.IdAdmon,TipoAdmon,FC.IdCuentaDeb AS IdCuentDeb ,FC.IdCuentaCre AS IdCuentCre ,FC.IdCueDebVp AS IdCueDbVp , FC.IdCueCreVp AS IdCueCrVp 
 ,SUM(ValorFactura) AS SvalFac
 FROM Trn_Causacion AS C INNER JOIN Trn_CausacVence AS VC ON C.TipDoc=VC.TipDoc AND C.Documento=VC.Documento AND C.IdCia=VC.IdCia
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
 INNER JOIN Fijos AS F ON VC.IdCargo=F.IdCargo LEFT JOIN Fijos_Cue AS FC ON VC.IdCargo=FC.IdCargo AND V.IdAdmon=FC.IdAdmon
 WHERE Anulado=0 AND Comprobante<=0 AND C.TipDoc=@pmTipDoc AND Modalidad IN ('DIARIO','OCASIONAL')
 AND FecFactura BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY VC.IdCargo,Concepto,V.IdAdmon,TipoAdmon,FC.IdCuentaDeb,FC.IdCuentaCre,FC.IdCueDebVp,FC.IdCueCreVp

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionD_Cr @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null

AS
 SELECT C.TipDoc AS TDoc,C.Documento AS NumDoc,C.IdCia AS CodCia,Fecha,FecFactura,IdCliente AS NitPropietario,RazonSocial,V.IdVehiculo AS PlacaVeh,V.VehPropio AS Espropio,IdVendedor AS CedConductor,V.IdCargo AS CodCargo,Concepto
,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp,SUM(ValorFactura) AS SvalFac
 FROM Trn_Causacion AS C INNER JOIN Trn_CausacVence AS V ON C.TipDoc=V.TipDoc AND C.Documento=V.Documento AND C.IdCia=V.IdCia
 INNER JOIN Fijos AS F ON V.IdCargo=F.IdCargo INNER JOIN Terceros AS P ON V.IdCliente=P.IdTercero
 WHERE Anulado=0 AND Comprobante<=0  AND Modalidad IN ('DIARIO','OCASIONAL')  AND C.TipDoc=@pmTipDoc
 AND FecFactura BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY C.TipDoc,C.Documento,C.IdCia,Fecha,FecFactura,IdCliente,RazonSocial,V.IdVehiculo,V.VehPropio,IdVendedor,V.IdCargo,Concepto,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionDia @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmDocumentoIni INT=Null ,@pmDocumentoFin INT=Null
AS
SELECT   Documento, IdCia, Fecha,Referencia,Modalidad,IdVehiculo, IdPropietario,T.RazonSocial AS Propietario, IdConductor,O.RazonSocial AS Conductor,VehPropio,ValorNeto, Anulado, NumDev, FecDev, TipCom, 
                     Comprobante, IdCiaCom,C.Observacion AS Observ,C.IdEstado AS IdEstad,Estado, TimeSys, FecUpdate, IdCiaCrea,C.IdUsuario AS IdUsuari
FROM  Trn_Causacion AS C LEFT JOIN Terceros AS T ON C.IdPropietario=T.IdTercero
   LEFT JOIN Terceros AS O ON C.IdConductor=O.IdTercero
  LEFT JOIN EstadoDoc  AS E ON C.IdEstado=E.IdEstado
WHERE   TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,0) AND ISNULL(@pmDocumentoFin,2147483647)
ORDER BY IdCia,Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionFac @pmFactura VARCHAR(15),@pmIdProveedor VARCHAR(16)

AS
SELECT   TipDoc, Documento, IdCia, Fecha, Factura, IdProveedor, FecFactura, FecVence
FROM     Trn_Causacion
WHERE  Factura=@pmFactura AND IdProveedor=@pmIdProveedor

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionFec @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmModalidad  VARCHAR(10),@pmFecha SMALLDATETIME

AS
SELECT     TipDoc, Documento, IdCia, Fecha, IdConcepto, Factura, IdProveedor, FecFactura, FecVence, Referencia, Modalidad, ValorNeto, Anulado, NumDev, FecDev, TipCom,Comprobante, IdCiaCom
FROM     Trn_Causacion
WHERE  Anulado=0  AND  TipDoc=@pmTipDoc  AND  IdCia=@pmIdCia AND Modalidad=@pmModalidad AND Fecha=@pmFecha
ORDER BY Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionFfa @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmModalidad  VARCHAR(10),@pmFecFactura SMALLDATETIME

AS
SELECT     TipDoc, Documento, IdCia, Fecha, IdConcepto, Factura, IdProveedor, FecFactura, FecVence, Referencia, Modalidad, ValorNeto, Anulado, NumDev, FecDev, TipCom, 
                      Comprobante, IdCiaCom
FROM     Trn_Causacion
WHERE Anulado=0  AND  TipDoc=@pmTipDoc  AND  IdCia=@pmIdCia AND Modalidad=@pmModalidad AND FecFactura=@pmFecFactura
ORDER BY Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionFij @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
 SELECT  IdCliente,VC.IdVehiculo AS PlacaVeh,V.IdAdmon AS CodAdm,TipoAdmon,VC.VehPropio AS Espropio,IdVendedor,VC.IdCargo AS CodCargo,Concepto,IdCuentaDeb,IdCuentaCre
,IdCueDebVp,IdCueCreVp,SUM(ValorFactura) AS SvalFac
 FROM Trn_Causacion AS C INNER JOIN Trn_CausacVence AS VC ON C.TipDoc=VC.TipDoc AND C.Documento=VC.Documento AND C.IdCia=VC.IdCia
 INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
 INNER JOIN Fijos AS F ON VC.IdCargo=F.IdCargo
 WHERE Anulado=0 AND Comprobante<=0  AND Modalidad IN ('DIARIO','OCASIONAL')  AND C.TipDoc=@pmTipDoc
 AND FecFactura BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,VC.IdVehiculo,V.IdAdmon,TipoAdmon,VC.VehPropio,IdVendedor,VC.IdCargo,Concepto,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionFijTot @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
 SELECT  SUM(ValorFactura) AS SValTot
 FROM Trn_Causacion AS C INNER JOIN Trn_CausacVence AS V ON C.TipDoc=V.TipDoc AND C.Documento=V.Documento AND C.IdCia=V.IdCia
 WHERE Anulado=0 AND Comprobante<=0 AND Modalidad IN ('DIARIO','OCASIONAL')  AND C.TipDoc=@pmTipDoc
 AND FecFactura BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionInt @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
 SELECT   C.TipDoc AS Tdoc, C.Documento AS NDoc, C.IdCia AS CodCia, Fecha,C.Factura AS NumFact, FecFactura,ValorTotal,Anulado,TipCom,Comprobante, IdCiaCom,IdEstado,SUM(ValorFactura) AS SValFac
 FROM    Trn_Causacion AS C INNER JOIN Trn_CausacVence AS V ON C.TipDoc=V.TipDoc AND C.Documento=V.Documento AND C.IdCia=V.IdCia 
 WHERE  Anulado=0 AND Modalidad IN ('DIARIO','OCASIONAL')  AND  C.TipDoc=@pmTipDoc  AND FecFactura BETWEEN @pmFechaIni AND @pmFechaFin AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY C.TipDoc, C.Documento, C.IdCia, Fecha, C.Factura, FecFactura,ValorTotal,Anulado,TipCom,Comprobante, IdCiaCom,IdEstado

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacionOtr @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdVehiculo VARCHAR(10)
,@pmIdProveedor VARCHAR(16),@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null
AS
SELECT     Documento, IdCia, Fecha, IdConcepto, Factura, IdProveedor,P.RazonSocial AS Proveedor, FecFactura, FecVence, Referencia, Modalidad, IdVehiculo, IdPropietario,O.RazonSocial AS Propietario,IdConductor, 
                     D.RazonSocial AS Conductor, VehPropio, ValorTotal, ValorDcto, ValorIva, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorFletes, ValorNeto,ValorCCobrar, BaseIva, BaseRet, TarifaIva, 
                      TarifaRet, TarifaIca, IdTarIva, IdTarRet, IdTarIca, MulPlazos,C.IdPlazo AS IdPlaz, CxPagar,CxcVarios, TipEgr, Egreso, IdCiaEgr, Anulado, NumDev, FecDev, TipCom, 
                      Comprobante, IdCiaCom,C.Observacion AS Observ,C.IdEstado AS IdEstad,Estado,CierreCte,TimeSys, FecUpdate, IdCiaCrea, C.IdUsuario AS IdUsuari
FROM    Trn_Causacion AS C  INNER JOIN Terceros AS P ON C.IdProveedor=P.IdTercero
 INNER JOIN Terceros AS O ON C.IdPropietario=O.IdTercero
 INNER JOIN Terceros AS D ON C.IdConductor=D.IdTercero
 INNER JOIN EstadoDoc  AS E ON C.IdEstado=E.IdEstado
WHERE   TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia ,'%%')
 AND IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%')
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,0) AND ISNULL(@pmDocumentoFin,2147483647)
ORDER BY IdCia,Documento

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryCausacion_Cr @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
AS
SELECT  C.TipDoc AS CodTipDoc,TipoDoc, C.Documento AS nDocumento, C.IdCia AS CodCia,Compania, Fecha, C.IdConcepto AS CodConcepto,CC.Concepto AS Concept, C.Factura AS NumFactura, C.IdProveedor AS NitProveedor,NV.RazonSocial AS Proveedor
,FecFactura, C.FecVence AS FechaVence, C.Referencia AS Referenc, C.Modalidad AS ModCausac
,C.IdVehiculo AS PlacaVeh,C.IdPropietario AS NitPropietario, C.IdConductor AS CedConductor, C.VehPropio AS VehProp, ValorTotal, ValorDcto, ValorIva, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorFletes, ValorNeto,ValorCCobrar
, BaseIva, BaseRet, TarifaIva,TarifaRet, C.TarifaIca AS PorceIca, IdTarIva, IdTarRet, IdTarIca, MulPlazos, C.IdPlazo AS CodPlazo,Plazo,DiasPago,NVmto, CxPagar,CxcVarios, TipEgr, Egreso, IdCiaEgr, Anulado, NumDev, FecDev,TipCom,TM.TipoCom  AS TipoComprobante
,Comprobante, IdCiaCom, C.Observacion AS Observ, C.IdEstado AS IdEstadDoc,ED.Estado AS EstadDoc,TimeSys, FecUpdate, IdCiaCrea, C.IdUsuario AS IdUsuari,Usuario,Leyenda
--detalles
,Item,VF.FecVence AS FecVenceFac,ValorFactura,ValorAbono,IdCliente,IdAgencia,VF.IdVehiculo AS PlacaVehvce,IdVendedor,VF.IdCargo AS IdCarg,F.Concepto AS NomCargo,VF.IdClase AS CodClase
,ClaseCuenta,FecUltPago,VF.Factura AS NFacVce,VF.Referencia AS RefVce,Detalle,TipRec,Recibo,IdCiaRec,ValorAtcpo,IdCuenta,CierreCte
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.RazonSocial AS Propietario,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.RazonSocial AS Conductor,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_Causacion  AS C  INNER JOIN Trn_CausacVence AS VF ON C.TipDoc = VF.TipDoc AND C.Documento = VF.Documento AND C.IdCia =VF.IdCia
  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc   INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
  INNER JOIN Conceptos AS CC ON C.IdConcepto=CC.IdConcepto
  INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
  INNER JOIN Plazos AS PZ ON C.IdPlazo=PZ.IdPlazo 
  INNER JOIN Terceros AS  NV ON C.IdProveedor=NV.IdTercero
  INNER JOIN Terceros AS NP ON VF.IdCliente=NP.IdTercero  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Terceros AS NC ON VF.IdVendedor=NC.IdTercero INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN Fijos AS F ON VF.IdCargo=F.IdCargo   INNER JOIN BusClaseCue AS CU ON VF.IdClase=CU.IdClase
    INNER JOIN Vehiculos AS V ON VF.IdVehiculo=V.IdVehiculo   INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TiposCom AS TM ON C.TipCom=TM.IdCom   LEFT JOIN TercCndtores AS CT ON VF.IdVendedor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE   C.TipDoc=@pmTipDoc  AND C.Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin  AND  C.IdCia=@pmIdCia
 ORDER BY C.IdCia,C.Documento 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCausacion_Crr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME ,@pmIdCia CHAR(2)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProveedor VARCHAR(16)=Null, @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmAnulado BIT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT TipDoc,Documento,C.IdCia AS CodCia,Compania,Fecha,Factura,FecFactura,C.IdPlazo AS CodPlazo,Plazo,DiasPago,NVmto,MulPlazos,FecVence,C.Modalidad AS ModCausacion,C.IdProveedor AS NitProveed, NV.RazonSocial AS Proveedor
,C.IdVehiculo AS PlacaVeh,C.VehPropio AS VhPropio,C.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,ValorTotal, ValorDcto, ValorIva, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorFletes, ValorNeto,ValorCCobrar
, C.IdConcepto AS CodConcepto,Concepto,CxPagar,CxcVarios, BaseIva, BaseRet, TarifaIva,TarifaRet, TarifaIca,Anulado, NumDev, FecDev,TipCom,TipoCom,Comprobante,IdCiaCom, C.Observacion AS Observ,Referencia, C.IdEstado AS IdEstadDoc
,ED.Estado AS EstadDoc, IdCiaCrea, C.IdUsuario AS IdUsuari,Usuario,TipEgr, Egreso, IdCiaEgr,TipoDoc,TimeSys, FecUpdate
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_Causacion  AS C  INNER JOIN Sys_TiposDoc  AS TD ON C.TipDoc=TD.IdDoc   INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Conceptos AS CC ON C.IdConcepto=CC.IdConcepto  INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Plazos AS PZ ON C.IdPlazo=PZ.IdPlazo 
 INNER JOIN Terceros AS  NV ON C.IdProveedor=NV.IdTercero
 INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero 
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TiposCom AS TM ON  C.TipCom=TM.IdCom   LEFT JOIN TercCndtores AS CT ON C.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  TipDoc LIKE ISNULL(@pmTipDoc,'%')  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.Modalidad LIKE ISNULL(@pmModalidad,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND C.IdProveedor LIKE ISNULL(@pmIdProveedor,'%')
 AND C.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND C.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') 
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1))
 ORDER BY TipDoc,C.IdCia,Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCentroCosto @pmIdCCosto VARCHAR(16)
 AS 
 IF @pmIdCCosto IS NULL
  SELECT IdCCosto,CCosto,FechaAdd,FechaUpdate 
  FROM CentroCosto
  WHERE Inactivo=0
  ORDER BY CCosto
ELSE
  SELECT IdCCosto,CCosto,FechaAdd,FechaUpdate,Inactivo 
  FROM CentroCosto
  WHERE IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCentroCostoDso @pmInactivo BIT=Null
 AS 
  SELECT IdCCosto,CCosto,CCosto+' '+IdCCosto AS DsCco 
  FROM CentroCosto
  WHERE   (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
  ORDER BY CCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCentrosServ @pmIdCenSer VARCHAR(4)
 AS 
 SELECT IdCenSer,CentroServ,DirCentro,IdLocal,Telefonos,TipoCentro,NitResp,Respnsable,Inactivo
 FROM CentrosServ
 WHERE IdCenSer=@pmIdCenSer
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCentrosServLta 
 AS 
 SELECT IdCenSer,CentroServ,DirCentro,C.IdLocal AS CodCiudad,Localidad,L.IdDep AS CodDpto,Departamento
,Telefonos,TipoCentro,NitResp,Respnsable
 FROM CentrosServ AS C INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE C.Inactivo=0
 ORDER BY CentroServ
 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCheques @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 SELECT TipForma,Numero,IdBanco,IdCia,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle,NitCliente,CdAgencia,CdVend,IdLocal
,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,Anulado,FecDev,IdEstado,IdCiaCrea,IdUsuario
 FROM Trn_Cheques
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryChequesCns  @pmNitCliente VARCHAR(16)=Null,@pmCdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT TipForma,Numero,C.IdBanco AS CodBanco,Banco,C.IdCia AS CodCia,Compania,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle
,NitCliente,T.RazonSocial AS NomCliente,CdAgencia,CdVend,V.RazonSocial AS NomVendedor,C.IdLocal AS CodCiudad,Localidad,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc
,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,C.IdEstado AS CdEstado,Estado,C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Cheques AS C INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS T ON C.NitCliente=T.IdTercero
 LEFT JOIN Terceros AS V ON C.CdVend=V.IdTercero 
 WHERE Anulado=0 AND NumConsig<=0 AND  NitCliente LIKE ISNULL(@pmNitCliente,'%') AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')
  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Fecha,TipForma,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryChequesDoc  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipForma,Numero,IdBanco,IdCia,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle,NitCliente,CdAgencia,CdVend,IdLocal
,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,Anulado,FecDev,IdEstado,IdCiaCrea,IdUsuario
 FROM Trn_Cheques
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
 AND  IdCia=@pmIdCiaDoc
 ORDER BY TipForma,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryChequesLta @pmTipForma VARCHAR(3)=Null,@pmIdBanco VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmNitCliente VARCHAR(16)=Null,@pmCdVend VARCHAR(16)=Null
,@pmTipRec VARCHAR(3)=Null,@pmTipDoc VARCHAR(3)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null
 AS 
 SELECT TipForma,Numero,C.IdBanco AS CodBanco,Banco,C.IdCia AS CodCia,Compania,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle
,NitCliente,T.RazonSocial AS NomCliente,CdAgencia,Agencia,CdVend,V.RazonSocial AS NomVendedor,C.IdLocal AS CodCiudad,Localidad,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig
,IdCiaCon,FecConsig,CdCta,Anulado,FecDev,C.IdEstado AS CdEstado,Estado,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Cheques AS C INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS T ON C.NitCliente=T.IdTercero
 LEFT JOIN Terceros AS V ON C.CdVend=V.IdTercero 
 LEFT JOIN Agencias AS A ON C.CdAgencia=A.IdAgencia
 WHERE  TipForma LIKE ISNULL(@pmTipForma,'%') AND C.IdBanco LIKE ISNULL(@pmIdBanco,'%')  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%') AND CdVend LIKE ISNULL(@pmCdVend,'%')
 AND TipRec LIKE ISNULL(@pmTipRec,'%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND Recibo BETWEEN ISNULL(@pmReciboIni,-1) AND ISNULL(@pmReciboFin,2147483647)
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,-1) AND ISNULL(@pmDocumentoFin,2147483647)
 ORDER BY Fecha,TipForma,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryChequesPen  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT TipForma,Numero,IdBanco,IdCia,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle,NitCliente,CdAgencia,CdVend,IdLocal
,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,Anulado,FecDev,IdEstado,IdCiaCrea,IdUsuario
 FROM Trn_Cheques
 WHERE Anulado=0  AND Documento<=0 AND IdEstado<>'9998'  AND TipRec='CHE' AND Recibo>0
  AND  Fecha BETWEEN @pmFechaIni  AND @pmFechaFin  AND NitCliente LIKE ISNULL(@pmIdCliente,'%')
  AND CdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND CdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryChequesPos @pmFecPago SMALLDATETIME,@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 SELECT TipForma,Numero,C.IdBanco AS CodBanco,Banco,C.IdCia AS CodCia,Compania,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2
,Detalle,NitCliente,CdAgencia,CdVend,RazonSocial,C.IdLocal AS CodCiudad,Localidad,TipRec,Recibo
,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,C.IdEstado AS CodEstado,Estado 
 FROM Trn_Cheques AS C INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 LEFT JOIN Terceros AS V ON C.CdVend=V.IdTercero 
 WHERE  Anulado=0  AND Recibo>0 AND Documento<=0 AND FecPago<=@pmFecPago
 AND NitCliente=@pmNitCliente AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY FecPago,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryChequesRec  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipForma,Numero,IdBanco,IdCia,Fecha,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle,NitCliente,CdAgencia,CdVend,IdLocal
,TipRec,Recibo,TipDoc,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,Anulado,FecDev,IdEstado,IdCiaCrea,IdUsuario
 FROM Trn_Cheques
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo  AND IdCia=@pmIdCia
 ORDER BY TipForma,IdBanco,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseBan @pmIdClase VARCHAR(4)
 AS 
IF @pmIdClase IS NULL
  SELECT IdClase,ClaseBanco 
  FROM ClaseBan
  WHERE Inactivo=0
 ORDER BY ClaseBanco
ELSE 
  SELECT IdClase,ClaseBanco,Inactivo
  FROM ClaseBan
  WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseCta @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
  SELECT IdClase,ClaseCuenta
  FROM ClaseCta
  WHERE Inactivo=0
  ORDER BY ClaseCuenta
 ELSE
  SELECT IdClase,ClaseCuenta,Inactivo
  FROM ClaseCta
  WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseDiv @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,NomClase
 FROM ClaseDiv
 WHERE Inactivo=0
 ORDER BY IdClase
 ELSE
 SELECT IdClase,NomClase,Inactivo
 FROM ClaseDiv
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseDivDso @pmGrupo VARCHAR(20)
 AS 
 IF @pmGrupo='FACTURA' 
   SELECT IdClase,NomClase FROM ClaseDiv  WHERE Inactivo=0 AND IdClase<>'1001' AND IdClase<>'1002' ORDER BY  IdClase
 ELSE -- GASTOS,EGRESOS,INGRESOS,OTROS
   SELECT IdClase,NomClase FROM ClaseDiv  WHERE Inactivo=0 AND IdClase IN ('1001','1002') ORDER BY  IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseFon @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
  SELECT IdClase,ClaseFondo
  FROM ClaseFon
  WHERE  Inactivo=0
  ORDER BY  IdClase
 ELSE
  SELECT IdClase,ClaseFondo,Inactivo
  FROM ClaseFon
  WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClasePol @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,ClasePoliza
 FROM ClasePol
 WHERE Inactivo=0
 ORDER BY IdClase
 ELSE
 SELECT IdClase,ClasePoliza,Inactivo
 FROM ClasePol
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseRie @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
  SELECT IdClase,ClaseRiesgo,Tarifa
  FROM ClaseRie
  WHERE  Inactivo=0
  ORDER  BY IdClase
 ELSE
  SELECT IdClase,ClaseRiesgo,Tarifa,Inactivo
  FROM ClaseRie
  WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseRieDso
 AS 
  SELECT IdClase,ClaseRiesgo,Tarifa,ClaseRiesgo+ ' ' + STR(Tarifa,5,3) AS DsRie
  FROM ClaseRie
  WHERE  Inactivo=0
  ORDER  BY IdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseSub @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,NomClase
 FROM ClaseSub
 WHERE Inactivo=0
 ORDER BY  IdClase
 ELSE
 SELECT IdClase,NomClase,Inactivo
 FROM ClaseSub
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseTar @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
  SELECT IdClase,NomClase
  FROM ClaseTar
  WHERE Inactivo=0
  ORDER BY IdClase
 ELSE
  SELECT IdClase,NomClase,Inactivo
  FROM ClaseTar
  WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClaseTarImp
 AS 
  SELECT IdClase,NomClase
  FROM ClaseTar
  WHERE Inactivo=0 AND IdClase IN ('DEP','GLO','ICA','IVA','MUN','NAC','RET','RIV','SOB','SOL','MGM','PRE','TSN','TSD','TSM','SUB','MAR','COS','IVI','IGP')
  ORDER BY NomClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClieContado @pmIdCliente VARCHAR(16)
 AS 
 SELECT IdCliente,Nombre,TipoId,Dv,Direccion,IdLocal,Telefono,email,Excento,PosIniNom,Inactivo,FechaAdd,FechaUpdate
 FROM ClieContado
 WHERE IdCliente=@pmIdCliente
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClieContadoLta @pmIdLocal VARCHAR(8)=Null
 AS 
 SELECT IdCliente,Nombre,TipoId,Dv,Direccion,C.IdLocal AS CodCiudad,Localidad,L.IdDep AS CdDep,Departamento
,Telefono,email,Excento,PosIniNom,FechaAdd,FechaUpdate
 FROM ClieContado AS C INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE C.Inactivo=0 AND C.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 ORDER BY Nombre
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryClieContadoTel @pmTelefono VARCHAR(20)
 AS 
 SELECT IdCliente,Nombre,TipoId,Dv,Direccion,IdLocal,Telefono,email,Excento,PosIniNom,Inactivo,FechaAdd,FechaUpdate
 FROM ClieContado
 WHERE Telefono=@pmTelefono
 ORDER BY IdCliente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextna @pmIdReg INT
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd
,CnxOwner,CnxObserv,CnxPpties,CnxDriver,CnxPort
 FROM Sys_Cnxextna
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextnaItm 
 AS 
 SELECT MAX(IdReg) AS MXITM
 FROM Sys_Cnxextna
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextnaLta @pmCnxTipo VARCHAR(10)=Null
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxOwner,CnxObserv
,CnxPpties,CnxDriver,CnxPort,CnxPwd
 FROM Sys_Cnxextna
 WHERE  CnxTipo LIKE ISNULL(@pmCnxTipo,'%')
 ORDER BY IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCnxextnaTip @pmCnxTipo VARCHAR(10)
 AS 
 SELECT IdReg,CnxTipo,CnxEngine,CnxDso,CnxDbname,CnxAutentic,CnxUid,CnxPwd,CnxOwner,CnxObserv
,CnxPpties,CnxDriver,CnxPort
 FROM Sys_Cnxextna
 WHERE  CnxTipo=@pmCnxTipo
 ORDER BY IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalle @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,Referncia
,NitDoc,TipDocRef,DocRef,IdCiaRef,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_ComDetalle
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleAge @pmFechaIni  SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
 AS 
  SELECT  IdCia,D.IdCuenta AS CdCuenta,IdTercero,CodAgncia AS CdAgncia
,SUM(VrDebito) AS TotalDeb,SUM(VrCredito) AS TotalCre
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Tercero<>0 
  AND (Agencia<>0 OR CodAgncia<>'0')
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') 
  GROUP BY  IdCia,D.IdCuenta,IdTercero,CodAgncia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleChe @pmNumCheque VARCHAR(20),@pmCodCta VARCHAR(4), @pmTipCom VARCHAR(3),@pmIdCia CHAR(2)
AS
 SELECT TipCom, Comprobante, IdCia, Item, Fecha,CodCta, NumCheque
 FROM    Trn_ComDetalle
 WHERE NumCheque=@pmNumCheque AND CodCta like ISNULL(@pmCodCta,'%') AND TipCom like ISNULL(@pmTipCom,'%') AND  IdCia like ISNULL(@pmIdCia,'%%')
 ORDER BY  TipCom,IdCia,Comprobante,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleCueNit @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdSubCos VARCHAR(16)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmCodAgncia VARCHAR(16)=Null,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null
,@pmIdCueIni VARCHAR(16)=Null,@pmIdCueFin VARCHAR(16)=Null
 AS 
 IF @pmIdCueIni IS NULL
  SELECT D.IdCuenta AS CdCuenta,IdTercero,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Tercero<>0  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,IdTercero,TipCom,IdCia
ELSE
  SELECT D.IdCuenta AS CdCuenta,IdTercero,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Tercero<>0  AND  D.IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin
   AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,IdTercero,TipCom,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleCueTip @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdSubCos VARCHAR(16)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmCodAgncia VARCHAR(16)=Null,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null
,@pmIdCueIni VARCHAR(16)=Null,@pmIdCueFin VARCHAR(16)=Null
 AS 
 IF @pmIdCueIni IS NULL
  SELECT D.IdCuenta AS CdCuenta,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,TipCom,IdCia
ELSE
  SELECT D.IdCuenta AS CdCuenta,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  D.IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin
   AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,TipCom,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryComDetalleDia  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIntegrado BIT=Null,@pmCueIni VARCHAR(16)=Null,@pmCueFin VARCHAR(16)=Null
AS
 IF @pmCueIni IS NULL --NO RANGOS
  SELECT IdVehiculo,IdTercero,Fecha,SUM(VrDebito) AS  tValorDebito,SUM(VrCredito) AS tValorCredito,SUM(VrCredito-VrDebito) AS ValorDiario
  FROM  Trn_ComDetalle AS D INNER JOIN Puc  AS P ON D.IdCuenta=P.IdCuenta
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Tercero<>0 AND  IdCia like ISNULL(@pmIdCia,'%%') AND D.IdCuenta like ISNULL(@pmIdCuenta,'%') 
  AND IdTercero like ISNULL(@pmIdTercero,'%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 AND   (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
  GROUP BY  IdVehiculo,IdTercero,Fecha
  ORDER BY IdVehiculo,IdTercero,Fecha
 ELSE --RANGOS Y LIKE
  BEGIN
  IF @pmCueIni='LIKE'
   SELECT IdVehiculo,IdTercero,Fecha,SUM(VrDebito) AS  tValorDebito,SUM(VrCredito) AS tValorCredito,SUM(VrCredito-VrDebito) AS ValorDiario
   FROM  Trn_ComDetalle AS D INNER JOIN Puc  AS P ON D.IdCuenta=P.IdCuenta
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Tercero<>0 AND D.IdCuenta LIKE @pmIdCuenta AND IdCia LIKE ISNULL(@pmIdCia,'%%')
   AND IdTercero LIKE ISNULL(@pmIdTercero,'%')   AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')   
  AND   (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
   GROUP BY IdVehiculo,IdTercero,Fecha
   ORDER BY  IdVehiculo,IdTercero,Fecha
  ELSE --RANGO DE CUENTAS
   SELECT  IdVehiculo,IdTercero,Fecha,SUM(VrDebito) AS  tValorDebito,SUM(VrCredito) AS tValorCredito,SUM(VrCredito-VrDebito) AS ValorDiario
   FROM  Trn_ComDetalle AS D INNER JOIN Puc  AS P ON D.IdCuenta=P.IdCuenta
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Movimiento<>0 AND Tercero<>0  AND  D.IdCuenta BETWEEN @pmCueIni AND @pmCueFin  AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
   AND IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
  AND   (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
   GROUP BY  IdVehiculo,IdTercero,Fecha
   ORDER BY IdVehiculo,IdTercero,Fecha
 END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleDoc  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmIntegrado BIT=Null
 AS 
 IF @pmIntegrado IS NULL
  SELECT TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
 ,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,Referncia
,NitDoc,TipDocRef,DocRef,IdCiaRef,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
  FROM Trn_ComDetalle
  WHERE TipDoc=@pmTipDoc  AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
  ORDER BY TipCom,Comprobante,Item
 ELSE
  SELECT TipCom,Comprobante,IdCia,Item,Fecha,IdCuenta,Detalle,VrDebito,VrCredito,IdTercero,IdVehiculo,IdCCosto,IdSubCos,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
 ,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,Referncia
,NitDoc,TipDocRef,DocRef,IdCiaRef,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
  FROM Trn_ComDetalle
  WHERE TipDoc=@pmTipDoc  AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
  AND Integrado=@pmIntegrado  AND IdCia=@pmIdCiaDoc
  ORDER BY TipCom,Comprobante,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmTipDoc VARCHAR(3)=Null,@pmEsEgreso BIT=Null,@pmIntegrado BIT=Null
,@pmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT C.TipCom AS IdTipoComp,TipoCom,C.Comprobante AS NumComp,C.IdCia AS CdCia,Compania,C.Fecha AS FecComp,C.IdTercero AS Nit,T.RazonSocial AS RaznSocial
,VrTotal,C.IdCta AS CodCta,NumeroCta,CTE.IdBanco AS CodBanco,Banco,EnEfectivo,C.NumCheque AS NroCheque,FecCheque,C.TipDoc AS IdTipoDoc,TipoDoc,C.Documento AS NroDoc,C.IdCiaDoc AS CdCiaDoc
,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,C.VehPropio AS EsVehPropio,C.CodConce AS CdConcep,Concepto,Beneficiario,C.Integrado AS EsIntegrado,C.EsEgreso AS EsCompEgreso
,Anticipo,C.Observacion AS Observ,C.IdEstado AS CodEstado,Estado,Propddes
--detalles de comprobantes
,Item,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,IdVehiculo
,D.IdCCosto AS CdCentCosto,CCosto,D.IdSubCos AS CdSubCentro,SubCosto,VrBase,TarifaBase
,D.TipDoc AS DetTipDoc,D.Documento AS DetNumDoc,D.IdCiaDoc AS DetCiaDoc,D.CodConce AS DetConcept,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,D.NumCheque AS DetNumCheque
,TipoAplica,Consolida,CodCargo,NitOtros,NIO.RazonSocial AS NomNitOtros,CodSubgpo,Subgrupo,CiuOrigen,LD.Localidad AS NomCiudad,CodAgncia,Referncia
,NitDoc,ND.RazonSocial AS NomNitDoc,TipDocRef,DocRef,IdCiaRef
,C.TimeSys AS FechaAdd,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,VrDivisa1,VrDivisa2,VrDivisa3,TC.IdDiario AS CdDiario,Diario
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_Comprobantes AS C INNER JOIN Trn_ComDetalle AS D ON C.TipCom=D.TipCom AND C.Comprobante=D.Comprobante
 AND C.IdCia=D.IdCia  INNER JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN  Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN CtasCorrientes AS CTE ON C.IdCta=CTE.IdCta INNER JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero
 INNER JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NIO ON D.NitOtros=NIO.IdTercero
 LEFT JOIN Terceros AS ND ON D.NitDoc=ND.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN Localidades AS LD ON D.CiuOrigen=LD.IdLocal
 LEFT JOIN SubGrupos AS SG ON  D.CodSubgpo=SG.IdSubgrupo
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.TipCom LIKE ISNULL(@pmTipCom,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND C.TipDoc LIKE ISNULL(@pmTipDoc,'%')  AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND (C.EsEgreso=ISNULL(@pmEsEgreso,0) or C.EsEgreso=ISNULL(@pmEsEgreso,1))
 AND (C.Integrado=ISNULL(@pmIntegrado,0) or C.Integrado=ISNULL(@pmIntegrado,1))
 ORDER BY  C.TipCom,C.IdCia,C.Comprobante,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleNitCc @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdSubCos VARCHAR(16)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmCodAgncia VARCHAR(16)=Null,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null
,@pmIdCueIni VARCHAR(16)=Null,@pmIdCueFin VARCHAR(16)=Null
 AS 
 IF @pmIdCueIni IS NULL
  SELECT D.IdCuenta AS CdCuenta,IdTercero,IdCCosto,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Tercero<>0 AND CentroCosto<>0  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,IdTercero,IdCCosto,TipCom,IdCia
ELSE
  SELECT D.IdCuenta AS CdCuenta,IdTercero,IdCCosto,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Tercero<>0  AND CentroCosto<>0 AND  D.IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin
   AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,IdTercero,IdCCosto,TipCom,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleNitSub @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdSubCos VARCHAR(16)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmCodAgncia VARCHAR(16)=Null,@pmTipCom VARCHAR(3)=Null,@pmIntegrado BIT=Null
,@pmIdCueIni VARCHAR(16)=Null,@pmIdCueFin VARCHAR(16)=Null
 AS 
 IF @pmIdCueIni IS NULL
  SELECT D.IdCuenta AS CdCuenta,IdTercero,IdCCosto,IdSubCos,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Tercero<>0 AND CentroCosto<>0 AND SubCentro<>0
   AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,IdTercero,IdCCosto,IdSubCos,TipCom,IdCia
ELSE
  SELECT D.IdCuenta AS CdCuenta,IdTercero,IdCCosto,IdSubCos,TipCom,IdCia,SUM(VrDebito) AS STOTDEB,SUM(VrCredito) AS STOTCRE 
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta 
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Tercero<>0  AND CentroCosto<>0  AND  SubCentro<>0
  AND  D.IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin
   AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
  AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%')
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 GROUP BY D.IdCuenta,IdTercero,IdCCosto,IdSubCos,TipCom,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleRet @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCuenta VARCHAR(16)=Null
,@pmIdCueIni VARCHAR(16)=Null,@pmIdCueFin VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmTipCom VARCHAR(3)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null,@pmBaseGravable BIT=Null,@pmTercero BIT=Null,@pmIntegrado BIT=Null
 AS 
 IF @pmIdCueIni IS NULL
  SELECT D.IdCuenta AS CdCuenta,NomCuenta,Tercero,Naturaleza,BaseGravable,Porcntaje,D.IdTercero AS NitTercero,RazonSocial,TipoId,Dv,D.IdCia AS CdCia,Compania
 ,SUM(VrDebito-VrCredito) AS ValorTotal,SUM(VrBase) AS TotalBase,MAX(TarifaBase) AS TarifBase
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  INNER JOIN Terceros  AS T ON D.IdTercero=T.IdTercero
  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
  WHERE Movimiento<>0 AND Fecha  BETWEEN  @pmFechaIni AND @pmFechaFin AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND (BaseGravable=ISNULL(@pmBaseGravable,0) or BaseGravable=ISNULL(@pmBaseGravable,1))
  AND (Tercero=ISNULL(@pmTercero,0) or Tercero=ISNULL(@pmTercero,1))
  AND (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
  GROUP BY D.IdCuenta,NomCuenta,Tercero,Naturaleza,BaseGravable,Porcntaje,D.IdTercero,RazonSocial,TipoId,Dv,D.IdCia,Compania 
 ELSE
  SELECT D.IdCuenta AS CdCuenta,NomCuenta,Tercero,Naturaleza,BaseGravable,Porcntaje,D.IdTercero AS NitTercero,RazonSocial,TipoId,Dv,D.IdCia AS CdCia,Compania
 ,SUM(VrDebito-VrCredito) AS ValorTotal,SUM(VrBase) AS TotalBase,MAX(TarifaBase) AS TarifBase
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  INNER JOIN Terceros  AS T ON D.IdTercero=T.IdTercero
  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
  WHERE Movimiento<>0 AND Fecha  BETWEEN  @pmFechaIni AND @pmFechaFin AND D.IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND (BaseGravable=ISNULL(@pmBaseGravable,0) or BaseGravable=ISNULL(@pmBaseGravable,1))
  AND (Tercero=ISNULL(@pmTercero,0) or Tercero=ISNULL(@pmTercero,1))
  AND (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
  GROUP BY D.IdCuenta,NomCuenta,Tercero,Naturaleza,BaseGravable,Porcntaje,D.IdTercero,RazonSocial,TipoId,Dv,D.IdCia,Compania
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleRetTot @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCuenta VARCHAR(16)=Null
,@pmIdCueIni VARCHAR(16)=Null,@pmIdCueFin VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmTipCom VARCHAR(3)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmCodAgncia VARCHAR(16)=Null,@pmBaseGravable BIT=Null,@pmTercero BIT=Null,@pmIntegrado BIT=Null
 AS 
 IF @pmIdCueIni IS NULL
  SELECT SUM(VrDebito-VrCredito) AS ValorTotal,SUM(VrBase) AS TotalBase
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  WHERE Movimiento<>0 AND Fecha  BETWEEN  @pmFechaIni AND @pmFechaFin AND D.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND  IdTercero LIKE ISNULL(@pmIdTercero,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND (BaseGravable=ISNULL(@pmBaseGravable,0) or BaseGravable=ISNULL(@pmBaseGravable,1))
  AND (Tercero=ISNULL(@pmTercero,0) or Tercero=ISNULL(@pmTercero,1))
  AND (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 ELSE
  SELECT SUM(VrDebito-VrCredito) AS ValorTotal,SUM(VrBase) AS TotalBase
  FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
  WHERE Movimiento<>0 AND Fecha  BETWEEN  @pmFechaIni AND @pmFechaFin AND D.IdCuenta BETWEEN @pmIdCueIni AND @pmIdCueFin
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipCom LIKE ISNULL(@pmTipCom,'%')
  AND IdTercero LIKE ISNULL(@pmIdTercero,'%') AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
  AND (BaseGravable=ISNULL(@pmBaseGravable,0) or BaseGravable=ISNULL(@pmBaseGravable,1))
  AND (Tercero=ISNULL(@pmTercero,0) or Tercero=ISNULL(@pmTercero,1))
  AND (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleTot @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
AS
 SELECT SUM(VrDebito) AS TotalDeb,SUM(VrCredito) AS TotalCre,SUM(VrBase) AS TotalBaseGrav
 FROM  Trn_ComDetalle 
 WHERE TipCom=@pmTipCom 
 AND (Comprobante>=ISNULL(@pmComprobante,0)  AND Comprobante<=ISNULL(@pmComprobante,2147483647))
 AND IdCia like ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleVen @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,C.IdCuenta AS CodCuenta,Detalle,VrDebito,VrCredito,IdTercero,CodAgncia,IdVehiculo,IdCCosto,IdSubCos,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,Referncia,NitDoc,TipDocRef,DocRef,IdCiaRef,TimeSys,FecUpdate,IdCiaCrea
 FROM Trn_ComDetalle AS C INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia 
 AND  Vencimiento<>0  AND (TipoAplica='A' OR TipoAplica='C' ) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalleVis @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,D.IdCuenta AS CodCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,RazonSocial,IdVehiculo,D.IdCCosto AS IdCcsto,CCosto
,D.IdSubCos AS IdSbcosto,SubCosto,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado
,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,Referncia,NitDoc,TipDocRef,DocRef,IdCiaRef,D.IdUsuario AS IdUsuari,Usuario,TimeSys,D.FecUpdate AS FecUpdte,IdCiaCrea
 FROM Trn_ComDetalle AS D INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 INNER JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComDetalle_Crd @pmTipCom VARCHAR(3),@pmComprobanteIni INT,@pmComprobanteFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,TipoCom,Comprobante,D.IdCia AS CdCia,Compania,Item,Fecha,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,RazonSocial,IdVehiculo
,D.IdCCosto AS CdCentCosto,CCosto,D.IdSubCos AS CdSubCentro,SubCosto,VehPropio,VrBase,TarifaBase,TipDoc,Documento,IdCiaDoc,CodConce
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,CodSubgpo,CiuOrigen,CodAgncia,Referncia
,NitDoc,TipDocRef,DocRef,IdCiaRef,TimeSys,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_ComDetalle AS D INNER JOIN Companias AS C ON D.IdCia=C.IdCia
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 INNER JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 WHERE TipCom=@pmTipCom AND Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin AND D.IdCia=@pmIdCia
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFactura @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCia CHAR(2),@pmIdProveedor VARCHAR(16),@pmItem INT
 AS 
 SELECT TipFac,Factura,IdCia,Item,IdProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante,ItemCom
,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM Trn_ComFactura
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura AND IdCia=@pmIdCia AND IdProveedor=@pmIdProveedor
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaAbo @pmFecActual SMALLDATETIME,@pmIdCuenta VARCHAR(16)=Null,@pmIdProveedor VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null,@pmTipFac VARCHAR(3)=Null,@pmTipDoc VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT C.IdCuenta AS CdCuenta,NomCuenta,C.IdProveedor AS NitProveedor,RazonSocial,C.TipFac AS CdTipoFac,TipoDoc,C.Factura AS NumFactura,C.IdCia AS CdCia,CI.Compania AS NomCompania
,C.Item AS NumVence,VrFactura,VrAbonado,VrFactura-VrAbonado AS ValorSaldo,FecEmision,FecVence,DATEDIFF(day,FecVence,@pmFecActual) AS DiasMora
,C.TipDoc AS CdTipDoc,C.Documento AS NumDoc,C.IdCiaDoc AS CdCiaDoc,C.TipCom AS IdTipComFact,TC.TipoCom AS TipComFact,C.Comprobante AS NumComFact
,C.ItemCom AS ItemComFact,Detalle,Referencia,C.pVehiculo AS PlacaVeh,TipRef,DocRef,IdCiaRef
--Datos de abonos
,EG.TipCom AS CdTipoEgr,TEG.TipoCom AS TipoEgreso,EG.Comprobante AS NumEgreso,EG.IdCia AS CdCiaEgreso,CEG.Compania AS NomCiaEgreso
,EG.Item AS ItemAbono,EG.Fecha AS FecAbono,VrAbono,EG.EsEgreso AS Es_Egreso,DetAbono,EG.ItemCom AS ItemEgreso,EG.pVehiculo AS PlacaVehAbono,TipDev,EG.NumDev AS Num_Dev
--datos del egreso
,VrTotal,CM.IdCta AS CodCta,NumeroCta,CTE.IdBanco AS CodBanco,B.Banco AS NomBanco,EnEfectivo,NumCheque,FecCheque,Beneficiario,CM.Observacion AS ComObserv
--datos del proveedor
,TipoId,Dv,T.Codigo AS CodigoProv,NomCial,SiglaRaz,T.Direccion AS DirProveedor,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDep,Departamento
,Telefono,Fax,e_mail,SitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,BP.Banco AS BancoCuenta,Autoret
 FROM Trn_ComFactura AS C INNER JOIN Terceros AS T ON C.IdProveedor=T.IdTercero
 INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta INNER JOIN  Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TiposCom AS TC ON C.TipCom=TC.IdCom  LEFT JOIN Sys_TiposDoc AS TD ON C.TipFac=TD.IdDoc
 LEFT JOIN TercProvee AS TP ON C.IdProveedor=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 LEFT JOIN Trn_EgrFactura AS EG ON C.TipFac=EG.TipFac AND C.Factura=EG.Factura AND C.IdCia=EG.IdCiaFac 
 AND C.Item=EG.ItemFac AND C.IdProveedor=EG.IdProveedor
 LEFT JOIN TiposCom AS TEG ON EG.TipCom=TEG.IdCom
 LEFT JOIN  Companias AS CEG ON EG.IdCia=CEG.IdCia
 LEFT JOIN Trn_Comprobantes AS CM ON EG.TipCom=CM.TipCom  AND EG.Comprobante=CM.Comprobante  AND EG.IdCia=CM.IdCia
 LEFT  JOIN CtasCorrientes AS CTE ON CM.IdCta=CTE.IdCta  LEFT JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 WHERE  C.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND C.IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.TipDoc LIKE ISNULL(@pmTipDoc,'%' ) AND C.TipFac LIKE ISNULL(@pmTipFac,'%')
 AND (FecEmision>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecEmision<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY C.IdCuenta,RazonSocial,FecVence
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaCmp @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipFac,Factura,IdCia,Item,IdProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante
,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM Trn_ComFactura
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 ORDER BY TipFac,Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryComFacturaDab  @pmTipFac VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProveedor VARCHAR(16),@pmFactura VARCHAR(15)
 AS 
 SELECT C.Factura AS NumFact,C.Item AS NItem,C.IdProveedor AS NitProvee,C.ItemCom AS ItmComp
,VrFactura,VrAbonado,SUM(VrAbono) AS STOTABO
 FROM Trn_ComFactura AS C LEFT JOIN Trn_EgrFactura AS E ON C.TipFac=E.TipFac AND C.Factura=E.Factura
 AND C.IdCia=E.IdCiaFac AND C.Item=E.ItemFac AND C.IdProveedor=E.IdProveedor
 WHERE C.TipFac=@pmTipFac AND TipDoc=@pmTipFac AND Documento=@pmDocumento AND C.IdCia=@pmIdCia AND IdCiaDoc=@pmIdCia
 AND C.IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  C.Factura LIKE ISNULL(@pmFactura,'%')
 GROUP BY  C.Factura,C.Item,C.IdProveedor,C.ItemCom,VrFactura,VrAbonado
 ORDER BY  C.Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryComFacturaDoc  @pmTipFac VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProveedor VARCHAR(16),@pmFactura VARCHAR(15),@pmItemCom INT
 AS 
 SELECT TipFac,Factura,IdCia,Item,IdProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante
,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr 
 FROM Trn_ComFactura
 WHERE TipFac=@pmTipFac AND TipDoc=@pmTipFac AND Documento=@pmDocumento AND IdCia=@pmIdCia AND IdCiaDoc=@pmIdCia
 AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  Factura LIKE ISNULL(@pmFactura,'%')
 AND (ItemCom>=ISNULL(@pmItemCom,-10)  AND ItemCom<=ISNULL(@pmItemCom,0)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaDso @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmSaldo DECIMAL(14,2),@pmIdCia CHAR(2)=Null,@pmTipFac VARCHAR(3)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmIdProveedor VARCHAR(16)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmpVehiculo VARCHAR(10)=Null,@pmTipCom VARCHAR(3)=Null,@pmComprobante INT=Null
AS
 SELECT  TipFac, Factura, IdCia, Item,IdCuenta,IdProveedor,T.RazonSocial AS Proveedor,TipDoc, Documento, IdCiaDoc, FecEmision, FecVence, VrFactura,VrAbonado,VrFactura-VrAbonado AS ValorSaldo
,TipCom, Comprobante, ItemCom, Referencia, Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM   Trn_ComFactura   AS  V INNER JOIN Terceros AS T ON V.IdProveedor=T.IdTercero
 WHERE  FecVence BETWEEN @pmFechaIni AND @pmFechaFin AND (VrFactura-VrAbonado)>=@pmSaldo AND IdCia like ISNULL(@pmIdCia,'%%') AND TipFac like ISNULL(@pmTipFac,'%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
 AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND (Comprobante>=ISNULL(@pmComprobante,-1)  AND Comprobante<=ISNULL(@pmComprobante,2147483647)) 
ORDER BY TipFac,Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaDve @pmIdProveedor VARCHAR(16),@pmNitFletes VARCHAR(16)=Null
 AS 
 SELECT TipFac,Factura,IdCia,Item,VrFactura-VrAbonado AS ValorSaldo,VrFactura,VrAbonado,FecEmision,FecVence,TipDoc,Documento,IdCiaDoc
,Referencia,Detalle,IdProveedor,RazonSocial, F.IdCuenta AS IdCuent,NomCuenta,TipCom,Comprobante, ItemCom,TipRef,DocRef,IdCiaRef,pVehiculo,VehPropio
 FROM  Trn_ComFactura AS F INNER JOIN Terceros AS T ON F.IdProveedor=T.IdTercero  INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 WHERE VrFactura>VrAbonado AND IdProveedor IN (@pmIdProveedor,ISNULL(@pmNitFletes,@pmIdProveedor))
 ORDER BY FecVence,TipFac,IdCia,Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaGnit @pmSaldo DECIMAL(14,2),@pmIdProveedor VARCHAR(16)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmTipDoc VARCHAR(3)=Null,@pmTipFac VARCHAR(3)=Null
,@pmFecEmiIni SMALLDATETIME=Null,@pmFecEmiFin SMALLDATETIME=Null,@pmFecVenceIni SMALLDATETIME=Null,@pmFecVenceFin SMALLDATETIME=Null
AS
SELECT  IdProveedor,RazonSocial,SUM(VrFactura-VrAbonado) AS ValorSaldo,SUM(VrFactura) AS StotalFact,SUM(VrAbonado) AS StotalAbo
 FROM Trn_ComFactura AS F INNER JOIN Terceros AS T ON F.IdProveedor=T.IdTercero
 WHERE (VrFactura-VrAbonado)>@pmSaldo AND F.IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND F.IdCuenta LIKE ISNULL(@pmIdCuenta,'%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%' ) AND TipFac LIKE ISNULL(@pmTipFac,'%')
 AND (FecEmision>=ISNULL(@pmFecEmiIni,CAST('19100101' AS SMALLDATETIME)) AND FecEmision<=ISNULL(@pmFecEmiFin,CAST('20781230' AS SMALLDATETIME)))
 AND (FecVence>=ISNULL(@pmFecVenceIni,CAST('19100101' AS SMALLDATETIME)) AND FecVence<=ISNULL(@pmFecVenceFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY IdProveedor,RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaLta @pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME
,@pmIdCuenta VARCHAR(16)=Null,@pmIdProveedor VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmTipFac VARCHAR(3)=Null,@pmTipDoc VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT C.IdCuenta AS CdCuenta,NomCuenta,IdProveedor,RazonSocial,TipFac,TipoDoc,Factura,C.IdCia AS CdCia,Compania
,Item,VrFactura,VrAbonado,VrFactura-VrAbonado AS ValorSaldo,FecEmision,FecVence,DATEDIFF(day,FecVence,@pmFecActual) AS DiasMora
,TipDoc,Documento,IdCiaDoc,TipCom,TipoCom,Comprobante,ItemCom,Detalle,Referencia,pVehiculo,TipRef,DocRef,IdCiaRef,EstadoApr
--datos del proveedor
,TipoId,Dv,T.Codigo AS CodigoProv,NomCial,SiglaRaz,T.Direccion AS DirProveedor,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDep,Departamento
,Telefono,Fax,e_mail,SitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
 FROM Trn_ComFactura AS C INNER JOIN Terceros AS T ON C.IdProveedor=T.IdTercero
 INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta INNER JOIN  Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TiposCom AS TC ON C.TipCom=TC.IdCom  LEFT JOIN Sys_TiposDoc AS TD ON C.TipFac=TD.IdDoc
 LEFT JOIN TercProvee AS TP ON C.IdProveedor=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE (VrFactura-VrAbonado)>@pmSaldo AND C.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%' ) AND TipFac LIKE ISNULL(@pmTipFac,'%')
 AND (FecEmision>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecEmision<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY C.IdCuenta,RazonSocial,FecVence
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaNit @pmIdProveedor VARCHAR(16),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmTipFac VARCHAR(3)=Null,@pmpVehiculo VARCHAR(10)=Null
 AS 
 SELECT TipFac,Factura,IdCia,Item,VrFactura-VrAbonado AS ValorSaldo,VrFactura,VrAbonado,FecEmision,FecVence,TipDoc,Documento,IdCiaDoc
,IdProveedor,RazonSocial, F.IdCuenta AS IdCuent,NomCuenta,TipCom,Comprobante, ItemCom, Referencia, Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM  Trn_ComFactura AS F INNER JOIN Terceros AS T ON F.IdProveedor=T.IdTercero  INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 WHERE VrFactura>VrAbonado AND IdProveedor=@pmIdProveedor AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND TipFac LIKE ISNULL(@pmTipFac,'%') 
 --AND TipDoc LIKE ISNULL(@pmTipDoc,'%' ) AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
ORDER BY FecVence,TipFac,IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryComFacturaRef  @pmTipFac VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProveedor VARCHAR(16),@pmFactura VARCHAR(15),@pmReferencia VARCHAR(50)
 AS 
 SELECT TipFac,Factura,IdCia,Item,IdProveedor,IdCuenta,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante
,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM Trn_ComFactura
 WHERE TipFac=@pmTipFac AND TipDoc=@pmTipFac AND Documento=@pmDocumento AND IdCia=@pmIdCia AND IdCiaDoc=@pmIdCia
 AND Referencia=@pmReferencia AND ItemCom=0
 AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  Factura LIKE ISNULL(@pmFactura,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaSal @pmFechaAct SMALLDATETIME,@pmSaldo DECIMAL(14,2),@pmIdProveedor VARCHAR(16)=Null, @pmIdCia CHAR(2)=Null
,@pmIdCuenta VARCHAR(16)=Null,@pmTipDoc VARCHAR(3)=Null,@pmTipFac VARCHAR(3)=Null
AS
SELECT  TipFac, Factura, IdCia, Item,VrFactura-VrAbonado AS ValorSaldo,VrFactura,VrAbonado, FecEmision, FecVence,DATEDIFF(day,FecVence,@pmFechaAct) AS DiasMora,TipDoc, Documento, IdCiaDoc 
,IdProveedor,RazonSocial, F.IdCuenta AS IdCuent,NomCuenta,TipCom, Comprobante, ItemCom, Referencia, Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM Trn_ComFactura AS F INNER JOIN Terceros AS T ON F.IdProveedor=T.IdTercero  INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 WHERE (VrFactura-VrAbonado)>@pmSaldo AND F.IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%' ) AND TipFac LIKE ISNULL(@pmTipFac,'%')
ORDER BY RazonSocial,F.IdCuenta,FecVence,TipFac,IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComFacturaTnit @pmIdProveedor VARCHAR(16)
 AS 
 SELECT  COUNT(Documento) AS SCANT,SUM(VrFactura) AS STOTFAC,SUM(VrAbonado) AS STOTABO
 FROM Trn_ComFactura
 WHERE IdProveedor=@pmIdProveedor

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryComFactura_Ent  @pmTipFac VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
,@pmIdProveedor VARCHAR(16)=Null,@pmFactura VARCHAR(15)=Null
 AS 
 SELECT TipFac,Factura,C.IdCia AS CdCia,Compania,Item,IdProveedor,RazonSocial,C.IdCuenta AS CdCuenta,NomCuenta,TipDoc,Documento,IdCiaDoc
,FecEmision,FecVence,VrFactura,VrAbonado,TipCom,Comprobante,ItemCom,Referencia,Detalle,pVehiculo,VehPropio,TipRef,DocRef,IdCiaRef,EstadoApr
 FROM Trn_ComFactura AS C INNER JOIN Terceros AS T ON C.IdProveedor=T.IdTercero
 INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta INNER JOIN  Companias AS CI ON C.IdCia=CI.IdCia
 WHERE TipFac=@pmTipFac AND TipDoc=@pmTipFac AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin
 AND C.IdCia=@pmIdCia AND IdCiaDoc=@pmIdCia  AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%')
 AND  Factura LIKE ISNULL(@pmFactura,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCaja @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,IdTercero,CdAgencia,IdCCosto,IdSubCos,IdCajero
,NumForma,CdBanco,Referencia,NumVehiculo,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_CompCaja
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCajaBus @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT Recibo,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CdConcepto,Concepto,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,NumVehiculo
,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,B.Banco AS CtaBanco,C.IdTercero AS NitTercero,RazonSocial,CdAgencia,C.IdCCosto AS CdCcosto,CCosto,C.IdSubCos AS CdSubCosto,IdCajero,UC.Usuario AS NomCajero
,NumForma,CdBanco,BC.Banco AS NomBanco,Referencia,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ
,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDoc
 FROM Trn_CompCaja AS C INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero  INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN CentroCosto AS CC ON C.IdCCosto=CC.IdCCosto
 LEFT JOIN CtasCorrientes AS CTA ON C.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Bancos AS BC ON C.CdBanco=BC.IdBanco
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND C.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY C.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCajaBusDet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmIdUsuario VARCHAR(11)=Null
,@pmCdCta VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT R.TipDoc AS Tip_Doc,R.Recibo AS NumRecibo,R.IdCia AS CdCia,Compania,R.Fecha AS FechaDoc,R.IdConcepto AS CdConcepto,CN.Concepto AS ConceptoDoc
,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco,NumVehiculo
,R.IdTercero AS Nit_Tercero,T.RazonSocial AS NomTercero,CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,R.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,R.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto
,IdCajero,UC.Usuario AS NomCajero,NumForma,CdBanco,BR.Banco AS DocBanco,R.Referencia AS DocReferencia,OrigenAdd,TipCom,Comprobante,IdCiaCom,FacCalidad,SemInicial,SemFinal
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario
--detalle de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_CompCaja AS R INNER JOIN Trn_CompCajaDet AS D ON R.TipDoc=D.TipDoc AND R.Recibo=D.Recibo AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario  INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON R.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  CtasCorrientes AS CTA ON R.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON R.CdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco  
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 WHERE R.TipDoc=@pmTipDoc AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND CdCta LIKE ISNULL(@pmCdCta,'%')  AND R.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')  AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND R.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY R.IdCia,R.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCajaBusPag @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmIdUsuario VARCHAR(11)=Null
,@pmIdForma VARCHAR(4)=Null
 AS 
 SELECT Recibo,C.IdCia AS CdCia,Compania,C.Fecha AS FechaRec,C.IdConcepto AS CdConcepto,Concepto,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo
,C.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,C.IdCCosto AS CdCcosto,CCosto,C.IdSubCos AS CdSubCosto,IdCajero,UC.Usuario AS NomCajero,NumVehiculo
,Referencia,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,FacCalidad,SemInicial,SemFinal
,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalles pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NitCliente,TP.RazonSocial AS PagNomTercero
,P.NumForma AS NumCheque,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
,P.CdCta AS CtaCod,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono
 FROM Trn_CompCaja AS C INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero  INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN CentroCosto AS CC ON C.IdCCosto=CC.IdCCosto 
 INNER JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND  C.Recibo=P.Documento AND  C.IdCia=P.IdCia
 INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco   INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Terceros AS TP ON P.NitCliente=TP.IdTercero
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
  WHERE C.TipDoc=@pmTipDoc AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND C.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND P.IdForma LIKE ISNULL(@pmIdForma,'%')
 ORDER BY C.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCajaDet @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Recibo,IdCia,Item,IdConcepto,IdCuenta,Detalle,VrDebito,VrCredito,NitTercero,pVehiculo,CdCCosto,CdSubCos
,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumCheque,TipoAplica,NitOtros,CodAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
 FROM Trn_CompCajaDet
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCajaLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Recibo,IdCia,Fecha,C.IdConcepto AS CdConcepto,Concepto,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,C.IdTercero AS NitTercero,RazonSocial,CdAgencia,IdCCosto,IdSubCos
,IdCajero,UC.Usuario AS NomCajero,NumForma,CdBanco,Referencia,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDoc,NumVehiculo
 FROM Trn_CompCaja AS C INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCajaRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmCdCta VARCHAR(4)=Null,@pmEnEfectivo BIT=Null,@pmIdTercero VARCHAR(16)=Null,@pmCdAgencia VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmAnulado BIT=Null
 AS 
 SELECT R.TipDoc AS Tip_Doc,R.Recibo AS NumRecibo,R.IdCia AS CdCia,Compania,R.Fecha AS FechaDoc,R.IdConcepto AS CdConcepto,CN.Concepto AS ConceptoDoc
,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco
,R.IdTercero AS Nit_Tercero,T.RazonSocial AS NomTercero,CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,R.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,R.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto
,IdCajero,UC.Usuario AS NomCajero,NumForma,CdBanco,BR.Banco AS DocBanco,R.Referencia AS DocReferencia,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario
--detalle de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--agencia
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DAG.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,NumVehiculo
 FROM Trn_CompCaja AS R INNER JOIN Trn_CompCajaDet AS D ON R.TipDoc=D.TipDoc AND R.Recibo=D.Recibo AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario  INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON R.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  CtasCorrientes AS CTA ON R.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON R.CdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco  
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DAG ON LA.IdDep=DAG.IdDep
 WHERE R.TipDoc=@pmTipDoc AND R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND CdCta LIKE ISNULL(@pmCdCta,'%')  AND R.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')  AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY R.IdCia,R.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompCaja_Cr @pmTipDoc VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS Tip_Doc,TipoDoc,R.Recibo AS NumRecibo,R.IdCia AS CdCia,Compania,R.Fecha AS FechaDoc,R.IdConcepto AS CdConcepto,CN.Concepto AS ConceptoDoc
,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,EnEfectivo,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco
,R.IdTercero AS Nit_Tercero,T.RazonSocial AS NomTercero,CdAgencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,R.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,R.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto
,IdCajero,UC.Usuario AS NomCajero,NumForma,CdBanco,BR.Banco AS DocBanco,R.Referencia AS DocReferencia,FacCalidad,SemInicial,SemFinal,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario,Leyenda,NumVehiculo
--detalle de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--agencia
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DAG.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_CompCaja AS R INNER JOIN Trn_CompCajaDet AS D ON R.TipDoc=D.TipDoc AND R.Recibo=D.Recibo AND R.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario  INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON R.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  CtasCorrientes AS CTA ON R.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON R.CdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco   LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DAG ON LA.IdDep=DAG.IdDep
 WHERE R.TipDoc=@pmTipDoc AND R.Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND R.IdCia=@pmIdCia
 ORDER BY R.Recibo,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompGastos @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,IdPlantilla,IdTercero,TipFact,Factura,FecFactura,FecVence,VrTotal
,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_CompGastos
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompGastosLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdPlantilla VARCHAR(4)=Null
 AS 
 SELECT TipCom,TipoCom,Comprobante,C.IdCia AS CdCia,Compania,Fecha,C.IdPlantilla AS CdPlantilla,DescPlantilla,C.IdTercero AS NitTercero,RazonSocial,TipFact,Factura,FecFactura,FecVence,VrTotal
,Anulado,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
 FROM Trn_CompGastos AS C  INNER JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Plantillas AS PL ON C.IdPlantilla=PL.IdPlantilla
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND C.IdPlantilla LIKE ISNULL(@pmIdPlantilla,'%') 
 ORDER BY  TipCom,C.IdCia,Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryCompanias @pmIdCia CHAR(2)
 AS 
 SELECT IdCia,Compania,DirInterface,FechaActual,PeriodoActual,IdCCosto,IdSubCos,IdTercero
,CodSucsal,DireccSuc,TelefSuc,FaxSuc,IdLocal,TipoEDS,CodPlanta,FechaAdd,FechaUpdate,Inactivo
 FROM Companias
 WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompaniasDso
AS
  SELECT  IdCia, Compania,IdCia+'  '+Compania AS DsCia
  FROM Companias WHERE  Inactivo=0 ORDER BY IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompaniasLta @pmIdCia CHAR(2)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdCia,Compania,DirInterface,FechaActual,PeriodoActual,Inactivo,IdCCosto,IdSubCos,IdTercero
,CodSucsal,DireccSuc,TelefSuc,FaxSuc,IdLocal,TipoEDS,CodPlanta,FechaAdd,FechaUpdate
 FROM Companias
 WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCompanias_Cr @pmInactivo BIT=Null
 AS 
 SELECT IdCia,Compania,DirInterface,FechaActual,PeriodoActual,C.IdCCosto AS CodCcosto,CCosto,C.IdSubCos AS CodSubCosto,SubCosto
,IdTercero,CodSucsal,DireccSuc,TelefSuc,FaxSuc,C.IdLocal AS CodCiu,Localidad,TipoEDS,CodPlanta,C.FechaAdd AS Fecha_Add,C.FechaUpdate AS FecUpd,C.Inactivo AS Inactvo
 FROM Companias AS C INNER JOIN CentroCosto AS CC ON C.IdCCosto=CC.IdCCosto 
 INNER JOIN  SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal
 WHERE (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantes @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr
,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,Observacion,IdEstado,Propddes,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Comprobantes
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesCaj @pmFecha SMALLDATETIME,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
 AS 
 SELECT  TipCom,IdCia,IdUsuario,SUM(VrTotal) AS STOTAL
 FROM Trn_Comprobantes
 WHERE EsEgreso<>0 AND EnEfectivo<>0 AND Fecha=@pmFecha AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND  IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY TipCom,IdCia,IdUsuario 
 ORDER BY IdCia,IdUsuario 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesChe @pmNumCheque VARCHAR(20),@pmIdCta VARCHAR(4), @pmIdCia CHAR(2), @pmTipCom VARCHAR(3),@pmEsEgreso BIT
AS
 SELECT TipCom, Comprobante, IdCia, Fecha, IdCta, EnEfectivo, NumCheque,FecCheque,EsEgreso
 FROM  Trn_Comprobantes 
 WHERE NumCheque=@pmNumCheque AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCta LIKE ISNULL(@pmIdCta,'%')
 AND TipCom LIKE ISNULL(@pmTipCom,'%')  AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 ORDER BY  TipCom,IdCia,Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesChx @pmNumCheque VARCHAR(20),@pmIdCta VARCHAR(4), @pmIdCia CHAR(2),@pmEsEgreso BIT
--CONSULTA : SI EXISTE EL CHEQUE EN LA CUENTA
AS
 SELECT TipCom, Comprobante, IdCia, Fecha, IdCta, EnEfectivo, NumCheque,EsEgreso
 FROM  Trn_Comprobantes  
 WHERE EnEfectivo=0 AND Anulado=0 AND NumCheque=@pmNumCheque AND IdCta=@pmIdCta AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 ORDER BY  TipCom,IdCia,Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesDoc  @pmTipDoc VARCHAR(3), @pmDocumento INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,IdTercero,VrTotal,IdCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr
,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,Observacion,IdEstado,Propddes,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Comprobantes
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento  AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY  TipCom,IdCia, Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesDso @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME, @pmTipCom VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmAnulado BIT=Null,@pmIntegrado BIT=Null,@pmEsEgreso BIT=Null,@pmAnticipo BIT=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT TipCom,Comprobante,C.IdCia AS CodCia,Compania,Fecha,C.IdTercero AS NitTercero,RazonSocial,VrTotal,C.IdCta AS CodCta,NumeroCta,EnEfectivo,NumCheque,FecCheque,TipDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr
,NEgreso,pVehiculo,VehPropio,CedCondtor,CodConce,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3,PermEditar,Integrado,EsEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS IdEstad,Estado,Propddes,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Comprobantes AS C INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN CtasCorrientes AS CTA ON C.IdCta=CTA.IdCta INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipCom LIKE ISNULL(@pmTipCom,'%') AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
AND  (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1)) 
AND (Anticipo=ISNULL(@pmAnticipo,0) or Anticipo=ISNULL(@pmAnticipo,1)) 
 ORDER BY TipCom,C.IdCia,Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesEgr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCta VARCHAR(4)=Null
,@pmFecEgrIni SMALLDATETIME=Null,@pmFecEgrFin SMALLDATETIME=Null
 AS 
 SELECT TipCom,Comprobante,C.IdCia AS CdCia,Compania,Fecha,NumCheque,FecCheque,VrTotal,C.IdCta AS CodCta,NumeroCta
,CTE.IdBanco AS CodBanco,Banco,C.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,Beneficiario,pVehiculo
,C.CedCondtor AS NitOtros,CT.RazonSocial AS NomNitOtros,C.EsEgreso AS EsCompEgreso,Anticipo,C.Observacion AS Observ
,TipDoc,Documento,IdCiaDoc,C.IdEstado AS CdEstado,Propddes,TimeSys,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario
,TipoCom,TC.IdDiario AS CdDiario,Diario
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,Localidad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_Comprobantes AS C INNER JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN CtasCorrientes AS CTE ON C.IdCta=CTE.IdCta INNER JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS CT ON C.CedCondtor=CT.IdTercero
 WHERE  ((FecCheque BETWEEN @pmFechaIni AND @pmFechaFin) OR FecCheque IS NULL) 
 AND (C.EsEgreso<>0 OR Anticipo<>0)  AND EnEfectivo=0  AND Anulado=0
 AND TipCom LIKE ISNULL(@pmTipCom,'%') AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND C.IdCta LIKE ISNULL(@pmIdCta,'%')
 AND (Fecha>=ISNULL(@pmFecEgrIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFecEgrFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Fecha,TipCom,C.IdCia,Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipCom VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmTipDoc VARCHAR(3)=Null,@pmEsEgreso BIT=Null,@pmIntegrado BIT=Null
 AS 
 SELECT TipCom,TipoCom,Comprobante,C.IdCia AS CdCia,Compania,Fecha,C.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,VrTotal,C.IdCta AS CodCta,NumeroCta
,CTE.IdBanco AS CodBanco,Banco,EnEfectivo,NumCheque,FecCheque,TipDoc,TipoDoc,Documento,IdCiaDoc,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,VehPropio
,C.CedCondtor AS NitOtros,CT.RazonSocial AS NomNitOtros,CodConce,Concepto,Beneficiario,Integrado,C.EsEgreso AS EsCompEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS CodEstado,Estado,Propddes
,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,VrDivisa1,VrDivisa2,VrDivisa3,TC.IdDiario AS CdDiario,Diario
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,Localidad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_Comprobantes AS C INNER JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN  Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN CtasCorrientes AS CTE ON C.IdCta=CTE.IdCta INNER JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS CT ON C.CedCondtor=CT.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipCom LIKE ISNULL(@pmTipCom,'%')
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%') 
 AND (C.EsEgreso=ISNULL(@pmEsEgreso,0) or C.EsEgreso=ISNULL(@pmEsEgreso,1))
 AND (Integrado=ISNULL(@pmIntegrado,0) or Integrado=ISNULL(@pmIntegrado,1))
 ORDER BY  TipCom,C.IdCia,Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesNom  @pmTipDoc VARCHAR(3), @pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmCodConce VARCHAR(4)
,@pmNEgreso INT=Null,@pmIdTercero VARCHAR(16)=Null
 AS 
 SELECT TipCom,Comprobante,IdCia,Fecha,Integrado
 FROM Trn_Comprobantes
 WHERE Anulado=0 AND  Propddes='NOMINA'  AND TipDoc=@pmTipDoc AND Documento=@pmDocumento 
 AND CodConce=@pmCodConce AND NEgreso<=@pmNEgreso AND IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND  IdCiaDoc LIKE ISNULL(@pmIdCiaDoc,'%%')
 ORDER BY  TipCom,IdCia, Comprobante
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantesTotCaj @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT  TipCom,SUM(CASE EnEfectivo WHEN 1 THEN VrTotal ELSE 0 END)  AS  STOTCAJA
,SUM(CASE EnEfectivo WHEN 0 THEN  VrTotal ELSE 0 END)  AS  STOTOTROS
 FROM Trn_Comprobantes
 WHERE EsEgreso<>0 AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND  IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY TipCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryComprobantes_Cr @pmTipCom VARCHAR(3),@pmComprobanteIni INT,@pmComprobanteFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipCom AS IdTipoComp,TipoCom,C.Comprobante AS NumComprob,C.IdCia AS CdCia,Compania,C.Fecha AS FechaComp
,C.IdTercero AS ComNitTercero,T.RazonSocial AS ComNomTercero,VrTotal,C.IdCta AS IdCtaCte,NumeroCta,CTA.IdBanco AS CdBanco,Banco
,EnEfectivo,C.NumCheque AS ComNumCheque,FecCheque,Anulado,NumDev,FecDev,TipEgr,NEgreso,pVehiculo,C.VehPropio AS VehEsPropio
,CedCondtor,CD.RazonSocial AS NomConductor,C.CodConce AS CodConcepto,Concepto,Beneficiario,VrDivisa1,VrDivisa2,VrDivisa3
,PermEditar,C.Integrado AS EsIntegracion,C.EsEgreso AS ComEsEgreso,Anticipo,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado
,Propddes,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
--detalle
,Item,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,IdVehiculo,D.VehPropio AS DetVehPropio
,D.IdCCosto AS CdCentCosto,CCosto,D.IdSubCos AS CdSubCentro,SubCosto,VrBase,TarifaBase,D.TipDoc AS TipoDoc,D.Documento AS NumDoc,D.IdCiaDoc AS CdCiaDoc
,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,D.NumCheque AS DetNumCheque,TipoAplica,Consolida,CodCargo
,NitOtros,DO.RazonSocial AS NombreOtros,CodSubgpo,Subgrupo,CiuOrigen,DL.Localidad AS NomCiudadOrigen,CodAgncia,DA.Agencia AS NomAgencia,CodAgencia,Referencia
,Referncia,NitDoc,DND.RazonSocial AS NombreNitDoc,TipDocRef,DocRef,IdCiaRef
--informacion del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_Comprobantes  AS C INNER JOIN Trn_ComDetalle AS D ON C.TipCom=D.TipCom AND C.Comprobante=D.Comprobante AND C.IdCia=D.IdCia
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero  LEFT JOIN CentroCosto AS O ON D.IdCCosto=O.IdCCosto
 LEFT JOIN CtasCorrientes AS CTA ON C.IdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Terceros AS CD ON C.CedCondtor=CD.IdTercero LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS DO ON D.NitOtros=DO.IdTercero
 LEFT JOIN SubGrupos AS S ON D.CodSubgpo=S.IdSubgrupo
 LEFT JOIN Localidades AS DL ON D.CiuOrigen=DL.IdLocal
 LEFT JOIN Agencias AS DA ON D.CodAgncia=DA.IdAgencia
 LEFT JOIN Terceros AS DND ON D.NitDoc=DND.IdTercero
 WHERE C.Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin AND C.IdCia=@pmIdCia
 AND  C.TipCom LIKE ISNULL(@pmTipCom,'%') 
 ORDER BY C.Comprobante,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConcDivCue @pmIdConcepto VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdConcepto,IdClase,IdCuenta,CdCueDev,TipoMov,TipoTerc,FecAdd,FecUpdate
 FROM ConcDivCue
 WHERE IdConcepto=@pmIdConcepto
 AND IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConcDivCueLta @pmIdConcepto VARCHAR(4)=Null,@pmIdClase VARCHAR(4)=Null
,@pmGrupo VARCHAR(20)=Null,@pmEsServicio BIT=Null
 AS 
 SELECT D.IdConcepto AS CdConcepto,Concepto,D.IdClase AS CdClase,NomClase,D.IdCuenta AS IdCue,P.NomCuenta AS NombreCue
,CdCueDev,PD.NomCuenta AS NomCueDev,TipoMov,TipoTerc,Grupo,EsServicio,DeManfsto,FecAdd,FecUpdate
 FROM ConcDivCue AS D INNER JOIN  ConcDiversos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta INNER JOIN ClaseDiv AS CD ON D.IdClase=CD.IdClase
 LEFT JOIN Puc AS PD ON D.CdCueDev=PD.IdCuenta
 WHERE C.Inactivo=0 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND D.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND Grupo LIKE ISNULL(@pmGrupo,'%')
 AND (EsServicio=ISNULL(@pmEsServicio,0) or EsServicio=ISNULL(@pmEsServicio,1))
 ORDER BY D.IdConcepto,D.IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConcDiversos @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,Grupo,EsServicio,DeManfsto,CdIva,CdRet,CdIca,IdCueDeb,IdCueCre,Inactivo
,CdAutoret,FechaAdd,FechaUpdate,IdUsuario
 FROM ConcDiversos
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConcDiversosDso @pmGrupo VARCHAR(20)=Null
 AS 
 SELECT IdConcepto,Concepto,Grupo,Concepto+' '+IdConcepto+' ('+Grupo+')' AS DsCon
 FROM ConcDiversos 
 WHERE Inactivo=0 AND Grupo LIKE ISNULL(@pmGrupo,'%')
 ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConcDiversosLta @pmGrupo VARCHAR(20)=Null,@pmEsServicio BIT=Null
 AS 
 SELECT IdConcepto,Concepto,Grupo,EsServicio,DeManfsto,CdIva,TI.Tarifa AS TarfIva,CdRet
,TR.Tarifa AS TarfRet,CdIca,TC.Tarifa AS TarfIca,CdAutoret,IdCueDeb,PD.NomCuenta AS NomCueDeb,IdCueCre,PC.NomCuenta AS NomCueCre
,D.IdUsuario AS IdUsuari,Usuario,D.FechaAdd AS Fech_Add,D.FechaUpdate  AS Fech_Update
 FROM ConcDiversos AS D INNER JOIN  adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 LEFT JOIN  Tablapor AS TI ON D.CdIva=TI.IdTarifa
 LEFT JOIN  Tablapor AS TR ON D.CdRet=TR.IdTarifa
 LEFT JOIN  Tablapor AS TC ON D.CdIca=TC.IdTarifa
 LEFT JOIN Puc AS PD ON D.IdCueDeb=PD.IdCuenta
 LEFT JOIN Puc AS PC ON D.IdCueCre=PC.IdCuenta
 WHERE D.Inactivo=0 AND Grupo LIKE ISNULL(@pmGrupo,'%')
 AND  (EsServicio=ISNULL(@pmEsServicio,0) or EsServicio=ISNULL(@pmEsServicio,1))
 ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConceptos @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,IdDoc,IdCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado
,TrfaSanc,RndTotal,Inactivo,DcmtoEquiv,FechaAdd,FechaUpdate,IdUsuario
 FROM Conceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryConceptosAdmon @pmIdConcepto VARCHAR(4)
,@pmIdAdmon VARCHAR(4),@pmIdClase INT
 AS 
 SELECT IdConcepto,IdAdmon,IdClase,IdCuenta,FechaAdd,FechaUpdate
 FROM ConceptosAdmon
 WHERE IdConcepto=@pmIdConcepto AND IdAdmon=@pmIdAdmon 
 AND (IdClase>=ISNULL(@pmIdClase,0)  AND IdClase<=ISNULL(@pmIdClase,2147483647))
 ORDER BY IdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryConceptosAdmonLta @pmIdConcepto VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null,@pmIdDoc VARCHAR(3)=Null
 AS
 SELECT A.IdAdmon AS IdAdm,TipoAdmon,A.IdCuenta AS IdCuent,NomCuenta,A.IdConcepto AS IdConcept,Concepto
,IdClase,IdDoc,A.FechaAdd AS FechAdd,A.FechaUpdate AS FechUpdate
 FROM ConceptosAdmon AS A INNER JOIN Conceptos AS C ON A.IdConcepto=C.IdConcepto
 INNER JOIN TiposAdm AS TA ON A.IdAdmon=TA.IdAdmon
 INNER JOIN Puc AS P ON A.IdCuenta=P.IdCuenta
 WHERE A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND  A.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
  AND IdDoc LIKE ISNULL(@pmIdDoc,'%')
 ORDER BY A.IdConcepto,A.IdAdmon,IdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConceptosCia @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT C.IdConcepto,Concepto,Concepto+' '+C.IdConcepto+ '  ('+ IdDoc+')' AS DsCon
 FROM Conceptos AS C LEFT JOIN ConceptosCia AS CN ON C.IdConcepto=CN.IdConcepto
 WHERE Inactivo=0 AND IdDoc=@pmIdDoc AND (CN.IdCia=@pmIdCia OR CN.IdCia IS NULL)
 ORDER BY IdDoc,Concepto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConceptosCue @pmIdConcepto VARCHAR(4),@pmIdClase INT
 AS 
 SELECT IdConcepto,IdClase,Descripcion,IdCuenta,TipoMov,DetalleMov,CdCCosto,CdSubCos,TipoTerc,FecAdd,FecUpdate
 FROM ConceptosCue
 WHERE IdConcepto=@pmIdConcepto
 AND (IdClase>=ISNULL(@pmIdClase,0)  AND IdClase<=ISNULL(@pmIdClase,2147483647))
 ORDER BY IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConceptosCueLta @pmIdConcepto VARCHAR(4)=Null,@pmIdDoc VARCHAR(3)=Null
,@pmInactivo BIT=Null
 AS 
 SELECT CC.IdConcepto AS CdConcep,Concepto,IdClase,Descripcion,CC.IdCuenta AS CdCuenta,NomCuenta,TipoMov,DetalleMov,CdCCosto,CdSubCos
,C.IdDoc AS CdTipo,TipoDoc,C.IdCom AS CdComp,TipoCom,TipoTerc,FecAdd,FecUpdate
 FROM ConceptosCue AS CC INNER JOIN Conceptos AS C ON CC.IdConcepto=C.IdConcepto
 INNER JOIN Puc AS P ON CC.IdCuenta=P.IdCuenta
 INNER JOIN  Sys_TiposDoc AS T ON C.IdDoc=T.IdDoc
 INNER JOIN TiposCom AS TC ON C.IdCom=TC.IdCom
 WHERE CC.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND C.IdDoc LIKE ISNULL(@pmIdDoc,'%')
 AND  (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY CC.IdConcepto,IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConceptosDso @pmIdDoc VARCHAR(3)=Null
 AS 
 SELECT IdConcepto,Concepto,Pdterminado,Concepto+' '+IdConcepto+ '  ('+ IdDoc+')' AS DsCon
 FROM Conceptos
 WHERE Inactivo=0 AND IdDoc LIKE ISNULL(@pmIdDoc,'%')
 ORDER BY IdDoc,Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConceptosLta @pmIdDoc VARCHAR(3)=Null,@pmIdCom VARCHAR(3)=Null,@pmPdterminado INT=Null
,@pmInactivo  BIT=Null
 AS 
 SELECT IdConcepto,Concepto,C.IdDoc AS CdTipo,TipoDoc,C.IdCom AS CdTcmp,TipoCom,Vcmto,Imptos,Retfte,RetIca,Observacion,Pdterminado
,TrfaSanc,RndTotal,DcmtoEquiv,C.FechaAdd AS Fec_Add,C.FechaUpdate AS Fec_Upd,C.IdUsuario AS IdUsuari,Usuario,C.Inactivo AS Inactiv
 FROM Conceptos AS C INNER JOIN Sys_TiposDoc AS T ON C.IdDoc=T.IdDoc
 INNER JOIN TiposCom AS TC ON C.IdCom=TC.IdCom
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE C.IdDoc LIKE ISNULL(@pmIdDoc,'%') AND C.IdCom LIKE ISNULL(@pmIdCom,'%')
 AND (Pdterminado>=ISNULL(@pmPdterminado,-1)  AND Pdterminado<=ISNULL(@pmPdterminado,2147483647))
 AND  (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY C.IdDoc,Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConsigna @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Consignacion,IdCia,Fecha,IdConcepto,VrEfectivo,VrOtros,VrTotal,EnEfectivo,IdCta,IdCuenta,IdCliente,IdCCosto,IdSubCos,IdCajero,NitDeptante,Depositante
,TelDeptante,Referencia,Referncia2,NumConsigna,RangosDoc,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Consigna
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConsignaLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmConsignacionIni INT=Null
,@pmConsignacionFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdCta VARCHAR(4)=Null,@pmEnEfectivo BIT=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Consignacion,IdCia,Fecha,C.IdConcepto AS CdConcepto,Concepto,VrEfectivo,VrOtros,VrTotal,EnEfectivo,C.IdCta AS CdCta,NumeroCta,CT.IdBanco AS CdBanco,Banco,C.IdCuenta AS CdCuenta,IdCliente,RazonSocial
,IdCCosto,IdSubCos,IdCajero,UC.Usuario AS NomCajero,NitDeptante,Depositante,TelDeptante,Referencia,Referncia2,NumConsigna,RangosDoc,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_Consigna AS C INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN CtasCorrientes AS CT ON C.IdCta=CT.IdCta INNER JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Consignacion BETWEEN ISNULL(@pmConsignacionIni,0) AND ISNULL(@pmConsignacionFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdCta LIKE ISNULL(@pmIdCta,'%') AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY IdCia,Consignacion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConsignaRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCta VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
,@pmIdCliente VARCHAR(16)=Null,@pmAnulado BIT=Null
 AS 
 SELECT C.TipDoc AS Tip_Doc,Consignacion,C.IdCia AS CodCia,Compania,C.Fecha AS FechaDoc,C.IdConcepto AS CdConcepto,Concepto,VrEfectivo,VrOtros,VrTotal,EnEfectivo
,C.IdCta AS CdCta,NumeroCta,CT.IdBanco AS CdBanco,B.Banco AS BancoCta,B.IdClase AS CdBanClase,B.CodEntidad AS BanCodigo,B.Direccion AS BanDireccion,B.Contacto AS BanContacto
,C.IdCuenta AS CdCuenta,NomCuenta,IdCliente,T.RazonSocial AS RaznSocial,C.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,C.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS NomCajero
,NitDeptante,Depositante,TelDeptante,C.Referencia AS CnsReferencia,C.Referncia2 AS CnsReferencia2,NumConsigna,RangosDoc,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalle de pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco,NitCliente,PC.RazonSocial AS NomCliente,CdAgencia,A.Agencia AS NomAgencia
,CodAgencia,A.Referencia AS AgeReferencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2,CdLocal,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza,DF.Departamento AS DptoPlaza
,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
 FROM Trn_Consigna AS C  INNER JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND C.Consignacion=P.Documento AND C.IdCia=P.IdCia
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  
 INNER JOIN CtasCorrientes AS CT ON C.IdCta=CT.IdCta INNER JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero INNER JOIN Puc AS PU ON C.IdCuenta=PU.IdCuenta
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN CentroCosto AS CC ON C.IdCCosto=CC.IdCCosto 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos 
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Terceros AS PC ON P.NitCliente=PC.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 WHERE C.TipDoc=@pmTipDoc  AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdCta LIKE ISNULL(@pmIdCta,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY C.IdCia,Consignacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryConsigna_Cr @pmTipDoc VARCHAR(3),@pmConsignacionIni INT,@pmConsignacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,Consignacion,C.IdCia AS CodCia,Compania,C.Fecha AS FechaDoc,C.IdConcepto AS CdConcepto,Concepto,VrEfectivo,VrOtros,VrTotal,EnEfectivo
,C.IdCta AS CdCta,NumeroCta,CT.IdBanco AS CdBanco,B.Banco AS BancoCta,B.IdClase AS CdBanClase,B.CodEntidad AS BanCodigo,B.Direccion AS BanDireccion,B.Contacto AS BanContacto
,C.IdCuenta AS CdCuenta,NomCuenta,IdCliente,T.RazonSocial AS RaznSocial,C.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,C.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS NomCajero
,NitDeptante,Depositante,TelDeptante,C.Referencia AS CnsReferencia,C.Referncia2 AS CnsReferencia2,NumConsigna,RangosDoc,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--detalle de pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco,NitCliente,PC.RazonSocial AS NomCliente,CdAgencia,A.Agencia AS NomAgencia
,CodAgencia,A.Referencia AS AgeReferencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2,CdLocal,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza,DF.Departamento AS DptoPlaza
,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
 FROM Trn_Consigna AS C  INNER JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND C.Consignacion=P.Documento AND C.IdCia=P.IdCia
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN CtasCorrientes AS CT ON C.IdCta=CT.IdCta INNER JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero INNER JOIN Puc AS PU ON C.IdCuenta=PU.IdCuenta
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN CentroCosto AS CC ON C.IdCCosto=CC.IdCCosto 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos  LEFT JOIN TiposCom AS TC ON C.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Terceros AS PC ON P.NitCliente=PC.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 WHERE C.TipDoc=@pmTipDoc AND Consignacion BETWEEN @pmConsignacionIni AND @pmConsignacionFin AND C.IdCia=@pmIdCia
 ORDER BY Consignacion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacion @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Cotizacion,IdCia,Fecha,FechaVence,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,IdVend,TarifaCom
,CodTarCom,DirContacto,IdLocal,DiasVigencia,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,TipFac,Factura,IdCiaFac,FechaFact
,Modalidad,OrigenAdd,Anulado,FecDev,Observacion,IdEstado,NumAprob,FecAprob,CdUsuAprob,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Cotizacion
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacionDet  @pmCotizacion INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,Salidas,VrPrecio,Salidas*VrPrecio AS VrSubTotal
,TarifaDct,VrDctoSal,TarifaIva,VrIvaSal,((Salidas*VrPrecio)-VrDctoSal)+VrIvaSal AS VrTotal,VrBruto,K.ListaPrec AS LtaPrec,VrBase,K.Referencia AS Referncia,Referencia2,Unidades
,CodTarDct,CodTarIva,K.IdVend AS NitVend,V.RazonSocial AS Vendedor,Comision,K.CodTarCom AS CdTarifCom,DescripProd,Servcios,Tanques,CdTanque,CdMoneda,VrTasaCamb
,C.Fecha AS FecPedido,FechaVence,IdCliente,T.RazonSocial AS Cliente,IdAgencia,Modalidad,C.Factura AS NumFactura,IdCiaFac,FechaFact
 FROM  Trn_Cotizacion AS C INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.Cotizacion=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN Terceros AS  V ON K.IdVend=V.IdTercero
 WHERE C.Cotizacion=@pmCotizacion  AND C.IdCia=@pmIdCia
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacionFac @pmFactura INT,@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT Cotizacion,IdCia,Fecha,FechaVence,C.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros
,VrOtrDcto,VrNeto,Cantidad,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirContacto,C.IdLocal AS CdCiudad,DiasVigencia,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma
,DetallePago,MulPlazos,IdPlazo,CdMney,Modalidad,C.Observacion AS Observ,TimeSys,IdCiaCrea
 FROM Trn_Cotizacion AS C INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND Factura<=@pmFactura
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,Cotizacion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacionFact @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmCotizacionIni INT=Null,@pmCotizacionFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocal VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaFacIni SMALLDATETIME=Null,@pmFechaFacFin SMALLDATETIME=Null
,@pmTipoVentaIni INT=Null,@pmTipoVentaFin INT=Null
 AS 
 SELECT  O.Cotizacion AS NumCotizacion,O.IdCia AS CdCia,Compania,O.Fecha AS FechaCotiza,FechaVence,K.IdProducto AS CodProducto,DescripProd
,SUM(Salidas) AS CantPedido,SUM(Unidades) AS Unidades
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,Unidad,IdCliente,T.RazonSocial AS NombreCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Modalidad,O.IdConcepto AS CdConcepto,Concepto
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,VrFletes,DiasVigencia,NitContac,NomContac,NumAprob,FecAprob
,O.Observacion AS Observ,DetallePago,O.IdEstado AS CdEstado,Estado
,MAX(VrPrecio) AS CotValorUnit,SUM(Salidas*VrPrecio) AS CotValorTotal,SUM(VrIvaSal) AS CotIva,SUM(VrDctoSal) AS CotDcto
 FROM Trn_Cotizacion AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia 
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Cotizacion=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
  LEFT JOIN (SELECT  Cotizacion,IdCiaCot,K.TipDoc AS TipoFact,K.Documento AS NumFactura,K.IdCia AS CdCiaFac,KC.Compania AS NomCiaFac
  ,TipOrd,NumOrden,IdCiaOrd,K.Fecha AS FecFactura,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd
,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaEnt) AS IvaEntradas
,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoEnt) AS DctoEntradas,SUM(VrDctoSal) AS DctoSalidas,SUM(K.Unidades) AS TotalUnidades
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE NoVentas BETWEEN ISNULL(@pmTipoVentaIni,1) AND ISNULL(@pmTipoVentaFin,4) AND Cotizacion>0
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaFacIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFacFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY  Cotizacion,IdCiaCot,K.TipDoc,K.Documento,K.IdCia,KC.Compania,TipOrd,NumOrden,IdCiaOrd,K.Fecha,K.IdProducto,PM.DescripProd) AS FA 
 ON  O.Cotizacion=FA.Cotizacion AND O.IdCia=FA.IdCiaCot AND K.IdProducto=FA.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND O.Cotizacion  BETWEEN ISNULL(@pmCotizacionIni,0) AND ISNULL(@pmCotizacionFin,2147483647)
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
GROUP BY  O.Cotizacion,O.IdCia,Compania,O.Fecha,FechaVence,K.IdProducto,DescripProd
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,Modalidad,O.IdConcepto,Concepto
,O.IdVend,VN.RazonSocial,VrFletes,DiasVigencia,NitContac,NomContac,NumAprob,FecAprob
,O.Observacion,DetallePago,O.IdEstado,Estado

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacionLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmCotizacionIni INT=Null,@pmCotizacionFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Cotizacion,IdCia,Fecha,FechaVence,C.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros
,VrOtrDcto,VrNeto,Cantidad,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirContacto,C.IdLocal AS CdCiudad,DiasVigencia,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma
,DetallePago,MulPlazos,IdPlazo,CdMney,TipFac,Factura,IdCiaFac,FechaFact,Modalidad,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado
,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Cotizacion AS C INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Cotizacion BETWEEN ISNULL(@pmCotizacionIni,0) AND ISNULL(@pmCotizacionFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Cotizacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacionRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocal VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Cotizacion,O.IdCia AS CdCia,Compania,Fecha,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NombreCliente
,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirContacto,O.IdLocal AS CdCiudEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,DiasVigencia,O.NitContac AS CotNitContac,O.NomContac AS CotNomContac,O.TelContac AS CotTelContac,O.emlContac AS CotEmailContac,CargoContac,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago
,TipFac,Factura,IdCiaFac,FechaFact,Modalidad,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,TimeSys,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Cotizacion AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,O.Cotizacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCotizacion_Cr @pmTipDoc VARCHAR(3),@pmCotizacionIni INT,@pmCotizacionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc AS Tip_Doc,TipoDoc,O.Cotizacion AS NumCotizacion,O.IdCia AS CdCia,Compania,O.Fecha AS FechaDoc,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NombreCliente
,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirContacto,O.IdLocal AS CdCiudEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,DiasVigencia,O.NitContac AS CotNitContac,O.NomContac AS CotNomContac,O.TelContac AS CotTelContac,O.emlContac AS CotEmailContac,CargoContac,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago
,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact,Modalidad,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,O.TimeSys AS Fec_Add,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarRemision,K.IdCiaRem AS CdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev
,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,K.ListaPrec AS KarLtaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_Cotizacion AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Cotizacion=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE O.TipDoc=@pmTipDoc AND O.Cotizacion BETWEEN @pmCotizacionIni AND @pmCotizacionFin AND O.IdCia=@pmIdCia
 ORDER BY O.Cotizacion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCtasCorrientes @pmIdCta VARCHAR(4)
 AS 
 SELECT IdCta,NumeroCta,IdBanco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato,TipoPapel,Orientacion
,VistaPrevia,VerSetup,NumCopias,IdCiaDef,Inactivo
 FROM CtasCorrientes
 WHERE IdCta=@pmIdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCtasCorrientesDsa @pmIdBanco VARCHAR(4)=Null
AS
  SELECT  IdCta,NumeroCta,IdCta+ ' ' +NumeroCta+' ' + Banco AS DsCta,C.IdBanco AS CodBanco,Banco
  FROM   CtasCorrientes AS C INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco
  WHERE C.Inactivo=0  AND C.IdBanco LIKE ISNULL(@pmIdBanco,'%')
  ORDER BY  IdCta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCtasCorrientesDso @pmIdBanco VARCHAR(4),@pmIdCiaDef CHAR(2)=Null
AS
  SELECT  C.IdCta,NumeroCta,C.IdCta+ ' ' +NumeroCta+' ' + Banco AS DsCta,C.IdBanco AS CodBanco,Banco
  FROM   CtasCorrientes AS C INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco
  LEFT JOIN CtasCorrCia AS CN ON C.IdCta=CN.IdCta
  WHERE C.Inactivo=0   AND (CN.IdCia=@pmIdCiaDef OR CN.IdCia IS NULL)
  AND C.IdBanco LIKE ISNULL(@pmIdBanco,'%')
  ORDER BY  C.IdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCtasCorrientesLta @pmIdBanco VARCHAR(4)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmInactivo BIT=Null,@pmIdCiaDef CHAR(2)=Null
 AS 
 SELECT IdCta,NumeroCta,C.IdBanco AS CodBco,Banco,C.IdCuenta AS IdCuent,NomCuenta,ConsCheques,RangoCons,ConsActual,Formato,TipoPapel,Orientacion
,VistaPrevia,VerSetup,NumCopias,IdCiaDef,C.Inactivo AS Inactvo
 FROM CtasCorrientes AS C INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta
 WHERE C.IdBanco LIKE ISNULL(@pmIdBanco,'%') AND C.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND  (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND IdCiaDef LIKE ISNULL(@pmIdCiaDef,'%%')
ORDER BY  IdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCtasCorrientesNum @pmNumeroCta VARCHAR(20),@pmOper VARCHAR(4)

AS
 IF  @pmOper ='LIKE'
 SELECT IdCta,NumeroCta,C.IdBanco AS CodBanco,Banco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato
 ,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,C.Inactivo AS Inactiv,IdCiaDef
  FROM   CtasCorrientes AS C INNER JOIN Bancos ON C.IdBanco=Bancos.IdBanco
  WHERE  NumeroCta LIKE @pmNumeroCta
  ORDER BY IdCta
 ELSE
  SELECT IdCta,NumeroCta,C.IdBanco AS CodBanco,Banco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato
 ,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,C.Inactivo AS Inactiv,IdCiaDef
  FROM   CtasCorrientes AS C INNER JOIN Bancos ON C.IdBanco=Bancos.IdBanco
   WHERE  NumeroCta=@pmNumeroCta
  ORDER BY IdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryCtasCorrientesPuc @pmIdCuenta VARCHAR(16)
AS
 SELECT IdCta,NumeroCta,C.IdBanco AS CodBanco,Banco,IdCuenta,ConsCheques,RangoCons,ConsActual,Formato
 ,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,C.Inactivo AS Inactiv,IdCiaDef
  FROM   CtasCorrientes AS C INNER JOIN Bancos ON C.IdBanco=Bancos.IdBanco
  WHERE  IdCuenta=@pmIdCuenta
  ORDER BY IdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDepartamentos @pmIdDep VARCHAR(8)
 AS 
 IF @pmIdDep IS NULL
  SELECT IdDep,Departamento,Codigo,IdPais  FROM Departamentos
  WHERE Inactivo=0
  ORDER BY Departamento
 ELSE
  SELECT IdDep,Departamento,Codigo,IdPais,Inactivo
  FROM Departamentos
  WHERE IdDep=@pmIdDep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDependencias @pmIdDep VARCHAR(4)
 AS 
 SELECT IdDep,Dependencia,JefeDep,IdArea,IdSubCos,TipRiesgo,Inactivo
 FROM Dependencias
 WHERE IdDep=@pmIdDep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDependenciasLta @pmIdArea VARCHAR(4)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdDep,Dependencia,D.IdArea AS CodArea,Area,D.IdSubCos AS IdSubcc,SubCosto,JefeDep,D.Inactivo AS Inactvo
 FROM Dependencias AS D INNER JOIN Areas AS A ON D.IdArea=A.IdArea INNER JOIN SubCentros AS S ON D.IdSubCos=S.IdSubCos
 WHERE D.IdArea LIKE ISNULL(@pmIdArea,'%') 
 AND (D.Inactivo=ISNULL(@pmInactivo,0) or D.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Dependencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryDespachos @pmTipDoc VARCHAR(3),@pmCartulina INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Cartulina,IdCia,Fecha,IdRuta,NumOrden,FechaRec,IdVehiculo,IdPropietario,IdConductor,IdConductor1,IdRelevador,VehPropio,CentInicial
,CentFinal,NPasajeros,Recorridos,ValorProducido,ValorGastos,ValorAhorro,ValorOtros,ValorVales,ValorAbono,ValorPasaje,TipoAdmon,IdTipRec
,ReciboCaja,IdCiaRec,NumAutoriza,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Despachos
 WHERE TipDoc=@pmTipDoc AND Cartulina=@pmCartulina AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDespachosAbi @pmTipDoc VARCHAR(3),@pmIdVehiculo VARCHAR(10),@pmTipoAdmon VARCHAR(10)
AS
SELECT    TipDoc, Cartulina, IdCia, Fecha, IdRuta, NumOrden,FechaRec, IdVehiculo, IdPropietario, IdConductor
, IdTipRec, ReciboCaja, IdCiaRec,Anulado, NumDev, FecDev, IdEstado
FROM        Trn_Despachos
WHERE  TipDoc=@pmTipDoc  AND IdVehiculo=@pmIdVehiculo AND Anulado=0  
AND TipoAdmon=@pmTipoAdmon  AND IdEstado<>'0002'
ORDER BY IdCia,Cartulina


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDespachosFre @pmTipDoc VARCHAR(3),@pmIdVehiculo VARCHAR(10), @pmFechaRec SMALLDATETIME
AS
 SELECT    TipDoc, Cartulina, IdCia, Fecha, IdRuta, NumOrden,FechaRec, IdVehiculo, IdConductor,IdTipRec, ReciboCaja, IdCiaRec, Anulado,FecDev,IdEstado,TipoAdmon
 FROM         Trn_Despachos   
 WHERE  TipDoc=@pmTipDoc  AND IdVehiculo=@pmIdVehiculo AND FechaRec=@pmFechaRec AND Anulado=0
 ORDER BY IdCia,Cartulina
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDespachosLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmReciboCaja INT,@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
,@pmTipoAdmon VARCHAR(10)=Null
AS
SELECT Cartulina, IdCia, Fecha, IdRuta, NumOrden,FechaRec,D.IdVehiculo AS IdVehicul,NumVeh,D.IdPropietario AS IdPropiet,P.RazonSocial AS Propietario,D.IdConductor AS IdCond,C.RazonSocial AS Conductor, IdConductor1, IdRelevador,RV.RazonSocial AS Relevador,D.VehPropio AS VehProp,IdTipRec,ReciboCaja,IdCiaRec 
, D.CentInicial AS CenInicial,D.CentFinal AS CenFinal,NPasajeros, Recorridos,ValorPasaje, ValorProducido, ValorGastos,ValorVales, ValorGastos+ValorVales AS TotalGastos,ValorAhorro, ValorOtros,ValorAbono,ValorAhorro+ ValorOtros+ ValorAbono AS TotalRecibido
,TipoAdmon, Anulado,NumDev,FecDev, D.Observacion AS Observ,D.IdEstado AS IdEstad,Estado,TimeSys,FecUpdate, IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,NumAutoriza
FROM  Trn_Despachos  AS D  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
INNER JOIN Terceros AS P ON D.IdPropietario=P.IdTercero INNER JOIN Terceros AS C ON D.IdConductor=C.IdTercero
INNER JOIN  EstadoDoc AS E ON D.IdEstado=E.IdEstado INNER JOIN Terceros AS RV ON D.IdRelevador=RV.IdTercero
WHERE   Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND ReciboCaja<=@pmReciboCaja AND Anulado=0 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND TipoAdmon LIKE ISNULL(@pmTipoAdmon,'%')
ORDER BY IdCia, Cartulina
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDespachosPen @pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmReciboCaja INT,@pmIdVehiculo VARCHAR(10)=Null,@pmTipoAdmon VARCHAR(10)=Null
AS
SELECT    Cartulina, IdCia, Fecha, IdRuta, NumOrden,FechaRec,D.IdVehiculo AS IdVehicul,NumVeh,D.IdPropietario AS IdPropiet,P.RazonSocial AS Propietario,D.IdConductor AS IdCond,C.RazonSocial AS Conductor, IdConductor1, IdRelevador,D.VehPropio AS VehProp,IdTipRec,ReciboCaja,IdCiaRec 
, D.CentInicial AS CenInicial,D.CentFinal AS CenFinal,NPasajeros, Recorridos,ValorPasaje, ValorProducido,ValorAhorro+ ValorOtros+ ValorAbono AS TotalRecibido,TipoAdmon, Anulado,NumDev,FecDev, D.Observacion AS Observ,D.IdEstado AS IdEstad,Estado,TimeSys,FecUpdate, IdCiaCrea,D.IdUsuario AS IdUsuari,NumAutoriza
FROM   Trn_Despachos  AS D  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
                 INNER JOIN Terceros AS P ON D.IdPropietario=P.IdTercero  INNER  JOIN Terceros AS C ON D.IdConductor=C.IdTercero
                 INNER  JOIN  EstadoDoc AS E ON D.IdEstado=E.IdEstado
WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia=@pmIdCia AND ReciboCaja<=@pmReciboCaja AND Anulado=0
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND TipoAdmon LIKE ISNULL(@pmTipoAdmon,'%')
ORDER BY Cartulina

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDespachosVeh @pmTipDoc VARCHAR(3),@pmIdVehiculo VARCHAR(10),@pmReciboCaja INT

AS
 SELECT    TipDoc, Cartulina, IdCia, Fecha, IdRuta, NumOrden,FechaRec, IdVehiculo, IdPropietario, IdConductor, IdTipRec, ReciboCaja, IdCiaRec,Anulado, NumDev, FecDev, IdEstado,TimeSys
 FROM        Trn_Despachos
 WHERE  TipDoc=@pmTipDoc  AND IdVehiculo=@pmIdVehiculo AND Anulado=0  AND ReciboCaja<=@pmReciboCaja
 ORDER BY FechaRec,TimeSys,IdCia,Cartulina

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDespachos_Cr @pmTipDoc VARCHAR(3),@pmCartulinaIni INT=Null,@pmCartulinaFin INT=Null
,@pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 SELECT TipDoc,TipoDoc, Cartulina, D.IdCia AS CodCia,Compania, Fecha, D.IdRuta AS CodRuta,Ruta,NumOrden,FechaRec,D.IdVehiculo AS PlacaVeh,D.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario, D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor
 , IdConductor1,NC1.RazonSocial AS Conductor1,IdRelevador,NR.RazonSocial AS Relevador,D.VehPropio AS VehcPropio,D.CentInicial AS CenInicial,D.CentFinal AS CenFinal,NPasajeros, Recorridos, ValorProducido, ValorGastos, ValorAhorro, ValorOtros, ValorVales, ValorAbono, ValorPasaje
 , IdTipRec, ReciboCaja, IdCiaRec, NumAutoriza,Anulado, NumDev, FecDev, D.Observacion AS Observ,D.IdEstado AS IdEstadoDoc,ED.Estado AS EstadDoc, TimeSys,FecUpdate, IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,D.TipoAdmon AS TipoAdmon,Leyenda
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TA.TipoAdmon AS VehTipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,ES.RazonSocial AS VehEmpresaSoat,KmActual,V.CentInicial AS VehCentInicial,V.CentFinal AS VehCentFinal
,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo,RespCivil,VigRCivil,CertGases,VigCertGas,TarjOper,VigTarjOper,CertMovilizacion,VigCertMovil,V.Observacion AS VehObservacion
--datos del propietario
,NP.RazonSocial AS Propietario,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.RazonSocial AS Conductor,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,CT.TipoSangre AS CondTipoSangre,CT.FactorRh AS CondRH,CT.FecNacmto AS CondFechNaci,CT.LugarNacmto AS CondLugarNacmto,CT.Sexo AS CondSexo
,CT.CertJudicial AS CondCertJudicial,CT.VigJudicial AS CondVigJudicial,CT.Licencia AS CondLicencia,CT.CatLicencia AS CondCategoria,CT.VigLicencia AS CondVigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
--datos reelevador
,CTR.CertJudicial AS RelvCertJudicial,CTR.VigJudicial AS RelvVigJudicial,CTR.Licencia AS RelvLicencia,CTR.CatLicencia AS RelvCategoria,CTR.VigLicencia AS RelvVigLicencia
  FROM  Trn_Despachos   AS D INNER JOIN Sys_TiposDoc  AS TD ON D.TipDoc=TD.IdDoc   INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
  INNER JOIN Rutas AS R ON D.IdRuta=R.IdRuta  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero
  INNER JOIN Terceros AS NC1 ON D.IdConductor1=NC1.IdTercero
  INNER JOIN Terceros AS NR ON D.IdRelevador=NR.IdTercero  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor 
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN Terceros AS ES ON V.NitEmpSoat=ES.IdTercero 
  LEFT JOIN TercCndtores AS CTR ON D.IdRelevador=CTR.IdConductor 
  WHERE  TipDoc=@pmTipDoc  AND Cartulina BETWEEN ISNULL(@pmCartulinaIni,0) AND ISNULL(@pmCartulinaFin,2147483647)
  AND  D.IdCia like ISNULL(@pmIdCia,'%%')
  AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY D.IdCia,Cartulina

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDespachos_Crr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdRuta VARCHAR(4)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null,@pmTipoAdmon VARCHAR(10)=Null,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null
,@pmFechaRecIni SMALLDATETIME=Null,@pmFechaRecFin SMALLDATETIME=Null
AS
 SELECT Cartulina, D.IdCia AS CodCia,Compania, Fecha, D.IdRuta AS CodRuta,Ruta, NumOrden,FechaRec,D.IdVehiculo AS PlacaVeh,D.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario, D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor
 , IdConductor1,NC1.RazonSocial AS Conductor1,IdRelevador,NR.RazonSocial AS Relevador,D.VehPropio AS VehcPropio,D.CentInicial AS CenInicial,D.CentFinal AS CenFinal,NPasajeros, Recorridos, ValorProducido, ValorGastos,ValorVales,ValorGastos+ValorVales AS TotalGastos,ValorAhorro,ValorOtros,ValorAbono
,ValorAhorro+ValorOtros+ValorAbono AS TotalLiquida,ValorPasaje , IdTipRec, ReciboCaja, IdCiaRec, NumAutoriza,Anulado, NumDev, FecDev, D.Observacion AS Observ,D.IdEstado AS IdEstadoDoc,ED.Estado AS EstadDoc, TimeSys,FecUpdate, IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,D.TipoAdmon AS TipAdmon,Leyenda,TipDoc,TipoDoc
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TA.TipoAdmon AS VehTipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,V.CentInicial AS VehCentInicial,V.CentFinal AS VehCentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,CT.TipoSangre AS CondTipoSangre,CT.FactorRh AS CondRH,CT.FecNacmto AS CondFechaNcmto,CT.LugarNacmto AS CondLugarNac,CT.Sexo AS CondSexo,CT.CertJudicial AS CondCertJudicial
,CT.VigJudicial AS CondVigCert,CT.Licencia AS CondLicencia,CT.CatLicencia AS CondCategoria,CT.VigLicencia AS CondVigLicencia,CT.IdLugar AS CdLugarLic,ELC.LugarLic AS CondLugarLic
,RT.IdModd AS RelvCdMod,MCR.Modalidad AS ModReelevador,RT.TipoSangre AS RelvTipoSangre,RT.FactorRh AS RelvRh,RT.FecNacmto AS RelvFechaNcmto,RT.LugarNacmto AS RelvLugarNacmto,RT.Sexo AS RelvSexo,RT.CertJudicial AS RelvCertJudic
,RT.VigJudicial AS RelvVigCertJudi,RT.Licencia AS RelvLicencia,RT.CatLicencia AS RelvCategoria,RT.VigLicencia AS RelvVigLicencia,RT.IdLugar AS RelvIdLugar,ELR.LugarLic AS RelvLugarLic
  FROM  Trn_Despachos   AS D INNER JOIN Sys_TiposDoc  AS TD ON D.TipDoc=TD.IdDoc   INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
  INNER JOIN Rutas AS R ON D.IdRuta=R.IdRuta  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero
  INNER JOIN Terceros AS NC1 ON D.IdConductor1=NC1.IdTercero   INNER JOIN Terceros AS NR ON D.IdRelevador=NR.IdTercero  
  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor    INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa    LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN TercCndtores AS RT ON D.IdRelevador=RT.IdConductor   LEFT JOIN ExpLicencias AS ELR ON RT.IdLugar=ELR.IdLugar
  LEFT JOIN TiposMod AS MCR ON RT.IdModd=MCR.IdModd
  WHERE  TipDoc=@pmTipDoc  AND ( Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND  Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
  AND ( FechaRec>=ISNULL(@pmFechaRecIni,CAST('19100101' AS SMALLDATETIME)) AND  FechaRec<=ISNULL(@pmFechaRecFin,CAST('20781230' AS SMALLDATETIME)))
  AND  D.IdCia LIKE ISNULL(@pmIdCia,'%%')   AND D.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND D.TipoAdmon LIKE ISNULL(@pmTipoAdmon,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY D.IdCia,Cartulina
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDetCuentas @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmConcepto VARCHAR(10),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Concepto,Item,IdCuenta,Valor,Detalle,IdTercero,CodAgncia,CdCCosto,CdSubCos,pVehiculo,VrBase,TipFac,Factura,IdCiaFac,ItemFac
,FecVence,Cheque,CiuOrigen,Referencia
 FROM Trn_DetCuentas
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND Concepto LIKE ISNULL(@pmConcepto,'%')
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDetCuentas_Cr @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2),@pmConcepto VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Documento,D.IdCia AS CdCia,Compania,Concepto,Item,D.IdCuenta AS CdCuenta,NomCuenta,Valor,Detalle,D.IdTercero AS NitTercero,RazonSocial,D.CodAgncia AS IdAgncia,A.Agencia AS NomAgencia,CodAgencia
,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo,VrBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,Cheque,CiuOrigen,Localidad,D.Referencia AS Referncia
 FROM Trn_DetCuentas AS D  INNER JOIN Companias AS C ON D.IdCia=C.IdCia
 INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN CentroCosto AS O ON D.CdCCosto=O.IdCCosto
 LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN Agencias AS A ON D.CodAgncia=A.IdAgencia
 LEFT JOIN Localidades AS L ON D.CiuOrigen=L.IdLocal
 WHERE TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND D.IdCia=@pmIdCia 
 AND Concepto LIKE ISNULL(@pmConcepto,'%')
 ORDER BY Documento,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCaj @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito
,IdTercero,IdAgencia,IdCCosto,IdSubCos,IdCajero,EnEfectivo,CdCta,NumForma,CdBanco,Referencia,NumAutoriza,ModdDev,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario,NumVehiculo
 FROM Trn_DevCaj
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCajBus @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,D.IdTercero AS NitTercero,RazonSocial
,IdAgencia,D.IdCCosto AS CdCCosto,CCosto,D.IdSubCos AS CdSubCentro,IdCajero,UC.Usuario AS UsuCajero,EnEfectivo,NumForma,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,B.Banco AS CtaBanco,NumVehiculo
,CdBanco,BC.Banco AS NomBanco,Referencia,NumAutoriza,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev
 FROM Trn_DevCaj AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario  INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 LEFT JOIN CtasCorrientes AS CTA ON D.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON CTA.IdBanco=B.IdBanco
 LEFT JOIN Bancos AS BC ON D.CdBanco=BC.IdBanco
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL (@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND  Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  ModdDev LIKE ISNULL(@pmModdDev,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCajBusPag @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null,@pmIdForma VARCHAR(4)=Null
 AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipRec,Recibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero
,IdAgencia,NumVehiculo,D.IdCCosto AS CdCCosto,CCosto,D.IdSubCos AS CdSubCentro,IdCajero,UC.Usuario AS UsuCajero,EnEfectivo,D.NumForma AS DocNumForma,Referencia,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev
--detalles pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NitCliente,TP.RazonSocial AS PagNomTercero
,P.NumForma AS NumCheque,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,LF.Localidad AS CiudadPlaza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
,P.CdCta AS CtaCod,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono
 FROM Trn_DevCaj AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario  INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 INNER JOIN Trn_Pagos AS P ON D.TipDev=P.TipDoc AND  D.Devolucion=P.Documento AND  D.IdCia=P.IdCia
 INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco   INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Terceros AS TP ON P.NitCliente=TP.IdTercero
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 WHERE TipDev=@pmTipDev  AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL (@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND  Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  ModdDev LIKE ISNULL(@pmModdDev,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')
  AND P.IdForma LIKE ISNULL(@pmIdForma,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCajDoc @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,IdTercero,TipCom,Comprobante,IdCiaCom,IdEstado
 FROM Trn_DevCaj
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCajLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,D.IdTercero AS NitTercero,RazonSocial,IdAgencia,IdCCosto,IdSubCos
,IdCajero,UC.Usuario AS UsuCajero,EnEfectivo,CdCta,NumForma,CdBanco,Referencia,NumAutoriza,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev,NumVehiculo
 FROM Trn_DevCaj AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL (@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND  Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  ModdDev LIKE ISNULL(@pmModdDev,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCajRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmCdCta VARCHAR(4)=Null,@pmEnEfectivo BIT=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,DV.IdCia AS CdCia,Compania,Fecha,DV.IdConcepto AS CdConcepto,CN.Concepto AS DevConcepto
,DV.TipDoc AS TipRec,DV.Recibo AS NumRecibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,DV.IdTercero AS DevNitTercero,T.RazonSocial AS DevNomTercero
,DV.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,DV.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto
,DV.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS UsuCajero,EnEfectivo
,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco,NumForma,CdBanco,BR.Banco AS DocBanco
,DV.Referencia AS DevReferencia,NumAutoriza,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,DV.Observacion AS Observ
,DV.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,DV.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,NumVehiculo
--detalles de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--agencia
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DAG.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevCaj AS DV INNER JOIN Trn_CompCajaDet AS D ON DV.TipDoc=D.TipDoc AND DV.Recibo=D.Recibo AND DV.IdCiaDoc=D.IdCia
 INNER JOIN Companias AS CI ON DV.IdCia=CI.IdCia  
 INNER JOIN EstadoDoc AS ED ON DV.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON DV.IdCajero=UC.IdUsuario  INNER JOIN CentroCosto AS CC ON DV.IdCCosto=CC.IdCCosto 
 INNER JOIN  Conceptos AS CN ON DV.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON DV.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  CtasCorrientes AS CTA ON DV.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON DV.IdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON DV.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON DV.CdBanco=BR.IdBanco 
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DAG ON LA.IdDep=DAG.IdDep
 WHERE TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND DV.IdCia LIKE ISNULL(@pmIdCia,'%%') AND DV.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND CdCta LIKE ISNULL(@pmCdCta,'%')  AND DV.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND DV.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')  AND DV.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY DV.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCaj_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,DV.IdCia AS CdCia,Compania,Fecha,DV.IdConcepto AS CdConcepto,CN.Concepto AS DevConcepto
,DV.TipDoc AS TipRec,DV.Recibo AS NumRecibo,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,VrTotDebito,VrTotCredito,DV.IdTercero AS DevNitTercero,T.RazonSocial AS DevNomTercero
,DV.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,DV.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto
,DV.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS UsuCajero,EnEfectivo
,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco,NumForma,CdBanco,BR.Banco AS DocBanco
,DV.Referencia AS DevReferencia,NumAutoriza,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,DV.Observacion AS Observ
,DV.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,DV.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda,NumVehiculo
--detalles de cuentas
,Item,D.IdConcepto AS DetCodConcepto,CD.Concepto AS DetConcepto,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,NitTercero,DT.RazonSocial AS DetRazonSocial
,pVehiculo,CdCCosto,DC.CCosto AS DetCentCosto,CdSubCos,DS.SubCosto AS DetSubCentCos,VrBase,TarifaBase,TipFac,Factura,IdCiaFac,ItemFac,FecVence,NumCheque,TipoAplica
,NitOtros,D.CodAgencia AS DetCodAgencia,DA.Agencia AS DetAgencia,Referncia,TipDocRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--agencia
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DAG.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevCaj AS DV INNER JOIN Trn_CompCajaDet AS D ON DV.TipDoc=D.TipDoc AND DV.Recibo=D.Recibo AND DV.IdCiaDoc=D.IdCia
 INNER JOIN Companias AS CI ON DV.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON DV.TipDev=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON DV.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON DV.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON DV.IdCajero=UC.IdUsuario  INNER JOIN CentroCosto AS CC ON DV.IdCCosto=CC.IdCCosto 
 INNER JOIN  Conceptos AS CN ON DV.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON DV.IdTercero =T.IdTercero
 INNER JOIN ConcDiversos AS CD ON D.IdConcepto=CD.IdConcepto  INNER JOIN Puc AS P ON D.IdCuenta=P.IdCuenta
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  CtasCorrientes AS CTA ON DV.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON CTA.IdBanco=BCT.IdBanco
 LEFT JOIN Agencias  AS A ON DV.IdAgencia=A.IdAgencia LEFT JOIN SubCentros AS SC ON DV.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON DV.CdBanco=BR.IdBanco   LEFT JOIN TiposCom AS TCM ON DV.TipCom=TCM.IdCom
 LEFT JOIN Terceros AS DT ON D.NitTercero=DT.IdTercero  LEFT JOIN CentroCosto AS DC ON D.CdCCosto=DC.IdCCosto 
 LEFT JOIN SubCentros AS DS ON D.CdSubCos=DS.IdSubCos LEFT JOIN Agencias AS DA ON D.CodAgencia=DA.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DAG ON LA.IdDep=DAG.IdDep
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND DV.IdCia=@pmIdCia
 ORDER BY  Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevCausacion  @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)

AS
 SELECT     TipDev, Devolucion, IdCia, Fecha, IdConcepto, TipDoc, Documento, IdCiaDoc, FecDoc, IdVehiculo, IdConductor, IdPropietario, VehPropio, ValorTotal, 
                      ValorDcto, ValorIva, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorFletes, ValorNeto, ValorCxc, IdPlazo, CxPagar, Factura, IdProveedor, 
                      TipCom, Comprobante, IdCiaCom, Observacion, IdEstado, TimeSys, IdCiaCrea, IdUsuario
 FROM        Trn_DevCausacion
 WHERE  TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevCausacionLta  @pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
AS
 SELECT    Devolucion, IdCia, Fecha,D.IdConcepto AS IdConcept,Concepto,TipDoc, Documento, IdCiaDoc, FecDoc, IdVehiculo, IdConductor,O.RazonSocial AS Conductor, IdPropietario,P.RazonSocial AS Propietario,VehPropio, ValorTotal, 
                     ValorDcto, ValorIva, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorFletes, ValorNeto, ValorCxc, D.IdPlazo AS IdPlaz, CxPagar, Factura, IdProveedor,N.RazonSocial AS Proveedor,
                      TipCom, Comprobante, IdCiaCom,D.Observacion AS Observ, D.IdEstado AS IdEstad,Estado, TimeSys, IdCiaCrea, D.IdUsuario AS IdUsuari
 FROM  Trn_DevCausacion AS D INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
  INNER JOIN  Terceros AS O ON D.IdConductor=O.IdTercero
  INNER JOIN  Terceros AS P ON D.IdPropietario=P.IdTercero
  INNER JOIN  Terceros AS N ON D.IdProveedor=N.IdTercero
  INNER JOIN  EstadoDoc AS E ON D.IdEstado=E.IdEstado
 WHERE   Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCausacionNMes @pmAnno INT,@pmMes INT=Null,@pmIdCia CHAR(2)=Null,@pmIdPropietario VARCHAR(16)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT  IdCliente,VC.IdVehiculo AS PlacaVeh,D.IdCia AS CodCia,D.TipDoc AS TipoDcm,Month(Fecha) AS nMes,SUM(ValorFactura) AS SValNet
 FROM  Trn_DevCausacion AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Documento=VC.Documento AND D.IdCiaDoc=VC.IdCia
 WHERE Year(Fecha)=@pmAnno AND  Month(Fecha)=ISNULL(@pmMes, Month(Fecha))
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND IdCliente LIKE ISNULL(@pmIdPropietario,'%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 GROUP BY IdCliente,VC.IdVehiculo,D.IdCia,D.TipDoc,Month(Fecha)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCausacionVFec  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmTipDoc VARCHAR(3)=Null
AS
 SELECT IdCliente,VC.IdVehiculo AS IdVehic,SUM(ValorFactura) AS SValNet
 FROM  Trn_DevCausacion AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Documento=VC.Documento AND D.IdCiaDoc=VC.IdCia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdPropietario,'%')
 GROUP BY IdCliente,VC.IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevCausacionVc @pmIdPropietario VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT VC.TipDoc AS TDoc,VC.Documento AS NumDoc,VC.IdCia AS CodCia,Item,ValorFactura,IdCliente,IdAgencia,VC.IdVehiculo AS PlacaVeh
,VC.VehPropio AS VhProp,IdVendedor,Comision,IdCargo,IdClase,VC.IdConcepto AS CodConcp,VC.Factura AS NumFact,Referencia,Detalle,Devolucion,TipDev,D.IdCia AS CiaDev,Fecha,FecDoc,ValorCxc,ValorNeto
 FROM  Trn_DevCausacion AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Documento=VC.Documento AND D.IdCiaDoc=VC.IdCia
 WHERE IdCliente LIKE ISNULL(@pmIdPropietario,'%') AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY TipDev,Devolucion,D.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevCausacion_Cr  @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
AS
 SELECT TipDev,TD.TipoDoc AS TipoDev,D.Devolucion AS NDevolucion, D.IdCia AS CodCia,Compania, D.Fecha AS FechaDev, D.IdConcepto AS Concept,CC.Concepto AS ConceptoDev,D.TipDoc AS IdTipoDoc,TDC.TipoDoc AS TipoCausacion, D.Documento AS NCausacion, D.IdCiaDoc AS CiaDoc , FecDoc
, D.IdVehiculo AS PlacaVehc, D.IdConductor AS CedConductor,D.IdPropietario AS NitCedProp,D.VehPropio AS VehicProp, D.ValorTotal,D.ValorDcto,D.ValorIva, D.ValorReteFte, D.ValorReteIva,D.ValorReteIca, D.ValorOtros, D.ValorFletes, D.ValorNeto,ValorCxc, D.IdPlazo AS Idpzo,Plazo,DiasPago,NVmto, D.CxPagar, D.Factura, D.IdProveedor AS IdProveed, NV.RazonSocial AS Proveedor
,D.TipCom AS IdTipoComp,TM.TipoCom AS TipoComp, D.Comprobante,D.IdCiaCom, D.Observacion AS Observ, D.IdEstado AS IdEstadDev,ED.Estado AS EstadDoc, D.TimeSys, D.IdCiaCrea, D.IdUsuario AS IdUsuari,Usuario,TD.Leyenda
,Item,RF.TipDoc AS TipoDocApl,RF.Documento AS NCausacionApl,RF.IdCiaDoc AS CiaDocApl,ItemDoc,RF.IdCargo AS CodCargo,F.Concepto AS Cargo,RF.IdClase AS CodClase,ClaseCuenta,ValorAbono,IdCliente,IdAgencia,RF.IdVehiculo AS PlacaVehApl,RF.VehPropio AS VehPropApl,IdVendedor,Detalle,FecPago
,FecFactura,FecVence,Referencia,BaseIva,BaseRet,TarifaIva,TarifaRet,DTC.TarifaIca,MulPlazos,CxcVarios
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.RazonSocial AS Propietario,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.RazonSocial AS Conductor,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM   Trn_DevCausacion AS D INNER JOIN Sys_TiposDoc  AS TD ON D.TipDev=TD.IdDoc  INNER JOIN Sys_TiposDoc  AS TDC ON D.TipDoc=TDC.IdDoc
 INNER JOIN Trn_Causacion AS DTC ON D.TipDoc=DTC.TipDoc AND D.Documento=DTC.Documento AND D.IdCiaDoc=DTC.IdCia
 INNER JOIN Trn_RecFactura AS RF ON D.TipDev=RF.TipRec AND D.Devolucion=RF.Recibo AND D.IdCia=RF.IdCia
 INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia INNER JOIN Conceptos AS CC ON D.IdConcepto=CC.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON  D.IdUsuario=U.IdUsuario
 INNER JOIN Plazos AS PZ ON D.IdPlazo=PZ.IdPlazo  
 INNER JOIN Terceros AS  NV ON D.IdProveedor=NV.IdTercero INNER JOIN Vehiculos AS V ON RF.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON RF.IdCliente=NP.IdTercero INNER JOIN Terceros AS NC ON RF.IdVendedor=NC.IdTercero
 INNER JOIN Fijos AS F ON RF.IdCargo=F.IdCargo INNER JOIN BusClaseCue AS CU ON RF.IdClase=CU.IdClase
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON RF.IdVendedor=CT.IdConductor  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal
  LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN TiposCom AS TM ON D.TipCom=TM.IdCom
 WHERE  TipDev=@pmTipDev AND D.Devolucion between @pmDevolucionIni  AND @pmDevolucionFin
  AND D.IdCia=@pmIdCia
 ORDER BY D.IdCia,D.Devolucion,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCausacion_Crr  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipDev VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
,@pmModalidad VARCHAR(10)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdProveedor VARCHAR(16)=Null, @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmCxPagar BIT=Null,@pmDocumento INT=Null,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT TipDev, Devolucion,D.IdCia AS CodCia,Compania,D.Fecha AS FechaDev,D.TipDoc AS TipDocm, D.Documento AS NumCausac, IdCiaDoc, FecDoc,D.Factura AS NumFact,FecFactura,C.Modalidad AS DevModalidad
,D.IdProveedor AS NitProveed,NV.RazonSocial AS Proveedor,D.IdVehiculo AS PlacaVeh,D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,D.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,D.VehPropio AS VhPropio,D.ValorTotal AS VrTotal,D.ValorDcto AS VrDcto, D.ValorIva AS VrIva, D.ValorReteFte AS VrReteFte, D.ValorReteIva AS VrReteIva, D.ValorReteIca AS VrReteIca, D.ValorOtros AS VrOtros, D.ValorFletes AS VrFletes, D.ValorNeto AS VrNeto
, ValorCxc, D.IdConcepto AS CodConcepto,Concepto,D.CxPagar AS Cxpagr,D.IdPlazo AS CodPlazo,Plazo,DiasPago,NVmto,D.TipCom AS TComp,TipoCom,D.Comprobante AS Comprob, D.IdCiaCom AS CodCiaComp,D.Observacion AS Observ, D.IdEstado AS IdEstad,ED.Estado AS EstadDoc, D.TimeSys AS FechaAdd,D.IdCiaCrea AS IdCiaCre
,TipoDoc, D.IdUsuario AS IdUsuari,Usuario
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_DevCausacion AS D INNER JOIN Trn_Causacion AS C ON D.TipDoc=C.TipDoc AND D.Documento=C.Documento AND D.IdCiaDoc=C.IdCia
 INNER JOIN Sys_TiposDoc  AS TD ON D.TipDev=TD.IdDoc   INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS CC ON D.IdConcepto=CC.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN Plazos AS PZ ON D.IdPlazo=PZ.IdPlazo 
 INNER JOIN Terceros AS  NV ON D.IdProveedor=NV.IdTercero
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero
 INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero 
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
 INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
 INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
 LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
 LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
 LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa   LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor
 LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
 LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN TiposCom AS TM ON D.TipCom=TM.IdCom
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  TipDev LIKE ISNULL(@pmTipDev,'%')  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.Modalidad LIKE ISNULL(@pmModalidad,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND D.IdProveedor LIKE ISNULL(@pmIdProveedor,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') 
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')   AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND (D.CxPagar=ISNULL(@pmCxPagar,0) or D.CxPagar=ISNULL(@pmCxPagar,1))
 AND (D.Documento>=ISNULL(@pmDocumento,0)  AND D.Documento<=ISNULL(@pmDocumento,2147483647))
 ORDER BY TipDev, D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCns @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Consignacion,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,EnEfectivo,IdCta,IdCuenta,IdCliente
,IdCCosto,IdSubCos,IdCajero,Referencia,Referncia2,NumConsigna,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevCns
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCnsDoc @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Consignacion,IdCiaDoc,FecDoc
,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,IdEstado
 FROM Trn_DevCns
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCnsLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmConsignacionIni INT=Null,@pmConsignacionFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdCta VARCHAR(4)=Null,@pmEnEfectivo BIT=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Consignacion,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,EnEfectivo,D.IdCta AS CodCta,NumeroCta,CT.IdBanco AS CdBanco,Banco
,D.IdCuenta AS CdCuenta,IdCliente,RazonSocial,IdCCosto,IdSubCos,IdCajero,UC.Usuario AS NomCajero,Referencia,Referncia2,NumConsigna,ModdDev,OrigenAdd
,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_DevCns AS D  INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN CtasCorrientes AS CT ON D.IdCta=CT.IdCta INNER JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND Consignacion BETWEEN ISNULL(@pmConsignacionIni,0) AND ISNULL(@pmConsignacionFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdCta LIKE ISNULL(@pmIdCta,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCnsRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCta VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmEnEfectivo BIT=Null
,@pmIdCliente VARCHAR(16)=Null 
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto
,D.TipDoc AS TipCons,Consignacion,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,EnEfectivo,D.IdCta AS DevCodCta,NumeroCta
,CT.IdBanco AS DevCodBanco,B.Banco AS DevBanco,D.IdCuenta AS CdCuenta,IdCliente,T.RazonSocial AS NomCliente
,D.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,D.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS NomCajero
,D.Referencia AS DevReferencia,D.Referncia2 AS DevReferncia2,NumConsigna,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalle de pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco,NitCliente,PC.RazonSocial AS PagNomCliente,CdAgencia,A.Agencia AS NomAgencia
,CodAgencia,A.Referencia AS AgeReferencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2,CdLocal,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza,DF.Departamento AS DptoPlaza
,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
 FROM Trn_DevCns AS D  INNER JOIN Trn_Pagos AS P ON D.TipDev=P.TipDoc AND D.Devolucion=P.Documento AND D.IdCia=P.IdCia
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia 
 INNER JOIN CtasCorrientes AS CT ON D.IdCta=CT.IdCta INNER JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Puc AS PU ON D.IdCuenta=PU.IdCuenta
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos 
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Terceros AS PC ON P.NitCliente=PC.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 WHERE TipDev=@pmTipDev AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdCta LIKE ISNULL(@pmIdCta,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevCns_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto
,D.TipDoc AS TipCons,Consignacion,IdCiaDoc,FecDoc,VrEfectivo,VrOtros,VrTotal,EnEfectivo,D.IdCta AS DevCodCta,NumeroCta
,CT.IdBanco AS DevCodBanco,B.Banco AS DevBanco,D.IdCuenta AS CdCuenta,IdCliente,T.RazonSocial AS NomCliente
,D.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,D.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS NomCajero
,D.Referencia AS DevReferencia,D.Referncia2 AS DevReferncia2,NumConsigna,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--detalle de pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco,NitCliente,PC.RazonSocial AS PagNomCliente,CdAgencia,A.Agencia AS NomAgencia
,CodAgencia,A.Referencia AS AgeReferencia,NumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2,CdLocal,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza,DF.Departamento AS DptoPlaza
,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
 FROM Trn_DevCns AS D  INNER JOIN Trn_Pagos AS P ON D.TipDev=P.TipDoc AND D.Devolucion=P.Documento AND D.IdCia=P.IdCia
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN CtasCorrientes AS CT ON D.IdCta=CT.IdCta INNER JOIN Bancos AS B ON CT.IdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Puc AS PU ON D.IdCuenta=PU.IdCuenta
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos  LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Terceros AS PC ON P.NitCliente=PC.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevEnt @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Entrada,IdCiaDoc,FecDoc,IdProv,Factura,VrSubTotal,VrDescuento,VrImpuesto
,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,CxPagar,DocEquiv
,BaseImp,BaseRet,CdCCosto,CdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevEnt
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevEntDoc @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Entrada,IdCiaDoc,FecDoc,CxPagar,VrSubTotal,VrDescuento,VrImpuesto,VrNeto
,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevEnt
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevEntLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmEntradaIni INT=Null,@pmEntradaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Entrada,IdCiaDoc,FecDoc,IdProv,RazonSocial,Factura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,CxPagar,DocEquiv,BaseImp,BaseRet,CdCCosto,CdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TipDev,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
 FROM Trn_DevEnt AS D INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipDev=@pmTipDev
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND Entrada BETWEEN ISNULL(@pmEntradaIni,0) AND ISNULL(@pmEntradaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevEntRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Entrada,IdCiaDoc,FecDoc
,D.IdProv AS NitProvee,RazonSocial,Factura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal
,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,CxPagar,DocEquiv,BaseImp,BaseRet,CdCCosto,CCosto,CdSubCos,SubCosto,Modalidad,ModdDev,OrigenAdd
,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
 FROM Trn_DevEnt AS D INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero 
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdProv LIKE ISNULL(@pmIdProv,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND  ModdDev LIKE ISNULL(@pmModdDev,'%')
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1)) 
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevEnt_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipEnt,Entrada,IdCiaDoc,FecDoc
,D.IdProv AS NitProvee,RazonSocial,D.Factura AS EntFactura,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal
,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,FletesCosto,CxPagar,DocEquiv,BaseImp,BaseRet,D.CdCCosto AS CodCenCosto,CC.CCosto AS CentCosto
,D.CdSubCos AS CodSubCent,SC.SubCosto AS SubcCosto,Modalidad,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
--Detalle
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_DevEnt AS D  INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv 
 LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco

 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFco @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,nClieCon,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,BaseImp,BaseRet,IdCajero,IdVend,TarifaCom,CodTarCom,NitCliente,CdAgencia,IdCCosto
,IdSubCos,IdLocal,EnEfectivo,Pedido,IdCiaPed,Remision,IdCiaRem,FecPedido,Modalidad,ModdDev,OrigenAdd,NumAutoriza,PlacaVehic,KmtVehic
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevFco
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcoDoc  @pmTipDoc VARCHAR(3),@pmFactura INT ,@pmIdCiaDoc CHAR(2)
AS
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,nClieCon,VrNeto,VrEfectivo,VrPagosOtr
,Modalidad,ModdDev,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevFco
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcoLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmnClieCon VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null
,@pmNitCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc,nClieCon,Nombre,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,BaseImp,BaseRet,IdCajero,UC.Usuario AS Cajero,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,NitCliente
,T.RazonSocial AS NomCliente,CdAgencia,IdCCosto,IdSubCos,D.IdLocal AS CodCiudad,EnEfectivo,Pedido,IdCiaPed,Remision,IdCiaRem,FecPedido,Modalidad,ModdDev,NumAutoriza,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev
 FROM Trn_DevFco AS D INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN ClieContado AS C ON D.nClieCon=C.IdCliente
 LEFT JOIN  Terceros AS T ON D.NitCliente=T.IdTercero
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND nClieCon LIKE ISNULL(@pmnClieCon,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%') 
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%') 
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcoRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmnClieCon VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend  VARCHAR(16)=Null
,@pmNitCliente VARCHAR(16)=Null,@pmEnEfectivo BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc
,nClieCon,Nombre,CC.TipoId AS CliTipoId,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad,LE.Localidad AS CiudadClie
,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo
,VrPagosOtr,Cantidad,CantPuntos,BaseImp,BaseRet,IdCajero,UC.Usuario AS Cajero,D.IdVend AS NitVendedor,V.RazonSocial AS Vendedor
,TarifaCom,NitCliente,T.RazonSocial AS NomCliente,D.CdAgencia AS IdAgncia,Agencia,CodAgencia,D.IdCCosto AS IdCentCost,CCosto,D.IdSubCos AS IdSubCent,SubCosto
,D.IdLocal AS CodCiudad,LD.Localidad AS DocCiudad,EnEfectivo,Pedido,IdCiaPed,Remision,IdCiaRem,FecPedido,Modalidad,ModdDev,NumAutoriza,PlacaVehic,KmtVehic
,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_DevFco AS D INNER JOIN ClieContado AS CC ON D.nClieCon=CC.IdCliente
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia   INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS V ON D.IdVend=V.IdTercero  INNER JOIN  Localidades AS LD ON D.IdLocal=LD.IdLocal
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS T ON D.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON D.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CT ON D.IdCCosto=CT.IdCCosto LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN TercCliente AS CLI ON D.NitCliente=CLI.IdClie LEFT JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipDev LIKE ISNULL(@pmTipDev,'%')
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND nClieCon LIKE ISNULL(@pmnClieCon,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND NitCliente LIKE ISNULL(@pmNitCliente ,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')  AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFco_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipoFac,D.Factura AS NumFactura,IdCiaDoc,FecDoc
,nClieCon,Nombre,CC.TipoId AS CliTipoId,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad,LE.Localidad AS CiudadClie
,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo
,VrPagosOtr,Cantidad,CantPuntos,BaseImp,BaseRet,IdCajero,UC.Usuario AS Cajero,D.IdVend AS NitVendedor,V.RazonSocial AS Vendedor
,TarifaCom,NitCliente,T.RazonSocial AS NomCliente,D.CdAgencia AS IdAgncia,Agencia,CodAgencia,D.IdCCosto AS IdCentCost,CCosto,D.IdSubCos AS IdSubCent,SubCosto
,D.IdLocal AS CodCiudad,LD.Localidad AS DocCiudad,EnEfectivo,Pedido,IdCiaPed,D.Remision AS NumRemision,D.IdCiaRem AS CdCiaRem,FecPedido,Modalidad,ModdDev,NumAutoriza,PlacaVehic,KmtVehic
,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CdSubCos,pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd
,K.Factura AS KarFactura,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_DevFco AS D INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ClieContado AS CC ON D.nClieCon=CC.IdCliente
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS V ON D.IdVend=V.IdTercero  INNER JOIN  Localidades AS LD ON D.IdLocal=LD.IdLocal
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 

 LEFT JOIN Terceros AS T ON D.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON D.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 LEFT JOIN CentroCosto AS CT ON D.IdCCosto=CT.IdCCosto LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 WHERE TipDev=@pmTipDev  AND Devolucion BETWEEN  @pmDevolucionIni AND @pmDevolucionFin  AND D.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA
,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,IdCCosto,IdSubCos,IdVend,TarifaCom
,CodTarCom,IdLocEnv,Pedido,IdCiaPed,Remision,IdCiaRem,Cotizacion,IdCiaCot,FecPedido,PlacaVehic,KmtVehic,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Observacion,IdEstado,ZonaFrontera,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevFcr
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryDevFcrAcu @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
AS
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrNeto) AS STOTNET
 FROM Trn_DevFcr
 WHERE YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY  IdCliente,IdAgencia,IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcrDoc  @pmTipDoc VARCHAR(3),@pmFactura INT ,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrNeto
,Modalidad,ModdDev,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevFcr
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcrLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion
,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,IdCCosto,IdSubCos
,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,IdLocEnv,Pedido,IdCiaPed,Remision,IdCiaRem,Cotizacion,IdCiaCot,FecPedido,PlacaVehic,KmtVehic,Modalidad,ModdDev,ZonaFrontera,OrigenAdd
,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,TipDev
 FROM Trn_DevFcr AS D  INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND ModdDev LIKE ISNULL(@pmModdDev,'%') 
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcrRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente
,D.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos
,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet,D.IdCCosto AS IdCenCost,CCosto,D.IdSubCos AS IdSubCent,SubCosto
,D.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,D.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Pedido,IdCiaPed,Remision,IdCiaRem,Cotizacion,IdCiaCot,FecPedido,PlacaVehic,KmtVehic,Modalidad,ModdDev,ZonaFrontera,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevFcr AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON D.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipDev LIKE ISNULL(@pmTipDev ,'%') 
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND D.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFcr_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipFact,D.Factura AS NumFactura
,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion
,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,Cantidad,CantPuntos,BaseImp,BaseRet
,D.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,D.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,D.IdVend AS NitVend,VN.RazonSocial AS Vendedor
,TarifaCom,D.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,Pedido,IdCiaPed,D.Remision AS NumRemision,D.IdCiaRem AS CdCiaRem
,D.Cotizacion AS NumCotizacion,D.IdCiaCot AS CdCiaCot,FecPedido,Modalidad,ModdDev,PlacaVehic,KmtVehic,ZonaFrontera,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdAgencia,KA.Agencia AS KarAgencia,KA.CodAgencia AS KarCodAgencia,KA.NContrato AS KarAgeContrato,KA.Referencia AS KarAgeRefencia,CdLocal,KL.Localidad AS KarCiudad
,CdCCosto,KC.CCosto AS Kar_Ccosto,K.CdSubCos AS CodSubCos,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,K.Cotizacion AS KarNumCotiza,K.IdCiaCot AS KarCiaCotiza,K.Remision AS KarRemision,K.IdCiaRem AS KarCiaRem
,K.Factura AS KarFactura,TipDocDev,NumDocDev,K.IdVend AS KarNitVend,KV.RazonSocial AS KarVendedor,K.Comision AS KarTarifaCom,CdOperario,KO.RazonSocial AS NomOperario,ComisnOper,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
,CdMngra,NumInicial,NumFinal
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_DevFcr AS D  INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON D.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos 
 LEFT JOIN Agencias  AS KA ON K.CdAgencia=KA.IdAgencia LEFT JOIN Localidades AS KL ON K.CdLocal=KL.IdLocal
 LEFT JOIN Terceros AS KV ON K.IdVend=KV.IdTercero  LEFT JOIN Terceros AS KO ON K.CdOperario=KO.IdTercero
 LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom
 WHERE TipDev=@pmTipDev  AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryDevFvc @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrSubTotal,VrDescuento
,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca,IdVend,TarifaCom,CodTarCom
,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_DevFvc
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFvcDoc  @pmTipDoc VARCHAR(3),@pmFactura INT ,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia
,VrNeto,ModdDev,TipCom,Comprobante,IdCiaCom
 FROM  Trn_DevFvc
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY Devolucion,IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFvcLta  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null, @pmDevolucionFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT TipDev,Devolucion,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,Agencia
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca
,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,TipCom,TipoCom,Comprobante,IdCiaCom
, ModdDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,OrigenAdd,F.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
 FROM Trn_DevFvc AS F  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario  INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TC ON F.TipCom=TC.IdCom
 WHERE Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND (Devolucion>=ISNULL(@pmDevolucionIni,0)  AND Devolucion<=ISNULL(@pmDevolucionFin,2147483647))
 AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY F.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFvcNMes @pmAnno INT,@pmMes INT=Null,@pmIdCia CHAR(2)=Null,@pmIdPropietario VARCHAR(16)=Null
,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT  VC.IdCliente AS NitCliente,VC.IdVehiculo AS PlacaVeh,D.IdCia AS CodCia,Month(Fecha) AS nMes,SUM(ValorFactura) AS SValNet
 FROM  Trn_DevFvc AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Factura=VC.Documento AND D.IdCiaDoc=VC.IdCia
 WHERE Year(Fecha)=@pmAnno AND  Month(Fecha)=ISNULL(@pmMes, Month(Fecha))
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND VC.IdCliente LIKE ISNULL(@pmIdPropietario,'%')
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 GROUP BY VC.IdCliente,VC.IdVehiculo,D.IdCia,Month(Fecha)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFvcVFec  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null 
AS
 SELECT VC.IdCliente AS NitCliente,VC.IdVehiculo AS IdVehic,SUM(ValorFactura) AS SValNet
 FROM  Trn_DevFvc AS D INNER JOIN Trn_CausacVence AS VC ON D.TipDoc=VC.TipDoc AND D.Factura=VC.Documento AND D.IdCiaDoc=VC.IdCia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  
 AND VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND VC.IdCliente LIKE ISNULL(@pmIdPropietario,'%')
 GROUP BY VC.IdCliente,VC.IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevFvc_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,F.IdCia AS CdCia,Compania,F.Fecha AS FechaDev,F.IdConcepto AS CdConcepto,Concepto,F.TipDoc AS TipFact,F.Factura AS NumFactura
,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrCostos,VrNeto,BaseImp,BaseRet,BaseIca
,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,F.CodTarCom AS CdTarifCom,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom
,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,OrigenAdd,F.TimeSys AS Fecha_Add,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,Entradas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm
,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,VrCostoEnt,VrCostoSal,VrBruto,K.Referencia AS KarReferencia,Descripcion
,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarRemision,K.IdCiaRem AS KarCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Unidades,CodTarCmc,pVehiculo
,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,Referencia2
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_DevFvc AS F INNER JOIN Trn_Kardex AS K ON F.TipDev=K.TipDoc AND F.Devolucion=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON F.TipDev=TD.IdDoc
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TCM ON F.TipCom=TCM.IdCom
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND F.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY F.Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevNpr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,NumNota,IdCiaDoc,FecDoc,IdTercero,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden
,IdCCosto,IdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevNpr
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevNprDoc @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,NumNota,IdCiaDoc,FecDoc,IdTercero,VrSubTotal,VrCostos
,NumOrden,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevNpr
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevNprLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmNumNotaIni INT=Null,@pmNumNotaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,NumNota,IdCiaDoc,FecDoc,D.IdTercero AS NitTercero,RazonSocial,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden
,D.IdCCosto AS IdCenCosto,CCosto,IdSubCos,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
 FROM Trn_DevNpr AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND NumNota BETWEEN ISNULL(@pmNumNotaIni,0) AND ISNULL(@pmNumNotaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevNprRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,NumNota,IdCiaDoc,FecDoc
,D.IdTercero AS NitTercero,RazonSocial,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,D.IdCCosto AS IdCenCosto,CCosto
,D.IdSubCos AS CodSubCentro,SubCosto,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--INFORMACION DEL TERCERO
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
 FROM Trn_DevNpr AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 WHERE  TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND D.IdCCosto LIKE ISNULL(@pmIdCCosto ,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevNpr_Cr   @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipNota,NumNota,IdCiaDoc,FecDoc
,D.IdTercero AS NitTercero,RazonSocial,VrSubTotal,VrCostos,CantEnt,CantSal,D.NumOrden AS NroOrden,D.IdCCosto AS IdCenCosto,CC.CCosto AS NomCentCosto
,D.IdSubCos AS CodSubCentro,SC.SubCosto AS NomSubCentro,Modalidad,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,Leyenda
--INFORMACION DEL TERCERO
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--DETALLES
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,VrReteEnt,VrReteSal,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,K.NumOrden AS KarNumOrden,IdCiaOrd,Cotizacion,IdCiaCot
,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_DevNpr AS D  INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd  
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 WHERE  TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPas @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,nNota,IdCiaDoc,FecDoc,NitTercero,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto,Cantidad,IdCajero
,IdVend,TarifaCom,CodTarCom,IdCCosto,IdSubCos,Referencia,ModdDev,NumAutoriza,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevPas
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPasDoc @pmTipDoc VARCHAR(3),@pmnNota  INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,nNota,IdCiaDoc,FecDoc,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevPas
 WHERE TipDoc=@pmTipDoc  AND  nNota=@pmnNota   AND  IdCiaDoc=@pmIdCiaDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPasLta  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmnNotaIni INT=Null,@pmnNotaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmCdJornada VARCHAR(4)=Null
,@pmNitTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmModdDev VARCHAR(10)=Null 
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,nNota,IdCiaDoc,FecDoc,NitTercero,T.RazonSocial AS NomCliente,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto
,Cantidad,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,IdCCosto,IdSubCos,Referencia,ModdDev,NumAutoriza,OrigenAdd
,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev
 FROM Trn_DevPas AS D INNER JOIN  Terceros AS T ON D.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND nNota BETWEEN ISNULL(@pmnNotaIni,0) AND ISNULL(@pmnNotaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND CdJornada LIKE ISNULL(@pmCdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND NitTercero LIKE ISNULL(@pmNitTercero,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%')  AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPas_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipoNota,nNota,IdCiaDoc,FecDoc
,NitTercero,T.RazonSocial AS NomCliente,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto
,Cantidad,IdCajero,UC.Usuario AS UsuCajero,D.IdVend AS NitVendedor,V.RazonSocial AS NomVendedor,TarifaCom,D.Referencia AS DocReferencia,ModdDev,NumAutoriza,OrigenAdd
,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd
,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,CdMngra,NumInicial,NumFinal
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_DevPas AS D INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN  Conceptos AS CNC ON D.IdConcepto=CNC.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS V ON D.IdVend=V.IdTercero  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPla @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Planilla,IdCiaDoc,FecDoc,NitTercero,IdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos
,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero
,IdVend,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad,FecCorte,NumCorte,ModdDev,NumAutoriza,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevPla
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPlaDoc @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Planilla,IdCiaDoc,FecDoc,NitTercero,IdJornada,VrSubTotal,VrNeto
,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevPla
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla  AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPlaLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmPlanillaIni INT=Null,@pmPlanillaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdJornada VARCHAR(4)=Null
,@pmNitTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmModdDev VARCHAR(10)=Null 
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Planilla,IdCiaDoc,FecDoc,NitTercero,T.RazonSocial AS NomCliente,D.IdJornada AS CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb
,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad
,FecCorte,NumCorte,ModdDev,NumAutoriza,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev
 FROM Trn_DevPla  AS D INNER JOIN  Terceros AS T ON D.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN Jornadas AS J ON D.IdJornada=J.IdJornada
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND Planilla BETWEEN ISNULL(@pmPlanillaIni,0) AND ISNULL(@pmPlanillaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND D.IdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND NitTercero LIKE ISNULL(@pmNitTercero,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')  AND IdVend LIKE ISNULL(@pmIdVend,'%')  
 AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPlaRel  @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdJornada VARCHAR(4)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Planilla,IdCiaDoc,FecDoc
,NitTercero,T.RazonSocial AS NomCliente,D.IdJornada AS CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal
,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal
,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,UC.Usuario AS UsuCajero
,D.IdVend AS NitVendedor,VN.RazonSocial AS NomVendedor,TarifaCom,NitRef,D.Referencia AS DocReferencia,Modalidad,FecCorte,NumCorte,ModdDev
,NumAutoriza,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_DevPla  AS D   INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON D.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON D.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON D.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero 
 WHERE TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdJornada LIKE ISNULL(@pmIdJornada,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPla_Cr  @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipPla,Planilla,IdCiaDoc,FecDoc
,NitTercero,T.RazonSocial AS NomCliente,D.IdJornada AS CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal
,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal
,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,UC.Usuario AS UsuCajero
,D.IdVend AS NitVendedor,VN.RazonSocial AS NomVendedor,TarifaCom,NitRef,D.Referencia AS DocReferencia,Modalidad,FecCorte,NumCorte,ModdDev
,NumAutoriza,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado
,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd
,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,K.IdVend AS KarNitVend,KV.RazonSocial AS KarVendedor,Comision,CdOperario,KO.RazonSocial AS NomOperario,ComisnOper,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,CdMngra,NumInicial,NumFinal
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_DevPla  AS D  INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN  Conceptos AS CNC ON D.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON D.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON D.IdVend=VN.IdTercero
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero  LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN Terceros AS KV ON K.IdVend=KV.IdTercero  LEFT JOIN Terceros AS KO ON K.CdOperario=KO.IdTercero
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin  AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryDevPrestamos @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Prestamo,IdCiaDoc,FecDoc,FechaPrest,TipoPlazo,NumCuotas,TipoCuota,IdVehiculo,IdPropietario,IdConductor,VehPropio,ValorTotal,TasaInteres,CxPagar,Factura,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario
 FROM Trn_DevPrestamos
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevPrestamosLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
 AS
 SELECT  Devolucion, IdCia, Fecha, D.IdConcepto AS IdConcept,Concepto,TipDoc,Prestamo, IdCiaDoc, FecDoc, FechaPrest, TipoPlazo, NumCuotas, TipoCuota, D.IdVehiculo AS PlacaVeh,NumVeh
,D.IdPropietario AS NitPropietario, T.RazonSocial AS Propietario,D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor, D.VehPropio AS VhPropio, ValorTotal, TasaInteres, CxPagar, Factura
, TipCom, Comprobante, IdCiaCom, D.Observacion AS Observ, D.IdEstado AS IdEstad,Estado,TimeSys,IdCiaCrea, D.IdUsuario AS IdUsuari,Usuario
 FROM   Trn_DevPrestamos AS D  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto 
 INNER JOIN Terceros AS T ON D.IdPropietario=T.IdTercero INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
 INNER JOIN  EstadoDoc AS E ON D.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND IdCia like ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevPrestamos_Cr @pmTipDev VARCHAR(3), @pmDevolucionIni INT=Null, @pmDevolucionFin INT=Null, @pmIdCia CHAR(2)=Null
 AS
 SELECT   TipDev,TipoDoc,Devolucion, D.IdCia AS CodCia,Compania, Fecha, D.IdConcepto AS IdConcept,Concepto,TipDoc, Prestamo, IdCiaDoc, FecDoc, FechaPrest, TipoPlazo, NumCuotas, TipoCuota, D.IdVehiculo AS PlacaVeh
,D.IdPropietario AS NitPropietario, D.IdConductor AS CedConductor, D.VehPropio AS VhPropio, ValorTotal, TasaInteres, CxPagar, Factura, TipCom,TipoCom, Comprobante
, IdCiaCom, D.Observacion AS Observ,D.IdEstado AS IdEstad,ED.Estado AS EstadDoc,TimeSys,IdCiaCrea, D.IdUsuario AS IdUsuari,Usuario
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.RazonSocial AS Propietario,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.RazonSocial AS Conductor,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM   Trn_DevPrestamos AS D INNER JOIN Sys_TiposDoc  AS TD ON D.TipDev=TD.IdDoc   INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa    LEFT JOIN TiposCom AS TM ON D.TipCom=TM.IdCom
   LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal 
 LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
   WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni  AND  @pmDevolucionFin
  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY D.IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevPrestamos_Crr  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmTipoPlazo VARCHAR(10)=Null,@pmTipoCuota VARCHAR(10)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmPrestamo INT=Null,@pmFechaPrest SMALLDATETIME=Null
 AS
 SELECT   Devolucion, D.IdCia AS CodCia,Compania, Fecha, D.IdConcepto AS IdConcept,Concepto,TipDoc, Prestamo, IdCiaDoc, FecDoc, FechaPrest, TipoPlazo, NumCuotas, TipoCuota, D.IdVehiculo AS PlacaVeh
,D.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario, D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor, D.VehPropio AS VhPropio, ValorTotal, TasaInteres, CxPagar, Factura, TipCom,TipoCom, Comprobante
, IdCiaCom, D.Observacion AS Observ,D.IdEstado AS IdEstad,ED.Estado AS EstadDoc, D.IdUsuario AS IdUsuari,Usuario,TipDev,TipoDoc,TimeSys,IdCiaCrea
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM   Trn_DevPrestamos AS D INNER JOIN Sys_TiposDoc  AS TD ON D.TipDev=TD.IdDoc   INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON D.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa   LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN TiposCom AS TM ON D.TipCom=TM.IdCom
  WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
  AND TipoPlazo LIKE ISNULL(@pmTipoPlazo,'%') AND TipoCuota LIKE ISNULL(@pmTipoCuota,'%') AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND D.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Prestamo>=ISNULL(@pmPrestamo,0)  AND Prestamo<=ISNULL(@pmPrestamo,2147483647))
 AND (FechaPrest>=ISNULL(@pmFechaPrest,CAST('19100101' AS SMALLDATETIME)) AND FechaPrest<=ISNULL(@pmFechaPrest,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo
,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdCCosto,IdSubCos,IdCajero,IdVend,TarifaCom,CodTarCom,IdLocal,EnEfectivo,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,NumAutoriza,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryDevRecAcu @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrNeto) AS STOTNET
 FROM Trn_DevRec
 WHERE YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY  IdCliente,IdAgencia,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevRecBuses @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)

AS
 SELECT    TipDev, Devolucion, IdCia, Fecha, IdConcepto, TipDoc, Recibo, IdCiaDoc, FecDoc, IdCliente, IdAgencia, IdVehiculo, IdConductor, VehPropio, Modalidad, 
                      ValorRecibido, ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorOtrosDct, ValorPagMas,ValorAhorro,ValorOConce, ValorNeto, ValorAplicado, 
                      ValorAnticipo, IdVendedor, Comision, IdTarCom, IdCta, EnEfectivo, NumCheque, IdBanco, EsParcial, IdClase, TipCom, Comprobante, IdCiaCom, 
                      Observacion, IdEstado, TimeSys, IdCiaCrea, IdUsuario
 FROM     Trn_DevRecBuses
 WHERE TipDev=@pmTipDev AND Devolucion= @pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevRecBusesAnt  @pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null
AS
  SELECT D.IdVehiculo AS IdVehic,R.IdClase AS IdClas,D.IdCia AS CodCia,SUM(R.ValorAnticipo) AS StotAnt,SUM(ValorAbono) AS STotAbo 
  FROM Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses AS R ON D.TipDoc=R.TipDoc  AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
  WHERE R.ValorAnticipo>0 AND D.IdVehiculo  LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 GROUP BY   D.IdVehiculo,R.IdClase,D.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevRecBusesAntc  @pmIdCliente VARCHAR(16)=Null
AS
 --devolucion de recibos anticipos por cliente
  SELECT D.IdCliente AS IdClie,SUM(R.ValorAnticipo) AS StotAnt,SUM(ValorAbono) AS STotAbo 
  FROM Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses AS R ON D.TipDoc=R.TipDoc  AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
  WHERE R.ValorAnticipo>0 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 GROUP BY   D.IdCliente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevRecBusesLta @pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCliente VARCHAR(16),@pmIdVehiculo VARCHAR(10)

AS
 SELECT  Devolucion, IdCia, Fecha,D.IdConcepto AS IdConcept,Concepto,TipDoc, Recibo, IdCiaDoc, FecDoc, IdCliente,T.RazonSocial AS Cliente, IdAgencia, IdVehiculo, IdConductor,O.RazonSocial AS Conductor,VehPropio, Modalidad, 
                      ValorRecibido, ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorOtrosDct, ValorPagMas,ValorAhorro,ValorOConce, ValorNeto, ValorAplicado, 
                      ValorAnticipo, IdVendedor, D.Comision AS Comison, IdTarCom, IdCta, EnEfectivo, NumCheque,D.IdBanco AS IdBanc,Banco, EsParcial,D.IdClase AS IdClas,ClaseCuenta, TipCom, Comprobante, IdCiaCom, 
                      D.Observacion AS Observ, D.IdEstado AS IdEstad,Estado, TimeSys, IdCiaCrea, D.IdUsuario AS IdUsuari
 FROM Trn_DevRecBuses  AS D INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto
   INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
   INNER JOIN Terceros AS O ON D.IdConductor=O.IdTercero
   LEFT JOIN Bancos AS B ON D.IdBanco=B.IdBanco
   LEFT JOIN BusClaseCue AS L ON D.IdClase=L.IdClase
   LEFT JOIN EstadoDoc AS E ON D.IdEstado=E.IdEstado
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevRecBusesNMes @pmAnno INT,@pmMes INT=Null, @pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdClase VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT IdCliente,IdVehiculo,IdCia,IdClase,Month(Fecha) AS NMes,SUM(ValorTotal) AS SvalTot,SUM(ValorNeto) AS SValNet
 FROM     Trn_DevRecBuses
 WHERE Year(Fecha)=@pmAnno AND Month(Fecha)=ISNULL(@pmMes,Month(Fecha))  AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 GROUP BY  IdCliente,IdVehiculo,IdCia,IdClase,Month(Fecha)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRecBusesVFec @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16),@pmIdVehiculo VARCHAR(10)=Null,@pmIdClase VARCHAR(4)=Null
AS
 SELECT IdCliente,IdVehiculo,SUM(ValorTotal) AS SvalTot,SUM(ValorNeto) AS SValNet
 FROM   Trn_DevRecBuses
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  IdCia like ISNULL(@pmIdCia,'%%') 
 AND IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdClase like ISNULL(@pmIdClase,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY IdCliente,IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryDevRecBuses_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)

AS
 SELECT D.TipDev AS IdTipoDev,TD.TipoDoc AS TipoDev, D.Devolucion AS NDevolucion, D.IdCia AS CodCia,Compania, D.Fecha AS FechaDev, D.IdConcepto AS IdConcept, CC.Concepto AS ConceptoDev,D.TipDoc AS IdTipoDoc,TDC.TipoDoc AS TipoRecibo, D.Recibo AS nRecibo
, D.IdCiaDoc AS IdCiaRecibo, FecDoc, D.IdCliente AS NitPropietario,NP.RazonSocial AS Propietario, D.IdAgencia, D.IdVehiculo AS PlacaVeh, D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor, D.VehPropio AS VehEsPropio, D.Modalidad,D.ValorRecibido, D.ValorTotal, D.ValorDcto, D.ValorReteFte
, D.ValorReteIva, D.ValorReteIca, D.ValorOtros, D.ValorOtrosDct, D.ValorPagMas,ValorAhorro,ValorOConce, D.ValorNeto, D.ValorAplicado,D.ValorAnticipo, D.IdVendedor,D.Comision, D.IdTarCom, D.IdCta AS CodCta, D.EnEfectivo, D.NumCheque, D.IdBanco AS CodBanco,Banco, EsParcial, D.TipCom AS IdTipoCom,TM.TipoCom AS TipoComprobante
,D.Comprobante, D.IdCiaCom,D.Observacion AS Observ, D.IdEstado AS IdEstadDev,ED.Estado AS EstadoDev,D.TimeSys, D.IdCiaCrea, D.IdUsuario AS IdUsuari,Usuario,TD.Leyenda
,TipCar,Cartulina,IdCiaCar,R.CentInicial AS CenInicial,R.CentFinal AS CenFinal,ValorPasaje
,Item,RF.TipDoc AS TipoFactura,Documento,RF.IdCiaDoc AS CiaFactura,ItemDoc,RF.IdCargo AS CodCargo,F.Concepto AS Cargo,RF.IdClase AS CodClase,ClaseCuenta,RF.ValorAbono AS ValorAbonado,RF.Devolucion AS ValorDevolucion,Descuento,Detalle,FecPago
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,V.CentInicial AS VehCentInicial,V.CentFinal AS VehCentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM   Trn_DevRecBuses  AS D INNER JOIN Sys_TiposDoc  AS TD ON D.TipDev=TD.IdDoc  INNER JOIN Sys_TiposDoc  AS TDC ON D.TipDoc=TDC.IdDoc 
 INNER JOIN Trn_RecBuses AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia  INNER JOIN Conceptos AS CC ON D.IdConcepto=CC.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON  D.IdUsuario=U.IdUsuario
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS NP ON D.IdCliente=NP.IdTercero
 INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero  INNER JOIN Bancos AS B ON D.IdBanco=B.IdBanco
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa    LEFT JOIN TercCndtores AS CT ON D.IdVendedor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN Trn_RecFactura AS RF ON D.TipDev=RF.TipRec AND D.Devolucion=RF.Recibo AND D.IdCia=RF.IdCia
 LEFT JOIN Fijos AS F ON RF.IdCargo=F.IdCargo  LEFT JOIN BusClaseCue  AS CU ON RF.IdClase=CU.IdClase
 LEFT JOIN TiposCom AS TM ON D.TipCom=TM.IdCom
 WHERE TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin 
  AND D.IdCia=@pmIdCia 
 ORDER BY D.IdCia,D.Devolucion,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRecBuses_Crr @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmRecibo INT=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null,@pmIdUsuario VARCHAR(11)=Null
AS
 SELECT TipDev,Devolucion,D.IdCia AS CodCia,Compania, D.Fecha AS FechaDev,D.TipDoc AS IdTipoRec,D.Recibo AS nRecibo,IdCiaDoc,FecDoc,D.IdCliente AS NitPropietario,NP.RazonSocial AS Propietario,D.IdVehiculo AS PlacaVeh
,D.IdConductor AS CedConductor,NC.RazonSocial AS Conductor, D.VehPropio AS VehEsPropio, D.Modalidad AS Modaldad,D.ValorRecibido, D.ValorTotal, D.ValorDcto, D.ValorReteFte, D.ValorReteIva, D.ValorReteIca, D.ValorOtros, D.ValorOtrosDct, D.ValorPagMas
,ValorAhorro,ValorOConce, D.ValorNeto AS VrNeto, D.ValorAplicado,D.ValorAnticipo,D.IdConcepto AS IdConcept,Concepto,D.IdCta AS CodCta, D.EnEfectivo AS Efectivo,D.NumCheque, D.IdBanco AS CodBanco,Banco,D.TipCom AS IdTipoCom,TipoCom,D.Comprobante
,D.IdCiaCom,D.Observacion AS Observ, D.IdEstado AS IdEstadDev,ED.Estado AS EstadoDev,D.TimeSys AS FechaAdd, D.IdCiaCrea AS IdCiaCre, D.IdUsuario AS IdUsuari,Usuario,TipoDoc,TipCar,Cartulina,IdCiaCar,R.CentInicial AS CenInicial,R.CentFinal AS CenFinal,ValorPasaje
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,V.CentInicial AS VehCentInicio,V.CentFinal AS VehCentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_DevRecBuses AS D INNER JOIN Trn_RecBuses AS R ON D.TipDoc=R.TipDoc AND D.Recibo=R.Recibo AND D.IdCiaDoc=R.IdCia
 INNER JOIN Sys_TiposDoc  AS TD ON D.TipDev=TD.IdDoc  INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS CC ON D.IdConcepto=CC.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON  D.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON D.IdCliente=NP.IdTercero  INNER JOIN Terceros AS NC ON D.IdConductor=NC.IdTercero
 INNER JOIN Bancos AS B ON D.IdBanco=B.IdBanco  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON  D.IdVendedor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN TiposCom AS TM ON D.TipCom=TM.IdCom
  WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  TipDev=@pmTipDev AND D.IdCia like ISNULL(@pmIdCia,'%%')
  AND ( D.Recibo>=ISNULL(@pmRecibo,0)  AND  D.Recibo<=ISNULL(@pmRecibo,2147483647))
 AND D.IdConcepto like ISNULL(@pmIdConcepto,'%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRecDoc @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia
,TipCom,Comprobante,IdCiaCom,IdEstado,Modalidad
 FROM Trn_DevRec
 WHERE  TipDoc=@pmTipDoc AND Recibo=@pmRecibo  AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRecLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmReciboIni INT=Null,@pmReciboFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmEnEfectivo BIT=Null,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto
,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdCCosto,IdSubCos,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,D.IdLocal AS CodLocal,EnEfectivo,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,NumAutoriza,Modalidad,ModdDev
,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDev
 FROM Trn_DevRec  AS D INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL (@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND  Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND D.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRecRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmEnEfectivo BIT=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,T.RazonSocial AS NomCliente
,D.IdAgencia AS  IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto
,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,D.IdCCosto AS IdCenCost,CCosto,D.IdSubCos AS IdSubCent,SubCosto
,IdCajero,UC.Usuario AS NomCajero,D.IdVend AS NitVendedor,V.RazonSocial AS Vendedor,TarifaCom,D.IdLocal AS CdCiudadRec,LR.Localidad AS CiudadRecibo
,EnEfectivo,D.Referencia AS DocReferncia,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,TipDcm,D.Documento AS NumDoc,IdCiaDcm
,D.NumAutoriza AS NAutoriza,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevRec AS D  INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero  INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia  INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Localidades AS LR ON D.IdLocal=LR.IdLocal LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS TC ON D.CdConductor=TC.IdTercero
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND D.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND  IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND  D.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevRec_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc ,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS Tip_Rec
,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS  IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo
,VrEfectivo,BaseRet,BaseIca,BaseRiv,D.IdCCosto AS IdCenCost,CCosto,D.IdSubCos AS IdSubCent,SubCosto
,IdCajero,UC.Usuario AS NomCajero,D.IdVend AS NitVendedor,V.RazonSocial AS Vendedor,TarifaCom,D.IdLocal AS CdCiudadRec,LR.Localidad AS CiudadRecibo
,EnEfectivo,D.Referencia AS DocReferncia,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,TipDcm,D.Documento AS NumDoc,IdCiaDcm
,D.NumAutoriza AS NAutoriza,Modalidad,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario,Leyenda
--detalle de pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco,NitCliente,PC.RazonSocial AS NomCliente
,CdAgencia,PA.Agencia AS PagNomAgencia,PA.CodAgencia AS PagCodAgencia,PA.Referencia AS PagRefeAgencia,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1
,P.Referncia2 AS PagReferencia2,CdLocal,LF.Localidad AS CiudadPlaza,P.NumAutoriza AS PagAutorizacion,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevRec AS D  INNER JOIN Trn_Pagos AS P ON D.TipDev=P.TipDoc AND D.Devolucion=P.Documento AND D.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero  INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Localidades AS LR ON D.IdLocal=LR.IdLocal LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom
 LEFT JOIN Terceros AS TC ON D.CdConductor=TC.IdTercero
 LEFT JOIN Terceros AS PC ON P.NitCliente=PC.IdTercero  LEFT JOIN Agencias  AS PA ON P.CdAgencia=PA.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevSal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Salida,IdCiaDoc,FecDoc,IdTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp
,IdCCosto,IdSubCos,TipOrd,NumOrden,IdCiaOrd,pVehiculo,NContrato,IdCiaCont,NitCliente,CdAgencia,ModRequis
,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevSal
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevSalDoc @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Salida,IdCiaDoc,FecDoc,VrSubTotal,VrImpuesto,VrCostos,VrNeto
,TipCom,Comprobante,IdCiaCom
 FROM Trn_DevSal
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCiaDoc=@pmIdCiaDoc
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevSalLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null
,@pmSalidaIni INT=Null,@pmSalidaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmModdDev VARCHAR(10)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Devolucion,IdCia,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Salida,IdCiaDoc,FecDoc,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero
,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,D.IdCCosto AS IdCenCosto,CCosto,IdSubCos,TipOrd,NumOrden,IdCiaOrd,pVehiculo,NContrato,IdCiaCont
,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,ModRequis,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
 FROM Trn_DevSal AS D INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 LEFT JOIN Terceros AS CL ON D.NitCliente=CL.IdTercero
 WHERE TipDev=@pmTipDev AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0)  AND ISNULL(@pmDevolucionFin,2147483647)
 AND Salida BETWEEN ISNULL(@pmSalidaIni,0) AND ISNULL(@pmSalidaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevSalRel @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Salida,IdCiaDoc,FecDoc,D.IdTercero AS NitTercero
,T.RazonSocial AS NomTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,D.IdCCosto AS IdCenCosto,CCosto,D.IdSubCos AS IdSubCosto,SubCosto,TipOrd
,NumOrden,IdCiaOrd,NContrato,IdCiaCont,NitCliente,ModRequis,Modalidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_DevSal AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')  AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevSal_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipoSal
,Salida,IdCiaDoc,FecDoc,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp
,D.IdCCosto AS IdCenCosto,CC.CCosto AS NomCentCosto,D.IdSubCos AS IdSubCosto,SC.SubCosto AS SubCentCosto,D.TipOrd AS TipoOrden
,D.NumOrden AS NroOrden,D.IdCiaOrd AS CodCiaOrden,NContrato,IdCiaCont,NitCliente,ModRequis
,Modalidad,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--Detalle
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,KT.RazonSocial AS KarNomTercero,K.CdAgencia AS KarAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,CdLocal,KL.Localidad AS KarCiudad,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,K.TipOrd AS KarTipOrden,K.NumOrden AS KarNumOrden,K.IdCiaOrd AS KarCiaOrden,Cotizacion,IdCiaCot
,Remision,IdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_DevSal AS D INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd INNER JOIN Terceros AS KT ON K.IdTercero=KT.IdTercero
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos LEFT JOIN Localidades AS KL ON K.CdLocal=KL.IdLocal
 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND  @pmDevolucionFin AND D.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Devolucion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevVal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,nRadica,IdCiaDoc,FecDoc,IdTercero,TotalPagos,TotalCobros,Referencia,CxPagar,VrDescuento,ModdDev,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_DevVal
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevValDoc @pmTipDoc VARCHAR(3),@pmnRadica  INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,nRadica,IdCiaDoc,FecDoc,TipCom,Comprobante,IdCiaCom,ModdDev,CxPagar
 FROM Trn_DevVal
 WHERE TipDoc=@pmTipDoc  AND  nRadica=@pmnRadica   AND  IdCiaDoc=@pmIdCiaDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevValLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmDevolucionIni INT=Null,@pmDevolucionFin INT=Null,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmModdDev VARCHAR(10)=Null
 AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,nRadica,IdCiaDoc,FecDoc,D.IdTercero AS NitTercero,RazonSocial,TotalPagos,TotalCobros
,Referencia,CxPagar,VrDescuento,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,TipDev
 FROM Trn_DevVal AS D  INNER JOIN  Terceros AS T ON D.IdTercero=T.IdTercero  INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE Fecha BETWEEN   @pmFechaIni AND @pmFechaFin AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND Devolucion BETWEEN ISNULL(@pmDevolucionIni,0) AND ISNULL(@pmDevolucionFin,2147483647)
 AND D.IdConcepto  LIKE ISNULL(@pmIdConcepto,'%') AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND ModdDev LIKE ISNULL(@pmModdDev,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDevVal_Cr @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,TipoDoc,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipRad,D.nRadica AS NumRadica,IdCiaDoc,FecDoc
,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,TotalPagos,TotalCobros,D.Referencia AS DocReferencia,CxPagar,VrDescuento,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,Leyenda
--detalles
,Item,TipoRango,IdRango,NumSerie,NumForma,V.Fecha AS FechaServ,HoraServ,IdCliente,CL.RazonSocial AS NomCliente,V.IdAgencia AS ValAgencia,Agencia,IdCuenta,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera
,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,PlacaVeh,CdConductor,NomConductor,CantPasaj,Usuarios,TipFac,NumFactura,IdCiaFac,FechaFac,V.Anulado AS ValAnulado,V.NumDev AS ValNumAnul,V.FecDev AS ValFechaDev
,V.Observacion AS ValObserv,FechaUp,CdUsuario
--datos del cliente
,CL.TipoId AS CliTipoId,CL.Dv AS CliDv,CL.Codigo AS CliCodigo,CL.NomCial AS CliNomCial,CL.Direccion AS CliDireccion,CL.IdLocal AS CliCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,CL.Telefono AS CliTelefono,CL.Fax AS CliFax,CL.e_mail AS CliEmail,CL.IdSector AS CdSector,SectorEco,CL.IdRegimen AS CdRegimen,Regimen,CL.TipEnte AS CliTipoEnte
 FROM Trn_DevVal AS D INNER JOIN Trn_Vales AS V ON D.TipDoc=V.TipDoc AND D.nRadica=V.nRadica AND D.IdCiaDoc=V.IdCia
 INNER JOIN  Terceros AS T ON D.IdTercero=T.IdTercero  INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON D.TipDev=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS CL ON V.IdCliente=CL.IdTercero
 INNER JOIN Localidades AS L ON CL.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON CL.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON CL.IdSector=SE.IdSector
 LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 WHERE TipDev=@pmTipDev AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDeveloper
AS
 SELECT NitCia,Nombre,Direccion,Telefono,Fax,Ciudad,Internet,Email,RepLegal
 FROM Sys_Developer
 ORDER BY NitCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiarios @pmIdDiario VARCHAR(4)
 AS 
 IF @pmIdDiario IS NULL
  SELECT IdDiario,Diario
  FROM Diarios
  WHERE Inactivo=0
  ORDER BY IdDiario
 ELSE
  SELECT IdDiario,Diario,Inactivo
  FROM Diarios
  WHERE IdDiario=@pmIdDiario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidos @pmIdDiferido VARCHAR(30)
 AS 
 SELECT IdDiferido,NomDiferido,IdTipDif,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,DesUbicacion,IdEstado
,FecRetiro,IdCausal,ComentRetiro,Comentarios,FecCreacion,IdUsuario,TipoAmtza,NitCliente,AgenCliente,CantGalones,IdProv
,NumPlaca,CodInstala
 FROM Diferidos
 WHERE IdDiferido=@pmIdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidosAmt @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT IdDiferido,NomDiferido,IdTipDif,IdGrupo,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,CorrMonDiferida,NumMesesDif,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA
,IdEstado,FecRetiro,TipoAmtza,NitCliente,AgenCliente,CantGalones,IdProv,NumPlaca,CodInstala
 FROM Diferidos
 WHERE FecInicio<=@pmFechaFin
  AND (IdEstado<>'9999'  OR (IdEstado='9999' AND FecRetiro>=@pmFechaIni))
 ORDER BY IdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidosHis @pmIdDiferido VARCHAR(30)
 AS 
 SELECT A.IdDiferido AS CdDiferido,NomDiferido,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro
,CC.CCosto AS DifCentroCosto,A.IdSubCos AS CdSubcentro,SC.SubCosto AS DifSubCentro,A.IdDep AS CdDep,DP.Dependencia AS DifDependencia,DesUbicacion
,IdRespons,T.RazonSocial AS NomResponsable,A.IdCia AS CdCia,CIA.Compania AS DifNomCia,FecInicio,A.ValorTotal AS DifValTotal,A.VrAdiciones AS DifAdiciones,A.VrDisminucion AS DifDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumDoc,Referencia,A.IdEstado AS CdEstado,ED.Estado AS DifEstado
,FecRetiro,A.IdCausal AS CdCausal,Causal,ComentRetiro,Comentarios,FecCreacion,A.IdUsuario AS IdUsuari
--DATOS DEL DIFERIDO
,AMZ.Numero AS NumRegistro,Fecha,AMZ.Estado AS EstadoDoc,AMZ.IdUsuario AS CdUsuario,Usuario,FechaCrea,AMZ.IdCia AS CdCia,CI.Compania AS NomCia
,Item,D.ValorTotal AS ValorInicial, D.VrAdiciones AS ValAdiciones, D.VrDisminucion AS ValDisminucion, VrAcuAjustes, VrAmortizaHis, VrAmortizaMes, VrAjustesInf
, VrAjustesAmtza,VrAmtzaHisAcum, VrAmortizaAcum, VrAjuAmtzaAcum, VrCorrMonAcum, VrBaseCorrMon, VrCorrMonDif, TarifaAju
,D.IdCia AS DetIdCia,CID.Compania AS DetNomCia,CdCCosto,CCD.CCosto AS NomCentroCosto,CdSubCos,SCD.SubCosto AS NomSubcentro, CdDep, DPD.Dependencia AS NomDepend
,NitRespons,TRD.RazonSocial AS DetResponsable,FecInicioAmtza, NPeriodos,TipoAmtza,NitCliente,AgenCliente,CantGalones
,IdProv,PV.RazonSocial AS NomProveedor,NumPlaca,CodInstala,Instlacion
 FROM Diferidos  AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN Companias AS CIA ON A.IdCia=CIA.IdCia  INNER JOIN Terceros AS PV ON A.IdProv=PV.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN Trn_AfAmtDetalle AS D ON A.IdDiferido=D.IdDiferido LEFT  JOIN Trn_AfAmortiza AS AMZ  ON D.Numero=AMZ.Numero
 LEFT JOIN Companias AS CI ON AMZ.IdCia=CI.IdCia  LEFT JOIN adm_Usuarios AS U ON AMZ.IdUsuario=U.IdUsuario
 LEFT JOIN Companias AS CID ON D.IdCia=CID.IdCia 
 LEFT JOIN CentroCosto AS CCD ON D.CdCCosto=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON D.CdDep=DPD.IdDep LEFT JOIN Terceros AS TRD ON D.NitRespons=TRD.IdTercero
 LEFT JOIN SubCentros AS SCD ON D.CdSubCos=SCD.IdSubCos  LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE A.IdDiferido LIKE ISNULL(@pmIdDiferido ,'%')
 ORDER BY NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDiferidosLta @pmIdGrupo VARCHAR(4)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdTipDif VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdDiferido,NomDiferido,A.IdTipDif AS CdTipDif,TipoDiferido,G.IdLinea AS CodLinea,Linea,A.IdGrupo AS CodGrupo,Grupo,A.IdCCosto AS CdCentro,CCosto,A.IdSubCos AS CdSubcentro,SubCosto
,A.IdDep AS CdDep,Dependencia,DesUbicacion,IdRespons,T.RazonSocial  AS NomResponsable,A.IdCia AS CdCia,Compania,FecInicio,ValorTotal,VrAdiciones,VrDisminucion,NumPeriodos
,FecFinAmtza,Ajustable,Contable,VrAjuInfAcum,VrAmtzaAcuHis,VrAmtzaAcum,VrAjuInfAmtza,CuotasAmtza,VrCorrMonDifAcu,CorrMonDiferida,NumMesesDif
,FecUltAmtza,CueEnGrupo,IdCuenta,IdCueAmortiza,IdCueAjuDif,IdCueCorrDif,IdCueAjuAmtza,IdCueCorrAA,NumPlaca,NumDoc,Referencia,A.IdEstado AS CdEstado,Estado
,FecRetiro,A.IdCausal AS CdCausal,Causal,ComentRetiro,Comentarios,FecCreacion,A.IdUsuario AS IdUsuari,Usuario,TipoAmtza,NitCliente,AgenCliente,CantGalones
,IdProv,PV.RazonSocial AS NomProveedor,CodInstala,Instlacion
--datos del responsable
,T.Codigo AS TercCodigo,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigla,T.Direccion AS TercDirecc,T.IdLocal AS CdLocalidad,Localidad
,LC.IdDep AS Cd_Depto,Departamento,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM Diferidos  AS A INNER JOIN AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN CentroCosto AS CC ON A.IdCCosto=CC.IdCCosto INNER JOIN AfTiposDif AS TD ON A.IdTipDif=TD.IdTipDif
 INNER JOIN Dependencias AS DP ON A.IdDep=DP.IdDep INNER JOIN Terceros AS T ON A.IdRespons=T.IdTercero
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS PV ON A.IdProv=PV.IdTercero
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal INNER JOIN Departamentos AS D ON LC.IdDep=D.IdDep
 LEFT JOIN SubCentros AS SC ON A.IdSubCos=SC.IdSubCos LEFT JOIN AfCausales AS CA ON A.IdCausal=CA.IdCausal
 LEFT JOIN Instalaciones AS I ON A.CodInstala=I.IdInstala
 WHERE A.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND A.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND  A.IdTipDif LIKE ISNULL(@pmIdTipDif,'%')
 AND A.IdDep LIKE ISNULL(@pmIdDep,'%') AND IdRespons LIKE ISNULL(@pmIdRespons,'%')
 AND  A.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (FecInicio>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicio<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryDisciplinario @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Numero,IdCia,Fecha,FecSuceso,IdVehiculo,IdPropietario,IdConductor,IdRuta,Lugar,NomReporta,CedReporta,VehiculoRep,RepCond
,Coordinador,Despachador,CitaDescargos,FecCita,MemoCita,Imputacion,Descargos,FecDesc,RecibeDesc,TipoSancion,DescSancion,FecSancion,CantSancion
,CodInfracc,Referencia,Observacion,Anulado,NumDev,FecDev,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Disciplinario
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDisciplinarioFmt @pmTipDoc VARCHAR(3),@pmNumeroIni INT,@pmNumeroFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.Numero AS NumInf,D.IdCia AS CdCia,Compania,Fecha,FecSuceso,D.IdVehiculo AS PlacaVeh,NumVeh,D.IdPropietario AS NitPropietario,P.RazonSocial AS Propietario
,D.IdConductor AS CedConductor,C.RazonSocial AS Conductor,D.IdRuta AS CdRuta,Ruta,Lugar,NomReporta,CedReporta,VehiculoRep,RepCond
,Coordinador,Despachador,CitaDescargos,FecCita,MemoCita,Imputacion,Descargos,FecDesc,RecibeDesc,TipoSancion,DescSancion,FecSancion,CantSancion
,CodInfracc,I.Infraccion AS NomInfracc,Referencia,D.Observacion AS Observ,Anulado,NumDev,FecDev,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
,Item,DT.Infraccion AS DescInfracc
--datos del vehículo
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo
,C.Codigo AS CondCodigo,C.Direccion AS CondDireccion,C.IdLocal AS CondCdCiudad,L.Localidad AS CondCiudad,L.IdDep AS CondCdDpto,DP.Departamento AS CondDpto
,C.Telefono AS CondTelefono,C.Fax AS CondFax,C.TelMovil AS CondTelMovil,C.e_mail AS CondEmail,C.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,C.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MC.Modalidad AS ModConductor,CT.CertJudicial AS CondCertJudicial,CT.VigJudicial AS CondVigJudicial,CT.Licencia AS CondLicencia
,CT.CatLicencia AS CondCategoria,CT.VigLicencia AS CondVigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM Trn_Disciplinario AS D INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS P ON D.IdPropietario=P.IdTercero
 INNER JOIN Terceros AS C ON D.IdConductor=C.IdTercero INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Trn_DisInfraccion AS DT ON D.TipDoc=DT.TipDoc   AND  D.Numero=DT.Numero  AND  D.IdCia=DT.IdCia 
 INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN Infracciones AS I ON D.CodInfracc=I.NumInf LEFT JOIN Rutas AS R ON D.IdRuta=R.IdRuta
 LEFT JOIN  Localidades AS LCE ON C.IdLugarCed=LCE.IdLocal
 LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor  LEFT JOIN TiposMod AS MC ON CT.IdModd=MC.IdModd
 LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE D.TipDoc=@pmTipDoc AND D.Numero BETWEEN @pmNumeroIni AND @pmNumeroFin  AND D.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDisciplinarioLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT D.Numero AS NumInf,D.IdCia AS CdCia,Compania,Fecha,FecSuceso,D.IdVehiculo AS PlacaVeh,NumVeh,D.IdPropietario AS NitPropietario,P.RazonSocial AS Propietario
,D.IdConductor AS CedConductor,C.RazonSocial AS Conductor,D.IdRuta AS CdRuta,Ruta,Lugar,NomReporta,CedReporta,VehiculoRep,RepCond
,Coordinador,Despachador,CitaDescargos,FecCita,MemoCita,Imputacion,Descargos,FecDesc,RecibeDesc,TipoSancion,DescSancion,FecSancion,CantSancion
,CodInfracc,I.Infraccion AS NomInfracc,Referencia,D.Observacion AS Observ,Anulado,NumDev,FecDev,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--datos del vehículo
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo
,C.Codigo AS CondCodigo,C.Direccion AS CondDireccion,C.IdLocal AS CondCdCiudad,L.Localidad AS CondCiudad,L.IdDep AS CondCdDpto,DP.Departamento AS CondDpto
,C.Telefono AS CondTelefono,C.Fax AS CondFax,C.TelMovil AS CondTelMovil,C.e_mail AS CondEmail,C.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,C.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MC.Modalidad AS ModConductor,CT.CertJudicial AS CondCertJudicial,CT.VigJudicial AS CondVigJudicial,CT.Licencia AS CondLicencia
,CT.CatLicencia AS CondCategoria,CT.VigLicencia AS CondVigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM Trn_Disciplinario AS D INNER JOIN Companias  AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS P ON D.IdPropietario=P.IdTercero
 INNER JOIN Terceros AS C ON D.IdConductor=C.IdTercero INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN Infracciones AS I ON D.CodInfracc=I.NumInf LEFT JOIN Rutas AS R ON D.IdRuta=R.IdRuta
 LEFT JOIN  Localidades AS LCE ON C.IdLugarCed=LCE.IdLocal
 LEFT JOIN TercCndtores AS CT ON D.IdConductor=CT.IdConductor  LEFT JOIN TiposMod AS MC ON CT.IdModd=MC.IdModd
 LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE D.TipDoc=@pmTipDoc AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND D.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND D.IdEstado LIKE ISNULL(@pmIdEstado,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryDocMemo @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Comentarios,Nota1,Nota2,Nota3
 FROM Trn_DocMemo
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCalibra @pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,nNota,IdCia,Fecha,IdConcepto,NitTercero,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto
,Cantidad,IdCajero,IdVend,TarifaCom,CodTarCom,IdCCosto,IdSubCos,Referencia,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsCalibra
 WHERE TipDoc=@pmTipDoc AND nNota=@pmnNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCalibraLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmnNotaIni INT=Null,@pmnNotaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmNitTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT nNota,IdCia,Fecha,N.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto,Cantidad
,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,IdCCosto,IdSubCos,Referencia,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev
,FecDev,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDoc
 FROM Trn_EdsCalibra AS N  INNER JOIN Terceros AS T ON N.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero
 INNER JOIN Conceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON N.IdCajero=UC.IdUsuario 
-- INNER JOIN Jornadas AS J ON N.IdJornada=J.IdJornada
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND nNota BETWEEN ISNULL(@pmnNotaIni,0) AND ISNULL(@pmnNotaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') 
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND NitTercero LIKE ISNULL(@pmNitTercero,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%')  AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,nNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCalibra_Cr @pmTipDoc VARCHAR(3),@pmnNotaIni INT,@pmnNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,nNota,C.IdCia AS CdCia,Compania,C.Fecha AS FechaDoc,C.IdConcepto AS CdConcepto,Concepto
,NitTercero,T.RazonSocial AS NomTercero,CdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrCostos,VrNeto,Cantidad
,IdCajero,UC.Usuario AS NomCajero,C.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom
,C.Referencia AS DocReferencia,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update
,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd
,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,CdMngra,NumInicial,NumFinal
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_EdsCalibra AS C INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.nNota=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto  INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado  
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS VN ON C.IdVend=VN.IdTercero  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed
 LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero  LEFT JOIN TiposCom AS TCM ON C.TipCom=TCM.IdCom
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE C.TipDoc=@pmTipDoc AND nNota BETWEEN @pmnNotaIni AND @pmnNotaFin AND C.IdCia=@pmIdCia
 ORDER BY nNota,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortes @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Planilla,IdCia,Fecha,IdConcepto,NitTercero,IdJornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros
,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,IdVend,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad
,FecCorte,NumCorte,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsCortes
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortesCor @pmFecCorte SMALLDATETIME,@pmNumCorte INT,@pmModalidad VARCHAR(10)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 SELECT TipDoc,Planilla,IdCia,Fecha,Modalidad,FecCorte,NumCorte,Anulado,NumDev,FecDev
 FROM Trn_EdsCortes
 WHERE FecCorte=@pmFecCorte AND NumCorte=@pmNumCorte AND Modalidad LIKE ISNULL(@pmModalidad,'%')
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia,Planilla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortesLta  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmPlanillaIni INT=Null,@pmPlanillaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdJornada VARCHAR(4)=Null
,@pmNitTercero VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Planilla,IdCia,Fecha,C.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,C.IdJornada AS  CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros
,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,NitRef,IdCCosto,IdSubCos,Referencia,Modalidad
,FecCorte,NumCorte,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDoc
 FROM Trn_EdsCortes AS C  INNER JOIN Terceros AS T ON C.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero
 INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario 
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Planilla BETWEEN ISNULL(@pmPlanillaIni,0) AND ISNULL(@pmPlanillaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND C.IdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND NitTercero LIKE ISNULL(@pmNitTercero,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')  AND IdVend LIKE ISNULL(@pmIdVend,'%')  
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Planilla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortesNet  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdJornada VARCHAR(4)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Planilla,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CodConcepto,Concepto,NitTercero,T.RazonSocial AS NomTercero,C.IdJornada AS CdJornada,Jornada
,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp
,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant
,IdCajero,UC.Usuario AS NomCajero,C.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitRef,TR.RazonSocial AS NomTerceroRef,C.Referencia AS DocReferencia,Modalidad
,FecCorte,NumCorte,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_EdsCortes  AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON C.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON C.NitRef=TR.IdTercero
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND C.IdJornada LIKE ISNULL(@pmIdJornada,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,D.IdJornada AS CdJornada,Jornada
,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal
,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant,IdCajero,UC.Usuario AS UsuCajero
,D.IdVend AS NitVendedor,VN.RazonSocial AS NomVendedor,TarifaCom,NitRef,TR.RazonSocial AS NomTerceroRef,D.Referencia AS DocReferencia,Modalidad
,FecCorte,NumCorte,TipCom,Comprobante,IdCiaCom,0,Planilla,FecDoc,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_DevPla  AS D   INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON D.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON D.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON D.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON D.NitRef=TR.IdTercero
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 UNION
 SELECT TipDoc,nNota,N.IdCia AS CdCia,Compania,Fecha,N.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,CdJornada,Jornada
,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,0,0,VrCostos,VrNeto,0,0,0,0,0,Cantidad,0,0,0,0,0,0,0,0,0,0
,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,'0','',Referencia,'',Null,0,TipCom,Comprobante,IdCiaCom,Anulado,NumDev
,FecDev,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_EdsCalibra AS N  INNER JOIN Terceros AS T ON N.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero
 INNER JOIN Conceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON N.IdCajero=UC.IdUsuario
 INNER JOIN Companias AS CI ON N.IdCia=CI.IdCia 
 LEFT JOIN Jornadas AS J ON N.CdJornada=J.IdJornada
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND CdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND IdVend LIKE ISNULL(@pmIdVend,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,CdJornada,Jornada
,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,0,0,VrCostos,VrNeto,0,0,0,0,0,Cantidad,0,0,0,0,0,0,0,0,0,0
,IdCajero,UC.Usuario AS UsuCajero,IdVend,V.RazonSocial AS NomVendedor,TarifaCom,'0','',Referencia,ModdDev,Null,0
,TipCom,Comprobante,IdCiaCom,0,nNota,FecDoc,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario 
 FROM Trn_DevPas AS D INNER JOIN  Terceros AS T ON D.NitTercero=T.IdTercero
 INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia 
 INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 LEFT JOIN Jornadas AS J ON D.CdJornada=J.IdJornada
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND CdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')   AND IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY C.IdCia,Planilla

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortesNetDet  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdJornada VARCHAR(4)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
 AS 
 SELECT C.TipDoc AS TipoPla,Planilla,C.IdCia AS CdCia,Compania,C.Fecha AS FechaPla,C.IdConcepto AS CodConcepto,Concepto,NitTercero,T.RazonSocial AS NomTercero,C.IdJornada AS CdJornada,Jornada
,VrOtros,VrSobrantes,VrFaltantes,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,IdCajero,UC.Usuario AS NomCajero
,NitRef,TR.RazonSocial AS NomTerceroRef,C.Referencia AS DocReferencia,Modalidad,FecCorte,NumCorte,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,C.TimeSys AS Fecha_Add,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalles
,K.IdProducto AS CdProducto,DescripProd,Item,CdTanque,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom
,ImpGlobal,CdMngra,NumInicial,NumFinal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,K.Comision AS TarifCom
,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper,CdCCosto,CCosto,CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd
,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,TarifaDct,VrDctoEnt,VrDctoSal
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
 FROM Trn_EdsCortes  AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario 
 INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.Planilla=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN CentroCosto AS CCT ON K.CdCCosto=CCT.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON C.NitRef=TR.IdTercero
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND C.IdJornada LIKE ISNULL(@pmIdJornada,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND C.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,D.IdJornada AS CdJornada,Jornada
,VrOtros,VrSobrantes,VrFaltantes,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,IdCajero,UC.Usuario AS UsuCajero
,NitRef,TR.RazonSocial AS NomTerceroRef,D.Referencia AS DocReferencia,Modalidad,FecCorte,NumCorte,TipCom,Comprobante,IdCiaCom,0,Planilla,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fecha_Add,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalles
,K.IdProducto AS CdProducto,DescripProd,Item,CdTanque,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom
,ImpGlobal,CdMngra,NumInicial,NumFinal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,K.Comision AS TarifCom
,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper,CdCCosto,CCosto,CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd
,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,TarifaDct,VrDctoEnt,VrDctoSal
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
 FROM Trn_DevPla  AS D   INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON D.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON D.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN CentroCosto AS CCT ON K.CdCCosto=CCT.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 LEFT JOIN Terceros AS T ON D.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON D.NitRef=TR.IdTercero
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND D.IdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND D.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
--pasadas
 UNION
 SELECT N.TipDoc AS TipoNota,nNota,N.IdCia AS CdCia,Compania,N.Fecha AS FechaNota,N.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,CdJornada,Jornada
,VrOtros,0,0,VrNeto,0,0,0,0,0,IdCajero,UC.Usuario AS UsuCajero,'0','',N.Referencia AS Refncia,'',Null,0,TipCom,Comprobante,IdCiaCom,Anulado,NumDev
,FecDev,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,N.TimeSys AS FechaAdd,IdCiaCrea,N.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalles
,K.IdProducto AS CdProducto,DescripProd,Item,CdTanque,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom
,ImpGlobal,CdMngra,NumInicial,NumFinal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,K.Comision AS TarifCom
,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper,CdCCosto,CCosto,CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd
,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,TarifaDct,VrDctoEnt,VrDctoSal
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
 FROM Trn_EdsCalibra AS N  INNER JOIN Terceros AS T ON N.NitTercero=T.IdTercero
 INNER JOIN Conceptos AS CN ON N.IdConcepto=CN.IdConcepto INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario  INNER JOIN adm_Usuarios AS UC ON N.IdCajero=UC.IdUsuario
 INNER JOIN Companias AS CI ON N.IdCia=CI.IdCia 
 INNER JOIN Trn_Kardex AS K ON N.TipDoc=K.TipDoc AND N.nNota=K.Documento AND N.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN CentroCosto AS CCT ON K.CdCCosto=CCT.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero  LEFT JOIN Jornadas AS J ON N.CdJornada=J.IdJornada
 WHERE N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND CdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')  AND N.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,NitTercero,T.RazonSocial AS NomCliente,CdJornada,Jornada
,VrOtros,0,0,VrNeto,0,0,0,0,0,IdCajero,UC.Usuario AS UsuCajero,'0','',D.Referencia AS Refcia,ModdDev,Null,0
,TipCom,Comprobante,IdCiaCom,0,nNota,FecDoc,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FechaAdd,IdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario 
--detalles
,K.IdProducto AS CdProducto,DescripProd,Item,CdTanque,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom
,ImpGlobal,CdMngra,NumInicial,NumFinal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,K.Comision AS TarifCom
,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper,CdCCosto,CCosto,CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd
,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo,TarifaDct,VrDctoEnt,VrDctoSal
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
 FROM Trn_DevPas AS D INNER JOIN  Terceros AS T ON D.NitTercero=T.IdTercero  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia 
 INNER JOIN Conceptos AS CN ON D.IdConcepto=CN.IdConcepto  INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario  INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN CentroCosto AS CCT ON K.CdCCosto=CCT.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero  LEFT JOIN Jornadas AS J ON D.CdJornada=J.IdJornada
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND CdJornada LIKE ISNULL(@pmIdJornada,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')   AND D.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 ORDER BY  C.IdCia,Planilla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortesRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdJornada VARCHAR(4)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Planilla,C.IdCia AS CdCia,Compania,Fecha,C.IdConcepto AS CodConcepto,Concepto,NitTercero,T.RazonSocial AS NomTercero,C.IdJornada AS CdJornada,Jornada
,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros,VrPagosOtr,Cantidad,VrConTotal,VrConImp
,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant
,IdCajero,UC.Usuario AS NomCajero,C.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitRef,TR.RazonSocial AS NomTerceroRef,C.Referencia AS DocReferencia,Modalidad
,FecCorte,NumCorte,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
 FROM Trn_EdsCortes  AS C INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON C.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON C.NitRef=TR.IdTercero
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND C.IdJornada LIKE ISNULL(@pmIdJornada,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY C.IdCia,Planilla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortesUlt @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Planilla,IdCia,Fecha,IdConcepto
 FROM Trn_EdsCortes
 WHERE TipDoc=@pmTipDoc AND Planilla>@pmPlanilla AND IdCia=@pmIdCia AND Anulado=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsCortes_Cr @pmTipDoc VARCHAR(3),@pmPlanillaIni INT,@pmPlanillaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,Planilla,C.IdCia AS CdCia,Compania,C.Fecha AS FechaDoc,C.IdConcepto AS CodConcepto,Concepto
,NitTercero,T.RazonSocial AS NomTercero,C.IdJornada AS CdJornada,Jornada,VrSubTotal,VrSobretasa,VrImpGlobal,VrOtros,VrSobrantes,VrFaltantes,VrCostos,VrNeto,VrEfectivo,VrCreditos,VrGastComb,VrGastOtros
,VrPagosOtr,Cantidad,VrConTotal,VrConImp,VrConDcto,VrConCos,ConCant,VrCreTotal,VrCreImp,VrCreDcto,VrCreCos,CreCant
,IdCajero,UC.Usuario AS NomCajero,C.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitRef,TR.RazonSocial AS NomTerceroRef,C.Referencia AS DocReferencia,Modalidad
,FecCorte,NumCorte,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,C.TimeSys AS Fec_Add,C.FecUpdate AS Fec_Update,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CCosto,CdSubCos,SubCosto,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd
,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,K.IdVend AS KarNitVend,KV.RazonSocial AS KarVendedor,Comision,CdOperario,KO.RazonSocial AS NomOperario,ComisnOper,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto
,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,CdMngra,NumInicial,NumFinal
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_EdsCortes  AS C  INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.Planilla=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN  Conceptos AS CNC ON C.IdConcepto=CNC.IdConcepto INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario INNER JOIN  Terceros AS VN ON C.IdVend=VN.IdTercero
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed
 LEFT JOIN Terceros AS T ON C.NitTercero=T.IdTercero LEFT JOIN Terceros AS TR ON C.NitRef=TR.IdTercero
 LEFT JOIN TiposCom AS TCM ON C.TipCom=TCM.IdCom
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN Terceros AS KV ON K.IdVend=KV.IdTercero  LEFT JOIN Terceros AS KO ON K.CdOperario=KO.IdTercero
 WHERE C.TipDoc=@pmTipDoc AND Planilla BETWEEN @pmPlanillaIni AND @pmPlanillaFin  AND C.IdCia=@pmIdCia
 ORDER BY Planilla,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsForItems @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmItem INT
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,Servicios,Descripcion,ListaPrec
 FROM Trn_EdsForItems
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie
 AND NumForma=@pmNumForma 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER  BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormas @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Planilla,IdCia,Fecha,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo,IdCajero
,IdVend,CdProducto,Cantidad,IdForma,Item,Referencia,CantPuntos,Observacion,TipDcm,Documento,IdCiaDcm,FechaDcm
,AgencDcm,Anulado,NumDev,FecDev,CdCCosto,CdSubCos
 FROM Trn_EdsFormas
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasFac @pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 SELECT COUNT(NumForma) AS SCANT
 FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla AND IdCia=@pmIdCia AND Documento>0 AND TipDcm<>'PLA' AND TipDcm<>'0'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasLta @pmTipoRango VARCHAR(3)=Null,@pmIdRangoIni INT=Null,@pmIdRangoFin INT=Null,@pmNumSerie VARCHAR(5)=Null
,@pmPlanillaIni INT=Null,@pmPlanillaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Planilla,F.IdCia AS CdCia,Compania,Item,Fecha,VrTotal,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia
,F.IdCuenta AS  CdCuenta,NomCuenta,pVehiculo,nVehiculo,IdCajero,Usuario,F.IdVend AS CdVend,V.RazonSocial AS Vendedor,CdProducto,DescripProd,Cantidad,F.IdForma AS CdForma,FormaPago
,F.Referencia AS Referncia,CantPuntos,F.Observacion AS Observ,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm,Anulado,NumDev,FecDev,CdCCosto,CdSubCos
 FROM Trn_EdsFormas AS F INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia
 INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 INNER JOIN adm_Usuarios AS U ON F.IdCajero=U.IdUsuario
 INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
 INNER JOIN Companias AS C ON F.IdCia=C.IdCia
 LEFT JOIN ProdMcias AS R ON F.CdProducto=R.IdProducto  
 WHERE  TipoRango LIKE ISNULL(@pmTipoRango,'%') AND  NumSerie LIKE ISNULL(@pmNumSerie,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND F.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND  IdRango BETWEEN ISNULL(@pmIdRangoIni,0)  AND  ISNULL(@pmIdRangoFin,2147483647)
 AND  Planilla BETWEEN ISNULL(@pmPlanillaIni,0)  AND  ISNULL(@pmPlanillaFin,2147483647)
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND  Documento BETWEEN ISNULL(@pmDocumentoIni,0)  AND  ISNULL(@pmDocumentoFin,2147483647)
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY TipoRango,IdRango,NumSerie,NumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasPla @pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Planilla,IdCia,Fecha,VrTotal,IdCliente,IdAgencia,IdCuenta,pVehiculo,nVehiculo,IdCajero
,IdVend,CdProducto,Cantidad,IdForma,Item,Referencia,CantPuntos,Observacion,TipDcm,Documento,IdCiaDcm
,FechaDcm,AgencDcm,Anulado,NumDev,FecDev,CdCCosto,CdSubCos
 FROM Trn_EdsFormas
 WHERE Planilla=@pmPlanilla  AND IdCia=@pmIdCia
 ORDER BY TipoRango,IdRango,NumSerie,NumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasRan @pmTipoRango VARCHAR(3),@pmIdRango INT
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Planilla,IdCia,Fecha
 FROM Trn_EdsFormas
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasRel @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipoRango VARCHAR(3)=Null
,@pmNumSerie VARCHAR(5)=Null,@pmPlanillaIni INT=Null,@pmPlanillaFin INT=Null,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmAnulado BIT=Null
,@pmFechaDocIni SMALLDATETIME=Null,@pmFechaDocFin SMALLDATETIME=Null
 AS 
 SELECT TipoRango,NumSerie,NumForma,F.Planilla AS NumPlanilla,F.IdCia AS CdCia,Compania,F.Fecha AS FechaVale,VrTotal,IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS Id_Agencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,pVehiculo,nVehiculo,F.IdCajero AS CdCajero,Usuario,F.IdVend AS NitVend,V.RazonSocial AS Vendedor
,CdProducto,DescripProd,F.Cantidad AS Cant,F.IdCuenta AS CodCuenta,NomCuenta,F.IdForma AS CdForma,FormaPago,F.Referencia AS Referncia,F.Observacion AS Observ
,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm,AD.Agencia AS DocAgencia,F.Anulado AS EstaAnulado,F.FecDev AS FechaDev
,IdRango,Item,CP.IdJornada AS CdJornada,Jornada,NumCorte,FecCorte
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
,CdCCosto,F.CdSubCos AS CodSubCos
 FROM Trn_EdsFormas AS F INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia  INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 INNER JOIN adm_Usuarios AS U ON F.IdCajero=U.IdUsuario  INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma  INNER JOIN Companias AS C ON F.IdCia=C.IdCia
 INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN ProdMcias AS R ON F.CdProducto=R.IdProducto   LEFT JOIN Agencias AS AD ON F.AgencDcm=AD.IdAgencia
 LEFT JOIN Trn_EdsCortes AS CP ON F.Planilla=CP.Planilla AND F.IdCia=CP.IdCia  LEFT JOIN Jornadas AS J ON CP.IdJornada=J.IdJornada
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipoRango LIKE ISNULL(@pmTipoRango,'%') AND  NumSerie LIKE ISNULL(@pmNumSerie,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND F.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND  F.Planilla BETWEEN ISNULL(@pmPlanillaIni,-1)  AND  ISNULL(@pmPlanillaFin,2147483647)
 AND  Documento BETWEEN ISNULL(@pmDocumentoIni,-1)  AND  ISNULL(@pmDocumentoFin,2147483647)
 AND (F.Anulado=ISNULL(@pmAnulado,0) or F.Anulado=ISNULL(@pmAnulado,1))
 AND (ISNULL(FechaDcm,F.Fecha)>=ISNULL(@pmFechaDocIni,CAST('19100101' AS SMALLDATETIME)) AND ISNULL(FechaDcm,F.Fecha)<=ISNULL(@pmFechaDocFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial,F.Fecha,TipoRango,NumSerie,NumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasRelDet @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipoRango VARCHAR(3)=Null
,@pmNumSerie VARCHAR(5)=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmAnulado BIT=Null,@pmFechaDocIni SMALLDATETIME=Null,@pmFechaDocFin SMALLDATETIME=Null
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT F.TipoRango AS TipRango,F.NumSerie AS Num_Serie,F.NumForma AS NumVale,F.IdCia AS CdCia,Compania,F.Fecha AS FechaVale
--detalle del vale
,FD.Item AS NumItem,FD.IdProducto AS CodProducto,DescripProd,FD.IdBodega AS CdBodega,Bodega,FD.Cantidad AS Cant,VrPrecio,FD.Cantidad*VrPrecio AS ValorTotal,TarifaDct,VrDcto,TarifaIva,VrIva
,Servicios,Descripcion,ListaPrec,VrTotal AS ValorVale,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS Id_Agencia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia
,pVehiculo,nVehiculo,F.IdCajero AS CdCajero,Usuario,F.IdVend AS NitVend,V.RazonSocial AS Vendedor
,F.IdCuenta AS CodCuenta,NomCuenta,F.IdForma AS CdForma,FormaPago,F.Referencia AS Referncia,F.Observacion AS Observ
,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm,AD.Agencia AS DocAgencia,F.Anulado AS EstaAnulado,F.FecDev AS FechaDev,F.IdRango AS NumRango,Planilla
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
,CdCCosto,F.CdSubCos AS CodSubCos
 FROM Trn_EdsFormas AS F INNER JOIN Trn_EdsForItems AS FD ON F.TipoRango=FD.TipoRango AND F.IdRango=FD.IdRango
 AND F.NumSerie=FD.NumSerie AND F.NumForma=FD.NumForma INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia  INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 INNER JOIN adm_Usuarios AS U ON F.IdCajero=U.IdUsuario  INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma  INNER JOIN Companias AS C ON F.IdCia=C.IdCia
 INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN ProdMcias AS R ON FD.IdProducto=R.IdProducto INNER JOIN  Bodegas AS B ON FD.IdBodega=B.IdBodega
 LEFT JOIN Agencias AS AD ON F.AgencDcm=AD.IdAgencia
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.TipoRango LIKE ISNULL(@pmTipoRango,'%') AND  F.NumSerie LIKE ISNULL(@pmNumSerie,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND F.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND  Documento BETWEEN ISNULL(@pmDocumentoIni,-1)  AND  ISNULL(@pmDocumentoFin,2147483647)
 AND (F.Anulado=ISNULL(@pmAnulado,0) or F.Anulado=ISNULL(@pmAnulado,1))
 AND (ISNULL(FechaDcm,F.Fecha)>=ISNULL(@pmFechaDocIni,CAST('19100101' AS SMALLDATETIME)) AND ISNULL(FechaDcm,F.Fecha)<=ISNULL(@pmFechaDocFin,CAST('20781230' AS SMALLDATETIME)))
 AND FD.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY T.RazonSocial,F.Fecha,F.TipoRango,F.NumSerie,F.NumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasTot @pmPlanilla INT,@pmIdCia CHAR(2)
 AS 
 SELECT SUM(VrTotal) AS STOT,SUM(Cantidad) AS SCANT
 FROM Trn_EdsFormas
 WHERE  Planilla=@pmPlanilla  AND  IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormasTotCaj  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCajero VARCHAR(11)=Null
 AS 
 SELECT SUM(VrTotal) AS STOTVAL
 FROM Trn_EdsFormas
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsFormas_Cr  @pmTipoRango VARCHAR(3),@pmIdRango INT
,@pmNumSerie VARCHAR(5),@pmNumFormaIni INT,@pmNumFormaFin INT
 AS 
 SELECT F.TipoRango AS TipRango,F.IdRango AS NumRango,F.NumSerie AS NroSerie,F.NumForma AS NumVale
,Fecha,VrTotal,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,F.IdCuenta AS  CdCuenta,NomCuenta
,pVehiculo,nVehiculo,IdCajero,Usuario,F.IdVend AS CdVend,V.RazonSocial AS Vendedor
,F.Referencia AS Referncia,F.Observacion AS Observ,CantPuntos,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm
,Anulado,NumDev,FecDev,Planilla,F.IdCia AS CdCia,Compania
--detalles
,D.Item AS nItem,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,Bodega,D.Cantidad AS Cant,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto
,Servicios,Descripcion,ListaPrec,PM.IdUnd AS CdUnid,Unidad,CdCCosto,CdSubCos
 FROM Trn_EdsFormas AS F INNER JOIN Trn_EdsForItems AS D ON F.TipoRango=D.TipoRango
 AND F.IdRango=D.IdRango AND F.NumSerie=D.NumSerie AND F.NumForma=D.NumForma
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON F.IdAgencia=A.IdAgencia
 INNER JOIN Puc AS P ON F.IdCuenta=P.IdCuenta
 INNER JOIN adm_Usuarios AS U ON F.IdCajero=U.IdUsuario
 INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN ProdMcias AS PM ON D.IdProducto=PM.IdProducto 
 INNER JOIN Bodegas AS B ON D.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 LEFT JOIN Companias AS C ON F.IdCia=C.IdCia
 WHERE F.TipoRango=@pmTipoRango AND F.IdRango=@pmIdRango
 AND F.NumSerie=@pmNumSerie  AND F.NumForma BETWEEN @pmNumFormaIni AND @pmNumFormaFin
 ORDER  BY D.Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsRangos @pmTipoRango VARCHAR(3),@pmIdRango INT
 AS 
 SELECT TipoRango,IdRango,IdForma,Fecha,FecVence,IdCliente,IdAgencia,pVehiculo,nVehiculo,IdCuenta,NumSerie,NumInicial,NumFinal
,NumTarjeta,VrCupo,VrConsumos,TipoConsumo,ReqPlaca,ReqProd,ReqGalones,VldAgencia,VldPlaca
,Modalidad,Anulado,Observacion,IdEstado,NumAuto,NumActual,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsRangosFor @pmTipoRango VARCHAR(3),@pmNumSerie VARCHAR(5)
,@pmNumForma INT
 AS 
 SELECT TipoRango,IdRango,IdForma,Fecha,FecVence,IdCliente,IdAgencia,pVehiculo,nVehiculo,IdCuenta,NumSerie,NumInicial,NumFinal
,NumTarjeta,VrCupo,VrConsumos,TipoConsumo,ReqPlaca,ReqProd,ReqGalones,VldAgencia,VldPlaca
,Modalidad,Anulado,Observacion,IdEstado,NumAuto,NumActual,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND NumSerie=@pmNumSerie
 AND @pmNumForma BETWEEN NumInicial AND NumFinal
 ORDER BY IdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsRangosId @pmTipoRango VARCHAR(3)
 AS 
 SELECT MAX(IdRango) AS MXITM 
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsRangosLta @pmTipoRango VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmRangoIni INT=Null,@pmRangoFin INT=Null,@pmIdCliente VARCHAR(16)=Null,@pmNumSerie VARCHAR(5)=Null,@pmAnulado BIT=Null
,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT TipoRango,IdRango,R.IdForma AS CdForma,FormaPago,Fecha,FecVence,IdCliente,RazonSocial,R.IdAgencia AS Id_Agencia,A.Agencia AS NomAgencia,pVehiculo,nVehiculo
,R.IdCuenta AS CdCuenta,NomCuenta,NumSerie,NumInicial,NumFinal,NumTarjeta,VrCupo,VrConsumos,TipoConsumo,ReqPlaca,ReqProd,ReqGalones,VldAgencia,VldPlaca
,Modalidad,R.Observacion AS Observ,Anulado,R.IdEstado AS IdEstad,Estado,NumAuto,NumActual,R.IdUsuario AS IdUsuari,Usuario,TimeSys,FecUpdate,IdCiaCrea
 FROM Trn_EdsRangos  AS R INNER JOIN  Formaspago AS F ON R.IdForma=F.IdForma
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero  INNER JOIN Puc AS P ON R.IdCuenta=P.IdCuenta
 INNER JOIN EstadoDoc AS E ON R.IdEstado=E.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 LEFT JOIN  Agencias AS A ON R.IdAgencia=A.IdAgencia
 WHERE TipoRango LIKE ISNULL(@pmTipoRango,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND NumSerie  LIKE ISNULL(@pmNumSerie ,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (IdRango>=ISNULL(@pmRangoIni,0)  AND IdRango<=ISNULL(@pmRangoFin,2147483647))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY TipoRango,IdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsRangosSer @pmTipoRango VARCHAR(3),@pmNumSerie VARCHAR(5)
,@pmNumInicial INT,@pmNumFinal INT,@pmIdRango INT
 AS 
 SELECT TipoRango,IdRango,IdForma,Fecha,IdCliente,NumSerie,NumInicial,NumFinal,NumAuto,NumActual
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND Anulado=0 AND NumSerie=@pmNumSerie
 AND NumInicial<=@pmNumFinal AND NumFinal>=@pmNumInicial AND IdRango<>@pmIdRango
 ORDER BY IdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEdsRangosTip @pmTipoRango VARCHAR(3)
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumInicial,NumFinal,NumAuto,NumActual,VrCupo,Modalidad
 FROM Trn_EdsRangos
 WHERE TipoRango=@pmTipoRango AND Anulado=0
 ORDER BY IdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryEdsServicio @pmIdEstacion VARCHAR(4)
 AS 
 IF @pmIdEstacion IS NULL
  SELECT IdEstacion,EstServicio,Direccion
  FROM EdsServicio
  WHERE Inactivo=0
 ORDER BY EstServicio
 ELSE
  SELECT IdEstacion,EstServicio,Direccion,Inactivo
  FROM EdsServicio
  WHERE IdEstacion=@pmIdEstacion


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFactura @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,IdCuenta,VrAbono
,EsEgreso,ItemCom,pVehiculo,VehPropio,TipDev,NumDev,DetAbono
 FROM Trn_EgrFactura
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFacturaAbo @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15) ,@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmIdProveedor VARCHAR(16)
 AS 
 SELECT  SUM(VrAbono) AS StotAbo
 FROM Trn_EgrFactura
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura AND  IdCiaFac=@pmIdCiaFac  AND IdProveedor=@pmIdProveedor 
 AND (ItemFac>=ISNULL(@pmItemFac,0)  AND ItemFac<=ISNULL(@pmItemFac,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFacturaApl @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipFac, Factura, IdCiaFac, ItemFac,IdProveedor,SUM(VrAbono) AS STOTABO
 FROM Trn_EgrFactura
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 GROUP BY TipFac, Factura, IdCiaFac, ItemFac,IdProveedor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFacturaDap @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmFactura VARCHAR(15)=Null,@pmIdProveedor VARCHAR(16)=Null
 AS 
 SELECT TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,SUM(VrAbono) AS STOTABO
 FROM Trn_EgrFactura
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 AND TipDev=@pmTipCom AND NumDev=@pmComprobante AND EsEgreso=0
 AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND Factura LIKE ISNULL(@pmFactura,'%')
 GROUP BY TipFac,Factura,IdCiaFac,ItemFac,IdProveedor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFacturaDev @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmFactura VARCHAR(15)=Null,@pmIdProveedor VARCHAR(16)=Null
 AS 
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,IdCuenta,VrAbono
,EsEgreso,ItemCom,pVehiculo,VehPropio,TipDev,NumDev,DetAbono
 FROM Trn_EgrFactura
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 AND TipDev=@pmTipCom AND NumDev=@pmComprobante AND EsEgreso=0
 AND IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND Factura LIKE ISNULL(@pmFactura,'%')
ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFacturaDoc @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15) ,@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmIdProveedor VARCHAR(16)
 AS 
 SELECT TipCom,Comprobante,IdCia,Item,Fecha,TipFac,Factura,IdCiaFac,ItemFac,IdProveedor,IdCuenta,VrAbono,EsEgreso,ItemCom,pVehiculo,VehPropio,TipDev,NumDev,DetAbono
 FROM Trn_EgrFactura
 WHERE  TipFac=@pmTipFac AND Factura=@pmFactura AND  IdCiaFac=@pmIdCiaFac AND IdProveedor=@pmIdProveedor
 AND (ItemFac>=ISNULL(@pmItemFac,0)  AND ItemFac<=ISNULL(@pmItemFac,2147483647)) 
 ORDER BY  TipCom, Comprobante, IdCia, Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFacturaTnit  @pmIdProveedor VARCHAR(16)
 AS 
 SELECT SUM(VrAbono) AS STOTABO
 FROM Trn_EgrFactura
 WHERE IdProveedor=@pmIdProveedor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFactura_Crc @pmTipCom VARCHAR(3),@pmComprobanteIni INT,@pmComprobanteFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT E.TipCom AS CodTipoCom,TipoCom,E.Comprobante AS NumComp,E.IdCia AS IdCiaComp,CI.Compania AS CiaComp,E.Item AS nItem
,Fecha,E.TipFac AS TipoFact,TipoDoc,E.Factura AS NumFactura,IdCiaFac,CF.Compania AS FactCompania
,ItemFac,E.IdProveedor AS NitProveedor,RazonSocial,E.IdCuenta AS CdCuenta,NomCuenta,VrAbono,E.EsEgreso AS Es_Egreso,E.ItemCom AS Item_Comp
,E.pVehiculo AS PlacaVeh,E.VehPropio AS Veh_Espropio,TipDev,NumDev,DetAbono
--datos del vencimiento
,TipDoc,Documento,IdCiaDoc,FecEmision,FecVence,VrFactura,C.VrAbonado AS TotalAbonos,C.TipCom AS VencTipCom,C.Comprobante AS VencComp
,C.ItemCom AS VencItemComp,Referencia,Detalle,TipRef,DocRef,IdCiaRef
 FROM Trn_EgrFactura AS E INNER JOIN Trn_ComFactura AS C ON E.TipFac=C.TipFac AND  E.Factura=C.Factura
  AND  E.IdCiaFac=C.IdCia AND  E.ItemFac=C.Item AND  E.IdProveedor=C.IdProveedor
 INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero  INNER JOIN Puc AS P ON E.IdCuenta=P.IdCuenta
 INNER JOIN  Companias AS CI ON E.IdCia=CI.IdCia INNER JOIN Companias AS CF ON E.IdCiaFac=CF.IdCia
 LEFT JOIN Sys_TiposDoc AS TD ON E.TipFac=TD.IdDoc LEFT JOIN  TiposCom AS TC ON E.TipCom=TC.IdCom
 WHERE E.TipCom=@pmTipCom  AND E.Comprobante BETWEEN @pmComprobanteIni AND @pmComprobanteFin  AND E.IdCia=@pmIdCia
 ORDER BY  E.Comprobante,E.Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEgrFactura_Fac @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2)
,@pmIdProveedor VARCHAR(16),@pmItemFac INT=Null
 AS 
 SELECT  TipCom,TipoCom,Comprobante,E.IdCia AS IdCiaComp,Compania,Item,Fecha,VrAbono,DetAbono,ItemFac
,E.IdCuenta AS IdCuent,NomCuenta,E.EsEgreso AS Es_Egreso,TipDev,NumDev,IdProveedor,RazonSocial,E.ItemCom AS Item_Comp,pVehiculo
 FROM Trn_EgrFactura AS E INNER JOIN Terceros AS T ON E.IdProveedor=T.IdTercero 
 INNER JOIN Puc AS P ON E.IdCuenta=P.IdCuenta  INNER JOIN  Companias AS CI ON E.IdCia=CI.IdCia
 LEFT JOIN  TiposCom AS TC ON E.TipCom=TC.IdCom
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura  AND IdCiaFac=@pmIdCiaFac AND IdProveedor=@pmIdProveedor
 AND (ItemFac>=ISNULL(@pmItemFac,0)  AND ItemFac<=ISNULL(@pmItemFac,2147483647))
 ORDER BY Fecha,Comprobante,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_CentrosLta @pmNContrato INT
 AS 
 SELECT EC.NContrato AS NumContrato,C.IdEmpleado AS IdEmplado,Apellidos,Nombres,FecIngreso,FecRetiro,Indefinido
,EC.IdCCosto AS CdCCosto,CCosto,Tarifa,C.Inactivo AS ConInactivo
 FROM Emp_Centros AS EC INNER JOIN Emp_Contrato AS C ON EC.NContrato=C.NContrato
 INNER JOIN Empleados AS E ON C.IdEmpleado=E.IdEmpleado
 INNER JOIN CentroCosto AS CO ON EC.IdCCosto=CO.IdCCosto
 WHERE  (EC.NContrato>=ISNULL(@pmNContrato,0)  AND EC.NContrato<=ISNULL(@pmNContrato,2147483647)) 
 ORDER BY EC.NContrato,EC.IdCCosto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_Contrato @pmNContrato INT
 AS 
 SELECT NContrato,IdEmpleado,IdTipCon,FecIngreso,FecVigencia,FecRetiro,Indefinido,IdCargo,IdJornada,IdDep,IdArea,IdInstala,IdCCosto,IdSubCos,IdCia,IdNom
,IdClase,TipoTrabj,IdCot,IdGrupo,VrUpc,TipoLiquida,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP
,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,IdEstado,Inactivo,SubTipoCot,DsoSabado,FecAdd,FecUpdate,IdUsuario
 FROM Emp_Contrato
 WHERE NContrato=@pmNContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoAct @pmIdEmpleado VARCHAR(16),@pmInactivo BIT=Null
 AS 
 SELECT NContrato,IdEmpleado,IdTipCon,FecIngreso,FecVigencia,FecRetiro,Indefinido,TipoLiquida,VrSalario,VrAuxTrans,IdEstado,Inactivo,Notas
 FROM Emp_Contrato
 WHERE IdEmpleado=@pmIdEmpleado
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY FecIngreso,NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoAnt @pmIdEmpleado VARCHAR(16),@pmNContratoExc INT
 AS 
 SELECT NContrato,IdEmpleado,IdTipCon,FecIngreso,FecVigencia,FecRetiro,Indefinido,IdCargo,IdJornada,IdDep,IdArea,IdInstala,IdCCosto,IdSubCos,IdCia,IdNom
,TipoTrabj,IdCot,IdGrupo,VrUpc,TipoLiquida,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP
,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,IdEstado,Inactivo,SubTipoCot,DsoSabado,FecAdd,FecUpdate,IdUsuario
 FROM Emp_Contrato
 WHERE IdEmpleado=@pmIdEmpleado AND NContrato<>@pmNContratoExc
 ORDER BY FecIngreso,NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoDso @pmIdEmpleado VARCHAR(16)=Null,@pmIndefinido BIT=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdArea VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdSubCos VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmTipoLiquida CHAR(1)=Null
,@pmSalMinimo BIT=Null,@pmSalIntegral BIT=Null,@pmFPnomina VARCHAR(10)=Null,@pmModoRet INT=Null,@pmIdClase VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmIdEstado VARCHAR(4)=Null
,@pmNContrato INT=Null,@pmFecIngresoIni SMALLDATETIME=Null,@pmFecIngresoFin SMALLDATETIME=Null,@pmATFinMes BIT=Null,@pmSegFinMes BIT=Null,@pmDchDotacion BIT=Null
--parametros de empleados
,@pmTipoSangre CHAR(2)=Null,@pmFactorRh CHAR(1)=Null,@pmSexo CHAR(1)=Null,@pmIdEstCivil VARCHAR(4)=Null,@pmClaseLib INT=Null, @pmIdProf VARCHAR(4)=Null,@pmTallaCam CHAR(2)=Null
,@pmTallaPan CHAR(2)=Null,@pmTallaZap CHAR(2)=Null,@pmIdFonPen VARCHAR(8)=Null,@pmIdFonEps VARCHAR(8)=Null,@pmIdFonArp VARCHAR(8)=Null,@pmIdFonCes VARCHAR(8)=Null,@pmIdCajaCom VARCHAR(8)=Null
,@pmIdBanco VARCHAR(4)=Null,@pmEmp_IdEstado VARCHAR(4)=Null,@pmEmp_Inactivo BIT=Null,@pmEsCondtor BIT=Null,@pmFecNacmtoIni SMALLDATETIME=Null,@pmFecNacmtoFin SMALLDATETIME=Null
 AS 

 SELECT  C.IdEmpleado AS IdEmplado,Apellidos,Nombres,C.NContrato AS NCntrato,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo
,C.IdJornada AS IdJrnada,Jornada,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS IdInstla,Instlacion,C.IdCCosto AS IdCCto,CCosto,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania
,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,TipoTrabj,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipoLiq,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP
,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,DsoSabado,C.IdEstado AS IdEstdCon,EC.Estado AS EstdoCon,EC.NColor AS ConEstcolor,C.Inactivo AS Inactvo,C.FecAdd AS Fec_Add,C.FecUpdate AS Fec_Update
,C.IdUsuario AS IdUsuari,U.Usuario AS Con_Usuario,IdCot,C.IdGrupo AS GrupAR,VrUpc
--empleados
,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,EstCivil,NHijos,TallaCam,TallaPan,TallaZap,UsaGafas,E.IdProf AS CodProf,Profesion
,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,IdLugarCed,LC.Localidad AS LugarCed,Conyugue,ConyFecNac,ConyTrabaja
,ConyEmpresa,ConyCargo,ConyFing,ConyTel,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes
,IdCajaCom,CC.Fondo AS CajaComp,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,FotoEmp,FotoLic,FotoFirma,Observacion,E.IdEstado AS Emp_IdEstado
,ET.Estado AS Emp_estado,E.Inactivo AS Emp_Inactivo,TipoId,Codigo,ET.NColor AS EmpEstcolor,E.FechaAdd AS EmpFecAdd,E.FechaUpdate AS EmpFecupd,E.IdUsuario AS Emp_IdUsuario,UE.Usuario AS Emp_Usuario,SubTipoCot
 FROM Emp_Contrato AS C INNER JOIN Empleados AS E ON C.IdEmpleado=E.IdEmpleado INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado 
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN EstadoCiv AS ECV ON E.IdEstCivil=ECV.IdEstado INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado INNER JOIN adm_Usuarios AS UE ON E.IdUsuario=UE.IdUsuario
 WHERE C.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND C.IdCargo LIKE ISNULL(@pmIdCargo,'%')
 AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND C.IdSubCos LIKE ISNULL(@pmIdSubCos,'%')  AND C.IdNom LIKE ISNULL(@pmIdNom,'%') 
 AND C.TipoLiquida LIKE ISNULL(@pmTipoLiquida,'%') AND FPnomina LIKE ISNULL(@pmFPnomina,'%')  AND C.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (C.NContrato>=ISNULL(@pmNContrato,0)  AND C.NContrato<=ISNULL(@pmNContrato,2147483647)) 
 AND (ModoRet>=ISNULL(@pmModoRet,0)  AND ModoRet<=ISNULL(@pmModoRet,2147483647)) 
 AND (SalMinimo=ISNULL(@pmSalMinimo,0) or SalMinimo=ISNULL(@pmSalMinimo,1))
 AND (Indefinido=ISNULL(@pmIndefinido,0) or Indefinido=ISNULL(@pmIndefinido,1))
 AND (SalIntegral=ISNULL(@pmSalIntegral,0) or SalIntegral=ISNULL(@pmSalIntegral,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (ATFinMes=ISNULL(@pmATFinMes,0) or ATFinMes=ISNULL(@pmATFinMes,1))
 AND (SegFinMes=ISNULL(@pmSegFinMes,0) or SegFinMes=ISNULL(@pmSegFinMes,1))
 AND (DchDotacion=ISNULL(@pmDchDotacion,0) or DchDotacion=ISNULL(@pmDchDotacion,1))
 AND (FecIngreso>=ISNULL(@pmFecIngresoIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngresoFin,CAST('20781230' AS SMALLDATETIME)))
 AND TipoSangre LIKE ISNULL(@pmTipoSangre,'%%') AND FactorRh LIKE ISNULL(@pmFactorRh,'%') AND Sexo LIKE ISNULL(@pmSexo,'%')
 AND IdEstCivil LIKE ISNULL(@pmIdEstCivil,'%') AND E.IdProf LIKE ISNULL(@pmIdProf,'%') AND TallaCam LIKE ISNULL(@pmTallaCam,'%%')
 AND TallaPan LIKE ISNULL(@pmTallaPan,'%%') AND TallaZap LIKE ISNULL(@pmTallaZap,'%%') AND IdFonPen LIKE ISNULL(@pmIdFonPen,'%')
 AND IdFonCes LIKE ISNULL(@pmIdFonCes,'%') AND IdFonArp LIKE ISNULL(@pmIdFonArp,'%') AND IdFonEps LIKE ISNULL(@pmIdFonEps,'%')
 AND IdCajaCom LIKE ISNULL(@pmIdCajaCom ,'%') AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%') AND E.IdEstado LIKE ISNULL(@pmEmp_IdEstado,'%')
 AND (ClaseLib>=ISNULL(@pmClaseLib,0)  AND ClaseLib<=ISNULL(@pmClaseLib,2147483647)) 
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 AND (EsCondtor=ISNULL(@pmEsCondtor,0) or EsCondtor=ISNULL(@pmEsCondtor,1))
 AND (FecNacmto>=ISNULL(@pmFecNacmtoIni,CAST('19100101' AS SMALLDATETIME)) AND FecNacmto<=ISNULL(@pmFecNacmtoFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,C.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoEca @pmIdCargo VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmIdProf VARCHAR(4)=Null,@pmSexo CHAR(1)=Null,@pmEsCondtor BIT=Null,@pmFecNacmtoIni SMALLDATETIME=Null,@pmFecNacmtoFin SMALLDATETIME=Null
AS 
 SELECT C.IdEmpleado AS IdEmplado,Apellidos,Nombres,C.NContrato AS NCntrato,TipoId,Codigo,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo
,IdEstCivil,EstCivil,NHijos,TallaCam,TallaPan,TallaZap,UsaGafas,E.IdProf AS CodProf,Profesion,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia
,IdLugarCed,LC.Localidad AS LugarCed,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps
,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes,IdCajaCom,CC.Fondo AS CajaComp,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo
,FotoEmp,FotoLic,FotoFirma,DsoSabado,Observacion,E.IdEstado AS Emp_IdEstado,ET.Estado AS Emp_estado,E.Inactivo AS Emp_Inactivo,ET.NColor AS EmpEstcolor,E.IdUsuario AS Emp_IdUsuario,UE.Usuario AS Emp_Usuario
--datos del contrato
,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdJornada AS IdJrnada,Jornada,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS IdInstla,Instlacion
,C.IdCCosto AS IdCCto,CCosto,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,TipoTrabj,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipoLiq,VrSalario,VrAuxTrans,SalMinimo,SalIntegral
,DchDotacion,DchCafeteria,Reclmto,Notas,C.IdEstado AS IdEstdCon,EC.Estado AS EstdoCon,EC.NColor AS ConEstcolor,C.Inactivo AS Inactvo,C.IdUsuario AS IdUsuari,U.Usuario AS Con_Usuario,SubTipoCot
 FROM Emp_Contrato AS C INNER JOIN Empleados AS E ON C.IdEmpleado=E.IdEmpleado INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado 
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN EstadoCiv AS ECV ON E.IdEstCivil=ECV.IdEstado INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado INNER JOIN adm_Usuarios AS UE ON E.IdUsuario=UE.IdUsuario
 WHERE   C.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%')  AND C.IdNom LIKE ISNULL(@pmIdNom,'%')  AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND Sexo LIKE ISNULL(@pmSexo,'%') AND E.IdProf LIKE ISNULL(@pmIdProf,'%')
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (EsCondtor=ISNULL(@pmEsCondtor,0) or EsCondtor=ISNULL(@pmEsCondtor,1))
 AND (FecNacmto>=ISNULL(@pmFecNacmtoIni,CAST('19100101' AS SMALLDATETIME)) AND FecNacmto<=ISNULL(@pmFecNacmtoFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,C.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoId
 AS 
 SELECT MAX(NContrato) AS MXNCON
 FROM Emp_Contrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoLab @pmNContrato INT,@pmInactivo BIT=Null
 AS 
 SELECT  C.IdEmpleado AS IdEmplado,Apellidos,Nombres,C.NContrato AS NCntrato,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo
,C.IdJornada AS IdJrnada,Jornada,Rangos,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdClase AS CodCla,ClaseRiesgo,Tarifa,TipoTrabj,C.IdInstala AS IdInstla,Instlacion,C.IdCCosto AS IdCCto,CCosto,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdNom AS CodNom
,TipoNomina,Asistencia,NoDevenga,C.TipoLiquida AS TipoLiq,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac
,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,DsoSabado,C.IdEstado AS IdEstdCon,EC.Estado AS EstdoCon,C.Inactivo AS Inactvo,IdCot,C.IdGrupo AS GrupAR,VrUpc,SubTipoCot
--datos del empleado
,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes,IdCajaCom,CC.Fondo AS CajaComp
,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecVincula,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo
,E.IdProf AS CodProf,Profesion,Observacion,E.IdEstado AS Emp_IdEstdo,ET.Estado AS Emp_Estdo,E.Inactivo AS Emp_Inactivo
,C.FecAdd AS Fec_Add,C.FecUpdate AS Fec_Update,C.IdUsuario AS IdUsuari,Usuario
 FROM Emp_Contrato AS C INNER JOIN Empleados AS E ON C.IdEmpleado=E.IdEmpleado INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado
 WHERE  C.NContrato=@pmNContrato  AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoNom @pmNContrato INT
 AS 
 SELECT  IdEmpleado,NContrato,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo
,C.IdJornada AS IdJrnada,Jornada,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS IdInstla,Instlacion,C.IdClase AS CodClase,ClaseRiesgo,TipoTrabj,C.IdCCosto AS IdCCto,CCosto
,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdNom AS CodNom,TipoNomina,C.IdEstado AS IdEstdCon,Estado,C.Inactivo AS Inactvo,C.IdUsuario AS IdUsuari,Usuario
 FROM Emp_Contrato AS C  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE NContrato=@pmNContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ContratoVce @pmFecAct SMALLDATETIME,@pmIdTipCon VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmFecIngresoIni SMALLDATETIME=Null,@pmFecIngresoFin SMALLDATETIME=Null,@pmIndefinido BIT=Null,@pmSalIntegral BIT=Null,@pmInactivo BIT=Null
AS
 SELECT C.IdEmpleado AS IdEmplado,Apellidos,Nombres,C.NContrato AS NCntrato,C.IdTipCon AS CodTCon,TipoContrato,Duracion,FecIngreso,FecVigencia,DATEDIFF(day,ISNULL(FecVigencia,@pmFecAct),@pmFecAct) AS DiasVig
,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS IdInstla,Instlacion,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipoLiq,C.IdCCosto AS IdCCto,CCosto
,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,TipoTrabj,ModPromdio,VrSalario,VrAuxTrans,VrUpc,SalMinimo,SalIntegral,FPnomina,ModoRet
,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,C.IdEstado AS IdEstdCon
,EC.Estado AS EstdoCon,EC.NColor AS ConEstcolor,C.Inactivo AS Inactvo,FecRetiro,DsoSabado,C.IdUsuario AS IdUsuari,U.Usuario AS Con_Usuario,IdCot,C.IdGrupo AS GrupAR,C.IdJornada AS IdJrnada,Jornada
--empleados
,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,EstCivil,NHijos,TallaCam,TallaPan,TallaZap,UsaGafas,E.IdProf AS CodProf,Profesion
,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,IdLugarCed,LC.Localidad AS LugarCed,Conyugue,ConyFecNac,ConyTrabaja
,ConyEmpresa,ConyCargo,ConyFing,ConyTel,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes
,IdCajaCom,CC.Fondo AS CajaComp,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,FotoEmp,FotoLic,FotoFirma,Observacion,E.IdEstado AS Emp_IdEstado
,ET.Estado AS Emp_estado,E.Inactivo AS Emp_Inactivo,TipoId,Codigo,ET.NColor AS EmpEstcolor,E.IdUsuario AS Emp_IdUsuario,UE.Usuario AS Emp_Usuario
 FROM Emp_Contrato AS C INNER JOIN Empleados AS E ON C.IdEmpleado=E.IdEmpleado INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado 
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN EstadoCiv AS ECV ON E.IdEstCivil=ECV.IdEstado INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado INNER JOIN adm_Usuarios AS UE ON E.IdUsuario=UE.IdUsuario
 WHERE  C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND C.IdCargo LIKE ISNULL(@pmIdCargo,'%')
 AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND C.IdNom LIKE ISNULL(@pmIdNom,'%')  AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Indefinido=ISNULL(@pmIndefinido,0) or Indefinido=ISNULL(@pmIndefinido,1))
 AND (SalIntegral=ISNULL(@pmSalIntegral,0) or SalIntegral=ISNULL(@pmSalIntegral,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecIngreso>=ISNULL(@pmFecIngresoIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngresoFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,C.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_Estudios @pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 SELECT IdEmpleado,Item,Estblecmto,Titulo,Fecha
 FROM Emp_Estudios
 WHERE IdEmpleado=@pmIdEmpleado 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_EstudiosDso @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT ER.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Item,Estblecmto,Titulo,Fecha
 FROM Emp_Estudios AS ER INNER JOIN Empleados AS E ON ER.IdEmpleado=E.IdEmpleado
 WHERE ER.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY ER.IdEmpleado,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ExpLaboral @pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 SELECT IdEmpleado,Item,Empresa,Cargo,FecInicio,FecRetiro,DirEmpresa,TelEmpresa
 FROM Emp_ExpLaboral
 WHERE IdEmpleado=@pmIdEmpleado  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ExpLaboralDso @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT L.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Item,Empresa,Cargo,FecInicio,FecRetiro,DirEmpresa,TelEmpresa
 FROM Emp_ExpLaboral AS L INNER JOIN Empleados AS E ON L.IdEmpleado=E.IdEmpleado
 WHERE L.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY L.IdEmpleado,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_Parientes @pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 SELECT IdEmpleado,Item,Parntsco,Nombre,FecNacmto,UltGrado,Dirccion,Telefno
 FROM Emp_Parientes
 WHERE IdEmpleado=@pmIdEmpleado  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647)) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmp_ParientesDso @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT P.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Item,Parntsco,Nombre,P.FecNacmto AS FechaNaci,UltGrado,Dirccion,Telefno
 FROM Emp_Parientes AS P INNER JOIN Empleados AS E ON P.IdEmpleado=E.IdEmpleado
 WHERE P.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY P.IdEmpleado,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpaques @pmIdEmp VARCHAR(4)
 AS 
 IF @pmIdEmp IS NULL
 SELECT IdEmp,Empaque,CodMintra
 FROM Empaques
 WHERE Inactivo=0
 ORDER BY IdEmp
 ELSE
 SELECT IdEmp,Empaque,CodMintra,Inactivo
 FROM Empaques
 WHERE IdEmp=@pmIdEmp
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleados @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,Codigo,TipoId,Dv,IdLugarCed,Direccion,IdLocal,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte
,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdProf,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,IdFonPen,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,CdFonAvp,IdClase,NumCuenta
,IdBanco,CdLocCue,NContrato,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecVincula,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,FotoEmp,FotoLic,FotoFirma,Observacion,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosBus @pmCodigo VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,Codigo,TipoId,Dv,IdLugarCed,Direccion,IdLocal,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte
,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdProf,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,IdFonPen,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,CdFonAvp,IdClase,NumCuenta
,IdBanco,CdLocCue,NContrato,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecVincula,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,FotoEmp,FotoLic,FotoFirma,EsCondtor,pVehiculo,Observacion,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Empleados
 WHERE  Codigo=@pmCodigo
 ORDER BY IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosCna @pmIdNom VARCHAR(4),@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT E.IdEmpleado AS IdEmpledo,Apellidos,Nombres,E.NContrato AS NumContrato,IdArea,IdDep,IdInstala,IdCCosto,IdSubCos,IdNom
 FROM Empleados AS E INNER JOIN Emp_Contrato AS C ON E.NContrato=C.NContrato
 WHERE E.Inactivo=0 AND  IdNom=@pmIdNom  AND IdArea LIKE ISNULL(@pmIdArea,'%') AND IdDep LIKE ISNULL(@pmIdDep,'%')
 AND E.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER  BY E.IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosCnt @pmIdNom VARCHAR(4),@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT E.IdEmpleado AS IdEmpledo,Apellidos,Nombres,IdLugarCed,LC.Localidad AS LugarCed,E.IdProf AS CodProf,Profesion,TallaCam,TallaPan,TallaZap
,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes,IdCajaCom
,CC.Fondo AS CajaComp,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,CdLocCue,E.NContrato AS NumContrato,FecUltIng,FecUltRet,FecVincula,NCAnterior,FecIngAnt,FecRetAnt
,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,FecUltAum,FecUltCes
,FecUltPri,FecUltVac,FecUltDot,FecUltExm,FecUltCap,EsCondtor,pVehiculo,E.IdEstado AS IdEst_Emp,ET.Estado AS Estad_Emp,E.Inactivo AS Inctivo,Codigo,E.IdUsuario  AS IdUsuar
--datos del contrato
,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdJornada AS IdJrnada,Jornada,Rangos,C.IdArea AS CodArea,Area,C.IdDep AS CodDep
,Dependencia,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,C.IdInstala AS IdInstla,Instlacion,C.IdCCosto AS IdCCto,CCosto,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdNom AS CodNom,TipoNomina,Asistencia,NoDevenga
,C.TipoLiquida AS TipoLiq,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac
,DenyHex,DenyDcto,BasePrest,BaseSeg,DchDotacion,DchCafeteria,TipoTrabj,IdCot,IdGrupo,VrUpc,Reclmto,Notas,C.IdEstado AS IdEst_Con,EC.Estado AS Estad_Con,C.Inactivo AS Inactvo,CdFonAvp
 FROM Empleados AS E INNER JOIN Emp_Contrato AS C ON E.NContrato=C.NContrato
 INNER JOIN  Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado
 WHERE E.Inactivo=0 AND  C.IdNom LIKE ISNULL(@pmIdNom,'%')  AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND E.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER  BY E.IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosCod @pmIdEmpleado VARCHAR(16),@pmCodigo VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,Codigo,TipoId,Dv,IdLugarCed,Direccion,IdLocal,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte
,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdProf,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,IdFonPen,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,CdFonAvp,IdClase,NumCuenta
,IdBanco,CdLocCue,NContrato,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecVincula,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,FotoEmp,FotoLic,FotoFirma,EsCondtor,pVehiculo,Observacion,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado or Codigo=@pmCodigo
 ORDER BY IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosCon @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT E.IdEmpleado AS IdEmpledo,Apellidos,Nombres,E.NContrato AS NContrto,Indefinido,IdDep,IdCCosto,IdNom,TipoLiquida,IdInstala,C.IdClase AS IdClaRie,ClaseRiesgo,Tarifa,TipoTrabj
,ModPromdio,VrSalario,FecVincula,NCAnterior,FecIngAnt,FecRetAnt,VrAuxTrans,SalMinimo,SalIntegral,E.IdEstado AS Emp_IdEstdo,E.Inactivo AS Emp_Inactivo,C.IdEstado AS Con_IdEstdo,C.Inactivo AS Con_Inactivo
 FROM Empleados AS E INNER JOIN Emp_Contrato AS C ON E.NContrato=C.NContrato AND E.IdEmpleado=C.IdEmpleado
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 WHERE E.IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosDso @pmTipoSangre CHAR(2)=Null,@pmFactorRh CHAR(1)=Null,@pmSexo CHAR(1)=Null,@pmIdEstCivil VARCHAR(4)=Null,@pmClaseLib INT=Null, @pmIdProf VARCHAR(4)=Null,@pmTallaCam CHAR(2)=Null
,@pmTallaPan CHAR(2)=Null,@pmTallaZap CHAR(2)=Null,@pmIdFonPen VARCHAR(8)=Null,@pmIdFonEps VARCHAR(8)=Null,@pmIdFonArp VARCHAR(8)=Null,@pmIdFonCes VARCHAR(8)=Null,@pmIdCajaCom VARCHAR(8)=Null,@pmIdBanco VARCHAR(4)=Null
,@pmNContrato INT=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmEsCondtor BIT=Null,@pmFecNacmtoIni SMALLDATETIME=Null,@pmFecNacmtoFin SMALLDATETIME=Null,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT Apellidos,Nombres,TipoId,IdEmpleado,IdLugarCed,LC.Localidad AS LugarCed,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,EstCivil,NHijos,LibMilitar,ClaseLib
,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,E.IdProf AS CodProf,Profesion,TallaCam,TallaPan,TallaZap,UsaGafas,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo
,ConyFing,ConyTel,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes,IdCajaCom,CC.Fondo AS CajaComp,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,CdLocCue,NContrato,FecVincula
,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,FotoEmp,FotoLic,FotoFirma,Observacion,E.IdEstado AS IdEstdo,Estado,E.Inactivo AS Inctivo,Dv,Codigo,NColor,FechaAdd,FechaUpdate,E.IdUsuario AS IdUsuari,Usuario,CdFonAvp
 FROM Empleados AS E INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN EstadoCiv AS EC ON E.IdEstCivil=EC.IdEstado INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 WHERE IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND TipoSangre LIKE ISNULL(@pmTipoSangre,'%%') AND FactorRh LIKE ISNULL(@pmFactorRh,'%') AND Sexo LIKE ISNULL(@pmSexo,'%')
 AND IdEstCivil LIKE ISNULL(@pmIdEstCivil,'%') AND E.IdProf LIKE ISNULL(@pmIdProf,'%') AND TallaCam LIKE ISNULL(@pmTallaCam,'%%')
 AND TallaPan LIKE ISNULL(@pmTallaPan,'%%') AND TallaZap LIKE ISNULL(@pmTallaZap,'%%') AND IdFonPen LIKE ISNULL(@pmIdFonPen,'%')
 AND IdFonCes LIKE ISNULL(@pmIdFonCes,'%') AND IdFonArp LIKE ISNULL(@pmIdFonArp,'%') AND IdFonEps LIKE ISNULL(@pmIdFonEps,'%')
 AND IdCajaCom LIKE ISNULL(@pmIdCajaCom ,'%') AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%') AND E.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (ClaseLib>=ISNULL(@pmClaseLib,0)  AND ClaseLib<=ISNULL(@pmClaseLib,2147483647)) 
 AND (NContrato>=ISNULL(@pmNContrato,-1)  AND NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (E.Inactivo=ISNULL(@pmInactivo,0) or E.Inactivo=ISNULL(@pmInactivo,1))
 AND (EsCondtor=ISNULL(@pmEsCondtor,0) or EsCondtor=ISNULL(@pmEsCondtor,1))
 AND (FecNacmto>=ISNULL(@pmFecNacmtoIni,CAST('19100101' AS SMALLDATETIME)) AND FecNacmto<=ISNULL(@pmFecNacmtoFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosLab @pmIdEmpleado VARCHAR(16),@pmCodigo VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,IdLugarCed,LC.Localidad AS LugarCed,E.IdProf AS CodProf,Profesion,TallaCam,TallaPan,TallaZap
,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes,IdCajaCom
,CC.Fondo AS CajaComp,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,CdLocCue,NContrato,FecVincula
,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,FecUltAum,FecUltCes
,FecUltPri,FecUltVac,FecUltIng,FecUltRet,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,E.IdEstado AS IdEstdo,Estado,E.Inactivo AS Inctivo,Codigo,IdUsuario,CdFonAvp
 FROM Empleados AS E INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado
 WHERE IdEmpleado=@pmIdEmpleado OR Codigo=@pmCodigo
 ORDER  BY IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosReg @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,Codigo,Direccion,IdLocal,Telefono,e_mail,NContrato,FecVincula
 FROM Empleados
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosSue @pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null
,@pmIdTipCon VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmSalMinimo BIT=Null,@pmSalIntegral BIT=Null,@pmIdEmpleado VARCHAR(16),@pmInactivo BIT=Null
,@pmEmp_Inactivo BIT=Null,@pmEsCondtor BIT=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT E.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Codigo,FecNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,EstCivil,NHijos,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco
,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes,IdCajaCom,CC.Fondo AS CajaComp,FecUltAum,FecVincula,FecUltDot,NCAnterior,FecIngAnt
,FecRetAnt,EsCondtor,pVehiculo,E.IdEstado AS Emp_IdEstdo,ET.Estado AS Emp_Estado,E.Inactivo AS Emp_Inactvo
--datos del contrato
,E.NContrato AS NCntrato,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdJornada AS IdJrnada,Jornada,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS IdInstla,Instlacion
,C.IdCCosto AS IdCCto,CCosto,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipoLiq,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina
,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyPEN,DenyEPS,DenyARP,DenyCaj,DenyCes,DenyPri,DenyVac,DenyHex,BasePrest,BaseSeg,DchDotacion,DchCafeteria,Reclmto,Notas,C.IdEstado AS IdEstdCon,EC.Estado AS EstdoCon,EC.NColor AS ConEstcolor
,C.Inactivo AS Inactvo,C.FecAdd AS Fec_Add,C.FecUpdate AS Fec_Update,C.IdUsuario AS IdUsuari,Usuario
 FROM Empleados  AS E INNER JOIN EstadoCiv AS ECV ON E.IdEstCivil=ECV.IdEstado  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo  INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo 
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo 
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado 
 INNER JOIN Emp_Contrato AS C  ON E.IdEmpleado=C.IdEmpleado INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado 
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 WHERE  C.IdNom LIKE ISNULL(@pmIdNom,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')  AND C.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND C.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')   AND  C.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND  E.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND E.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (SalIntegral=ISNULL(@pmSalIntegral,0) or SalIntegral=ISNULL(@pmSalIntegral,1))
 AND (SalMinimo=ISNULL(@pmSalMinimo,0) or SalMinimo=ISNULL(@pmSalMinimo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 AND (EsCondtor=ISNULL(@pmEsCondtor,0) or EsCondtor=ISNULL(@pmEsCondtor,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Apellidos,Nombres,E.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosVac @pmDias INT,@pmFecVacIni SMALLDATETIME,@pmFecVacFin SMALLDATETIME,@pmIdProf VARCHAR(4)=Null
,@pmEmp_IdEstado VARCHAR(4)=Null,@pmEmp_Inactivo BIT=Null,@pmEsCondtor BIT=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmIdEstado VARCHAR(4)=Null
,@pmFecIngresoIni SMALLDATETIME=Null,@pmFecIngresoFin SMALLDATETIME=Null,@pmSalIntegral BIT=Null
 AS 
 SELECT E.IdEmpleado AS IdEmplado,Apellidos,Nombres,Codigo,TipoId,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,FecNacmto,LugarNacmto,TipoSangre
,FactorRh,Sexo,IdEstCivil,EstCivil,NHijos,TallaCam,TallaPan,TallaZap,UsaGafas,E.IdProf AS CodProf,Profesion,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia
,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,IdLugarCed,LC.Localidad AS LugarCed,Conyugue,ConyFecNac,ConyTrabaja,ConyEmpresa,ConyCargo,ConyFing,ConyTel,E.IdClase AS CodClase
,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes
,IdCajaCom,CC.Fondo AS CajaComp,E.NContrato AS NCntrato,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo,FotoEmp,FotoLic
,FotoFirma,Observacion,E.IdEstado AS Emp_IdEstado,ET.Estado AS Emp_estado,E.Inactivo AS Emp_Inactivo,ET.NColor AS EmpEstcolor,E.IdUsuario AS Emp_IdUsuario,UE.Usuario AS Emp_Usuario
--Datos del contrato
,C.IdTipCon AS CodTCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdJornada AS IdJrnada,Jornada,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS IdInstla,Instlacion,C.IdCCosto AS IdCCto
,CCosto,C.IdSubCos AS IdSubcen,SubCosto,C.IdCia AS CodCia,Compania,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,TipoTrabj,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipoLiq,ModPromdio,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina
,ModoRet,FecAfiSeg,ATFinMes,SegFinMes,DenyCes,DenyPri,DenyVac,DchDotacion,DchCafeteria,C.IdEstado AS IdEstdCon,EC.Estado AS EstdoCon,EC.NColor AS ConEstcolor,C.Inactivo AS Inactvo,C.IdUsuario AS IdUsuari,U.Usuario AS Con_Usuario,VrUpc
,IdCot,C.IdGrupo AS GrupAR
 FROM Empleados AS E INNER JOIN Emp_Contrato AS C ON E.NContrato=C.NContrato
 INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
 INNER JOIN EstadoCiv AS ECV ON E.IdEstCivil=ECV.IdEstado INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado INNER JOIN adm_Usuarios AS UE ON E.IdUsuario=UE.IdUsuario
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo 
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada INNER JOIN Dependencias AS D ON C.IdDep=D.IdDep 
 INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala 
 INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom 
 INNER JOIN EstadoDoc AS EC ON C.IdEstado=EC.IdEstado  INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 WHERE DATEADD(day,@pmDias,ISNULL(FecUltVac,FecIngreso)) BETWEEN @pmFecVacIni AND @pmFecVacFin AND E.IdProf LIKE ISNULL(@pmIdProf,'%')
 AND E.IdEstado LIKE ISNULL(@pmEmp_IdEstado,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND C.IdCargo LIKE ISNULL(@pmIdCargo,'%')
 AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND C.IdNom LIKE ISNULL(@pmIdNom,'%')  AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 AND (EsCondtor=ISNULL(@pmEsCondtor,0) or EsCondtor=ISNULL(@pmEsCondtor,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (SalIntegral=ISNULL(@pmSalIntegral,0) or SalIntegral=ISNULL(@pmSalIntegral,1))
 AND (FecIngreso>=ISNULL(@pmFecIngresoIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngresoFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEmpleadosVeh  @pmpVehiculo VARCHAR(10),@pmIdEmpleado VARCHAR(16)
 AS 
 SELECT IdEmpleado,Apellidos,Nombres,NContrato,EsCondtor,pVehiculo
 FROM Empleados
 WHERE Inactivo=0 AND pVehiculo=@pmpVehiculo AND  IdEmpleado<>@pmIdEmpleado
 ORDER BY IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntArticulosPro @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4)=Null
 AS 
 SELECT Item,IdProducto,IdBodega,Entradas,Salidas,VrUnitario,TarifaIva,VrIva,TarifaDct,VrDcto
,TipOrd,NumOrden,IdCiaOrd,TipSal,NumSalida,IdCiaSal
 FROM Trn_EntArticulos
  WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia 
 AND IdProducto=@pmIdProducto AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntidades @pmIdTabla VARCHAR(20)
 AS 
 SELECT IdTabla,NivelRel,CamposPK,FngKey,Sp_Qry,Sp_Qpmt,Sp_Ins,Sp_Upd,Sp_Del
 FROM Sys_Entidades
 WHERE IdTabla LIKE ISNULL(@pmIdTabla,'%')
 ORDER BY IdTabla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradas @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Entrada,IdCia,Fecha,IdConcepto,IdProv,Factura,FechaFac,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion
,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos
,IdPlazo,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv
,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion
,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Entradas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasEqv @pmFactura VARCHAR(15),@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Entrada,IdCia,Fecha,IdProv,Factura,FechaFac,FechaVence,CxPagar,Anulado
 FROM Trn_Entradas
 WHERE  Factura=@pmFactura AND IdCia=@pmIdCia
 ORDER BY Entrada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasFac @pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Entrada,IdCia,Fecha,IdProv,Factura,FechaFac,FechaVence,CxPagar,Anulado
 FROM Trn_Entradas
 WHERE IdProv=@pmIdProv AND Factura=@pmFactura AND IdCia=@pmIdCia
 ORDER BY Entrada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasIns  @pmTipDoc VARCHAR(3),@pmEntradaIni INT=Null,@pmEntradaFin INT=Null,@pmIdCia CHAR(2)=Null
,@pmIdProv VARCHAR(16)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT Entrada,E.IdCia AS CdCia,Compania,E.Fecha AS FechaDoc,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega
,Entradas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,Entradas*VrUnitario AS CostoTotal,K.TarifaIva AS KarTarifIva,VrIvaEnt
,TarifaDct,VrDctoEnt,K.Referencia AS KarReferencia,Descripcion,Unidades,TipOrd,NumOrden,IdCiaOrd,Remision,IdCiaRem
,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro
,E.IdProv AS NitPrevee,RazonSocial,E.Factura AS NumFactura,E.IdConcepto AS CdConcepto,Concepto,E.Observacion AS Observ
 FROM Trn_Entradas AS E INNER JOIN Trn_Kardex AS K ON E.TipDoc=K.TipDoc AND E.Entrada=K.Documento AND E.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto 
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega  INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE E.TipDoc=@pmTipDoc AND Anulado=0
 AND Entrada BETWEEN ISNULL(@pmEntradaIni,0) AND ISNULL(@pmEntradaFin,2147483647)
 AND E.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdProv LIKE ISNULL(@pmIdProv,'%')
 AND (E.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND E.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY E.IdCia,Entrada,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmEntradaIni INT=Null,@pmEntradaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Entrada,IdCia,Fecha,E.IdConcepto AS CdConcepto,Concepto,IdProv,RazonSocial,Factura,FechaFac,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros
,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,IdPlazo,CxPagar,DocEquiv
,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv
,CdCCosto,CdSubCos,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,E.Observacion AS Observ
,E.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,E.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Entradas AS E INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Entrada BETWEEN ISNULL(@pmEntradaIni,0) AND ISNULL(@pmEntradaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND E.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Entrada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasNFac @pmFactura VARCHAR(15),@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Entrada,IdCia,Fecha,IdProv,Factura,FechaFac,FechaVence,CxPagar,Anulado
 FROM Trn_Entradas
 WHERE Factura=@pmFactura AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%')
 ORDER BY Entrada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasNet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Entrada,E.IdCia AS CdCia,Compania,Fecha,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,T.RazonSocial AS NomProveedor,Factura,FechaFac,FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto
,Cantidad,NitFletes,TF.RazonSocial AS NomFletes,VrRetFlete,VrIcaFlete,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,BaseImp,BaseRet
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CdCCosto,CCosto,CdSubCos,SubCosto,Modalidad,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,TimeSys,E.FecUpdate AS Fec_Update,IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDV,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCodLocal,Localidad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS TercCodSector,SectorEco,T.IdRegimen AS TercCodRegimen,Regimen,T.TipEnte AS TercTipoEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 FROM Trn_Entradas AS E INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CC ON E.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON E.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS TF ON E.NitFletes=TF.IdTercero
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND E.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND E.IdProv LIKE ISNULL(@pmIdProv,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1))
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.IdProv AS NitProvee,T.RazonSocial AS NomProveedor
,D.Factura AS NumFactura,FechaFac,FechaVence,D.VrSubTotal AS SubTotal,D.VrDescuento AS Descuento,D.VrImpuesto AS Impuesto,D.VrRetencion AS ReteFte,D.VrReteICA AS ReteICA
,D.VrReteIVA AS ReteIva,D.VrFletes AS Fletes,D.VrOtros AS ValOtros,D.VrSobretasa AS Sobretasas,D.VrImpGlobal AS ImpGlobal,D.VrCargos AS OtrosCargos,D.VrOtrDcto AS OtrosDctos
,D.VrNeto AS TotalNeto,D.Cantidad AS CantDev,D.NitFletes AS Nit_Fletes,TF.RazonSocial AS NomFletes,D.VrRetFlete,D.VrIcaFlete,D.FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago
,D.CxPagar AS Cx_Pagar,D.DocEquiv AS DocEquival,D.BaseImp AS VrBaseIva,D.BaseRet AS VrBaseRet,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,D.CdCCosto AS CodCcost,CCosto
,D.CdSubCos AS CodSubCent,SubCosto,D.Modalidad AS ModalidadEnt,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaComp,0,D.Entrada AS NumEntrada,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS FecAdd,D.FecUpdate AS Fec_Update,D.IdCiaCrea AS CodCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDV,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCodLocal,Localidad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS TercCodSector,SectorEco,T.IdRegimen AS TercCodRegimen,Regimen,T.TipEnte AS TercTipoEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga,Autoret
 FROM Trn_DevEnt AS D INNER JOIN Terceros AS T ON D.IdProv=T.IdTercero 
 INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Entradas AS E ON D.TipDoc=E.TipDoc AND D.Entrada=E.Entrada AND D.IdCiaDoc=E.IdCia
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 LEFT JOIN CentroCosto AS CC ON D.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON D.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON D.IdProv=TP.IdProv  LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Terceros AS TF ON D.NitFletes=TF.IdTercero
 WHERE D.TipDoc=@pmTipDoc AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdProv LIKE ISNULL(@pmIdProv,'%') AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (D.CxPagar=ISNULL(@pmCxPagar,0) or D.CxPagar=ISNULL(@pmCxPagar,1)) 
 ORDER BY E.IdCia,Entrada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasOdc @pmOCompra INT,@pmIdCiaOdc CHAR(2),@pmIdProv VARCHAR(16)=Null
 AS 
 SELECT  Entrada,E.IdCia AS CdCia,E.Fecha AS FecEntrada,IdProv,RazonSocial,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque
,Entradas,VrPrecio AS CostoBruto,Entradas*VrPrecio AS SubTotal,TarifaDct,VrDctoEnt,VrUnitario AS CostoNeto,Entradas*VrUnitario AS CostoTotal,K.TarifaIva AS TarfIva,VrIvaEnt
,Referencia,Unidades,Servcios,NumDocDev,Modalidad,CxPagar,E.Factura AS NumFact,FechaFac,FechaVence
 FROM Trn_Entradas AS E INNER JOIN Trn_Kardex AS  K ON E.Entrada=K.Documento AND E.IdCia=K.IdCia
 AND E.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipOdc='ODC' AND OCompra=@pmOCompra AND IdCiaOdc=@pmIdCiaOdc  AND Anulado=0
AND IdProv LIKE ISNULL(@pmIdProv,'%')
ORDER BY E.IdCia,Entrada,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradasRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmCxPagar BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Entrada,E.IdCia AS CdCia,Compania,Fecha,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,RazonSocial,Factura,FechaFac,FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto
,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CdCCosto,CCosto,CdSubCos,SubCosto,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,Comprobante
,IdCiaCom,Anulado,NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,TimeSys,E.FecUpdate AS Fec_Update,IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
 FROM Trn_Entradas AS E INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN CentroCosto AS CC ON E.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON E.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND E.IdCia LIKE ISNULL(@pmIdCia,'%%') AND E.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND E.IdProv LIKE ISNULL(@pmIdProv,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (CxPagar=ISNULL(@pmCxPagar,0) or CxPagar=ISNULL(@pmCxPagar,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY E.IdCia,Entrada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEntradas_Cr  @pmTipDoc VARCHAR(3),@pmEntradaIni INT,@pmEntradaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT E.TipDoc AS Tip_Doc,TipoDoc,Entrada,E.IdCia AS CdCia,Compania,E.Fecha AS FechaDoc,E.IdConcepto AS CdConcepto,Concepto,E.IdProv AS NitPrevee,RazonSocial,E.Factura AS NumFactura,FechaFac,FechaVence
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrSobretasa,VrImpGlobal,VrCargos,VrOtrDcto,VrNeto
,Cantidad,NitFletes,VrRetFlete,VrIcaFlete,CodTarRetFle,CodTarIcaFle,FletesCosto,TipOdc,OCompra,IdCiaOdc,MulPlazos,E.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CxPagar,DocEquiv,BaseImp,BaseRet,BaseIca,BaseRiv
,E.TarifaIva AS TarifIva,E.TarifaRet AS TarifRet,E.TarifaIca AS TarifIca,TarifaRiv,E.CdCCosto AS CodCenCosto,CC.CCosto AS CentCosto
,E.CdSubCos AS CodSubCent,SC.SubCosto AS SubcCosto,TipEgr,Egreso,IdCiaEgr,Modalidad,OrigenAdd,TipCom,TipoCom,Comprobante
,IdCiaCom,Anulado,NumDev,FecDev,E.Observacion AS Observ,E.IdEstado AS CdEstado,Estado,E.TimeSys AS Fec_add,E.FecUpdate AS Fec_Update,IdCiaCrea,E.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
--Detalle
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_Entradas AS E INNER JOIN Trn_Kardex AS K ON E.TipDoc=K.TipDoc AND E.Entrada=K.Documento AND E.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON E.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON E.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON E.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON E.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON E.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON E.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON E.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN TiposCom AS TC ON E.TipCom=TC.IdCom
 LEFT JOIN CentroCosto AS CC ON E.CdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON E.CdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON E.IdProv=TP.IdProv 
 LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE E.TipDoc=@pmTipDoc AND Entrada BETWEEN @pmEntradaIni AND @pmEntradaFin  AND E.IdCia=@pmIdCia
 ORDER BY Entrada,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstaciones @pmIdEstacion CHAR(2)
 AS 
 SELECT IdEstacion,Estacion
 FROM Sys_Estaciones
 WHERE IdEstacion LIKE ISNULL(@pmIdEstacion,'%%')
 ORDER BY IdEstacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstacionesDso
 AS 
 SELECT IdEstacion,Estacion,IdEstacion+' '+Estacion AS DsEst
 FROM Sys_Estaciones
 ORDER BY IdEstacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoCiv @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  SELECT IdEstado,EstCivil  FROM EstadoCiv
  WHERE  Inactivo=0
  ORDER BY IdEstado
 ELSE
  SELECT IdEstado,EstCivil,Inactivo
  FROM EstadoCiv
  WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoDoc @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  SELECT IdEstado,Estado,NColor,CodDcmto
  FROM EstadoDoc
  WHERE Inactivo=0
  ORDER BY IdEstado
 ELSE
  SELECT IdEstado,Estado,NColor,CodDcmto,Inactivo
  FROM EstadoDoc
  WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoDocDso @pmCodDcmto VARCHAR(3)=Null
 AS 
 IF @pmCodDcmto IS NULL
  SELECT IdEstado,Estado,NColor,CodDcmto
  FROM EstadoDoc
  WHERE Inactivo=0 
  ORDER BY IdEstado
 ELSE
  SELECT IdEstado,Estado,NColor,CodDcmto
  FROM EstadoDoc
  WHERE Inactivo=0 AND ( CodDcmto=@pmCodDcmto OR CodDcmto='0' OR LEN(CodDcmto)=0)
  ORDER BY  IdEstado

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoPro @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
 SELECT IdEstado,Estado,NColor
 FROM EstadoPro
 WHERE  Inactivo=0 
 ORDER BY  IdEstado
ELSE
  SELECT IdEstado,Estado,NColor,Inactivo
 FROM EstadoPro
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoSer @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
 SELECT IdEstado,Estado,NColor
 FROM EstadoSer
 WHERE Inactivo=0
 ORDER BY IdEstado
 ELSE
 SELECT IdEstado,Estado,NColor,Inactivo
 FROM EstadoSer
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoTer @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  SELECT IdEstado,Estado,NColor
  FROM EstadoTer
  WHERE Inactivo=0
  ORDER BY IdEstado
 ELSE
  SELECT IdEstado,Estado,NColor,Inactivo
  FROM EstadoTer
  WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstadoVeh @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
 SELECT IdEstado,Estado,NColor,OutDemand
 FROM EstadoVeh
 WHERE Inactivo=0
 ORDER BY IdEstado
 ELSE
 SELECT IdEstado,Estado,NColor,OutDemand,Inactivo
 FROM EstadoVeh
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryEstratos @pmIdEstrato VARCHAR(4)
 AS 
 IF @pmIdEstrato IS NULL
 SELECT IdEstrato,Estrato
 FROM Estratos
 WHERE  Inactivo=0
 ORDER BY IdEstrato
 ELSE
 SELECT IdEstrato,Estrato,Inactivo
 FROM Estratos
 WHERE IdEstrato=@pmIdEstrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryExpLicencias @pmIdLugar VARCHAR(8)
 AS 
 IF @pmIdLugar IS NULL
  SELECT IdLugar,LugarLic,CodDpto,Dpto  FROM ExpLicencias
  WHERE Inactivo=0
  ORDER BY LugarLic
 ELSE
  SELECT IdLugar,LugarLic,CodDpto,Dpto,Inactivo 
  FROM ExpLicencias
  WHERE IdLugar=@pmIdLugar
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryExpLicenciasDso
 AS 
 SELECT IdLugar,LugarLic,Dpto,LugarLic+' ('+ Dpto + ')' AS DsLug  FROM ExpLicencias
 WHERE Inactivo=0
 ORDER BY LugarLic

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactVales @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,Fecha,HoraServ,IdCliente,RazonSocial,V.IdAgencia AS CdAgencia,Agencia
,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,IdCuenta
,CantPasaj,Usuarios,V.Observacion AS Observ,TipFac,NumFactura,IdCiaFac,FechaFac,Anulado,NumDev,FecDev
,F.nRadica AS NumRadica,IdCiaRad,ItemRad,Factura,F.Item AS Fac_Item
 FROM Trn_FactVales AS F INNER JOIN Trn_Vales AS V ON F.TipRad=V.TipDoc AND F.nRadica=V.nRadica
  AND F.IdCiaRad=V.IdCia AND F.ItemRad=V.Item
 INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia
 WHERE F.TipDoc=@pmTipDoc AND F.Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND F.IdCia=@pmIdCia 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactcon @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Factura,IdCia,Fecha,IdConcepto,nClieCon,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr
,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCajero,IdVend,TarifaCom,CodTarCom
,NitCliente,CdAgencia,IdCCosto,IdSubCos,IdLocal,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,FecPedido
,Modalidad,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Factcon
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactconLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmFacturaIni INT=Null,@pmFacturaFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmnClieCon VARCHAR(16)=Null
,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Factura,IdCia,Fecha,F.IdConcepto AS CdConcepto,Concepto,nClieCon,Nombre,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr
,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCajero,UC.Usuario AS UsuCajero,IdVend,RazonSocial,TarifaCom,CodTarCom
,NitCliente,CdAgencia,IdCCosto,IdSubCos,F.IdLocal AS CdCiudad,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,FecPedido
,Modalidad,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,TipDoc
 FROM Trn_Factcon AS F INNER JOIN ClieContado AS CC ON F.nClieCon=CC.IdCliente
 INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON F.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON F.IdCajero=UC.IdUsuario 
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Factura BETWEEN ISNULL(@pmFacturaIni,0) AND ISNULL(@pmFacturaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND nClieCon LIKE ISNULL(@pmnClieCon,'%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCajero LIKE ISNULL(@pmIdCajero,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%')  AND F.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactconNet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmnClieCon VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Factura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,nClieCon,Nombre,CC.TipoId AS CliTipoId,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad
,LE.Localidad AS CiudadClie,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,IdCajero,UC.Usuario AS UsuCajero,UC.IdEstacion AS UsuEstacion
,F.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitCliente,T.RazonSocial AS NombreClie,F.CdAgencia AS CodAgencClie,Agencia,CodAgencia
,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,Banco,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,FecPedido,Modalidad,PlacaVehic,KmtVehic,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--datos del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_Factcon AS F  INNER JOIN ClieContado AS CC ON F.nClieCon=CC.IdCliente  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON F.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS T ON F.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON F.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TercCliente AS CLI ON F.NitCliente=CLI.IdClie LEFT JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Bancos AS BF ON F.CdBanco=BF.IdBanco  
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND nClieCon LIKE ISNULL(@pmnClieCon,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%')  AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.nClieCon AS NitClieCon,Nombre,CC.TipoId AS CliTipoId
,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad,LE.Localidad AS CiudadClie,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie
,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento,D.VrSubTotal AS SubTotal,D.VrDescuento AS Descuento,D.VrImpuesto AS Impuestos,D.VrRetencion AS ReteFte
,D.VrReteICA AS ReteICA,D.VrReteIVA AS ReteIVA,D.VrFletes AS Fletes,D.VrCargos AS OtrosCargos,D.VrOtrDcto AS OtrosDctos,D.VrCostos AS TotalCostos,D.VrNeto AS TotalNeto
,D.VrEfectivo AS TotalEfectivo,D.VrPagosOtr AS OtrosPagos,D.Cantidad AS CantDev,D.CantPuntos AS CantPuntosDev,PuntosAcum,D.BaseImp AS VrBaseImp,D.BaseRet AS VrBaseRet
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,D.IdCajero AS CdCajero,UC.Usuario AS Cajero,UC.IdEstacion AS UsuEstacion,D.IdVend AS NitVendedor,V.RazonSocial AS Vendedor,D.TarifaCom AS TarifCom
,D.NitCliente AS DevNitCliente,T.RazonSocial AS NomCliente,D.CdAgencia AS IdAgncia,Agencia,CodAgencia,D.EnEfectivo AS DevEnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,Banco,TipPed
,D.Pedido AS NumPedido,D.IdCiaPed AS CdCiaPed,TipRem,D.Remision AS NumRemision,D.IdCiaRem AS CdCiaRem,D.FecPedido AS FechaPed,D.Modalidad AS ModDoc
,D.PlacaVehic AS DevPlacaVehic,D.KmtVehic AS DevKmtVehic,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaCom,0,D.Factura AS NumFactura,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,D.FecUpdate AS Fec_Update,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_DevFco AS D INNER JOIN ClieContado AS CC ON D.nClieCon=CC.IdCliente
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia   INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 LEFT JOIN Terceros AS T ON D.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON D.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TercCliente AS CLI ON  D.NitCliente=CLI.IdClie LEFT JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Bancos AS BF ON F.CdBanco=BF.IdBanco  
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.nClieCon LIKE ISNULL(@pmnClieCon,'%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.NitCliente LIKE ISNULL(@pmNitCliente ,'%')
 AND (D.EnEfectivo=ISNULL(@pmEnEfectivo,0) or D.EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY F.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactconNetDet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmnClieCon VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null
,@pmIdSubgrupo VARCHAR(8)=Null,@pmTipoRef VARCHAR(10)=Null
 AS 
 SELECT F.TipDoc AS TipoFact,F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha AS FechaDoc,F.IdConcepto AS CdConcepto,Concepto,nClieCon,Nombre,CC.TipoId AS CliTipoId,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad
,LE.Localidad AS CiudadClie,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrNeto,VrEfectivo,VrPagosOtr,CantPuntos,PuntosAcum,IdCajero,UC.Usuario AS UsuCajero,UC.IdEstacion AS UsuEstacion
,NitCliente,T.RazonSocial AS NombreClie,F.CdAgencia AS CodAgencClie,Agencia,CodAgencia,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,Banco
,TipPed,Pedido,IdCiaPed,TipRem,F.Remision AS NumRemision,F.IdCiaRem AS CdCiaRem,FecPedido,Modalidad,PlacaVehic,KmtVehic,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,F.TimeSys AS Fech_Add,IdCiaCrea,F.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalles 
,K.IdProducto AS CdProducto,DescripProd,Item,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,K.TarifaRet AS TarifRet,VrReteEnt,VrReteSal
,K.TarifaIca AS TarifIca,VrIcaEnt,VrIcaSal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden
,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,K.Comision AS TarifCom,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper
,K.pVehiculo AS KarPlacaVeh,CdLocal,LK.Localidad AS KarCiudad,CdCCosto,CCosto,K.CdSubCos AS CodSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarNumRemision
,K.IdCiaRem AS KarCdCiaRem,K.Cotizacion AS KarNumCotizacion,K.IdCiaCot AS KarCiaCotiza,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_Factcon AS F  INNER JOIN ClieContado AS CC ON F.nClieCon=CC.IdCliente  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON F.IdCajero=UC.IdUsuario
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON F.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON F.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TercCliente AS CLI ON F.NitCliente=CLI.IdClie LEFT JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Bancos AS BF ON F.CdBanco=BF.IdBanco  
 LEFT JOIN CentroCosto AS CCT ON K.CdCCosto=CCT.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN  Localidades AS LK ON K.CdLocal=LK.IdLocal  LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsProdBase=0 AND F.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND nClieCon LIKE ISNULL(@pmnClieCon,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%')  AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.nClieCon AS NitClieCon,Nombre,CC.TipoId AS CliTipoId
,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad,LE.Localidad AS CiudadClie,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie
,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento,D.VrRetencion AS ReteFte,D.VrReteICA AS ReteICA,D.VrReteIVA AS ReteIVA,D.VrFletes AS Fletes
,D.VrCargos AS OtrosCargos,D.VrOtrDcto AS OtrosDctos,D.VrNeto AS TotalNeto,D.VrEfectivo AS TotalEfectivo,D.VrPagosOtr AS OtrosPagos
,D.CantPuntos AS CantPuntosDev,PuntosAcum,D.IdCajero AS CdCajero,UC.Usuario AS Cajero,UC.IdEstacion AS UsuEstacion
,D.NitCliente AS DevNitCliente,T.RazonSocial AS NomCliente,D.CdAgencia AS IdAgncia,Agencia,CodAgencia,D.EnEfectivo AS DevEnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,Banco
,TipPed,D.Pedido AS NumPedido,D.IdCiaPed AS CdCiaPed,TipRem,D.Remision AS NumRemision,D.IdCiaRem AS CdCiaRem,D.FecPedido AS FechaPed,D.Modalidad AS ModDoc,D.PlacaVehic AS DevPlacaVeh,D.KmtVehic AS DevKmtVehic
,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaCom,0,D.Factura AS NumFactura,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--detalles 
,K.IdProducto AS CdProducto,DescripProd,Item,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,K.TarifaRet AS TarifRet,VrReteEnt,VrReteSal
,K.TarifaIca AS TarifIca,VrIcaEnt,VrIcaSal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden
,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,K.Comision AS TarifCom,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper
,K.pVehiculo AS KarPlacaVeh,CdLocal,LK.Localidad AS KarCiudad,CdCCosto,CCosto,K.CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarNumRemision
,K.IdCiaRem AS KarCdCiaRem,K.Cotizacion AS KarNumCotizacion,K.IdCiaCot AS KarCiaCotiza,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_DevFco AS D INNER JOIN ClieContado AS CC ON D.nClieCon=CC.IdCliente
 INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia   INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 INNER JOIN Trn_Factcon AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN Terceros AS T ON D.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON D.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TercCliente AS CLI ON D.NitCliente=CLI.IdClie LEFT JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Bancos AS BF ON F.CdBanco=BF.IdBanco
 LEFT JOIN CentroCosto AS CCT ON K.CdCCosto=CCT.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN  Localidades AS LK ON K.CdLocal=LK.IdLocal  LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsProdBase=0 AND D.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.nClieCon LIKE ISNULL(@pmnClieCon,'%') AND D.IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.NitCliente LIKE ISNULL(@pmNitCliente ,'%')
 AND (D.EnEfectivo=ISNULL(@pmEnEfectivo,0) or D.EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY F.IdCia,F.Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactconRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmnClieCon VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null,@pmIdVend VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null
,@pmEnEfectivo BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Factura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,nClieCon,Nombre,CC.TipoId AS CliTipoId,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad
,LE.Localidad AS CiudadClie,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,IdCajero,UC.Usuario AS UsuCajero,UC.IdEstacion AS UsuEstacion
,F.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitCliente,T.RazonSocial AS NombreClie,F.CdAgencia AS CodAgencClie,Agencia,CodAgencia
,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,Banco,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,FecPedido,Modalidad,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM Trn_Factcon AS F  INNER JOIN ClieContado AS CC ON F.nClieCon=CC.IdCliente  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON F.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS T ON F.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON F.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN TercCliente AS CLI ON F.NitCliente=CLI.IdClie LEFT JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Bancos AS BF ON F.CdBanco=BF.IdBanco  
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND nClieCon LIKE ISNULL(@pmnClieCon,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%')  AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY F.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFactcon_Cr @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT F.TipDoc AS Tip_Doc,TipoDoc,F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha AS FechaFac,F.IdConcepto AS CdConcepto,Concepto
,nClieCon,Nombre,CC.TipoId AS CliTipoId,CC.Dv AS CliDV,CC.Direccion AS CliDireccion,CC.IdLocal AS CliCodCiudad,LE.Localidad AS CiudadClie,LE.IdDep AS CdDepClie,DE.Departamento AS DptoClie
,CC.Telefono AS CliTelefono,CC.email AS ClieEmail,Excento,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrCargos,VrOtrDcto,VrCostos,VrNeto,VrEfectivo,VrPagosOtr
,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,F.TarifaIva AS TarifIva,F.TarifaRet AS TarifRet,F.TarifaIca AS TarifIca,TarifaRiv,IdCajero,UC.Usuario AS UsuCajero,UC.IdEstacion AS UsuEstacion
,F.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,NitCliente,T.RazonSocial AS NombreClie,F.CdAgencia AS CodAgencClie,Agencia,CodAgencia
,EnEfectivo,CdForma,NumForma,DetallePago,Referncia1,Referncia2,CdBanco,Banco,TipPed,Pedido,IdCiaPed,TipRem,F.Remision AS NumRemision,F.IdCiaRem AS CdCiaRem,FecPedido
,Modalidad,PlacaVehic,KmtVehic,OrigenAdd,TipCom,Comprobante,TipoCom,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado
,F.TimeSys AS Fec_Add,FecUpdate,IdCiaCrea,F.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
--datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdCCosto,CdSubCos,pVehiculo,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd
,K.Factura AS KarFactura,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_Factcon AS F INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON F.TipDoc=TD.IdDoc
 INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario INNER JOIN adm_Usuarios AS UC ON F.IdCajero=UC.IdUsuario
 INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN ClieContado AS CC ON F.nClieCon=CC.IdCliente
 LEFT JOIN  Localidades AS LE ON CC.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS T ON F.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON F.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Bancos AS BF ON F.CdBanco=BF.IdBanco LEFT JOIN TiposCom AS TC ON F.TipCom=TC.IdCom
 WHERE F.TipDoc=@pmTipDoc AND F.Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND F.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturas @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv
,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,DiasEntraga
,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,CdRuta
,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCot,Cotizacion,IdCiaCot,FecPedido,AutzaMora,AutzaCupo,Modalidad,KmtVehic,OrigenAdd,TipCom,Comprobante,IdCiaCom
,ZonaFrontera,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Facturas
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasCom @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Factura,F.IdCia AS CdCia,Compania,Fecha,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia
,A.CodAgencia AS Cod_Agencia,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor
,DirEnvio,F.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,F.NitContac AS FacNitContac,F.NomContac AS FacNomContac,F.TelContac AS FacTelContac,F.emlContac AS FacEmailContac,CargoContac
,NitEmpTrans,EmpTrans,F.pVehiculo AS PlacaVeh,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta,TipPed,Pedido,IdCiaPed
,TipRem,F.Remision AS NumGuia,F.IdCiaRem AS CdCiaGuia,Modalidad,ZonaFrontera,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdUsuario AS CdUsuario,Usuario
--datos del contenedor
,tmNumComp,tmIdProducto,tmCantidad,tmDescProd,tmComp2,tmIdProd2,tmDescProd2,tmCantidad2
,tmComp3,tmIdProd3,tmDescProd3,tmCantidad3,tmComp4,tmIdProd4,tmDescProd4,tmCantidad4
,tmComp5,tmIdProd5,tmDescProd5,tmCantidad5
 FROM Trn_Facturas AS F  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero  INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia 
 LEFT JOIN tm_OccComp AS CMP ON F.TipDoc =CMP.tmTipDoc  AND F.Factura=CMP.tmOCargue AND F.IdCia=CMP.tmIdCia
 LEFT JOIN  Localidades AS LE ON F.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 WHERE  TipDoc=@pmTipDoc AND Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND F.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasCot @pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT F.Factura AS NumFact,F.IdCia AS CodCia,F.Fecha AS FechaFact,FechaVence,IdCliente,RazonSocial,IdAgencia,Modalidad
,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,K.TarifaIva AS TarfIva,VrIvaSal
,TarifaDct,VrDctoSal,((VrPrecio*Salidas)-VrDctoSal)+VrIvaSal AS SValTotal,VrUnitario,VrCostoSal,K.TarifaRet AS TarfRet,VrReteSal,K.TarifaIca AS TarfIca,VrIcaSal,VrBruto
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,K.IdVend AS CdVend,Comision,Referencia,Unidades,Pedido,FecPedido,K.Remision AS Remsion
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS  K ON F.Factura=K.Documento AND F.IdCia=K.IdCia
 AND F.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipCot='COT' AND F.Cotizacion=@pmCotizacion AND F.IdCiaCot=@pmIdCiaCot  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY F.IdCia,F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasGui @pmRemision INT,@pmIdCiaRem CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT F.Factura AS NumFact,F.IdCia AS CodCia,F.Fecha AS FechaFact,FechaVence,IdCliente,RazonSocial,IdAgencia,Modalidad
,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,K.TarifaIva AS TarifIva,VrIvaSal
,VrUnitario,VrCostoSal,K.TarifaRet AS TarifRet,VrReteSal,K.TarifaIca AS TarifIca,VrIcaSal,VrBruto
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,K.pVehiculo AS PlacaVeh,K.IdVend AS CdVend,Comision,Referencia,Unidades,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,TipPed,Pedido,IdCiaPed,TipCot,F.Cotizacion AS OCargue,F.IdCiaCot AS IdCiaOcc
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS  K ON F.Factura=K.Documento AND F.IdCia=K.IdCia
 AND F.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipRem='GUI' AND F.Remision=@pmRemision AND F.IdCiaRem=@pmIdCiaRem  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY F.IdCia,F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmFacturaIni INT=Null,@pmFacturaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Factura,IdCia,Fecha,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca
,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio
,IdLocEnv,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans
,EmpTrans,pVehiculo,CdConductor,KmtVehic,CdRuta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCot,Cotizacion,IdCiaCot,FecPedido,AutzaMora,AutzaCupo,Modalidad
,OrigenAdd,TipCom,Comprobante,IdCiaCom,ZonaFrontera,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Facturas F INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON F.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON F.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario 
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Factura BETWEEN ISNULL(@pmFacturaIni,0) AND ISNULL(@pmFacturaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND F.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasNet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Factura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia
,A.CodAgencia AS Cod_Agencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal
,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,TarifaIva,TarifaRet ,TarifaIca,TarifaRiv,F.IdCCosto AS IdCenCost,CCosto
,F.IdSubCos AS IdSubCent,SubCosto,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,F.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio
,DE.Departamento AS DptoEnvio,DiasEntraga,F.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,pVehiculo
,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCot,Cotizacion,IdCiaCot,FecPedido,KmtVehic,Modalidad,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,F.FecUpdate AS Fec_Update,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Facturas AS F  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
 INNER JOIN CentroCosto AS CC ON F.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON F.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos 
 WHERE TipDoc LIKE ISNULL(@pmTipDoc,'%') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND F.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%') AND F.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente
,D.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,FechaVence,D.VrSubTotal AS SubTotal,D.VrDescuento AS Descuentos,D.VrImpuesto AS Impuestos
,D.VrRetencion AS ReteFte,D.VrReteICA AS ReteIca,D.VrReteIVA AS ReteIVA,D.VrFletes AS Fletes,D.VrOtros AS Otros,D.VrCargos AS OtrosCargos,D.VrOtrDcto AS OtrosDctos,D.VrCostos AS TotalCostos
,D.VrSobretasa AS Sobretasas,D.VrImpGlobal AS ImpGlobal,D.VrFaltantes AS Faltantes,D.VrAnticipos AS Anticipos,D.VrNeto AS TotalNeto,0,D.Cantidad AS CantDev,D.CantPuntos AS CantPuntDev,0
,D.BaseImp AS BaseIVA,D.BaseRet AS BaseRetFte,TarifaIva,TarifaRet ,TarifaIca,TarifaRiv,D.IdCCosto AS IdCenCost,CCosto,D.IdSubCos AS IdSubCent,SubCosto,D.IdVend AS NitVend,VN.RazonSocial AS Vendedor
,D.TarifaCom AS TarifCom,DirEnvio,D.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,DiasEntraga,F.IdForma AS CdForma,FormaPago,DetallePago
,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta,TipPed,D.Pedido AS NumPedido,D.IdCiaPed AS CdCiaPed
,TipRem,D.Remision AS NumRemision,D.IdCiaRem AS CdCiaRem,TipCot,D.Cotizacion AS NumCotizacion,D.IdCiaCot AS CdCiaCotiza,D.FecPedido AS FechaPed,D.KmtVehic AS DevKmtVeh,D.Modalidad AS ModFactura
,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaCom,0,D.Factura AS NumFactura,FecDoc,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fech_Sys
,D.FecUpdate AS Fech_Update,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevFcr AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia 
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Facturas AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma INNER JOIN  Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
 LEFT JOIN  Localidades AS LE ON D.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND D.TipDoc LIKE ISNULL(@pmTipDoc ,'%') 
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND D.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY F.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasNetDet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmTipoRef VARCHAR(10)=Null
 AS 
 SELECT F.TipDoc AS TipoFact,F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha AS FechaDoc,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,FechaVence,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto
,VrNeto,CantPuntos,PuntosAcum,F.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,F.pVehiculo AS PlacaVeh,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta
,TipPed,Pedido,IdCiaPed,TipRem,F.Remision AS NumRemision,F.IdCiaRem AS CdCiaRem,TipCot,F.Cotizacion AS NumCotizacion,F.IdCiaCot AS CdCiaCotiza,FecPedido
,Modalidad,KmtVehic,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,F.TimeSys AS Fecha_Add,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
--detalles
,K.IdProducto AS CdProducto,DescripProd,Item,K.IdBodega AS CdBodega,Bodega,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,K.TarifaRet AS TarifRet,VrReteEnt,VrReteSal
,K.TarifaIca AS TarifIca,VrIcaEnt,VrIcaSal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden
,CdAgencia,KA.Agencia AS KarAgencia,KA.CodAgencia AS KarCodAgencia,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,Comision,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper
,K.pVehiculo AS KarPlacaVeh,CdLocal,LK.Localidad AS KarCiudad,CdCCosto,CCosto,K.CdSubCos AS CodSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarNumRemision,K.IdCiaRem AS KarCdCiaRem,K.Cotizacion AS KarNumCotizacion
,K.IdCiaCot AS KarCiaCotiza,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Facturas AS F  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo  INNER JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia 
 INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero LEFT JOIN Agencias  AS KA ON K.CdAgencia=KA.IdAgencia
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN  Localidades AS LK ON K.CdLocal=LK.IdLocal 
 LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 LEFT JOIN  Localidades AS LE ON F.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 WHERE F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsProdBase=0 AND F.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND F.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%') AND F.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 UNION
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente
,D.IdAgencia AS IdAgncia,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,FechaVence,D.VrRetencion AS ReteFte,D.VrReteICA AS ReteIca,D.VrReteIVA AS ReteIVA,D.VrFletes AS Fletes
,D.VrOtros AS Otros,D.VrCargos AS OtrosCargos,D.VrOtrDcto AS OtrosDctos,D.VrNeto AS TotalNeto,D.CantPuntos AS CantPuntDev,0,D.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio
,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,F.pVehiculo AS PlacaVeh,CdConductor,TC.RazonSocial AS Conductor
,CdRuta,Ruta,TipPed,D.Pedido AS NumPedido,D.IdCiaPed AS CdCiaPed,TipRem,D.Remision AS NumRemision,D.IdCiaRem AS CdCiaRem,TipCot,D.Cotizacion AS NumCotizacion,D.IdCiaCot AS CdCiaCotiza
,D.FecPedido AS FechaPed,D.Modalidad AS ModFactura,D.KmtVehic AS DevKmtVeh,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaCom,0,D.Factura AS NumFactura,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fech_Sys,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--detalles
,K.IdProducto AS CdProducto,DescripProd,Item,K.IdBodega AS CdBodega,Bodega,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario
,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaIva AS TarifIva,VrIvaEnt,VrIvaSal,K.TarifaRet AS TarifRet,VrReteEnt,VrReteSal
,K.TarifaIca AS TarifIca,VrIcaEnt,VrIcaSal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden
,CdAgencia,KA.Agencia AS KarAgencia,KA.CodAgencia AS KarCodAgencia,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,Comision,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper
,K.pVehiculo AS KarPlacaVeh,CdLocal,LK.Localidad AS KarCiudad,CdCCosto,CCosto,K.CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd,K.Remision AS KarNumRemision,K.IdCiaRem AS KarCdCiaRem,K.Cotizacion AS KarNumCotizacion
,K.IdCiaCot AS KarCiaCotiza,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_DevFcr AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN Agencias  AS A ON D.IdAgencia=A.IdAgencia 
 INNER JOIN TercCliente AS CLI ON D.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Facturas AS F ON D.TipDoc=F.TipDoc AND D.Factura=F.Factura AND D.IdCiaDoc=F.IdCia
 INNER JOIN  Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
 INNER JOIN Trn_Kardex AS K ON D.TipDev=K.TipDoc AND D.Devolucion=K.Documento AND D.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero LEFT JOIN Agencias  AS KA ON K.CdAgencia=KA.IdAgencia
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN  Localidades AS LK ON K.CdLocal=LK.IdLocal  LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 LEFT JOIN  Localidades AS LE ON D.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 WHERE D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsProdBase=0 AND D.TipDoc LIKE ISNULL(@pmTipDoc ,'%') 
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND D.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND D.Modalidad LIKE ISNULL(@pmModalidad,'%') AND  K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 ORDER BY F.IdCia,F.Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasOcc @pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT F.Factura AS NumFact,F.IdCia AS CodCia,F.Fecha AS FechaFact,FechaVence,IdCliente,RazonSocial,IdAgencia,Modalidad
,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,K.TarifaIva AS TarifIva,VrIvaSal
,VrUnitario,VrCostoSal,K.TarifaRet AS TarifRet,VrReteSal,K.TarifaIca AS TarifIca,VrIcaSal,VrBruto
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,K.pVehiculo AS PlacaVeh,K.IdVend AS CdVend,Comision,Referencia,Unidades,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,TipPed,Pedido,IdCiaPed,TipRem,F.Remision AS NumGuia,F.IdCiaRem AS CodCiaGuia
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS  K ON F.Factura=K.Documento AND F.IdCia=K.IdCia
 AND F.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipCot='OCC' AND F.Cotizacion=@pmCotizacion AND F.IdCiaCot=@pmIdCiaCot  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY F.IdCia,F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasOdc @pmTipCot VARCHAR(3),@pmCotizacion INT,@pmIdCiaCot CHAR(2)
,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,TipCot,Cotizacion,IdCiaCot
 FROM Trn_Facturas
 WHERE TipCot=@pmTipCot AND Cotizacion=@pmCotizacion AND IdCiaCot=@pmIdCiaCot
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasOdp @pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2)
,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,TipPed,Pedido,IdCiaPed
 FROM Trn_Facturas
 WHERE TipPed=@pmTipPed AND Pedido=@pmPedido AND IdCiaPed=@pmIdCiaPed
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasOdr @pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2)
,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,TipRem,Remision,IdCiaRem
 FROM Trn_Facturas
 WHERE  TipRem=@pmTipRem AND Remision=@pmRemision AND IdCiaRem=@pmIdCiaRem
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasPed @pmPedido INT,@pmIdCiaPed CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT F.Factura AS NumFact,F.IdCia AS CodCia,F.Fecha AS FechaFact,FechaVence,IdCliente,RazonSocial,IdAgencia,Modalidad
,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,K.TarifaIva AS TarfIva,VrIvaSal
,TarifaDct,VrDctoSal,((VrPrecio*Salidas)-VrDctoSal)+VrIvaSal AS SValTotal,VrUnitario,VrCostoSal,K.TarifaRet AS TarfRet,VrReteSal,K.TarifaIca AS TarifIca,VrIcaSal,VrBruto
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,K.IdVend AS CdVend,Comision,Referencia,Unidades
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS  K ON F.Factura=K.Documento AND F.IdCia=K.IdCia
 AND F.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipPed='PED' AND Pedido=@pmPedido AND IdCiaPed=@pmIdCiaPed  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY F.IdCia,F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Factura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia
,A.CodAgencia AS Cod_Agencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal
,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet ,TarifaIca,TarifaRiv,F.IdCCosto AS IdCenCost,CCosto
,F.IdSubCos AS IdSubCent,SubCosto,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,F.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio
,DE.Departamento AS DptoEnvio,DiasEntraga,F.NitContac AS FacNitContac,F.NomContac AS FacNomContac,F.TelContac AS FacTelContac,F.emlContac AS FacEmailContac,CargoContac
,F.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans,pVehiculo,KmtVehic,CdConductor,TC.RazonSocial AS Conductor
,CdRuta,Ruta,TipPed,Pedido,IdCiaPed,TipRem,Remision,IdCiaRem,TipCot,Cotizacion,IdCiaCot,FecPedido,AutzaMora,AutzaCupo,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,F.Observacion AS Observ,ZonaFrontera,F.IdEstado AS CdEstado,Estado,TimeSys,F.FecUpdate AS Fec_Update,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Facturas AS F  INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
 INNER JOIN CentroCosto AS CC ON F.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON F.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos 
 WHERE TipDoc LIKE ISNULL(@pmTipDoc,'%') AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND F.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%') AND F.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY F.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturasRem @pmRemision INT,@pmIdCiaRem CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
SELECT F.Factura AS NumFact,F.IdCia AS CodCia,F.Fecha AS FechaFact,FechaVence,IdCliente,RazonSocial,IdAgencia,Modalidad
,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,K.TarifaIva AS TarifIva,VrIvaSal
,TarifaDct,VrDctoSal,((VrPrecio*Salidas)-VrDctoSal)+VrIvaSal AS SValTotal,VrUnitario,VrCostoSal,K.TarifaRet AS TarifRet,VrReteSal,K.TarifaIca AS TarifIca,VrIcaSal,VrBruto
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,K.IdVend AS CdVend,Comision,Referencia,Unidades
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS  K ON F.Factura=K.Documento AND F.IdCia=K.IdCia
 AND F.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipRem='REM' AND F.Remision=@pmRemision AND F.IdCiaRem=@pmIdCiaRem  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY F.IdCia,F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFacturas_Cr @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT F.TipDoc AS Tip_Doc,TipoDoc,F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,F.Fecha AS FechaDoc,F.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,F.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes
,VrOtros,VrCargos,VrOtrDcto,VrCostos,VrSobretasa,VrImpGlobal,VrFaltantes,VrAnticipos,VrNeto,VrAplicado,Cantidad,CantPuntos,PuntosAcum,BaseImp,BaseRet,BaseIca,BaseRiv
,F.TarifaIva AS TarifIva,F.TarifaRet AS TarifRet,F.TarifaIca AS TarifIca,TarifaRiv,F.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,F.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,F.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom
,DirEnvio,F.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,DiasEntraga
,F.NitContac AS FacNitContac,F.NomContac AS FacNomContac,F.TelContac AS FacTelContac,F.emlContac AS FacEmailContac,CargoContac,F.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,NitEmpTrans,EmpTrans
,F.pVehiculo AS PlacaVeh,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta,TipPed,Pedido,IdCiaPed,TipRem,F.Remision AS NumRemision,F.IdCiaRem AS CdCiaRem,TipCot,F.Cotizacion AS NumCotizacion,F.IdCiaCot AS CiaCotiza,FecPedido
,AutzaMora,AutzaCupo,Modalidad,KmtVehic,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,F.Observacion AS Observ,ZonaFrontera,F.IdEstado AS CdEstado,Estado,F.TimeSys AS Fec_Add,F.FecUpdate AS Fec_Update,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,CdAgencia,KA.Agencia AS KarAgencia,KA.CodAgencia AS KarCodAgencia,KA.NContrato AS KarAgeContrato,KA.Referencia AS KarAgeRefencia,CdLocal,KL.Localidad AS KarCiudad
,CdCCosto,KC.CCosto AS Kar_Ccosto,K.CdSubCos AS CodSubCos,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,K.Cotizacion AS KarNumCotiza,K.IdCiaCot AS KarCiaCotiza,K.Remision AS KarRemision,K.IdCiaRem AS KarCiaRem
,K.Factura AS KarFactura,TipDocDev,NumDocDev,K.IdVend AS KarNitVend,KV.RazonSocial AS KarVendedor,K.Comision AS KarTarifaCom,CdOperario,KO.RazonSocial AS NomOperario,ComisnOper,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
,CdMngra,NumInicial,NumFinal
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
--campos de memo
,DM.Comentarios AS Comentario,Nota1,Nota2,Nota3
 FROM Trn_Facturas AS F  INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON F.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON F.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON F.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON F.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON F.IdForma=FP.IdForma
 INNER JOIN CentroCosto AS CC ON F.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON F.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON F.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON F.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON F.CdRuta=R.IdRuta
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos 
 LEFT JOIN Agencias  AS KA ON K.CdAgencia=KA.IdAgencia LEFT JOIN Localidades AS KL ON K.CdLocal=KL.IdLocal
 LEFT JOIN Terceros AS KV ON K.IdVend=KV.IdTercero  LEFT JOIN Terceros AS KO ON K.CdOperario=KO.IdTercero
 LEFT JOIN TiposCom AS TCM ON F.TipCom=TCM.IdCom
 LEFT JOIN Trn_DocMemo AS DM ON F.TipDoc=DM.TipDoc AND F.Factura=DM.Documento AND F.IdCia=DM.IdCia
 WHERE F.TipDoc=@pmTipDoc AND F.Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND F.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY F.Factura,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFcCausacion @pmTipDoc VARCHAR(3),@pmNumDoc INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumDoc,IdCia,Fecha,FecCausac,IdConcepto,VrNeto,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FcCausacion
 WHERE TipDoc=@pmTipDoc AND NumDoc=@pmNumDoc AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFcCausacionFmt @pmTipDoc VARCHAR(3),@pmNumDocIni INT,@pmNumDocFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  NumDoc,C.IdCia AS CdCia,Compania,C.Fecha AS FecDoc,FecCausac,C.IdConcepto AS CdConcepto,Concepto,VrNeto,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,OrigenAdd,C.TimeSys AS FechaCrea,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario,TipDoc
--DETALLES
,F.IdVehiculo AS PlacaVeh,NumVeh,nSemana,TipoSem,F.Fecha AS FecSemana,FecVence,VrCargo,VrOtros,VrPagado,TipRec,Recibo,IdCiaRec,FechaPago,FecSemIni,FecSemFin
--datos del vehículo
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,Modelo,IdPropietario,T.RazonSocial AS Propietario
,IdPoseedor,P.RazonSocial AS Poseedor,IdConductor,CD.RazonSocial AS Conductor,TipoAfil,FecIngreso,V.FechaAdd AS VehFecCrea,V.Inactivo AS VehInactivo
 FROM Trn_FcCausacion AS C INNER JOIN Trn_FcSemanas AS F ON C.NumDoc=F.NumCargue AND C.IdCia=F.CiaCargue
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Conceptos AS CN ON C.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Vehiculos AS V ON F.IdVehiculo=V.IdVehiculo
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN Terceros AS T ON V.IdPropietario=T.IdTercero INNER JOIN Terceros AS P ON V.IdPoseedor=P.IdTercero
 INNER JOIN Terceros AS CD ON V.IdConductor=CD.IdTercero
 WHERE C.TipDoc=@pmTipDoc AND C.NumDoc BETWEEN @pmNumDocIni AND @pmNumDocFin AND  C.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFcNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdVehiculo,IdPropietario,IdConductor,VehPropio,VrNota,VrNeto,VrAplicado,CdCCosto,CdSubCos
,Referencia,TipRec,ReciboFc,IdCiaRec,TipCom,Comprobante,IdCiaCom,Anulado,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FcNotas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFcNotasFmt @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  NumNota,N.IdCia AS CdCia,Compania,N.Fecha AS FecNota,N.IdConcepto AS CdConcepto,Concepto,N.IdVehiculo AS PlacaVeh,NumVeh,N.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,N.IdConductor AS CedConductor,CDT.RazonSocial AS Conductor
,VehPropio,VrNota,VrNeto,VrAplicado,CdCCosto,CCosto,CdSubCos,SubCosto,Referencia,N.TipRec AS TipRecFc,ReciboFc,IdCiaRec,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,FecDev,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario,TipDoc
--datos detalle
,Item,nSemana,VrAbono,A.Descripcion AS DetallePago
--datos del vehículo
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,Modelo,TipoAfil,FecIngreso,V.Inactivo AS VehInactivo
--tablas
 FROM Trn_FcNotas AS N INNER JOIN Companias AS CI ON N.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
  INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON N.IdPropietario=T.IdTercero
  INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
  INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo 
  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  LEFT JOIN Terceros AS CDT ON N.IdConductor=CDT.IdTercero LEFT JOIN CentroCosto AS CC ON N.CdCCosto=CC.IdCCosto
  LEFT JOIN  SubCentros AS SC ON N.CdSubCos=SC.IdSubCos LEFT JOIN TiposCom AS TCM ON N.TipCom=TCM.IdCom
  LEFT JOIN Trn_FcAbonos AS A ON N.TipDoc=A.TipRec  AND N.NumNota=A.Recibo AND N.IdCia=A.IdCia
  WHERE N.TipDoc=@pmTipDoc AND N.NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
  ORDER BY NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFcNotasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
 AS 
 SELECT TipDoc,NumNota,N.IdCia AS CdCia,Compania,N.Fecha AS FecNota,N.IdConcepto AS CdConcepto,Concepto,N.IdVehiculo AS PlacaVeh,NumVeh,N.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,N.IdConductor AS CedConductor,CDT.RazonSocial AS Conductor
,VehPropio,VrNota,VrNeto,VrAplicado,CdCCosto,CCosto,CdSubCos,SubCosto,Referencia,N.TipRec AS TipRecFc,ReciboFc,IdCiaRec,TipCom,TipoCom,Comprobante,IdCiaCom
,Anulado,FecDev,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
--datos del vehículo
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,Modelo,TipoAfil,FecIngreso,V.Inactivo AS VehInactivo
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
--tablas
 FROM Trn_FcNotas AS N INNER JOIN Companias AS CI ON N.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
  INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto  INNER JOIN Terceros AS T ON N.IdPropietario=T.IdTercero
  INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
  INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo 
  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
  LEFT JOIN Terceros AS CDT ON N.IdConductor=CDT.IdTercero LEFT JOIN CentroCosto AS CC ON N.CdCCosto=CC.IdCCosto
  LEFT JOIN  SubCentros AS SC ON N.CdSubCos=SC.IdSubCos LEFT JOIN TiposCom AS TCM ON N.TipCom=TCM.IdCom
  WHERE N.TipDoc=@pmTipDoc AND N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND N.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND N.IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 ORDER BY N.IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFcSemanas @pmIdVehiculo VARCHAR(10),@pmnSemana INT
 AS 
 SELECT IdVehiculo,nSemana,NumCargue,CiaCargue,TipoSem,Fecha,FecVence,VrCargo,VrOtros,VrPagado,TipRec,Recibo,IdCiaRec
,FechaPago,Observacion,FecSemIni,FecSemFin,TimeSys,IdUsuario
 FROM Trn_FcSemanas
 WHERE IdVehiculo=@pmIdVehiculo AND nSemana=@pmnSemana
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFcSemanasLta @pmIdVehiculo VARCHAR(10)=Null,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT F.IdVehiculo AS PlacaVeh,NumVeh,nSemana,NumCargue,CiaCargue,TipoSem,Fecha,FecVence,VrCargo,VrOtros,VrPagado
,TipRec,Recibo,IdCiaRec,FechaPago,FecSemIni,FecSemFin,F.Observacion AS Observ,TimeSys,F.IdUsuario AS CdUsuario,Usuario
--datos del vehículo
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,Modelo,IdPropietario,T.RazonSocial AS Propietario
,IdPoseedor,P.RazonSocial AS Poseedor,IdConductor,CD.RazonSocial AS Conductor,TipoAfil,FecIngreso,V.FechaAdd AS FechaCrea,V.Inactivo AS VehInactivo
 FROM Trn_FcSemanas AS F INNER JOIN Vehiculos AS V ON F.IdVehiculo=V.IdVehiculo
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN Terceros AS T ON V.IdPropietario=T.IdTercero INNER JOIN Terceros AS P ON V.IdPoseedor=P.IdTercero
 INNER JOIN Terceros AS CD ON V.IdConductor=CD.IdTercero
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 WHERE F.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY F.IdVehiculo,nSemana
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFields @pmIdEntidad VARCHAR(30),@pmIdCampo VARCHAR(20)=Null
 AS 
 SELECT IdEntidad,IdCampo,Descripcion,TipoDato,LongMax,Requerido,PmryKey,FmtValue,DftValue,AliasFld,AliasQry2,IndField
,TipoEntidad,LstTipo,LstClnaValor,LstClnaLista,LstQuery,Inactivo
 FROM Sys_Fields
 WHERE IdEntidad=@pmIdEntidad AND IdCampo LIKE ISNULL(@pmIdCampo,'%')
 ORDER BY IndField
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFieldsLta @pmIdEntidad VARCHAR(30),@pmTipoEntidad VARCHAR(5)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdEntidad,IdCampo,Descripcion,TipoDato,LongMax,Requerido,PmryKey,FmtValue,DftValue,AliasFld,AliasQry2,IndField
,TipoEntidad,LstTipo,LstClnaValor,LstClnaLista,LstQuery,Inactivo
 FROM Sys_Fields
 WHERE IdEntidad=@pmIdEntidad AND TipoEntidad LIKE ISNULL(@pmTipoEntidad,'%') 
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IndField
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos @pmIdCargo VARCHAR(4)
 AS 
 SELECT IdCargo,Concepto,Obligatorio,TipoAplica,DiasSemana,DiasMes,ValorMensual,ValorDiario
,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp,NCuotas,DiasVcmto,OrdenAbono,TipoDcto,DctoPpago,VrCargoAdic
,FecLimiteIni,FecLimiteFin,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Fijos
 WHERE IdCargo=@pmIdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijosDso
AS
  SELECT     IdCargo, Concepto,Concepto +  '  ' + IdCargo AS DsCar  FROM  Fijos WHERE Inactivo=0 ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijosLta  @pmInactivo BIT=Null
AS
  SELECT  IdCargo, Concepto,Obligatorio,TipoAplica,DiasSemana, DiasMes, ValorMensual, ValorDiario
,IdCuentaDeb, IdCuentaCre, IdCueDebVp, IdCueCreVp,NCuotas,DiasVcmto,OrdenAbono,TipoDcto,DctoPpago,VrCargoAdic
,FecLimiteIni,FecLimiteFin,Inactivo, FechaAdd, FechaUpdate, IdUsuario
 FROM Fijos 
 WHERE  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_Cla @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdCargo,IdClase,ValorMensual,ValorDiario
 FROM Fijos_Cla
 WHERE IdCargo=@pmIdCargo AND IdClase LIKE ISNULL(@pmIdClase,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_ClaDso @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT C.IdCargo AS CdCargo,Concepto,IdClase,Descripcion,C.ValorMensual AS ValorMes,C.ValorDiario AS ValorDia
 FROM Fijos_Cla AS C  INNER JOIN Fijos AS F ON C.IdCargo=F.IdCargo
 INNER JOIN TarifBuses AS TB ON C.IdClase=TB.IdTarifa
 WHERE C.IdCargo=@pmIdCargo AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_ClaLta @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT C.IdCargo AS CdCargo,Concepto,IdClase,Descripcion,C.ValorMensual AS ValorMes,C.ValorDiario AS ValorDia
 FROM Fijos_Cla AS C  INNER JOIN Fijos AS F ON C.IdCargo=F.IdCargo
 INNER JOIN TarifBuses AS TB ON C.IdClase=TB.IdTarifa
 WHERE C.IdCargo like ISNULL(@pmIdCargo,'%') AND IdClase like isnull(@pmIdClase,'%')
 ORDER BY Concepto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_ClaTar @pmIdCargo VARCHAR(4)
 AS 
 SELECT IdCargo,IdClase,Descripcion,ValorMensual,ValorDiario
 FROM Fijos_Cla AS C INNER JOIN TarifBuses AS TB ON C.IdClase=TB.IdTarifa
 WHERE IdCargo=@pmIdCargo
 ORDER BY IdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_Cue @pmIdCargo VARCHAR(4),@pmIdAdmon VARCHAR(4)
 AS 
 SELECT IdCargo,IdAdmon,IdCuentaDeb,IdCuentaCre,IdCueDebVp,IdCueCreVp
 FROM Fijos_Cue
 WHERE IdCargo=@pmIdCargo AND IdAdmon LIKE ISNULL(@pmIdAdmon,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_CueDso @pmIdCargo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS 
 SELECT FC.IdAdmon AS IdAdmn,TipoAdmon, FC.IdCuentaDeb AS IdCuentDeb, FC.IdCuentaCre AS IdCuentCre, FC.IdCueDebVp AS IdCueDbVp
, FC.IdCueCreVp AS IdCueCrVp,FC.IdCargo AS CodCargo,Concepto 
 FROM Fijos_Cue  AS FC  INNER JOIN TiposAdm AS TA ON FC.IdAdmon=TA.IdAdmon
  INNER JOIN Fijos AS F ON FC. IdCargo=F.IdCargo
 WHERE FC.IdCargo like ISNULL(@pmIdCargo,'%') AND FC.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY FC.IdAdmon,FC.IdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryFijos_Cue_Cr @pmIdCargo VARCHAR(4)=Null, @pmIdAdmon VARCHAR(4)=Null,@pmInactivo BIT=Null
AS
 SELECT  FC.IdAdmon AS IdAdmn,TipoAdmon, FC.IdCuentaDeb AS IdCuentDeb,P.NomCuenta AS NomCueDb, FC.IdCuentaCre AS IdCuentCre,PC.NomCuenta AS NomCueCr, FC.IdCueDebVp AS IdCueDbVp,FC.IdCueCreVp AS IdCueCrVp
,FC.IdCargo AS CodCargo,Concepto ,TipoAplica,DiasSemana,DiasMes, ValorMensual, ValorDiario,F.Inactivo AS Fij_Inactivo,F.FechaAdd AS FecAdd, F.FechaUpdate AS FecUpdate, F.IdUsuario AS IdUsuari,Usuario
 FROM   Fijos_Cue AS FC  INNER JOIN TiposAdm AS TA ON FC.IdAdmon=TA.IdAdmon
 INNER JOIN Fijos AS F ON FC. IdCargo=F.IdCargo  
 INNER JOIN Puc AS P ON FC.IdCuentaDeb=P.IdCuenta INNER JOIN Puc AS PC ON  FC.IdCuentaCre=PC.IdCuenta
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 WHERE FC.IdCargo like ISNULL(@pmIdCargo,'%')  AND FC.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 AND (F.Inactivo=ISNULL(@pmInactivo,0) or F.Inactivo=ISNULL(@pmInactivo,1))
ORDER BY FC.IdAdmon,FC.IdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_Veh @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10)
 AS 
 SELECT IdCargo,IdVehiculo,ValorMensual,ValorDiario,Excluir
 FROM Fijos_Veh
 WHERE IdCargo=@pmIdCargo AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_VehDso @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10)
 AS 
 SELECT C.IdCargo AS IdCon,Concepto,IdVehiculo,C.ValorMensual AS ValorMes,C.ValorDiario AS ValorDia,Excluir
 FROM Fijos_Veh AS C INNER JOIN Fijos AS F ON C.IdCargo=F.IdCargo
 WHERE C.IdCargo=@pmIdCargo AND IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFijos_VehLta @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10)
AS
  SELECT  F.IdCargo AS IdCon,Concepto,F.IdVehiculo AS PlacaVeh,NumVeh,F.ValorDiario AS ValorDia,F.ValorMensual AS ValorMes, Excluir
  FROM  Fijos_Veh AS F INNER JOIN Fijos ON F.IdCargo=Fijos.IdCargo
  INNER JOIN Vehiculos AS V ON F.IdVehiculo=V.IdVehiculo
  WHERE  F.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND F.IdVehiculo LIKE ISNULL(@pmIdVehiculo ,'%')
  ORDER BY Concepto,F.IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinAbonosFmt @pmTipRec VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT TipRec,Recibo,A.IdCia AS CdCia,Compania,A.Item AS ItemRec,A.Fecha AS FechaRec,A.TipDoc AS Tip_Doc,A.IdPrestamo AS NumPtmo,IdCiaPre,ItemPre
,TotalAbono,VrBaseCms,A.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,A.IdAgencia AS CdAgencia,NomAgencia AS Agencia,A.IdVend AS CdVend,V.RazonSocial AS NomVendedor,Comision
,CantGal, A.Detalle AS DetallePago,TipoAplica,FecPago,TimeSys,TipoDoc
--Datos de la cuota
,NumCuota,Concepto,C.Fecha AS FechaCausa,FechaVence,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,TipoCuota
,NumCausacion,IdCiaCau,C.Detalle AS DetalleCuota,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_Crea
 FROM  Trn_FinAbonos AS A INNER JOIN Trn_FinCuotas AS C ON A.TipDoc=C.TipDoc AND A.IdPrestamo=C.IdPrestamo AND A.IdCiaPre=C.IdCia AND A.ItemPre=C.Item
 INNER JOIN Companias AS CI ON A.IdCia=CI.IdCia INNER JOIN Terceros AS T ON A.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON A.IdVend=V.IdTercero 
 LEFT JOIN TercCliePrestamo AS CLI ON  A.IdCliente=CLI.IdClie AND A.IdAgencia=CLI.IdAgencia
 LEFT JOIN Sys_TiposDoc AS TD ON A.TipRec=TD.IdDoc
 WHERE A.TipRec=@pmTipRec AND A.Recibo BETWEEN @pmReciboIni AND  @pmReciboFin AND A.IdCia=@pmIdCia
 ORDER BY A.Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend,CodLinCred
 FROM Trn_FinAfavor
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinAlmacenes @pmIdAlmacen VARCHAR(4)
 AS 
 IF @pmIdAlmacen IS NULL
  SELECT IdAlmacen,Almacen
  FROM FinAlmacenes
 WHERE Inactivo=0
 ORDER BY IdAlmacen
 ELSE
  SELECT IdAlmacen,Almacen,Inactivo
  FROM FinAlmacenes
  WHERE IdAlmacen=@pmIdAlmacen

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryFinCauMoraFmt @pmTipoProc VARCHAR(3),@pmNumProcIni INT,@pmNumProcFin INT,@pmIdCia CHAR(2)
AS
 SELECT  CM.NumProc AS NumCausac,CM.IdCia AS CdCia,Compania,Fecha,Item,TipDoc, IdPrestamo, IdCiaPre, ItemPre,NCuotaLiq, IdCliente,T.RazonSocial AS NomCliente,CM.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,CM.IdVend AS CdVend,V.RazonSocial AS NomVendedor,FecVence,VrTotal, VrAbonado, FecUltLiq,FecIniLiq, FecFinLiq, DiasLiquida, CM.DiasGracia AS Dias_Gracia
, BaseLiquida, TasaEA, TasaEM, TasaDia, VrLiquida, TarifaIva, VrIvaInt, NumNotaDb, NumComp,C.Observacion AS Observ,CdLinCred,C.Anulado AS Anuldo,C.IdUsuario AS CdUsuario,Usuario,TipoCred
 FROM  Trn_FinCauMora  AS CM INNER JOIN Trn_FinCausacion AS C ON CM.TipoProc=C.TipoProc AND CM.NumProc=C.NumProc  AND CM.IdCia=C.IdCia
 INNER JOIN Companias AS CI ON CM.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON  C.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS T ON CM.IdCliente=T.IdTercero   INNER JOIN Terceros AS V ON CM.IdVend=V.IdTercero
 LEFT JOIN TercCliePrestamo AS CLI ON CM.IdCliente=CLI.IdClie AND CM.IdAgencia=CLI.IdAgencia
 WHERE CM.TipoProc=@pmTipoProc  AND  CM.NumProc BETWEEN @pmNumProcIni AND @pmNumProcFin AND CM.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinCausacionFmt @pmTipoProc VARCHAR(3),@pmNumProcIni INT,@pmNumProcFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT  NumProc,FC.IdCia AS CdCia,Compania,FC.Fecha AS FecCausac,FC.TipCom AS TipComp,FC.Comprobante AS NumComp,FC.Anulado AS Anuldo
 ,FC.Observacion AS CauObserv,C.TipDoc AS Tip_Doc,C.IdPrestamo AS NumPstmo,Item,NumCuota,FechaVence
,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,C.IdCliente AS NitCliente,RazonSocial,C.IdAgencia AS  CdAgencia,NomAgencia AS Agencia
,TipoCuota,Concepto,Detalle,FecUltPago,FecLiqMora,C.FechaCrea AS CuoFecCrea,FC.TimeSys AS CauFechaCrea,FC.IdUsuario AS CdUsuario,Usuario
--datos del prestamo
,FecPrestamo,FecCorte,NPlazos,Causacion,VrPrestamo,NumCredito,P.Observacion AS Observ,TipoCred
 FROM  Trn_FinCausacion AS FC INNER JOIN Trn_FinCuotas AS C ON FC.NumProc=C.NumCausacion AND FC.IdCia=C.IdCiaCau
 INNER JOIN Companias AS CI ON FC.IdCia=CI.IdCia INNER JOIN adm_Usuarios AS U ON FC.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero   
 INNER JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc AND C.IdPrestamo=P.IdPrestamo AND C.IdCia=P.IdCia
 LEFT JOIN TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 WHERE TipoProc=@pmTipoProc AND  NumProc BETWEEN @pmNumProcIni AND  @pmNumProcFin AND FC.IdCia=@pmIdCia
 AND C.TipDoc='PR1'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinClieSaldosLta @pmIdClie VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS
 SELECT  F.IdClie AS NitClie,RazonSocial,CdAgencia,NomAgencia AS  Agencia,F.IdCia AS CdCia,Compania,F.VrSaldo AS SaldoActual
 FROM  FinClieSaldos AS F INNER JOIN  Terceros AS T ON F.IdClie=T.IdTercero
 INNER JOIN TercCliePrestamo AS CLI ON F.IdClie=CLI.IdClie AND F.CdAgencia=CLI.IdAgencia
 INNER JOIN Companias AS CI ON F.IdCia=CI.IdCia
  WHERE F.IdClie LIKE ISNULL(@pmIdClie,'%') AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY RazonSocial,F.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinConcFacturaLta @pmTipoConc VARCHAR(10)=Null
 AS 
 SELECT IdConcepto,Concepto,TipoConc,Gravado,CodTarIva,TIV.Tarifa AS TarifIva,CodTarRet,TR.Tarifa AS TarifRet,CodTarIca,TIC.Tarifa AS TarifIca
,C.IdCuenta AS CdCuenta,NomCuenta,CdCueIva,CdCueRet,CdCueIca,C.Inactivo AS Inactvo,C.FechaAdd AS FechaCrea,C.FechaUpdate AS FechaAct,C.IdUsuario AS CdUsuario,Usuario
 FROM FinConcFactura AS C INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 LEFT JOIN Puc AS P ON C.IdCuenta=P.IdCuenta
 LEFT JOIN Tablapor AS TIV ON C.CodTarIva=TIV.IdTarifa  LEFT JOIN Tablapor AS TR ON C.CodTarRet=TR.IdTarifa
 LEFT JOIN Tablapor AS TIC ON C.CodTarIca=TIC.IdTarifa
 WHERE TipoConc LIKE ISNULL(@pmTipoConc,'%')
 ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinCuotas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2) 
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe
,IdCliente,IdAgencia,TipoCuota,Concepto,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,FechaCrea,CodLinCred,NumFactura
,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio,NumPreMora
 FROM Trn_FinCuotas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinCuotasFmt @pmTipDoc VARCHAR(3),@pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2) 
 AS 
 SELECT  IdPrestamo,C.IdCia AS CdCia,Compania,Item,NumCuota,Fecha,FechaVence,VrTotal,VrAbonado
,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,IdCliente,RazonSocial,C.IdAgencia AS  CdAgencia,NomAgencia AS Agencia,TipoCuota,Concepto
,NumCausacion,IdCiaCau,Detalle,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_crea,CodLinCred,NumFactura
,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,TipDoc,Convenio,NitConvenio,NumPreMora
 FROM Trn_FinCuotas AS C INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero  
 LEFT JOIN TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 WHERE TipDoc=@pmTipDoc AND IdPrestamo BETWEEN @pmIdPrestamoIni AND  @pmIdPrestamoFin AND C.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinCuotasLta @pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT C.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,NomAgencia AS Agencia,C.TipDoc AS CdTipDoc,TipoDoc,C.IdPrestamo AS NumDcmto,C.IdCia AS CdCia,Compania
,Item,VrTotal,VrAbonado,VrTotal-VrAbonado AS ValorSaldo,C.Fecha AS FechaEmision,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
,Concepto,NumCuota,Detalle,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,TipoCuota,NumCausacion,IdCiaCau,FecUltPago,FecLiqMora,C.FechaCrea AS Fecha_Crea,CodLinCred
,NumFactura,VrInteresFact,DiasLiquida,FecUltCausac,VrAcumCapital,Convenio,NitConvenio,NumPreMora
--Datos del prestamo
,FecPrestamo,VrPrestamo,NPlazos,Causacion,P.NContrato AS NumContrato,CdCiaCon,NActaJunta,NumCredito,ISNULL(P.IdVend,CLI.IdVend) AS CdVend,ISNULL(VN.RazonSocial,VNC.RazonSocial) AS Vendedor,P.IdLinea AS CdLinea,LinCredito
--Datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,Comentarios
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinCuotas AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliePrestamo AS CLI ON  C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Terceros AS VNC ON CLI.IdVend=VNC.IdTercero
 INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc  
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_FinPrestamo AS P ON C.TipDoc=P.TipDoc AND C.IdPrestamo=P.IdPrestamo AND C.IdCia=P.IdCia
 LEFT JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE  (VrTotal-VrAbonado)>@pmSaldo AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.IdCliente LIKE ISNULL(@pmIdCliente,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinDevFac @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto
,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,IdVend,IdLocal,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinDevFac
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevFacCr @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia
,NomAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,D.IdLocal AS CdLocal
,LD.Localidad AS CiudadFact,ModdDev,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,TipDev
--cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM  Trn_FinDevFac  AS D  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado  INNER JOIN Localidades AS LD ON D.IdLocal=LD.IdLocal 
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 WHERE  D.TipDev=@pmTipDev  AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY D.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevFacDoc @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia,ModdDev
 FROM Trn_FinDevFac
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura  AND IdCiaDoc=@pmIdCiaDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevFacFmt @pmTipDev VARCHAR(3),@pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,C.Concepto AS DevConcepto,D.TipDoc AS TipoFact,D.Factura AS NumFact,IdCiaDoc,FecDoc
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,NomAgencia,VrSubTotal,VrDescuento,VrImpuesto
,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,D.IdLocal AS CdLocal,CF.Localidad AS NomCiudad
,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,TipDev
,Item,FD.IdConcepto AS CdConc,FD.Concepto AS DescConcepto,TipoReg,Cantidad,VrUnitario,VrTotal,TarifDcto,VrDcto,TarifIva,VrIva,TarifRet,VrRetfte,TarifIca,VrRetica
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinDevFac AS D  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
 INNER JOIN Localidades AS CF ON D.IdLocal=CF.IdLocal 
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 INNER JOIN Trn_FinFactDet AS FD ON D.TipDoc=FD.TipDoc  AND D.Factura=FD.Factura  AND D.IdCiaDoc=FD.IdCia
 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE D.TipDev=@pmTipDev AND D.Devolucion BETWEEN @pmDevolucionIni AND  @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY D.Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevFacLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Factura,IdCiaDoc,FecDoc,IdCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia
,NomAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,D.IdLocal AS CdLocal
,Localidad,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario,TipDev
 FROM  Trn_FinDevFac  AS D  INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON D.IdLocal=L.IdLocal 
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 WHERE  D.TipDev=@pmTipDev  AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY D.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,IdPrestamo,IdCiaDoc,FecDoc,IdCliente,IdAgencia,VrPrestamo,IdVend,CxPagar,ModdDev,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinDevPre
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreCr @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipDocPre,D.IdPrestamo AS NumPrestamo,IdCiaDoc,FecDoc,FecPrestamo,FecCorte
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,NomAgencia AS Agencia,D.VrPrestamo AS ValorDev,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,NumCuotaIni,AcumCapital,AcumCuotas
,NPlazos,TipoPlazo,Causacion,NActaJunta,NumCredito,P.NContrato AS NumContrato,CdCiaCon,D.CxPagar AS Cx_pagar,ModdDev,D.TipCom AS Tip_Comp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito,TipoCredito
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.OrigenAdd AS Origen_Crea,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge  AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinDevPre AS D INNER JOIN Trn_FinPrestamo AS P ON D.TipDoc=P.TipDoc AND  D.IdPrestamo=P.IdPrestamo  AND D.IdCiaDoc=P.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom 
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE TipDev=@pmTipDev   AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY  D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreDoc @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,IdPrestamo,IdCiaDoc,FecDoc,IdCliente,ModdDev
 FROM Trn_FinDevPre
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCiaDoc=@pmIdCiaDoc


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreFmt @pmTipDev VARCHAR(3), @pmDevolucionIni INT,@pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipDocPre,D.IdPrestamo AS NumPrestamo,IdCiaDoc,FecDoc,FecPrestamo,FecCorte
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,NomAgencia AS Agencia,D.VrPrestamo AS ValorDev,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int
,TasaEfectiva,P.IdMora AS CdMora,DescMora,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,NPlazos,TipoPlazo,Causacion,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta
,DiaCausac,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito
,NumCredito,D.CxPagar AS Cx_pagar,ModdDev,D.TipCom AS Tip_Comp,TipoCom,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.OrigenAdd AS Origen_Crea,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM  Trn_FinDevPre AS D INNER JOIN Trn_FinPrestamo AS P ON D.TipDoc=P.TipDoc AND  D.IdPrestamo=P.IdPrestamo  AND D.IdCiaDoc=P.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON D.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE D.TipDev=@pmTipDev  AND  Devolucion  BETWEEN @pmDevolucionIni AND @pmDevolucionFin  AND D.IdCia=@pmIdCia
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevPreLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipDocPre,D.IdPrestamo AS NumPrestamo,IdCiaDoc,FecDoc,FecPrestamo,FecCorte
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,D.VrPrestamo AS ValorDev,D.IdVend AS CdVend,VN.RazonSocial AS NomVendedor
,NPlazos,Causacion,NActaJunta,NumCredito,D.CxPagar AS Cx_pagar,ModdDev,D.TipCom AS Tip_Comp,D.Comprobante AS NumComp,D.IdCiaCom AS CdCiaComp
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.OrigenAdd AS Origen_Crea,D.TimeSys AS FechaCrea,D.FecUpdate AS FechaAct,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
 FROM Trn_FinDevPre AS D INNER JOIN Trn_FinPrestamo AS P ON D.TipDoc=P.TipDoc AND  D.IdPrestamo=P.IdPrestamo  AND D.IdCiaDoc=P.IdCia
 INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia  INNER JOIN Conceptos AS C ON D.IdConcepto=C.IdConcepto  
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON D.IdVend=VN.IdTercero
 WHERE TipDev=@pmTipDev   AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND D.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY  D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,IdAgencia,VrRecibido,VrSubTotal,VrRetencion
,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo
,Referencia,NumAutoriza,ModdDev,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinDevRec
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevRecCr @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,T.RazonSocial AS NomCliente
,D.IdAgencia AS CdAgencia,NomAgencia AS Agencia,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv
,D.IdVend AS CdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo,D.Referencia AS Refrencia,NumAutoriza,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinDevRec AS D  INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo  AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom 
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryFinDevRecDoc @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2)
 AS 
 SELECT TipDev,Devolucion,IdCia,Fecha,IdConcepto,TipDoc,Recibo,IdCiaDoc,FecDoc,IdCliente,ModdDev
 FROM Trn_FinDevRec
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCiaDoc=@pmIdCiaDoc


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinDevRecFmt @pmTipDev VARCHAR(3),@pmDevolucionIni INT, @pmDevolucionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.TipDoc AS TipRec,Recibo,IdCiaDoc,FecDoc,FecPago
,D.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,D.IdAgencia AS CdAgencia,NomAgencia AS NomAgencia,VrRecibido,VrSubTotal,VrRetencion
,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,D.IdVend AS CdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo
,D.Referencia AS DevRefrencia,D.NumAutoriza AS NumAutza,ModdDev,TipCom,TipoCom,Comprobante,IdCiaCom,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS CdUsuario,Usuario
--datos de forma de pago
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco
,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2
,CdLocal,LF.Localidad AS CiudadPlaza,P.NumAutoriza AS PagAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM  Trn_FinDevRec AS D  INNER JOIN Trn_Pagos AS P ON D.TipDev=P.TipDoc AND D.Devolucion=P.Documento AND D.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario   INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo AS CLI ON D.IdCliente=CLI.IdClie AND D.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona  INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN TiposCom AS TCM ON D.TipCom=TCM.IdCom   LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE TipDev=@pmTipDev  AND Devolucion BETWEEN @pmDevolucionIni AND @pmDevolucionFin AND D.IdCia=@pmIdCia
 ORDER BY Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryFinDevRecLta @pmTipDev VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Devolucion,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,TipDoc,Recibo,IdCiaDoc,FecDoc,FecPago,IdCliente,T.RazonSocial AS NomCliente,IdAgencia
,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv
,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo,Referencia,NumAutoriza,ModdDev,TipCom,Comprobante,IdCiaCom,D.Observacion AS Observ
,D.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
 FROM Trn_FinDevRec AS D  INNER JOIN Terceros AS T ON D.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS V ON D.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario 
 WHERE TipDev=@pmTipDev  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND D.IdVend LIKE ISNULL(@pmIdVend,'%') 
 ORDER BY D.IdCia,Devolucion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinFactint @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Factura,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos
,VrOtrDcto,VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend
,TarifaCom,CodTarCom,IdLocal,MulPlazos,IdPlazo,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinFactint
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinFactintCr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Factura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,F.IdCliente AS CdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,NomAgencia,FechaVence,VrSubTotal,VrDescuento
,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv
,F.IdCCosto AS CdCCosto,CCosto,F.IdSubCos AS CdSubCos,SubCosto,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,F.IdLocal AS CdLocalFact,FL.Localidad AS CiudadFact,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto
,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,F.FecUpdate AS FechaAct,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,TipDoc
--cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinFactint AS F  INNER JOIN Companias AS CI ON F.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Localidades AS FL ON F.IdLocal=FL.IdLocal 
 INNER JOIN  TercCliePrestamo AS CLI ON F.IdCliente=CLI.IdClie AND F.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN CentroCosto AS CC ON F.IdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos
 LEFT JOIN Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo LEFT JOIN TiposCom AS TC ON F.TipCom=TC.IdCom
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND F.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY F.IdCia,Factura

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinFactintFmt @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  F.Factura AS NumFactura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,C.Concepto AS ConceptoFact,F.IdCliente AS CdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,NomAgencia,FechaVence,VrSubTotal,VrDescuento
,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv
,F.IdCCosto AS CdCCosto,CCosto,F.IdSubCos AS CdSubCos,SubCosto,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,F.IdLocal AS CdLocal,CF.Localidad AS NomCiudad,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto
,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,F.FecUpdate AS FechaAct,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,F.TipDoc AS TipoDoc
--detalles
,Item,FD.IdConcepto AS CdConc,FD.Concepto AS DescConcepto,TipoReg,Cantidad,VrUnitario,VrTotal,TarifDcto,VrDcto,TarifIva,VrIva,TarifRet,VrRetfte,TarifIca,VrRetica
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinFactint AS F  INNER JOIN Companias AS CI ON F.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS CF ON F.IdLocal=CF.IdLocal 
 INNER JOIN  TercCliePrestamo AS CLI ON F.IdCliente=CLI.IdClie AND F.IdAgencia=CLI.IdAgencia
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 INNER JOIN Trn_FinFactDet AS FD ON F.TipDoc=FD.TipDoc  AND F.Factura=FD.Factura  AND F.IdCia=FD.IdCia
 LEFT JOIN TiposCom AS TC ON F.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN CentroCosto AS CC ON F.IdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos
 LEFT JOIN Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
 WHERE F.TipDoc=@pmTipDoc  AND  F.Factura  BETWEEN  @pmFacturaIni AND @pmFacturaFin  AND F.IdCia=@pmIdCia
 ORDER BY  F.Factura

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinFactintLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT  Factura,F.IdCia AS CdCia,Compania,Fecha,F.IdConcepto AS CdConcepto,Concepto,F.IdCliente AS CdCliente,T.RazonSocial AS NomCliente,F.IdAgencia AS CdAgencia,NomAgencia,FechaVence,VrSubTotal,VrDescuento
,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCargos,VrOtrDcto,VrNeto,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv
,F.IdCCosto AS CdCCosto,CCosto,F.IdSubCos AS CdSubCos,SubCosto,F.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,TarifaCom,CodTarCom,F.IdLocal AS CdLocal,Localidad,MulPlazos,F.IdPlazo AS CdPlazo,Plazo,NVmto
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,F.Observacion AS Observ,F.IdEstado AS CdEstado,Estado,TimeSys,F.FecUpdate AS FechaAct,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario,TipDoc
 FROM Trn_FinFactint AS F  INNER JOIN Companias AS CI ON F.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON F.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON F.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON F.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON F.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON F.IdLocal=L.IdLocal 
 INNER JOIN  TercCliePrestamo AS CLI ON F.IdCliente=CLI.IdClie AND F.IdAgencia=CLI.IdAgencia
 LEFT JOIN CentroCosto AS CC ON F.IdCCosto=CC.IdCCosto  LEFT JOIN SubCentros AS SC ON F.IdSubCos=SC.IdSubCos
 LEFT JOIN Plazos AS PZ ON F.IdPlazo=PZ.IdPlazo
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND F.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND F.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY F.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinFacturas @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Item,NumFactura,Fecha,FechaVence,VrFactura,VrInteres,DiasLiq,TasaEfeMes,TasaEfeDia,FechaPago
,FechaLiq,FecInicio,FecFinal,VrAbono,ItemPago,Facturado,TipFac,FacturaInt,CdCiaFac,FecFactura
 FROM Trn_FinFacturas
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdVend,TarifaCom,CodTarCom,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,Modalidad,TipCom,Comprobante,IdCiaCom
,Observacion,Anulado,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinNotas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinNotasCr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  NumNota,N.IdCia AS CdCia,Compania,Fecha,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,N.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,N.IdCCosto AS CdcCosto,CCosto,N.IdSubCos AS CdSubCost,SubCosto,N.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,N.Referencia AS Refrencia,pVehiculo,CdConductor,CD.RazonSocial AS Conductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque
,CdBanco,Banco,MulPlazos,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,Modalidad,TipCom,TipoCom,Comprobante,IdCiaCom,N.Observacion AS Observ,Anulado
,N.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,N.FecUpdate AS FechaAct,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinNotas AS N INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON N.IdCia=CI.IdCia INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado  INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN  Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliePrestamo AS CLI ON N.IdCliente=CLI.IdClie AND N.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TC ON N.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Bancos AS BN ON N.CdBanco=BN.IdBanco LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS CD ON N.CdConductor=CD.IdTercero
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND N.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY N.IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinNotasFmt @pmTipDoc VARCHAR(3),@pmNumNotaIni INT, @pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  NumNota,N.IdCia AS CdCia,Compania,N.Fecha AS FechaNota,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,N.IdAgencia AS CdAgencia,NomAgencia AS NomAgencia
,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,N.IdCCosto AS CdcCosto,CC.CCosto AS NotCenCosto,N.IdSubCos AS CdSubCost,SC.SubCosto AS NotSubCentro,N.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,N.Referencia AS NotReferencia,pVehiculo,CdConductor,TipDcm,N.Documento AS NumDcmto,IdCiaDcm,FecDcm,CdForma,N.NumCheque AS NroCheque,CdBanco,BN.Banco AS NomBanco,MulPlazos
,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,Modalidad,N.TipCom AS TipComp,TipoCom,N.Comprobante AS NumComp,IdCiaCom,N.Observacion AS Observ,Anulado,N.IdEstado AS CdEstado,Estado
,OrigenAdd,N.TimeSys AS FechaCrea,N.FecUpdate AS FechaAct,N.IdCiaCrea  AS CdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
--detalle del comprobante
,Item,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,IdVehiculo,D.IdCCosto AS DetCodCentCost,DC.CCosto AS DetCentCost
,D.IdSubCos AS DetCodSubCentro,DS.SubCosto  AS DetSubCentro,VrBase,TarifaBase,D.TipDoc AS DetTipoDoc,D.Documento AS DetNumDoc,IdCiaDoc,TipFac,Factura,IdCiaFac,ItemFac
,FecVence,D.NumCheque AS DetNumCheque,TipoAplica,NitOtros,Referncia
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,CLI.Comentarios AS ClieComentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinNotas AS N INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto INNER JOIN  Companias AS CI ON N.IdCia=CI.IdCia
 INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero  INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado  INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN  Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo
 INNER JOIN  TercCliePrestamo AS CLI ON N.IdCliente=CLI.IdClie AND N.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona  INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Agencias  AS A ON N.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TC ON N.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Bancos AS BN ON N.CdBanco=BN.IdBanco LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 LEFT JOIN  Trn_ComDetalle AS D ON N.TipCom=D.TipCom AND N.Comprobante=D.Comprobante AND N.IdCiaCom=D.IdCia
 LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta LEFT JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero
 LEFT JOIN CentroCosto AS DC ON D.IdCCosto=DC.IdCCosto  LEFT JOIN SubCentros AS DS ON D.IdSubCos=DS.IdSubCos
 WHERE N.TipDoc=@pmTipDoc  AND NumNota BETWEEN  @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
 ORDER BY NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinNotasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT  NumNota,N.IdCia AS CdCia,Compania,Fecha,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrSancion,VrOtros,VrNeto
,VrAplicado,VrAnticipo,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,N.IdCCosto AS CdcCosto,N.IdSubCos AS CdSubCost,IdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,Modalidad,TipCom,Comprobante,IdCiaCom
,N.Observacion AS Observ,Anulado,N.IdEstado AS CdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
 FROM Trn_FinNotas AS N INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON N.IdCia=CI.IdCia INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
  AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
  ORDER BY N.IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPagosFmt @pmTipDoc VARCHAR(3),@pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,Numero,Fecha,FecPago,VrCuota,EdoForma,CdBanco,Banco,CtaCheque,Detalle,TipRec,Recibo,IdCiaRec,VrLiqMora,FecLiqMora
 FROM Trn_FinPagos AS P LEFT JOIN Bancos AS B ON P.CdBanco=B.IdBanco
 WHERE TipDoc=@pmTipDoc AND IdPrestamo BETWEEN @pmIdPrestamoIni AND @pmIdPrestamoFin AND IdCia=@pmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,IdPrestamo,IdCia,Fecha,FecPrestamo,FecCorte,IdConcepto,IdCliente,IdAgencia,VrPrestamo,IdTasa,NPlazos,Causacion,IdMora,NContrato,CdCiaCon,NActaJunta,NumCredito
,IdVend,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora
,CuotasPagadas,CuotasMora,TipoAprob,TipoCausac,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,DiaCausac ,DiaCausac2,DiasVence,IdLinea,TipoPlazo
,CdTasa2,VrSeguro,VrIntereses,VrNeto,VrCuota,TipoCredito,CalcIntMora,NitEmpConv,CuotasConv,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CdAlmacen,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinPrestamo
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoAcum  @pmIdCia CHAR(2)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmTipoCredito VARCHAR(10)=Null
 AS 
 SELECT T.RazonSocial AS NomCliente,IdCliente,NomAgencia AS Agencia,P.IdAgencia AS CdAgencia,P.IdPrestamo AS NumPrest,P.IdCia AS CdCia,Compania
,NumCredito,P.Fecha AS FecRadica,FecPrestamo,FecCorte,DiaCausac,DiaCausac2,P.DiasVence AS DiasVce,P.TipoAprob AS TipAprob,NActaJunta,VrPrestamo,VrSeguro,VrIntereses,VrNeto,VrCuota,NPlazos,P.TipoPlazo AS TipPlazo,Causacion
,P.IdLinea AS CdLinea,LinCredito,P.IdTasa AS CdTasa,DescTasa,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,NumCuotaIni
,TipoCredito,P.TipoCausac AS Tipo_Causac,P.CalcIntMora AS CalcMora,NitEmpConv,P.CuotasConv AS NCuoConv,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor,P.NContrato AS NumContrato,CdCiaCon
,P.TipoGarantia AS TipoGarant,P.DescGarantia AS Desc_Garant,P.VrGarantia AS ValGarant,P.Observacion AS Observ,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,P.IdConcepto AS CdConcepto,Concepto,P.IdEstado AS CdEstado,Estado
,Anulado,TI.TipoInteres AS Tipo_Int,TasaEfectiva,TipoPeriodo,CausaInteres,DTF_EA,Spread_TA,OpcCompra
--cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,CupoCredito,EsExento,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad
,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto   INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado 
 INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc INNER JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN TercClieLineas AS LCR ON P.IdCliente=LCR.IdClie AND P.IdAgencia=LCR.IdAgencia AND P.IdLinea=LCR.IdLinea
 WHERE  P.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND P.TipoCredito LIKE ISNULL(@pmTipoCredito,'%')  
 ORDER BY P.IdCia,P.IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoCr @pmTipDoc VARCHAR(3), @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdTasa VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdPrestamo,P.IdCia AS CdCia,Compania,Fecha,FecPrestamo,FecCorte,P.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia
,VrPrestamo,VrSeguro,VrIntereses,VrNeto,VrCuota,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int,TasaEfectiva,TipoPeriodo,CausaInteres,DTF_EA,Spread_TA,OpcCompra,NPlazos,Causacion
,P.IdMora AS CdMora,DescMora,TIM.TipoInteres AS TipoIntMora,TasaEA,TasaEM,TIM.DiasGracia AS Dias_Gracia,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta,NumCredito,P.IdVend AS CdVend,VN.RazonSocial AS NomVendedor
,pVehiculo,CdConductor,CD.RazonSocial AS NomConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito,TipoPlazo,CdTasa2
,TipoCredito,P.CalcIntMora AS CaclMora,NitEmpConv,CuotasConv,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CdAlmacen,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario,TipDoc
--cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad
,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado  INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON P.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Terceros AS CD ON P.CdConductor=CD.IdTercero  
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE TipDoc=@pmTipDoc  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.IdTasa LIKE ISNULL(@pmIdTasa,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY P.IdCia,IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoFac @pmTipDoc VARCHAR(3), @pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.IdPrestamo AS NumPtmo,P.IdCia AS CdCia,Compania,P.Fecha AS FechaPtmo,FecPrestamo,FecCorte,P.IdConcepto AS CdConcepto,C.Concepto AS DescConcept,P.IdCliente AS NitCliente
,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int,TasaEfectiva,NPlazos,TipoPlazo,P.Causacion AS TipoCausac,P.IdMora AS CdMora,DescMora
,TIM.TipoInteres AS Tipo_IntMora,TasaEM,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta,NumCredito
,P.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,CdTasa2,VrSeguro,VrIntereses,VrNeto,P.VrCuota AS ValCuota,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito
,TipoCredito,P.CalcIntMora AS CaclMora,NitEmpConv,CuotasConv,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CdAlmacen
,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario
--Datos de facturas
,Item,NumFactura,DF.Fecha AS FechaFact,FechaVence,VrFactura,VrInteres,DiasLiq,TasaEfeMes,TasaEfeDia,FechaPago,FechaLiq,FecInicio,FecFinal,VrAbono,ItemPago,Facturado,TipFac,FacturaInt,CdCiaFac,FecFactura
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN  TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 INNER JOIN Trn_FinFacturas AS DF ON P.TipDoc=DF.TipDoc AND P.IdPrestamo=DF.IdPrestamo AND P.IdCia=DF.IdCia
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON P.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE P.TipDoc=@pmTipDoc  AND P.IdPrestamo BETWEEN @pmIdPrestamoIni AND @pmIdPrestamoFin  AND P.IdCia=@pmIdCia
 ORDER BY  P.IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoFmt @pmTipDoc VARCHAR(3), @pmIdPrestamoIni INT,@pmIdPrestamoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.IdPrestamo AS NumPtmo,P.IdCia AS CdCia,Compania,P.Fecha AS FechaPtmo,FecPrestamo,FecCorte,P.IdConcepto AS CdConcepto,C.Concepto AS DescConcept,P.IdCliente AS NitCliente
,T.RazonSocial AS NomCliente,P.IdAgencia AS CdAgencia,NomAgencia AS Agencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa,TI.TipoInteres AS Tipo_Int,TasaEfectiva,NPlazos,TipoPlazo,P.Causacion AS TipoCausac,P.IdMora AS CdMora,DescMora
,TIM.TipoInteres AS Tipo_IntMora,TasaEM,P.NContrato AS NumContrato,CdCiaCon,TipoAprob,NActaJunta,NumCredito
,P.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia,CxPagar,CdTasa2,VrSeguro,VrIntereses,VrNeto,P.VrCuota AS ValCuota,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CuotasMora,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,P.Observacion AS Observ,DiaCausac ,DiaCausac2,DiasVence,P.IdLinea AS CdLinea,LinCredito
,TipoCredito,P.CalcIntMora AS CaclMora,NitEmpConv,CuotasConv,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CdAlmacen
,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario
--Datos del detalle
,Item,FecCausac,FecVence,NumDias,PC.VrCuota AS TotalCuota,VrCapital,VrInteres,VrSaldo,TasaEfeMes,TasaEfeDia,PC.Causacion AS NumCausac,CdCiaCausac,VrCuoCausac,VrIntCausac,VrSaldoCausac
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia
,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,Comentarios
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN  TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 LEFT JOIN TasasIntmora AS TIM ON P.IdMora=TIM.IdMora LEFT JOIN TiposCom AS TC ON P.TipCom=TC.IdCom
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_FinAmtza AS PC ON P.TipDoc=PC.TipDoc AND P.IdPrestamo=PC.IdPrestamo AND P.IdCia=PC.IdCia
 WHERE P.TipDoc=@pmTipDoc  AND P.IdPrestamo BETWEEN @pmIdPrestamoIni AND @pmIdPrestamoFin  AND P.IdCia=@pmIdCia
 ORDER BY  P.IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinPrestamoLta @pmTipDoc VARCHAR(3), @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdTasa VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdPrestamo,P.IdCia AS CdCia,Compania,Fecha,FecPrestamo,FecCorte,DiaCausac ,DiaCausac2,DiasVence,P.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrPrestamo,P.IdTasa AS CdTasa,DescTasa
,NPlazos,TipoPlazo,Causacion,IdLinea,IdMora,VrSeguro,VrIntereses,VrNeto,VrCuota,NContrato,CdCiaCon,TipoAprob,TipoCausac,NActaJunta,NumCredito,IdVend,VN.RazonSocial AS NomVendedor,pVehiculo,CdConductor,TipoGarantia,VrGarantia,DescGarantia
,CxPagar,NumCuotaIni,AcumCapital,AcumIntereses,AcumIntMora,AcumCuotas,PagosCapital,PagosIntereses,PagosIntMora
,CuotasPagadas,CuotasMora,CdTasa2,TipoCredito,CalcIntMora,NitEmpConv,CuotasConv,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,CdAlmacen,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS Cdusuario,Usuario
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 WHERE TipDoc=@pmTipDoc  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdVend LIKE ISNULL(@pmIdVend,'%') AND P.IdTasa LIKE ISNULL(@pmIdTasa,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY P.IdCia,IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinPrestamoRes @pmSaldo DECIMAL(12,2),@pmTipDoc VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdTasa VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT T.RazonSocial AS NomCliente,IdCliente,NomAgencia AS Agencia,P.IdAgencia AS CdAgencia,P.IdPrestamo AS NumPrest,P.IdCia AS CdCia,Compania
,NumCredito,P.Fecha AS FecRadica,FecPrestamo,DiaCausac,DiaCausac2,DiasVence,TipoAprob,NActaJunta,VrPrestamo,VrSeguro,VrIntereses,VrNeto,VrCuota,NPlazos,TipoPlazo,Causacion,P.IdLinea AS CdLinea,LinCredito,P.IdTasa AS CdTasa,DescTasa
,tmVrCuota AS VrCuotaDTF,tmDescCuota AS DescripCuota,tmVrMora AS VrEnMora,AcumCapital,AcumIntereses,VrPrestamo-AcumCapital AS SaldoCapital,tmDiasMora AS PromDiasMora
,CausaMora,GestionClie,AcuerdosClie,TipoGarantia,DescGarantia,VrGarantia,NumCuotaIni,AcumCuotas,CuotasMora
,PagosCapital,PagosIntereses,PagosIntMora,CuotasPagadas,CdTasa2,TipoCredito,P.CalcIntMora AS CalcMora,NitEmpConv,CuotasConv,P.IdVend AS CdVend,VN.RazonSocial AS NomAsesor,P.NContrato AS NumContrato,CdCiaCon,P.Observacion AS Observ
,Refinanciado,NumPrestRef,CiaPrestRef,VrRefinanc,DescRefinanc,P.IdConcepto AS CdConcepto,Concepto,P.IdEstado AS CdEstado,Estado
,TI.TipoInteres AS Tipo_Int,TasaEfectiva,TipoPeriodo,CausaInteres,DTF_EA,Spread_TA,OpcCompra
--cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,FecIngreso,VrSalBasico,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad
,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinPrestamo AS P  INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto   INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado 
 INNER JOIN TasasInteres AS TI ON P.IdTasa=TI.IdTasa
 INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero   INNER JOIN Terceros AS VN ON P.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliePrestamo AS CLI ON P.IdCliente=CLI.IdClie AND P.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Trn_FinObserv AS PD ON P.TipDoc=PD.TipDoc AND P.IdPrestamo=PD.IdPrestamo AND P.IdCia=PD.IdCia
 LEFT JOIN tm_PreMora AS PM ON P.TipDoc=PM.tmTipDoc AND P.IdPrestamo=PM.tmIdPrestamo AND P.IdCia=PM.tmIdCia
 LEFT JOIN LineasCred AS LC ON P.IdLinea=LC.IdLinea
 WHERE P.TipDoc=@pmTipDoc  AND P.Anulado=0   AND (VrPrestamo-AcumCapital)>=@pmSaldo
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
  AND P.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND P.IdVend LIKE ISNULL(@pmIdVend,'%') 
 AND P.IdTasa LIKE ISNULL(@pmIdTasa,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY P.IdCia,P.IdPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo
,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipoCredito,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinRecibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosCr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null 
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,Fecha,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,NomAgencia AS Agencia,FecPago
,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv
,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,R.IdVend AS CdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma
,DetallePago,CdBanco,Banco,CdCta,R.Referencia AS Refrencia,TipoCredito,pVehiculo,CdConductor,CD.RazonSocial AS Conductor,TipDcm,Documento,IdCiaDcm
,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario,TipDoc
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinRecibos AS R  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo AS CLI ON R.IdCliente=CLI.IdClie AND R.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona   INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom  
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Bancos AS B ON R.CdBanco=B.IdBanco  LEFT JOIN Terceros AS CD ON R.CdConductor=CD.IdTercero
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND R.IdVend LIKE ISNULL(@pmIdVend,'%')  AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY R.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosDoc  @pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipDcm VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo
,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipoCredito,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_FinRecibos
 WHERE Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm AND TipDcm LIKE ISNULL(@pmTipDcm,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosFmt @pmTipDoc VARCHAR(3),@pmReciboIni INT, @pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,R.Fecha AS FecRecibo,R.IdConcepto AS CdConcepto,Concepto,R.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,NomAgencia AS NomAgencia
,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo
,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,R.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,EnEfectivo,CdForma,R.NumForma AS RecNumForma,DetallePago,CdBanco,BR.Banco AS RecBanco,R.CdCta AS RecCodCta,R.Referencia AS RecReferncia,pVehiculo,CdConductor
,TipoCredito,TipDcm,R.Documento AS NumDcmto,IdCiaDcm,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
--datos de forma de pago
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco
,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS PagReferencia2
,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
--agencias
,DirAgencia,CLI.IdLocalAge AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_FinRecibos AS R  INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario   INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  TercCliePrestamo AS CLI ON R.IdCliente=CLI.IdClie AND R.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom  LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE R.TipDoc=@pmTipDoc  AND Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND R.IdCia=@pmIdCia
 ORDER BY  R.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFinRecibosLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT  Recibo,R.IdCia AS CdCia,Compania,Fecha,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrRetencion,VrReteICA,VrReteIVA
,VrOtros,VrOtrDcto,VrPagosMas,VrNeto,VrAplicado,VrEfectivo,VrAnticipo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco
,CdCta,Referencia,pVehiculo,CdConductor,TipoCredito,TipDcm,Documento,IdCiaDcm,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado
,OrigenAdd,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
 FROM Trn_FinRecibos AS R  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') 
 ORDER BY R.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFmtDoc @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2)
 AS 
 SELECT IdDoc,IdForma,Descripcion,Formato,Predtmnado,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location
 FROM Sys_FmtDoc
 WHERE IdDoc LIKE ISNULL(@pmIdDoc,'%')  AND IdForma LIKE ISNULL(@pmIdForma,'%%')
 ORDER BY IdDoc,IdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFmtDocDft @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2)=Null,@pmDescripcion VARCHAR(100)=Null
AS 
 IF @pmIdForma IS NULL And @pmDescripcion IS NULL
  SELECT IdDoc,IdForma,Descripcion,Formato,Predtmnado,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location
  FROM Sys_FmtDoc
  WHERE IdDoc=@pmIdDoc AND Predtmnado<>0
  ORDER BY IdForma
 ELSE
  SELECT IdDoc,IdForma,Descripcion,Formato,Predtmnado,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location
  FROM Sys_FmtDoc
  WHERE IdDoc=@pmIdDoc AND IdForma LIKE ISNULL(@pmIdForma,'%%') AND Descripcion LIKE ISNULL(@pmDescripcion,'%')
  ORDER BY IdForma

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFmtDocDso @pmIdDoc VARCHAR(3)
 AS
  SELECT IdDoc,IdForma,Descripcion,Formato,IdForma + ' '+Descripcion AS DsFor
  FROM Sys_FmtDoc
  WHERE IdDoc=@pmIdDoc
 ORDER BY IdForma

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFmtFactura @pmnFormato VARCHAR(10)
 AS 
 SELECT nFormato,IdDoc,CdForma
 FROM Sys_FmtFactura
 WHERE nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFmtFacturaLta @pmTipFac VARCHAR(3)=Null
 AS 
  SELECT FF.nFormato AS IdFmto,FmtoFactura,IdDoc,CdForma
  FROM Sys_FmtFactura AS FF INNER JOIN Sys_TFmtos AS TF ON FF.nFormato=TF.nFormato
  WHERE TipFac LIKE ISNULL(@pmTipFac,'%')
  ORDER BY FF.nFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFondos @pmIdFondo VARCHAR(8)
 AS 
  SELECT IdFondo,Fondo,IdClase,IdTercero,CodigoEps,CodigoPen,CodigoArp,Inactivo
  FROM Fondos
  WHERE IdFondo=@pmIdFondo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFondosCla @pmIdClase VARCHAR(4)
 AS 
  SELECT IdFondo,Fondo,IdClase,IdTercero,CodigoEps,CodigoPen,CodigoArp,Inactivo
  FROM Fondos
  WHERE IdClase=@pmIdClase
  ORDER BY IdFondo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFondosDso @pmIdClase VARCHAR(4)
 AS
 IF @pmIdClase IS NULL
  SELECT IdFondo,Fondo,IdTercero  FROM Fondos  WHERE Inactivo=0  ORDER BY Fondo
 ELSE
  SELECT IdFondo,Fondo,IdTercero  FROM Fondos
  WHERE Inactivo=0 AND (IdClase=@pmIdClase OR IdClase='0')
  ORDER BY Fondo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFondosLta @pmIdClase VARCHAR(4)=Null,@pmInactivo BIT=Null
 AS
  SELECT IdFondo,Fondo,F.IdClase AS CodClase,ClaseFondo,IdTercero,CodigoEps,CodigoPen,CodigoArp,F.Inactivo AS Inctivo
  FROM Fondos AS F INNER JOIN ClaseFon AS C ON F.IdClase=C.IdClase
  WHERE F.IdClase LIKE ISNULL(@pmIdClase,'%') 
  AND (F.Inactivo=ISNULL(@pmInactivo,0) or F.Inactivo=ISNULL(@pmInactivo,1))
  ORDER BY Fondo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFondosPen @pmIdClase VARCHAR(4)
 AS
  SELECT IdFondo,Fondo,IdTercero  FROM Fondos
  WHERE Inactivo=0 AND (IdClase=@pmIdClase OR IdClase='0' OR IdClase='0008')
  ORDER BY Fondo


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFormasCue @pmIdForma VARCHAR(4),@pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdForma,IdConcepto,IdCuenta,FechaAdd,FechaUpdate
 FROM FormasCue
 WHERE IdConcepto=@pmIdConcepto AND IdForma LIKE ISNULL(@pmIdForma,'%')
 ORDER BY IdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFormasCueLta @pmIdForma VARCHAR(4),@pmIdConcepto VARCHAR(4)
 AS 
 SELECT FC.IdForma AS CdForma,FormaPago,C.IdDoc AS CdTipo,TipoDoc,FC.IdConcepto AS CdConcep,Concepto
,FC.IdCuenta AS CdCuenta,NomCuenta
,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos
,FC.FechaAdd AS Fec_Add,FC.FechaUpdate AS Fec_Upd
 FROM FormasCue AS FC INNER JOIN Formaspago AS F ON FC.IdForma=F.IdForma
 INNER JOIN Conceptos AS C ON FC.IdConcepto=C.IdConcepto
 INNER JOIN Puc AS P ON FC.IdCuenta=P.IdCuenta
 INNER JOIN  Sys_TiposDoc AS T ON C.IdDoc=T.IdDoc
 WHERE FC.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND FC.IdForma LIKE ISNULL(@pmIdForma,'%')
 ORDER BY FC.IdForma,C.IdDoc,FC.IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFormaspago @pmIdForma VARCHAR(4)
 AS 
 IF @pmIdForma IS NULL
  SELECT IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos
  FROM Formaspago
  WHERE Inactivo=0
  ORDER BY Consecutivo
 ELSE
  SELECT IdForma,FormaPago,Consecutivo,CuadreCaja,RefDcmto,RefNit,RefBco,RefFech,RefAtza,RefOtro,VldRangos,Inactivo
  FROM Formaspago
  WHERE IdForma=@pmIdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFormaspagoChe 
 AS 
  SELECT IdForma,FormaPago,Consecutivo
  FROM Formaspago
  WHERE (IdForma IN ('CHE','BON','CON','TRA') OR  IdForma LIKE 'CH%')  AND Inactivo=0 
  ORDER BY Consecutivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryFormaspagoID
 AS 
  SELECT MAX(Consecutivo) AS MXCON
  FROM Formaspago
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryFuentesPago @pmIdFuente VARCHAR(4)
 AS 
 IF @pmIdFuente IS NULL
  SELECT IdFuente,FuentePago 
  FROM FuentesPago
  WHERE Inactivo=0
  ORDER BY IdFuente
 ELSE
  SELECT IdFuente,FuentePago,Inactivo 
  FROM FuentesPago
  WHERE IdFuente=@pmIdFuente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGrupos @pmIdGrupo VARCHAR(8)
 AS 
 SELECT IdGrupo,Grupo,IdLinea,TipoInvent,Inactivo
 FROM Grupos
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposAR @pmIdGrupo VARCHAR(4)
 AS 
 IF @pmIdGrupo IS NULL
  SELECT IdGrupo,AltoRiesgo,TarfaTrab,TarfaEmp
  FROM GruposAR
  WHERE Inactivo=0
  ORDER BY IdGrupo
 ELSE
  SELECT IdGrupo,AltoRiesgo,TarfaTrab,TarfaEmp,Inactivo
  FROM GruposAR
  WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryGruposAgenc @pmIdGrupo VARCHAR(4)
 AS 
 IF @pmIdGrupo IS NULL
  SELECT IdGrupo,GrupoAgenc
  FROM GruposAgenc
  WHERE Inactivo=0
 ORDER BY IdGrupo
 ELSE
 SELECT IdGrupo,GrupoAgenc,Inactivo
 FROM GruposAgenc
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposCli @pmIdGrupo VARCHAR(4)
 AS 
 SELECT IdGrupo,GrupoClie,ModApp,Inactivo
 FROM GruposCli
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposCliLta @pmModApp VARCHAR(10)=Null
 AS 
 SELECT IdGrupo,GrupoClie,ModApp
 FROM GruposCli
 WHERE Inactivo=0 AND ModApp like ISNULL(@pmModApp,'%')
 ORDER BY IdGrupo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposDso  @pmIdLinea VARCHAR(8)=Null,@pmTipoInvent VARCHAR(10)=Null
 AS 
 SELECT IdGrupo,Grupo,Grupo+' '+IdGrupo + ' ('+Linea+')'  AS DsGru
 FROM Grupos AS G INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 WHERE G.Inactivo=0 AND  G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND G.TipoInvent LIKE ISNULL(@pmTipoInvent,'%')
 ORDER BY Linea,Grupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposLin  @pmIdGrupo VARCHAR(8)=Null
 AS 
 SELECT IdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,G.Inactivo AS Inactiv,G.TipoInvent AS TipoInv
 FROM Grupos AS G INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 WHERE IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY Linea,Grupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposLta  @pmIdLinea VARCHAR(8)=Null,@pmTipoInvent VARCHAR(10)=Null
 AS 
 SELECT IdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,G.TipoInvent AS TipoInventario
 FROM Grupos AS G INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 WHERE G.Inactivo=0 AND  G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND G.TipoInvent LIKE ISNULL(@pmTipoInvent,'%')
 ORDER BY Linea,Grupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryGruposMcia @pmIdGrupo VARCHAR(10)
 AS 
 IF @pmIdGrupo IS NULL
  SELECT IdGrupo,GrupoMcia
  FROM GruposMcia
  WHERE Inactivo=0
 ORDER BY GrupoMcia
 ELSE
  SELECT IdGrupo,GrupoMcia,Inactivo
  FROM GruposMcia
  WHERE IdGrupo=@pmIdGrupo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposObj @pmIdGrupo VARCHAR(6),@pmnModulo VARCHAR(10)=Null
 AS 
 SELECT IdGrupo,GrupoObjeto,nModulo
 FROM Sys_GruposObj
 WHERE IdGrupo LIKE ISNULL(@pmIdGrupo,'%')  AND nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY nModulo,IdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposPro @pmIdGrupo VARCHAR(4)
 AS 
 IF @pmIdGrupo IS NULL
 SELECT IdGrupo,GrupoProp
 FROM GruposPro
 WHERE Inactivo=0 
 ORDER BY GrupoProp
 ELSE
 SELECT IdGrupo,GrupoProp,Inactivo
 FROM GruposPro
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposRot @pmIdGrupo VARCHAR(4)
 AS 
 IF @pmIdGrupo IS NULL
 SELECT IdGrupo,GrupoRot
 FROM GruposRot
 WHERE Inactivo=0
 ORDER BY GrupoRot
 ELSE
 SELECT IdGrupo,GrupoRot,Inactivo
 FROM GruposRot
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposUsu @pmIdGrupo INT
 AS 
 SELECT IdGrupo,Grupo,Descripcion
 FROM Sys_GruposUsu
 WHERE (IdGrupo>=ISNULL(@pmIdGrupo,0)  AND IdGrupo<=ISNULL(@pmIdGrupo,2147483647)) 
 ORDER BY IdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGruposUsuDso @pmIdGrupo INT
 AS 
 SELECT IdGrupo,Grupo,Descripcion,STR(IdGrupo,2,0)+'  '+Grupo AS DsGru
 FROM Sys_GruposUsu
 WHERE  IdGrupo<=@pmIdGrupo
 ORDER BY IdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuia @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Guia,IdCia,Fecha,FechaDesp,FechaVence,IdCliente,IdAgencia,Cantidad,DirEnvio,IdLocal,LugarEnv,HorasVig
,IdVehiculo,IdConductor,NitEmpTrans,EmpTrans,CdRuta,CdCiuOrigen,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,Factura,IdCiaFac,FechaFact
,OrigenAdd,ZonaFrontera,TipoTemp,Temperatura,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Guia
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaDet @pmGuia INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,K.IdProducto AS CdProducto,Descripcion,CdTanque,Salidas,VrPrecio,Salidas*VrPrecio AS VrSubTotal,Comptmntos
,VrBruto,K.ListaPrec AS LtaPrec,VrBase,K.Referencia AS Referncia,Unidades,Referencia2,K.IdVend AS NitVend,V.RazonSocial AS Vendedor,Comision,K.CodTarCom AS CdTarifCom
,DescripProd,Servcios,Tanques,K.IdBodega AS CdBodega,Bodega
,G.Fecha AS FecGuia,FechaDesp,FechaVence,IdCliente,T.RazonSocial AS Cliente,IdAgencia,DirEnvio,LugarEnv,HorasVig
,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor,NitEmpTrans,EmpTrans,Referncia,TipOcc,OCargue,IdCiaOcc
,G.Factura AS NumFactura,IdCiaFac,FechaFact,G.Observacion AS Observ
 FROM Trn_Guia AS G  INNER JOIN Trn_Kardex AS K ON G.TipDoc=K.TipDoc AND G.Guia=K.Documento AND G.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN Terceros AS V ON K.IdVend=V.IdTercero
 INNER JOIN Terceros AS T ON G.IdCliente=T.IdTercero
 INNER JOIN Terceros AS CD ON G.IdConductor=CD.IdTercero
 WHERE Guia=@pmGuia AND G.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaFac @pmFactura  INT,@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT Guia,IdCia,Fecha,T.RazonSocial AS NomCliente,IdCliente,IdAgencia,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor
,Cantidad,DirEnvio,G.IdLocal AS CdCiudad,LugarEnv,FechaDesp,FechaVence,HorasVig
,NitEmpTrans,EmpTrans,CdRuta,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,Factura,IdCiaFac,FechaFact
,G.Observacion AS Observ,G.IdEstado AS CdEstado,Estado,TimeSys,IdCiaCrea
 FROM Trn_Guia AS G  INNER JOIN Terceros AS T ON G.IdCliente=T.IdTercero
 INNER JOIN Terceros AS CD ON G.IdConductor=CD.IdTercero
 INNER JOIN EstadoDoc AS ED ON G.IdEstado=ED.IdEstado
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND Factura<=@pmFactura
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND G.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY IdCia,Guia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaFact @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmGuiaIni INT=Null,@pmGuiaFin INT=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmCdCiuOrigen VARCHAR(8)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaFacIni SMALLDATETIME=Null,@pmFechaFacFin SMALLDATETIME=Null
 AS 
 SELECT Guia,O.IdCia AS CdCia,Compania,O.Fecha AS FechaGuia,FechaDesp,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantOrden,SUM(Unidades) AS Unidades
--datos de factura
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,NumPedido,CdCiaPedido,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas
,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades,FacTarifSob,FacTarifSol,FacTarifIglobal
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia
,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,HorasVig,IdVehiculo,CdCiuOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CdDepOrigen,DO.Departamento AS DptoOrigen
,Referncia,TipOcc,OCargue,IdCiaOcc,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
 FROM  Trn_Guia AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero  INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Guia=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Localidades AS LO ON O.CdCiuOrigen=LO.IdLocal  LEFT JOIN Departamentos AS DO ON LO.IdDep=DO.IdDep 
 LEFT JOIN (SELECT Remision,IdCiaRem,TipOrd,NumOrden,IdCiaOrd,K.TipDoc AS TipoFact,K.Documento AS NumFactura,K.IdCia AS CdCiaFac,KC.Compania AS NomCiaFac
,K.Fecha AS FecFactura,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd,K.Cotizacion AS NumPedido,K.IdCiaCot  AS CdCiaPedido
,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaEnt) AS IvaEntradas
,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoEnt) AS DctoEntradas,SUM(VrDctoSal) AS DctoSalidas,SUM(K.Unidades) AS TotalUnidades
,MAX(Sobretasa) AS FacTarifSob,MAX(Soldicom) AS FacTarifSol,MAX(ImpGlobal) AS FacTarifIglobal
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE NoVentas BETWEEN 1 AND 4  AND Remision>0 AND TipOrd='OCC'
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaFacIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFacFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY Remision,IdCiaRem,TipOrd,NumOrden,IdCiaOrd,K.TipDoc,K.Documento,K.IdCia,KC.Compania,K.Fecha,K.IdProducto
,PM.DescripProd,K.Cotizacion,K.IdCiaCot) AS FA ON  O.Guia=FA.Remision AND O.IdCia=FA.IdCiaRem AND K.IdProducto=FA.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%') AND CdCiuOrigen LIKE ISNULL(@pmCdCiuOrigen,'%')
 AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND Guia  BETWEEN ISNULL(@pmGuiaIni,0) AND ISNULL(@pmGuiaFin,2147483647)
 GROUP BY Guia,O.IdCia,Compania,O.Fecha,FechaDesp,K.IdProducto,DescripProd
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,NumPedido,CdCiaPedido,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas
,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades,FacTarifSob,FacTarifSol,FacTarifIglobal
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,O.IdLocal,LE.Localidad
,LE.IdDep,DE.Departamento,LugarEnv,HorasVig,IdVehiculo,CdCiuOrigen,LO.Localidad,LO.IdDep,DO.Departamento
,Referncia,TipOcc,OCargue,IdCiaOcc,O.Observacion,O.IdEstado,Estado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaLta  @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmGuiaIni INT=Null,@pmGuiaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Guia,IdCia,Fecha,FechaDesp,FechaVence,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,Cantidad,DirEnvio,G.IdLocal AS CdCiudad,LugarEnv,HorasVig
,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor,NitEmpTrans,EmpTrans,CdRuta,CdCiuOrigen,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,Factura,IdCiaFac,FechaFact
,OrigenAdd,ZonaFrontera,TipoTemp,Temperatura,Anulado,FecDev,G.Observacion AS Observ,G.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,G.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Guia AS G  INNER JOIN Terceros AS T ON G.IdCliente=T.IdTercero
 INNER JOIN Terceros AS CD ON G.IdConductor=CD.IdTercero
 INNER JOIN EstadoDoc AS ED ON G.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON G.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Guia BETWEEN ISNULL(@pmGuiaIni,0) AND ISNULL(@pmGuiaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND G.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Guia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaPed @pmOCargue INT,@pmIdCiaOcc CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT Guia,G.IdCia AS CodCia,G.Fecha AS FechaGuia,FechaDesp,FechaVence,IdCliente,RazonSocial,IdAgencia,IdVehiculo
,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,TarifaIva,VrIvaSal
,Sobretasa,Soldicom,ImpGlobal,Comptmntos,VrUnitario,VrCostoSal,pVehiculo,K.IdVend AS CedVend,Comision,Referencia,Referencia2,K.Unidades AS Unddes
,G.Factura AS NumFactura,IdCiaFac,FechaFact
 FROM Trn_Guia AS G INNER JOIN Trn_Kardex AS  K ON G.Guia=K.Documento AND G.IdCia=K.IdCia
 AND G.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON G.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipOcc='OCC' AND OCargue=@pmOCargue AND IdCiaOcc=@pmIdCiaOcc  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY G.IdCia,Guia,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmCdCiuOrigen VARCHAR(8)=Null,@pmIdLocal VARCHAR(8)=Null
,@pmIdConductor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Guia,O.IdCia AS CdCia,Compania,Fecha,FechaDesp,FechaVence,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia
,Cantidad,DirEnvio,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,HorasVig,IdVehiculo,O.IdConductor AS CedConductor,TC.RazonSocial AS Conductor,LibMilitar,ClaseLib,FechaLib,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,NumTarjeta,VigTarjeta
,O.NitEmpTrans,EmpTrans,CdRuta,Ruta,CdCiuOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CdDepOrigen,DO.Departamento AS DptoOrigen
,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,Factura,IdCiaFac,FechaFact,OrigenAdd,ZonaFrontera,TipoTemp,Temperatura,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,TimeSys,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,CLI.NumCuenta AS NumeroCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM  Trn_Guia AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero 
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia   INNER JOIN Terceros AS TC ON O.IdConductor=TC.IdTercero
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Localidades AS LO ON O.CdCiuOrigen=LO.IdLocal  LEFT JOIN Departamentos AS DO ON LO.IdDep=DO.IdDep 
 LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN TercCndtores AS TCD ON O.IdConductor=TCD.IdConductor
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%') AND CdCiuOrigen LIKE ISNULL(@pmCdCiuOrigen,'%')
 AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,Guia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuiaRelDet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmCdCiuOrigen VARCHAR(8)=Null,@pmIdLocal VARCHAR(8)=Null
,@pmIdConductor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT O.TipDoc AS CdTipDoc,Guia,O.IdCia AS CdCia,Compania,O.Fecha AS FechaGuia,FechaDesp,FechaVence,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia
,Cantidad,DirEnvio,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,HorasVig,IdVehiculo,O.IdConductor AS CedConductor,TC.RazonSocial AS Conductor,LibMilitar,ClaseLib,FechaLib,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,NumTarjeta,VigTarjeta
,O.NitEmpTrans,EmpTrans,CdRuta,Ruta,CdCiuOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CdDepOrigen,DO.Departamento AS DptoOrigen
,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact,OrigenAdd,ZonaFrontera,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
,O.TimeSys,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,CLI.NumCuenta AS NumeroCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--datos de kardex
,K.IdProducto AS CdProducto,Salidas,K.Referencia,DescripProd,Descripcion
 FROM  Trn_Guia AS O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero 
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia   INNER JOIN Terceros AS TC ON O.IdConductor=TC.IdTercero
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Guia=K.Documento  AND  O.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Localidades AS LO ON O.CdCiuOrigen=LO.IdLocal  LEFT JOIN Departamentos AS DO ON LO.IdDep=DO.IdDep 
 LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN TercCndtores AS TCD ON O.IdConductor=TCD.IdConductor
 WHERE O.TipDoc=@pmTipDoc AND O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%') AND CdCiuOrigen LIKE ISNULL(@pmCdCiuOrigen,'%')
 AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,Guia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryGuia_Cr @pmTipDoc VARCHAR(3),@pmGuiaIni INT,@pmGuiaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT  O.TipDoc AS Tip_Doc,TipoDoc,Guia,O.IdCia AS CdCia,Compania,O.Fecha AS FechaDoc,FechaDesp,FechaVence,IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Cantidad,DirEnvio,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,HorasVig,O.IdVehiculo,O.IdConductor AS CedConductor,TC.RazonSocial AS Conductor,LibMilitar,ClaseLib,FechaLib,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,NumTarjeta,VigTarjeta
,O.NitEmpTrans,EmpTrans,CdRuta,Ruta,CdCiuOrigen,LO.Localidad AS CiudadOrigen,LO.IdDep AS CdDepOrigen,DO.Departamento AS DptoOrigen
,Referncia,TipOcc,OCargue,IdCiaOcc,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact,OrigenAdd,ZonaFrontera,Anulado,TipoTemp,O.Temperatura,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado
,O.TimeSys AS Fec_Add,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,CLI.NumCuenta AS NumeroCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,CdCCosto,K.CdSubCos AS CodSubCos,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia
,K.Descripcion AS KarDescricion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,K.Remision AS KarRemision,K.IdCiaRem AS CdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase
,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5,Compania
--Inf. vehículo
,CdRemque,VehArtic,NumVeh
 FROM  Trn_Guia AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Guia=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero 
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia   INNER JOIN Terceros AS TC ON O.IdConductor=TC.IdTercero
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Localidades AS LO ON O.CdCiuOrigen=LO.IdLocal  LEFT JOIN Departamentos AS DO ON LO.IdDep=DO.IdDep 
 LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN TercCndtores AS TCD ON O.IdConductor=TCD.IdConductor
 LEFT JOIN Vehiculos AS VH ON O.IdVehiculo=VH.IdVehiculo
 WHERE O.TipDoc=@pmTipDoc AND Guia BETWEEN @pmGuiaIni  AND @pmGuiaFin AND O.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Guia,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInstalaciones @pmIdInstala VARCHAR(4)
 AS 
 SELECT IdInstala,Instlacion,DirInstala,CodCiu,CodCia,IdClase,Inactivo
 FROM Instalaciones
 WHERE IdInstala=@pmIdInstala
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInstalacionesLta @pmCodCia CHAR(2)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdInstala,Instlacion,DirInstala,CodCiu,Localidad,CodCia,Compania,I.IdClase AS CodClase,ClaseRiesgo,Tarifa,I.Inactivo AS Inctivo
 FROM Instalaciones AS I INNER JOIN ClaseRie AS CR ON I.IdClase=CR.IdClase
 LEFT JOIN Localidades AS L ON I.CodCiu=L.IdLocal LEFT JOIN  Companias AS C ON I.CodCia=C.IdCia 
 WHERE CodCia LIKE ISNULL(@pmCodCia,'%%') 
 AND (I.Inactivo=ISNULL(@pmInactivo,0) or I.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Instlacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalle @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,nInventario,IdCia,Item,IdProducto,IdBodega,CdUbic,CdTanque,Cantidad,VrUnitario,CantExist
,Descripcion,Unidades,UndAforo,UndAgua,CantAgua,NoConteo,IdUsuario,IdEstacion,FechaAdd
 FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleDso @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdUsuario VARCHAR(11)=Null,@pmIdEstacion CHAR(2)=Null
 AS 
 SELECT Item,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,Bodega,CdUbic,CdTanque
,Cantidad,CantExist,VrUnitario,Cantidad*VrUnitario AS CostoTotal --Cantidad-CantExist AS CantDif
,Descripcion,Unidades,NoConteo,D.IdUsuario AS IdUsuari,Usuario,D.IdEstacion AS CdEstacion
,D.FechaAdd AS Fec_Add,UndAforo,UndAgua,CantAgua
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 INNER JOIN  Bodegas AS B ON D.IdBodega=B.IdBodega
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%') AND D.IdEstacion LIKE ISNULL(@pmIdEstacion,'%%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleEtq @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmCdTanque VARCHAR(4),@pmItem INT
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND CdTanque=@pmCdTanque  AND Item<>@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleExi @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia  AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleExp @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4)
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND  IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleFal @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdEstacion CHAR(2)=Null
 AS 
 SELECT D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,Bodega
,SUM(Cantidad) AS CantFisico,CantExist,SUM(Cantidad)-CantExist AS CantDif
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 INNER JOIN  Bodegas AS B ON D.IdBodega=B.IdBodega
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND IdEstacion LIKE ISNULL(@pmIdEstacion,'%%')
 GROUP BY  D.IdProducto,DescripProd,D.IdBodega,Bodega,CantExist
 HAVING (SUM(Cantidad)-CantExist)<0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleFalTq @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.IdProducto AS CdProducto,DescripProd,CdTanque,Cantidad,CantExist,Cantidad-CantExist AS CantDif
,UndAforo,UndAgua,CantAgua
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND (Cantidad-CantExist)<0
 ORDER BY DescripProd,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleItm @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetallePro @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,D.IdProducto AS CdProducto,DescripProd,TipoRef,D.IdBodega AS CdBodega,CdUbic,CdTanque,Cantidad,VrUnitario,CantExist
,Unidades,UndAforo,UndAgua,CantAgua,NoConteo,ExtciaAct,Tanques,Seriales,Lotes,VrCostPmd,VrCosto
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleSob @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdEstacion CHAR(2)=Null
 AS 
 SELECT D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,Bodega
,SUM(Cantidad) AS CantFisico,CantExist,SUM(Cantidad)-CantExist AS CantDif
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 INNER JOIN  Bodegas AS B ON D.IdBodega=B.IdBodega
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND IdEstacion LIKE ISNULL(@pmIdEstacion,'%%')
 GROUP BY  D.IdProducto,DescripProd,D.IdBodega,Bodega,CantExist
 HAVING (SUM(Cantidad)-CantExist)>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleSobTq @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.IdProducto AS CdProducto,DescripProd,CdTanque,Cantidad,CantExist,Cantidad-CantExist AS CantDif
,UndAforo,UndAgua,CantAgua
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND (Cantidad-CantExist)>0
 ORDER BY DescripProd,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleTan @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,D.IdProducto AS CdProducto,DescripProd,D.IdBodega AS CdBodega,CdTanque,UndAforo,UndAgua,Cantidad,CantAgua
,CantExist,Cantidad-CantExist AS CantDif,VrUnitario,Cantidad*VrUnitario AS CostoTotal
,Descripcion,Unidades,NoConteo,D.IdUsuario AS IdUsuari,Usuario,D.IdEstacion AS CdEstacion
,D.FechaAdd AS Fec_Add
 FROM Trn_InvDetalle AS D  INNER JOIN ProdMcias AS P ON D.IdProducto=P.IdProducto
 INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvDetalleTot @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT COUNT(Item) AS SITEMS,SUM(Cantidad) AS SCANT,SUM(VrUnitario*Cantidad) AS SCOSTOT
,SUM(CantExist) AS SCANEXI,SUM(VrUnitario*CantExist) AS SEXITOT,SUM(CantAgua) AS SCANTAGUA
,SUM(Unidades) AS STOTUNI
 FROM Trn_InvDetalle
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia 
 AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvFisico @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,nInventario,IdCia,Fecha,Modalidad,CdBodega,CantFisico,VrTotalFisico,Existencias,VrTotalCosto
,OrigenAdd,Procesado,FecProceso,NumAjuSob,NumAjuFal,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_InvFisico
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvFisicoDia @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmFecha SMALLDATETIME
,@pmModalidad VARCHAR(10),@pmnInventario INT
 AS 
 SELECT TipDoc,nInventario,IdCia,Fecha,Modalidad,Procesado,FecProceso,NumAjuSob,NumAjuFal,CdBodega,IdEstado
 FROM Trn_InvFisico
 WHERE TipDoc=@pmTipDoc AND IdCia=@pmIdCia AND Modalidad=@pmModalidad
 AND Fecha=@pmFecha AND Anulado=0 AND  nInventario<>@pmnInventario
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvFisicoLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmModalidad VARCHAR(10)=Null,@pmProcesado BIT=Null,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null
,@pmnInventarioIni INT=Null,@pmnInventarioFin INT=Null
 AS 
 SELECT nInventario,F.IdCia AS CdCia,Compania,Fecha,Modalidad,CdBodega,CantFisico,VrTotalFisico,Existencias,VrTotalCosto
,OrigenAdd,Procesado,FecProceso,NumAjuSob,NumAjuFal,Anulado,FecDev,Observacion
,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS CdUsuario,Usuario
 FROM Trn_InvFisico AS F INNER JOIN Companias AS C ON F.IdCia=C.IdCia
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  EstadoDoc AS E ON F.IdEstado=E.IdEstado
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND F.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND nInventario BETWEEN ISNULL(@pmnInventarioIni,0) AND ISNULL(@pmnInventarioFin,2147483647)
 AND (Procesado=ISNULL(@pmProcesado,0) or Procesado=ISNULL(@pmProcesado,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY F.IdCia,nInventario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvFisicoPen @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2)
,@pmModalidad VARCHAR(10),@pmnInventario INT
 AS 
 SELECT TipDoc,nInventario,IdCia,Fecha,Modalidad,Procesado,FecProceso,NumAjuSob,NumAjuFal,CdBodega,IdEstado
 FROM Trn_InvFisico
 WHERE TipDoc=@pmTipDoc AND IdCia=@pmIdCia AND Modalidad=@pmModalidad AND Procesado=0
 AND Anulado=0 AND  nInventario<>@pmnInventario
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvFisicoTan @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmCdTanque VARCHAR(4)=Null
 AS 
 SELECT Fecha,SUM(Cantidad) AS SaldoFisico,SUM(CantAgua) AS NivelAgua
 FROM Trn_InvFisico AS F INNER JOIN Trn_InvDetalle AS D ON F.TipDoc=D.TipDoc AND F.nInventario=D.nInventario  AND F.IdCia=D.IdCia
 INNER JOIN ProdMcias AS PM ON D.IdProducto=PM.IdProducto 
 WHERE Fecha BETWEEN @pmFechaIni AND  @pmFechaFin AND Modalidad='COMBUSTBLE' AND Anulado=0
  AND F.IdCia LIKE ISNULL(@pmIdCia,'%%') AND D.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND CdTanque LIKE ISNULL(@pmCdTanque,'%')
 GROUP BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryInvFisico_Cr @pmTipDoc VARCHAR(3),@pmnInventarioIni INT,@pmnInventarioFin INT
,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)=Null,@pmIdEstacion CHAR(2)=Null
 AS 
 SELECT F.nInventario AS NumeroInv,F.IdCia AS CdCia,Compania,Fecha,Modalidad,CantFisico,VrTotalFisico,Existencias,VrTotalCosto
,Procesado,FecProceso,NumAjuSob,NumAjuFal,Anulado,FecDev,Observacion
,F.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,F.IdUsuario AS CdUsuarioCrea,U.Usuario AS UsuarioCrea
--Detalles de inventario
,Item,D.IdProducto AS CodProducto,DescripProd,D.IdBodega AS CdBodega,Bodega,CdTanque,Cantidad,PM.IdUnd AS CdUnidad,UM.Unidad AS UnidPresenta,VrUnitario,CantExist
,Descripcion,Unidades,UndAforo,UndAgua,CantAgua,NoConteo,D.IdUsuario AS DetIdUsuario,UD.Usuario AS DetUsuario,D.IdEstacion AS CdTerminal,D.FechaAdd AS Fech_Add
--información del producto
,Descrip AS DesBodega,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,Tamano,PM.UndMed AS CdUndMed,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_InvFisico AS F INNER JOIN Trn_InvDetalle AS D ON F.TipDoc=D.TipDoc AND F.nInventario=D.nInventario  AND F.IdCia=D.IdCia
 INNER JOIN Companias AS C ON F.IdCia=C.IdCia  INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 INNER JOIN  EstadoDoc AS E ON F.IdEstado=E.IdEstado  INNER JOIN ProdMcias AS PM ON D.IdProducto=PM.IdProducto
 INNER JOIN  Bodegas AS B ON D.IdBodega=B.IdBodega INNER JOIN adm_Usuarios AS UD ON D.IdUsuario=UD.IdUsuario
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed  INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 WHERE F.TipDoc=@pmTipDoc AND F.nInventario BETWEEN @pmnInventarioIni AND @pmnInventarioFin  AND F.IdCia=@pmIdCia
 AND D.IdUsuario LIKE ISNULL(@pmIdUsuario,'%') AND D.IdEstacion LIKE ISNULL(@pmIdEstacion,'%%')
 ORDER BY F.nInventario,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJornadas @pmIdJornada VARCHAR(4)
 AS 
 IF @pmIdJornada IS NULL
  SELECT IdJornada,Jornada,HorasDia,Rangos  FROM Jornadas
  WHERE Inactivo=0
  ORDER BY IdJornada
 ELSE
  SELECT IdJornada,Jornada,HorasDia,Rangos,Inactivo 
  FROM Jornadas
  WHERE IdJornada=@pmIdJornada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurAutoridades @pmIdAutoridad VARCHAR(4)
 AS 
 IF @pmIdAutoridad IS NULL
 SELECT IdAutoridad,NomAutoridad,Direccion
 FROM JurAutoridades
 WHERE Inactivo=0
 ORDER BY NomAutoridad
 ELSE
 SELECT IdAutoridad,NomAutoridad,Direccion,Inactivo
 FROM JurAutoridades
 WHERE IdAutoridad=@pmIdAutoridad

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurCalifica @pmIdCalifica VARCHAR(4)
 AS 
 SELECT IdCalifica,CalificaCaso,IdClase,Inactivo
 FROM JurCalifica
 WHERE IdCalifica=@pmIdCalifica


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurCalificaDso @pmIdClase VARCHAR(4)=Null
 AS 
 SELECT IdCalifica,CalificaCaso,C.IdClase AS CdClase,ClaseAccdte
 FROM JurCalifica AS C INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase
 WHERE C.Inactivo=0 AND C.IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY CalificaCaso





GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryJurCasos @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,CasoID,IdCia,Fecha,Radicacion,IdVehiculo,IdPropietario,IdConductor,SinRecorrido,TipCar,Cartulina,IdCiaCar,FechaSuc,IdRuta,IdAbogado,IdAuxiliar,IdAutoridad,IdTipoPro,IdClase,IdCalifica,LugarAcc,CausaAcc
,Comentario,Observacion,Contraparte,DirContraparte,TelContraparte,ValDanosCParte,ValDanosEmp,ValDeducible,EmpAsegdora,AuxMutuo,ArchivoCaso,Documntos,Croquis,Anulado,FecDev,IdEstado,Reclamaciones,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_JurCasos
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurCasosHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT
 AS 
 SELECT TipDoc,CasoID,IdCia,ActualizaID,FechaUp,Historial,EstadoAct,CierreProc,ArchivoProc,NombreCparte,DirCparte,TelCparte,DanosCParte,DanosEmp,ValorDeducible,Aseguradora,AuxilioMutuo,IdEstado,IdUsuario
 FROM Trn_JurCasosHis
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))
 ORDER BY ActualizaID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurCasosHisItm  @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2)
AS
 SELECT MAX(ActualizaID) AS MxItem
 FROM  Trn_JurCasosHis
 WHERE  TipDoc= @pmTipDoc AND CasoID= @pmCasoID AND IdCia= @pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurCasosHisLta  @pmTipDoc VARCHAR(3), @pmCasoID INT=Null, @pmIdCia CHAR(2)=Null, @pmActualizaID INT=Null
,@pmFechaUp SMALLDATETIME=Null,@pmCierreProc BIT=Null,@pmIdEstado VARCHAR(4)=Null
AS
 SELECT  CasoID, ActualizaID, FechaUp, Historial, EstadoAct, CierreProc, ArchivoProc, NombreCparte, DirCparte, TelCparte
, DanosCParte,DanosEmp, ValorDeducible, Aseguradora, AuxilioMutuo,H.IdEstado AS IdEstad,Estado, H.IdUsuario AS IdUsuari,Usuario,TipDoc,IdCia
 FROM  Trn_JurCasosHis AS H INNER JOIN JurEstados AS E ON H.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON H.IdUsuario=U.IdUsuario
 WHERE  TipDoc= @pmTipDoc  AND (CasoID>=ISNULL(@pmCasoID,0)  AND CasoID<=ISNULL(@pmCasoID,2147483647))
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
  AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))
  AND (FechaUp>=ISNULL(@pmFechaUp,CAST('19100101' AS SMALLDATETIME)) AND FechaUp<=ISNULL(@pmFechaUp,CAST('20781230' AS SMALLDATETIME)))
 AND (CierreProc=ISNULL(@pmCierreProc,0) or CierreProc=ISNULL(@pmCierreProc,1))
 AND H.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY IdCia,CasoID,ActualizaID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasosLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmAnulado BIT=Null,@pmIdVehiculo VARCHAR(10)=Null
AS 
SELECT CasoID, IdCia, Fecha, Radicacion,C.IdVehiculo AS PlacaVeh,NumVeh,C.IdPropietario AS NitPropiterio,T.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, C.IdRuta AS CodRuta,Ruta
,IdAbogado,A.RazonSocial AS Abogado,IdAuxiliar,AX.RazonSocial AS Auxiliar,C.IdAutoridad AS IdAutrdad,NomAutoridad, C.IdTipoPro AS IdTProc,TipoProceso, C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso, LugarAcc, CausaAcc, Comentario, C.Observacion AS Observ
, Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis,Reclamaciones, Anulado, FecDev,C.IdEstado AS IdEstad,Estado
,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,TipDoc,FecUpdate,TimeSys
 FROM Trn_JurCasos AS C  INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS T ON C.IdPropietario=T.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad
 INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta INNER JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro
 INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase INNER JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE  TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  IdCia like ISNULL(@pmIdCia,'%%')
 AND C.IdVehiculo like ISNULL(@pmIdVehiculo,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
ORDER BY IdCia,CasoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasosPen @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmAnulado BIT=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdTipoPro VARCHAR(4)=Null
,@pmIdClase VARCHAR(4)=Null
AS 
SELECT CasoID, IdCia, Fecha, Radicacion,C.IdVehiculo AS PlacaVeh,NumVeh,C.IdPropietario AS NitPropiterio,T.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, C.IdRuta AS CodRuta,Ruta
,IdAbogado,A.RazonSocial AS Abogado,IdAuxiliar,AX.RazonSocial AS Auxiliar,C.IdAutoridad AS IdAutrdad,NomAutoridad, C.IdTipoPro AS IdTProc,TipoProceso, C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso, LugarAcc, CausaAcc, Comentario, C.Observacion AS Observ
, Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis, Anulado, FecDev,C.IdEstado AS IdEstad,Estado,Reclamaciones
,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,TipDoc,FecUpdate,TimeSys
 FROM Trn_JurCasos AS C  INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS T ON C.IdPropietario=T.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad
 INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta INNER JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro
 INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase INNER JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
WHERE  TipDoc=@pmTipDoc AND C.IdEstado<>'9998' AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
  AND C.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND C.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND C.IdTipoPro LIKE ISNULL(@pmIdTipoPro,'%')
 AND C.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
ORDER BY IdCia,CasoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurCasosQtnLta @pmTipDoc VARCHAR(3), @pmCasoID INT=Null, @pmIdCia CHAR(2)=Null, @pmItem INT=Null
,@pmIdPregunta VARCHAR(4)=Null
AS
 SELECT CasoID, Item, Q.IdPregunta AS IdPregnta,PreguntaBas,Descripcion,TipDoc,IdCia
 FROM  Trn_JurCasosQtn AS Q INNER JOIN JurPreguntas AS P ON Q.IdPregunta=P.IdPregunta
 WHERE  TipDoc=@pmTipDoc   
  AND (CasoID>=ISNULL(@pmCasoID,0)  AND CasoID<=ISNULL(@pmCasoID,2147483647))
  AND  IdCia like ISNULL(@pmIdCia,'%%') 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
  AND Q.IdPregunta like ISNULL(@pmIdPregunta,'%')
 ORDER BY IdCia,CasoID,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasosRad @pmTipDoc VARCHAR(3),@pmRadicacion VARCHAR(20), @pmIdCia CHAR(2)=Null
AS 
SELECT   TipDoc, CasoID, IdCia, Fecha, Radicacion, IdVehiculo, IdPropietario, IdConductor, SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, IdRuta
,IdAbogado, IdAuxiliar, IdAutoridad, IdTipoPro, IdClase, IdCalifica, LugarAcc, CausaAcc, Comentario, Observacion, Contraparte, DirContraparte,TelContraparte
,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis, Reclamaciones,Anulado, FecDev,IdEstado, TimeSys, FecUpdate, IdCiaCrea, IdUsuario
 FROM Trn_JurCasos
 WHERE  TipDoc=@pmTipDoc AND Radicacion=@pmRadicacion  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY  IdCia,CasoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasos_Cr @pmTipDoc VARCHAR(3),@pmCasoIDIni INT,@pmCasoIDFin INT,@pmIdCia CHAR(2)
AS 
SELECT C.CasoID AS NumCaso, C.IdCia AS CodCia,Compania,Fecha, Radicacion,C.IdVehiculo AS PlacaVeh,C.IdPropietario AS NitPropiterio,C.IdConductor AS CedConductor,SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, C.IdRuta AS CodRuta,Ruta
,IdAbogado,A.RazonSocial AS Abogado,IdAuxiliar,AX.RazonSocial AS Auxiliar,C.IdAutoridad AS IdAutrdad,NomAutoridad, C.IdTipoPro AS IdTProc,TipoProceso, C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso, LugarAcc, CausaAcc, Comentario, C.Observacion AS Observ
, Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis,Item,QT.IdPregunta AS IdPregnta,PreguntaBas,QT.Descripcion AS RptaPregunta,Anulado, FecDev,C.IdEstado AS IdEstad,E.Estado AS EstadDocm
,Reclamaciones,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,C.TipDoc AS CodTipo,TipoDoc,FecUpdate,TimeSys
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.RazonSocial AS Propietario,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.RazonSocial AS Conductor,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
 FROM Trn_JurCasos AS C  INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad
 INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta INNER JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro
 INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase INNER JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia INNER JOIN Sys_TiposDoc  AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
    INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa   LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal 
 LEFT JOIN Trn_JurCasosQtn AS QT ON C.TipDoc=QT.TipDoc AND C.CasoID=QT.CasoID AND C.IdCia=QT.IdCia
 LEFT JOIN JurPreguntas AS JP ON QT.IdPregunta=JP.IdPregunta
 WHERE  C.TipDoc=@pmTipDoc AND C.CasoID BETWEEN @pmCasoIDIni AND @pmCasoIDFin
 AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
ORDER BY C.IdCia,C.CasoID,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasos_Crh @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT=Null
AS 
SELECT C.CasoID AS NumCaso, C.IdCia AS CodCia,Compania,Fecha, Radicacion,C.IdVehiculo AS PlacaVeh,C.IdPropietario AS NitPropiterio,NP.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, C.IdRuta AS CodRuta,Ruta
,IdAbogado,A.RazonSocial AS Abogado,IdAuxiliar,AX.RazonSocial AS Auxiliar,C.IdAutoridad AS IdAutrdad,NomAutoridad, C.IdTipoPro AS IdTProc,TipoProceso, C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso, LugarAcc, CausaAcc, Comentario, C.Observacion AS Observ
, Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis,ActualizaID, FechaUp, Historial, EstadoAct, CierreProc, ArchivoProc, NombreCparte, DirCparte, TelCparte,DanosCParte,DanosEmp, ValorDeducible, Aseguradora
,Reclamaciones,AuxilioMutuo,H.IdEstado AS His_IdEstado,H.IdUsuario AS His_IdUsuario,UH.Usuario AS His_Usuario,Anulado, FecDev,C.IdEstado AS IdEstad,E.Estado AS EstadDocm,IdCiaCrea,C.IdUsuario AS IdUsuari,U.Usuario AS Cas_Usuario,C.TipDoc AS CodTipo,TipoDoc,FecUpdate,TimeSys
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
 FROM Trn_JurCasos AS C  INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad
 INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta INNER JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro
 INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase INNER JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia INNER JOIN Sys_TiposDoc  AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  INNER JOIN Trn_JurCasosHis AS H ON C.TipDoc=H.TipDoc AND C.CasoID=H.CasoID AND C.IdCia=H.IdCia
  INNER JOIN adm_Usuarios AS UH ON H.IdUsuario=UH.IdUsuario 
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
 LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal
 WHERE  C.TipDoc=@pmTipDoc
 AND ( C.CasoID>=ISNULL(@pmCasoID,0)  AND  C.CasoID<=ISNULL(@pmCasoID,2147483647))
 AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND  ActualizaID<=ISNULL(@pmActualizaID,2147483647))
ORDER BY C.IdCia,C.CasoID,ActualizaID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasos_Crr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
,@pmIdAbogado VARCHAR(16)=Null,@pmIdAuxiliar VARCHAR(16)=Null,@pmIdAutoridad VARCHAR(4)=Null,@pmIdTipoPro VARCHAR(4)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdCalifica VARCHAR(4)=Null,@pmSinRecorrido BIT=Null
,@pmRadicacion VARCHAR(20)=Null,@pmCasoID INT=Null,@pmCartulina INT=Null,@pmFechaSuc SMALLDATETIME=Null,@pmIdRuta VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null
AS 
SELECT CasoID,C.IdCia AS CodCia,Fecha,Radicacion,C.IdVehiculo AS PlacaVeh,C.IdPropietario AS NitPropiterio,NP.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, C.IdRuta AS CodRuta,Ruta
,IdAbogado,A.RazonSocial AS Abogado,IdAuxiliar,AX.RazonSocial AS Auxiliar,C.IdAutoridad AS IdAutrdad,NomAutoridad, C.IdTipoPro AS IdTProc,TipoProceso,C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso, LugarAcc, CausaAcc, Comentario, C.Observacion AS Observ
, Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis,Reclamaciones,Anulado,FecDev,C.IdEstado AS IdEstad,E.Estado AS EstadDocm,C.IdUsuario AS IdUsuari,Usuario,Compania,TipDoc,IdCiaCrea,FecUpdate,TimeSys
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM Trn_JurCasos AS C  INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad
 INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta INNER JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro
 INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase INNER JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
 LEFT JOIN TercCndtores AS CT ON C.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  C.IdCia like ISNULL(@pmIdCia,'%%') AND C.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND C.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND C.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND IdAbogado LIKE ISNULL(@pmIdAbogado,'%')
 AND IdAuxiliar LIKE ISNULL(@pmIdAuxiliar,'%') AND C.IdAutoridad LIKE ISNULL(@pmIdAutoridad,'%') AND C.IdTipoPro LIKE ISNULL(@pmIdTipoPro,'%')
 AND  C.IdClase LIKE ISNULL(@pmIdClase,'%') AND C.IdCalifica LIKE ISNULL(@pmIdCalifica,'%')   AND Radicacion LIKE ISNULL(@pmRadicacion,'%')
 AND (CasoID>=ISNULL(@pmCasoID,0)  AND CasoID<=ISNULL(@pmCasoID,2147483647))
  AND (Cartulina>=ISNULL(@pmCartulina,0)  AND Cartulina<=ISNULL(@pmCartulina,2147483647))
  AND (FechaSuc>=ISNULL(@pmFechaSuc,CAST('19100101' AS SMALLDATETIME)) AND FechaSuc<=ISNULL(@pmFechaSuc,CAST('20781230' AS SMALLDATETIME)))
  AND C.IdRuta LIKE ISNULL(@pmIdRuta,'%') 
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
  AND  (SinRecorrido=ISNULL(@pmSinRecorrido,0) or SinRecorrido=ISNULL(@pmSinRecorrido,1))
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY C.IdCia,CasoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryJurCasos_CrrH @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null
,@pmIdAbogado VARCHAR(16)=Null,@pmIdAuxiliar VARCHAR(16)=Null,@pmIdAutoridad VARCHAR(4)=Null,@pmIdTipoPro VARCHAR(4)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdCalifica VARCHAR(4)=Null,@pmSinRecorrido BIT=Null
,@pmRadicacion VARCHAR(20)=Null,@pmCasoID INT=Null,@pmCartulina INT=Null,@pmFechaSuc SMALLDATETIME=Null,@pmIdRuta VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null,@pmActualizaID INT=Null
AS 
SELECT C.CasoID AS NumCaso,C.IdCia AS CodCia,Fecha,Radicacion,C.IdVehiculo AS PlacaVeh,C.IdPropietario AS NitPropiterio,NP.RazonSocial AS Propietario,C.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,SinRecorrido, TipCar, Cartulina, IdCiaCar, FechaSuc, C.IdRuta AS CodRuta,Ruta
,IdAbogado,A.RazonSocial AS Abogado,IdAuxiliar,AX.RazonSocial AS Auxiliar,C.IdAutoridad AS IdAutrdad,NomAutoridad, C.IdTipoPro AS IdTProc,TipoProceso,C.IdClase AS IdClaAcc,ClaseAccdte, C.IdCalifica AS IdCalf,CalificaCaso, LugarAcc, CausaAcc, Comentario, C.Observacion AS Observ
, Contraparte, DirContraparte,TelContraparte,ValDanosCParte, ValDanosEmp, ValDeducible, EmpAsegdora, AuxMutuo, ArchivoCaso, Documntos, Croquis,ActualizaID, FechaUp, Historial, EstadoAct, CierreProc, ArchivoProc, NombreCparte, DirCparte, TelCparte,DanosCParte,DanosEmp, ValorDeducible, Aseguradora
,AuxilioMutuo,Reclamaciones,H.IdEstado AS His_IdEstado,EH.Estado AS His_Estado,H.IdUsuario AS His_IdUsuario,UH.Usuario AS His_Usuario
,Anulado,FecDev,C.IdEstado AS IdEstad,E.Estado AS EstadDocm,C.IdUsuario AS IdUsuari,U.Usuario AS Usuario_Doc,Compania,C.TipDoc AS IdTipDcm,IdCiaCrea,FecUpdate,TimeSys
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.RazonSocial AS Propietario,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.RazonSocial AS Conductor,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM Trn_JurCasos AS C  INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad
 INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta INNER JOIN JurTipos AS TP ON C.IdTipoPro=TP.IdTipoPro
 INNER JOIN JurClaseAcc AS CA ON C.IdClase=CA.IdClase INNER JOIN JurCalifica AS CL ON C.IdCalifica=CL.IdCalifica
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
 INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
 INNER JOIN Trn_JurCasosHis AS H ON C.TipDoc=H.TipDoc AND C.CasoID=H.CasoID AND C.IdCia=H.IdCia
 INNER JOIN adm_Usuarios AS UH ON H.IdUsuario=UH.IdUsuario   INNER JOIN JurEstados AS EH ON H.IdEstado=EH.IdEstado
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
   LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON C.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  C.IdCia like ISNULL(@pmIdCia,'%%') AND C.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND C.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND C.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND IdAbogado LIKE ISNULL(@pmIdAbogado,'%')
 AND IdAuxiliar LIKE ISNULL(@pmIdAuxiliar,'%') AND C.IdAutoridad LIKE ISNULL(@pmIdAutoridad,'%') AND C.IdTipoPro LIKE ISNULL(@pmIdTipoPro,'%')
 AND  C.IdClase LIKE ISNULL(@pmIdClase,'%') AND C.IdCalifica LIKE ISNULL(@pmIdCalifica,'%')   AND Radicacion LIKE ISNULL(@pmRadicacion,'%')
 AND (C.CasoID>=ISNULL(@pmCasoID,0)  AND C.CasoID<=ISNULL(@pmCasoID,2147483647))
  AND (Cartulina>=ISNULL(@pmCartulina,0)  AND Cartulina<=ISNULL(@pmCartulina,2147483647))
  AND (FechaSuc>=ISNULL(@pmFechaSuc,CAST('19100101' AS SMALLDATETIME)) AND FechaSuc<=ISNULL(@pmFechaSuc,CAST('20781230' AS SMALLDATETIME)))
  AND C.IdRuta LIKE ISNULL(@pmIdRuta,'%') 
AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
  AND  (SinRecorrido=ISNULL(@pmSinRecorrido,0) or SinRecorrido=ISNULL(@pmSinRecorrido,1))
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
  AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))
 ORDER BY C.IdCia,C.CasoID,ActualizaID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurClaseAcc @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,ClaseAccdte
 FROM JurClaseAcc
 WHERE Inactivo=0 
 ORDER BY ClaseAccdte
 ELSE
 SELECT IdClase,ClaseAccdte,Inactivo
 FROM JurClaseAcc
 WHERE IdClase=@pmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurDocumtos @pmIdDoc VARCHAR(4)
 AS 
 IF @pmIdDoc IS NULL
  SELECT IdDoc,Documnto
  FROM JurDocumtos
  WHERE Inactivo=0
 ORDER BY Documnto
 ELSE
  SELECT IdDoc,Documnto,Inactivo
  FROM JurDocumtos
  WHERE IdDoc=@pmIdDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurEstados @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  SELECT IdEstado,Estado,NColor 
  FROM JurEstados
  WHERE  Inactivo=0
 ORDER BY IdEstado
 ELSE
 SELECT IdEstado,Estado,Inactivo,NColor
 FROM JurEstados
 WHERE IdEstado=@pmIdEstado



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurInvesHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT
 AS 
 SELECT TipDoc,CasoID,IdCia,ActualizaID,FechaUp,Historial,EstadoAct,Pruebas,Documntos,CierreProc,ArchivoProc
,NombreCparte,DirCparte,TelCparte,IdEstado,IdUsuario
 FROM Trn_JurInvesHis
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurInvesHisDso @pmTipDoc VARCHAR(3), @pmCasoID INT=Null, @pmIdCia CHAR(2)=Null, @pmActualizaID INT=Null
,@pmFechaUp SMALLDATETIME=Null,@pmCierreProc BIT=Null,@pmIdEstado VARCHAR(4)=Null
AS
SELECT CasoID, ActualizaID, FechaUp, Historial, EstadoAct, Pruebas, Documntos, CierreProc, ArchivoProc, NombreCparte, DirCparte
, TelCparte,H.IdEstado AS IdEstad,Estado,H.IdUsuario AS IdUsuari ,Usuario,TipDoc,IdCia
 FROM  Trn_JurInvesHis AS H INNER JOIN JurEstados AS E ON H.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON H.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc 
 AND (CasoID>=ISNULL(@pmCasoID,0)  AND CasoID<=ISNULL(@pmCasoID,2147483647))
 AND IdCia like ISNULL(@pmIdCia,'%%') 
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))
 AND (FechaUp>=ISNULL(@pmFechaUp,CAST('19100101' AS SMALLDATETIME)) AND FechaUp<=ISNULL(@pmFechaUp,CAST('20781230' AS SMALLDATETIME)))
 AND  (CierreProc=ISNULL(@pmCierreProc,0) or CierreProc=ISNULL(@pmCierreProc,1))
 AND H.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY IdCia,CasoID,ActualizaID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurInvesHisItm @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2)
AS
 SELECT MAX(ActualizaID) AS MxItem
 FROM  Trn_JurInvesHis
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurInvestiga @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,CasoID,IdCia,Fecha,Radicacion,IdAbogado,IdAuxiliar,IdAutoridad,IdEmpresa,Observacion,Contraparte,DirContraparte,TelContraparte,ArchivoCaso,Anulado,FecDev,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_JurInvestiga
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryJurInvestigaLta  @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmAnulado BIT=Null,@pmIdAbogado VARCHAR(16)=Null,@pmIdEmpresa VARCHAR(16)=Null
AS
SELECT CasoID, C.IdCia AS CodCia, Fecha, Radicacion, IdAbogado,A.RazonSocial AS Abogado, IdAuxiliar,AX.RazonSocial AS Auxiliar, C.IdAutoridad AS IdAutordad,NomAutoridad,IdEmpresa,T.RazonSocial AS Empresa
,C.Observacion AS Observ, Contraparte, DirContraparte,TelContraparte,ArchivoCaso, Anulado, FecDev, C.IdEstado AS IdEstad,Estado,C.IdUsuario AS IdUsuari,Usuario,TipDoc,Compania,IdCiaCrea,FecUpdate,TimeSys
 FROM Trn_JurInvestiga AS C  INNER JOIN  Terceros AS T ON C.IdEmpresa=T.IdTercero 
 INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero  INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero 
 INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia
 WHERE  TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdAbogado like ISNULL(@pmIdAbogado,'%') AND IdEmpresa LIKE ISNULL(@pmIdEmpresa,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
ORDER BY C.IdCia,CasoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryJurInvestigaRad  @pmTipDoc VARCHAR(3),@pmRadicacion VARCHAR(20), @pmIdCia CHAR(2)=Null
AS
SELECT TipDoc, CasoID, IdCia, Fecha, Radicacion, IdAbogado, IdAuxiliar, IdAutoridad, IdEmpresa, Observacion, Contraparte, DirContraparte
, TelContraparte,ArchivoCaso, Anulado, FecDev, IdEstado, TimeSys, FecUpdate, IdCiaCrea, IdUsuario
 FROM   Trn_JurInvestiga
 WHERE  TipDoc=@pmTipDoc AND Radicacion=@pmRadicacion  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY  IdCia,CasoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryJurInvestiga_Cr  @pmTipDoc VARCHAR(3),@pmCasoIDIni INT,@pmCasoIDFin INT,@pmIdCia CHAR(2)
AS
SELECT CasoID, C.IdCia AS CodCia, Fecha, Radicacion, IdAbogado,A.RazonSocial AS Abogado, IdAuxiliar,AX.RazonSocial AS Auxiliar, C.IdAutoridad AS IdAutordad,NomAutoridad,IdEmpresa,T.RazonSocial AS Empresa,T.Dv AS Emp_DvNit
,C.Observacion AS Observ, Contraparte, DirContraparte,TelContraparte,ArchivoCaso, Anulado, FecDev, C.IdEstado AS IdEstad,Estado,C.IdUsuario AS IdUsuari,Usuario,TipDoc,TipoDoc,Compania,IdCiaCrea,FecUpdate,TimeSys
 FROM Trn_JurInvestiga AS C  INNER JOIN  Terceros AS T ON C.IdEmpresa=T.IdTercero 
 INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero  INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero 
 INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Sys_TiposDoc  AS TD ON C.TipDoc=TD.IdDoc
 WHERE  TipDoc=@pmTipDoc AND CasoID BETWEEN  @pmCasoIDIni AND @pmCasoIDFin 
 AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%')
ORDER BY C.IdCia,CasoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryJurInvestiga_Crh  @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT=Null
AS
SELECT C.CasoID AS NumCaso, C.IdCia AS CodCia, Fecha, Radicacion, IdAbogado,A.RazonSocial AS Abogado, IdAuxiliar,AX.RazonSocial AS Auxiliar, C.IdAutoridad AS IdAutordad,NomAutoridad,IdEmpresa,T.RazonSocial AS Empresa,T.Dv AS Emp_DvNit
,Contraparte, DirContraparte,TelContraparte,ArchivoCaso,C.Observacion AS Observ,ActualizaID, FechaUp, Historial, EstadoAct, Pruebas, Documntos, CierreProc, ArchivoProc, NombreCparte, DirCparte, TelCparte,H.IdEstado AS His_IdEstado,HE.Estado AS His_Estado
,H.IdUsuario AS His_IdUsuario ,UH.Usuario AS His_Usuario, Anulado, FecDev, C.IdEstado AS IdEstad,E.Estado AS EstadDoc,C.IdUsuario AS IdUsuari,U.Usuario AS Dcm_Usuario,C.TipDoc AS TipDcm,TipoDoc,Compania,IdCiaCrea,FecUpdate,TimeSys
 FROM Trn_JurInvestiga AS C INNER JOIN Trn_JurInvesHis AS H ON C.TipDoc=H.TipDoc AND C.CasoID=H.CasoID AND C.IdCia=H.IdCia
 INNER JOIN  Terceros AS T ON C.IdEmpresa=T.IdTercero  INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero  
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero  INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad 
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN Companias   AS CI ON C.IdCia=CI.IdCia INNER JOIN Sys_TiposDoc  AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN JurEstados AS HE ON H.IdEstado=HE.IdEstado INNER JOIN adm_Usuarios  AS UH ON H.IdUsuario=UH.IdUsuario 
 WHERE  C.TipDoc=@pmTipDoc 
 AND ( C.CasoID>=ISNULL(@pmCasoID,0)  AND  C.CasoID<=ISNULL(@pmCasoID,2147483647))
 AND  C.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND  ActualizaID<=ISNULL(@pmActualizaID,2147483647))
ORDER BY C.IdCia,C.CasoID,ActualizaID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryJurInvestiga_Crr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdAbogado VARCHAR(16)=Null
,@pmIdAuxiliar VARCHAR(16)=Null,@pmIdEmpresa VARCHAR(16)=Null,@pmIdAutoridad VARCHAR(4)=Null,@pmRadicacion VARCHAR(20)=Null,@pmCasoID INT=Null
,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null
AS
SELECT CasoID, C.IdCia AS CodCia, Fecha, Radicacion, IdAbogado,A.RazonSocial AS Abogado, IdAuxiliar,AX.RazonSocial AS Auxiliar, C.IdAutoridad AS IdAutordad,NomAutoridad,IdEmpresa,T.RazonSocial AS Empresa,T.Dv AS Emp_DvNit
,C.Observacion AS Observ, Contraparte, DirContraparte,TelContraparte,ArchivoCaso, Anulado, FecDev, C.IdEstado AS IdEstad,Estado,C.IdUsuario AS IdUsuari,Usuario,TipDoc,Compania,IdCiaCrea,FecUpdate,TimeSys
 FROM Trn_JurInvestiga AS C  INNER JOIN  Terceros AS T ON C.IdEmpresa=T.IdTercero 
 INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero  INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero 
 INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdAbogado LIKE ISNULL(@pmIdAbogado,'%')
 AND IdAuxiliar LIKE ISNULL(@pmIdAuxiliar,'%') AND IdEmpresa LIKE ISNULL(@pmIdEmpresa,'%') AND C.IdAutoridad LIKE ISNULL(@pmIdAutoridad,'%')
 AND (CasoID>=ISNULL(@pmCasoID,0)  AND CasoID<=ISNULL(@pmCasoID,2147483647))
 AND Radicacion LIKE ISNULL(@pmRadicacion,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
ORDER BY C.IdCia,CasoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryJurInvestiga_CrrH  @pmTipDoc VARCHAR(3), @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdAbogado VARCHAR(16)=Null
,@pmIdAuxiliar VARCHAR(16)=Null,@pmIdEmpresa VARCHAR(16)=Null,@pmIdAutoridad VARCHAR(4)=Null,@pmRadicacion VARCHAR(20)=Null,@pmCasoID INT=Null
,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null,@pmActualizaID INT=Null
AS
SELECT C.CasoID AS NumCaso, C.IdCia AS CodCia, Fecha, Radicacion, IdAbogado,A.RazonSocial AS Abogado, IdAuxiliar,AX.RazonSocial AS Auxiliar, C.IdAutoridad AS IdAutordad,NomAutoridad,IdEmpresa,T.RazonSocial AS Empresa,T.Dv AS Emp_DvNit
,C.Observacion AS Observ, Contraparte, DirContraparte,TelContraparte,ArchivoCaso,ActualizaID, FechaUp, Historial, EstadoAct, Pruebas, Documntos, CierreProc, ArchivoProc, NombreCparte, DirCparte, TelCparte
,H.IdEstado AS His_IdEstado,HE.Estado AS His_Estado,H.IdUsuario AS His_IdUsuario ,UH.Usuario AS His_Usuario
,Anulado, FecDev, C.IdEstado AS IdEstad,E.Estado AS EstadoDcm,C.IdUsuario AS IdUsuari,U.Usuario AS UsuarioDcm,C.TipDoc AS TipDcm,Compania,IdCiaCrea,FecUpdate,TimeSys
 FROM Trn_JurInvestiga  AS C INNER JOIN Trn_JurInvesHis AS H ON C.TipDoc=H.TipDoc AND C.CasoID=H.CasoID AND C.IdCia=H.IdCia
 INNER JOIN  Terceros AS T ON C.IdEmpresa=T.IdTercero  INNER JOIN Terceros AS A ON C.IdAbogado=A.IdTercero  
 INNER JOIN Terceros AS AX ON C.IdAuxiliar=AX.IdTercero  INNER JOIN JurAutoridades AS AU ON C.IdAutoridad=AU.IdAutoridad 
 INNER JOIN JurEstados AS E ON C.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia INNER JOIN JurEstados AS HE ON H.IdEstado=HE.IdEstado
 INNER JOIN adm_Usuarios AS UH ON H.IdUsuario=UH.IdUsuario 
 WHERE C.TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdAbogado LIKE ISNULL(@pmIdAbogado,'%')
 AND IdAuxiliar LIKE ISNULL(@pmIdAuxiliar,'%') AND IdEmpresa LIKE ISNULL(@pmIdEmpresa,'%') AND C.IdAutoridad LIKE ISNULL(@pmIdAutoridad,'%')
 AND (C.CasoID>=ISNULL(@pmCasoID,0)  AND C.CasoID<=ISNULL(@pmCasoID,2147483647))
 AND Radicacion LIKE ISNULL(@pmRadicacion,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (ActualizaID>=ISNULL(@pmActualizaID,0)  AND ActualizaID<=ISNULL(@pmActualizaID,2147483647))
ORDER BY C.IdCia,C.CasoID,ActualizaID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurPreguntas @pmIdPregunta VARCHAR(4)
 AS 
 SELECT IdPregunta,PreguntaBas,IdTipoPro,TipoDato,Inactivo
 FROM JurPreguntas
 WHERE IdPregunta=@pmIdPregunta


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryJurPreguntasDso @pmIdTipoPro VARCHAR(4)
 AS 
 SELECT IdPregunta,PreguntaBas,JP.IdTipoPro AS CdTipoPro,TipoProceso,TipoDato
 FROM JurPreguntas AS JP INNER JOIN JurTipos AS JT ON JP.IdTipoPro=JT.IdTipoPro
 WHERE JP.Inactivo=0 AND JP.IdTipoPro LIKE ISNULL(@pmIdTipoPro,'%')
 ORDER BY PreguntaBas


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryJurTipos @pmIdTipoPro VARCHAR(4)
 AS 
 IF @pmIdTipoPro IS NULL 
  SELECT IdTipoPro,TipoProceso 
  FROM JurTipos
  WHERE Inactivo=0
 ORDER BY IdTipoPro
 ELSE
  SELECT IdTipoPro,TipoProceso,Inactivo 
  FROM JurTipos
  WHERE IdTipoPro=@pmIdTipoPro



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKarUbic @pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,IdProducto,IdBodega,IdUbic,Entradas,Salidas,Fecha,Linea
 FROM Trn_KarUbic
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento 
 AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
ORDER BY  Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKarUbicGru @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT U.IdProducto AS CodProducto,DescripProd,U.IdBodega AS CodBodega,U.IdUbic AS CodUbic,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
 FROM Trn_KarUbic AS U INNER JOIN ProdMcias AS P ON U.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento  AND IdCia=@pmIdCia 
 AND TipoRef<>'SERVICIO' 
 GROUP BY  U.IdProducto,DescripProd,U.IdBodega,U.IdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardex @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Fecha,IdProducto,IdBodega,CdTanque,Entradas,Salidas,IdUnd,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,IdConcepto,IdTercero,CdAgencia,CdCCosto,CdSubCos,CdLocal,CdSzona
,pVehiculo,IdVend,Comision,CdOperario,ComisnOper,Referencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura,TipDocDev
,NumDocDev,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,CodTarDct
,CodTarIva,CodTarIca,CodTarRet,CodTarCom,CodTarCmc,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden,galsbruto,galsneto,Temperatura
,UmTemp,Densidad,TimeSys,IdUsuario,Rec_Costo
 FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexAdi @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)
 AS 
 SELECT IdCia,K.IdProducto AS CdProducto,DescripProd,SUM(Entradas) AS SCANENT,SUM(Salidas) AS SCANSAL
,SUM(VrCostoEnt) AS SCOSENT,SUM(VrCostoSal) AS SCOSSAL
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('FCR','FC1','FC2','FC3','FC4','FC5','DFC','DF1','DF2','DF3','DF4','DF5')
 AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia=@pmIdCia AND EsProdBase=0
 AND TipoRef='PRODUCTO' AND K.IdProducto IN (SELECT IdProducto FROM ProdAditivos)
 GROUP BY IdCia,K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexBDev @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT TipDev,Devolucion,F.IdCia AS CdCia,F.Fecha AS FechaDev,F.TipDoc AS TipoFact,F.Factura AS NumFact
,IdCiaDoc,FecDoc,IdCliente,IdAgencia,F.IdVend AS CdVend,TarifaCom,Modalidad,ModdDev
--datos del kardex
,K.IdProducto AS CdProducto,DescripProd,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,P.IdUnd AS CdUnid,Tanques
,SUM(Entradas) AS STOTENT,SUM(VrCostoEnt) AS STOTCOS,SUM(Entradas*VrPrecio) AS SVALTOT
,SUM(VrIvaEnt) AS STOTIVA,SUM(VrDctoEnt) AS STOTDCT
 FROM Trn_DevFcr AS F INNER JOIN Trn_Kardex AS K ON F.TipDev=K.TipDoc AND F.Devolucion=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo
 WHERE TipDev IN ('DFC','DF1','DF2','DF3','DF4','DF5')  AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND NoVentas>0
 AND IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  TipDev,Devolucion,F.IdCia,F.Fecha,F.TipDoc,F.Factura,IdCiaDoc,FecDoc,IdCliente,IdAgencia
,F.IdVend,TarifaCom,Modalidad,ModdDev,K.IdProducto,DescripProd,TipoRef,P.IdSubgrupo,Subgrupo,P.IdUnd,Tanques
 ORDER BY TipDev,Devolucion,F.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexBFcr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT F.TipDoc AS TipoFact,F.Factura AS NumFact,F.IdCia AS CdCia,F.Fecha AS FecFact,FechaVence,IdCliente,IdAgencia,IdCCosto,IdSubCos
,F.IdVend AS CdVend,TarifaCom,Modalidad,Anulado,NumDev
--datos del kardex
,K.IdProducto AS CdProducto,DescripProd,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,P.IdUnd AS CdUnid,Tanques
,SUM(Salidas) AS STOTSAL,SUM(VrCostoSal) AS STOTCOS,SUM(Salidas*VrPrecio) AS SVALTOT
,SUM(VrIvaSal) AS STOTIVA,SUM(VrDctoSal) AS STOTDCT
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo
 WHERE F.TipDoc IN  ('FCR','FC1','FC2','FC3','FC4','FC5') AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND NoVentas>0
 AND IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY F.TipDoc,F.Factura,F.IdCia,F.Fecha,FechaVence,IdCliente,IdAgencia,IdCCosto,IdSubCos,F.IdVend,TarifaCom
,Modalidad,Anulado,NumDev,K.IdProducto,DescripProd,TipoRef,P.IdSubgrupo,Subgrupo,P.IdUnd,Tanques
 ORDER BY  F.TipDoc,F.Factura,F.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexBIva @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT
,@pmIdCia CHAR(2)
 AS 
 SELECT Documento,TarifaIva,SUM((VrPrecio*Entradas)-VrDctoEnt) AS SBASEENT,SUM((VrPrecio*Salidas)-VrDctoSal) AS SBASESAL
,SUM(VrIvaEnt) AS SIVAENT,SUM(VrIvaSal) AS SIVASAL
 FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin
 AND IdCia=@pmIdCia AND (VrIvaEnt>0 OR VrIvaSal>0)
 GROUP BY  Documento,TarifaIva
 ORDER BY Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexBIvac @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT
,@pmIdCia CHAR(2)
 AS 
 SELECT Documento,K.IdProducto AS CodProducto,DescripProd
,SUM(Entradas*TarifaIva) AS SIVAENT,SUM(Salidas*TarifaIva) AS SIVASAL
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin
 AND IdCia=@pmIdCia AND TarifaIva>0 AND Tanques<>0
 GROUP BY  Documento,K.IdProducto,DescripProd
 ORDER BY Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexBon @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,P.IdUnd AS CdUnid,Tanques
,SUM(Salidas) AS STOTSAL,SUM(VrCostoSal) AS STOTCOS,SUM(Salidas*VrPrecio) AS SVALTOT
,SUM(VrIvaSal) AS STOTIVA,SUM(VrDctoSal) AS STOTDCT
 FROM Trn_Facturas AS F INNER JOIN Trn_Kardex AS K ON F.TipDoc=K.TipDoc AND F.Factura=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo
 WHERE F.TipDoc IN  ('FCR','FC1','FC2','FC3','FC4','FC5') AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND NoVentas>0
 AND IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  K.IdProducto,DescripProd,TipoRef,P.IdSubgrupo,Subgrupo,P.IdUnd,Tanques
 ORDER BY P.IdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexBond @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,P.IdUnd AS CdUnid,Tanques
,SUM(Entradas) AS STOTENT,SUM(VrCostoEnt) AS STOTCOS,SUM(Entradas*VrPrecio) AS SVALTOT
,SUM(VrIvaEnt) AS STOTIVA,SUM(VrDctoEnt) AS STOTDCT
 FROM Trn_DevFcr AS F INNER JOIN Trn_Kardex AS K ON F.TipDev=K.TipDoc AND F.Devolucion=K.Documento AND F.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo
 WHERE TipDev IN ('DFC','DF1','DF2','DF3','DF4','DF5')  AND F.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND NoVentas>0
 AND IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND F.IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  K.IdProducto,DescripProd,TipoRef,P.IdSubgrupo,Subgrupo,P.IdUnd,Tanques
 ORDER BY P.IdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexCc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)
 AS 
 SELECT IdProducto AS tmIdProducto,CdCCosto AS tmCdCCosto,CdSubCos AS tmCdSubCos
,CdLocal AS tmCdLocal,CodTarIca AS tmCodTarIca
 FROM  Trn_Kardex
 WHERE   TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
  AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexDoc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CodBodega,Bodega,CdTanque,Entradas+Salidas AS Cantidad,K.IdUnd AS IdUndad,Unidad,VrUnitario,(Entradas+Salidas)*VrUnitario AS CostoTotal
,VrPrecio,(Entradas+Salidas)*VrPrecio AS ValorTotal,K.Referencia AS Refncia,Descripcion,Unidades,TarifaDct,VrDctoEnt+VrDctoSal AS TotalDcto,TarifaIva,VrIvaEnt+VrIvaSal AS ValorIva
,TarifaRet,VrReteEnt+VrReteSal AS ValorRetFte,TarifaIca,VrIcaEnt+VrIcaSal AS ValorRetIca,ImpGlobal,CdMngra,NumInicial,NumFinal
,Servcios,TipoRef,Seriales,Lotes,Combo,Tanques,EsCombo,EsProdBase,ItemCombo,CdCCosto,CdSubCos,CdLocal,TipDocDev,NumDocDev
,IdVend,Comision,CdOperario,ComisnOper,CodTarDct,CodTarIva,CodTarRet,CodTarIca,CodTarCom,VrBruto,CdAgencia,pVehiculo,Comptmntos
,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,TipDoc,Documento,IdCia,Referencia2
--,VrCostProm,VrCostoEnt,VrCostoSal,CdUbic,NumLote,FechLote,IdConcepto,IdTercero,CdSzona
--,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,Factura
--,,OtroImpto,NoVentas,CodTarCmc,ListaPrec,VrDivisa1,VrDivisa2,VrDivisa3,,FecOrden,TimeSys,IdUsuario,Fecha
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS U ON K.IdUnd=U.IdUnd
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexGTan @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmEsCombo BIT=Null
 AS 
 SELECT  CdTanque,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND EsProdBase<2  AND Tanques<>0  AND TipoRef<>'SERVICIO'
 AND (EsCombo=ISNULL(@pmEsCombo,0) or EsCombo=ISNULL(@pmEsCombo,1))
 GROUP BY CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexMng @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT CdMngra,NumInicial,NumFinal,K.IdProducto AS CdProducto,CdTanque,Entradas,Salidas,Servcios
 FROM Trn_Kardex  AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND Tanques<>0
 ORDER BY CdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexMovTan @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,CdTanque,SUM(Entradas) AS TotalEnt,SUM(Salidas) AS TotalSal
,SUM(VrCostoEnt) AS TotalCosEnt,SUM(VrCostoSal) AS TotalCosSal
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE  Fecha  BETWEEN @pmFechaIni AND @pmFechaFin AND TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')
 AND TipoRef<>'SERVICIO' AND Tanques<>0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY K.IdProducto,CdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexMovinv @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmCdLocal VARCHAR(8)=Null,@pmVentaIni INT=Null,@pmVentaFin INT=Null
 AS 
 SELECT TipDoc,K.IdCia AS CdCia,Compania,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,Bodega
,K.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,CdAgencia,Agencia,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor
,CdLocal,Localidad,L.IdDep AS CodDep,Departamento,CdSzona,Subzona,SZ.IdZona AS CdZona,Zona,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed
--datos del producto
,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripAbrv,Precio1,Precio2,Precio3,Precio4,Precio5,IdProv,PV.RazonSocial AS NomProveedor
,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,PM.Inactivo AS ProdInactivo
--Datos del cliente
,T.IdSector AS CdSector,SectorEco,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.CdBandera AS CodBandera,TipoBandera
,SUM(Entradas) AS CantEnt,SUM(Salidas) AS CantSal,SUM(VrCostoEnt) AS CostoEnt,SUM(VrCostoSal) AS CostoSal
,SUM(VrPrecio*Entradas) AS ValorEnt,SUM(VrPrecio*Salidas) AS ValorSal,SUM(VrDctoEnt) AS DctosEnt,SUM(VrDctoSal) AS DctosSal
,SUM(VrIvaEnt) AS TotalIvaEnt,SUM(VrIvaSal) AS TotalIvaSal
,SUM(CASE WHEN Entradas>0 THEN K.Unidades ELSE K.Unidades*-1 END) AS UnidVolumen
 FROM Trn_Kardex AS K INNER JOIN Companias AS CN ON K.IdCia=CN.IdCia 
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS PV ON PM.IdProv=PV.IdTercero INNER JOIN Terceros AS T ON K.IdTercero=T.IdTercero
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN Localidades AS L ON K.CdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN TercCliente AS CLI ON K.IdTercero=CLI.IdClie  LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera
 LEFT JOIN Agencias  AS A ON K.CdAgencia=A.IdAgencia
 LEFT JOIN  Subzonas AS SZ ON K.CdSzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND K.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND K.IdVend LIKE ISNULL(@pmIdVend,'%') AND CdLocal LIKE ISNULL(@pmCdLocal,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 AND PM.IdMarca LIKE ISNULL(@pmIdMarca,'%')
 AND NoVentas BETWEEN ISNULL(@pmVentaIni,0) AND ISNULL(@pmVentaFin,999999)
 GROUP BY  TipDoc,K.IdCia,Compania,K.IdProducto,DescripProd,K.IdBodega,Bodega
,K.IdTercero,T.RazonSocial,CdAgencia,Agencia,K.IdVend,VN.RazonSocial
,CdLocal,Localidad,L.IdDep,Departamento,CdSzona,Subzona,SZ.IdZona,Zona,K.IdUnd,UM.Unidad
,TipoRef,PM.IdSubgrupo,Subgrupo,S.IdGrupo,Grupo,G.IdLinea,Linea,PM.IdMarca,Marca
,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripAbrv,Precio1,Precio2,Precio3,Precio4,Precio5,IdProv,PV.RazonSocial
,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,PM.Inactivo
,T.IdSector,SectorEco,CLI.IdGrupo,GrupoClie,CLI.CdBandera,TipoBandera

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexOdcCom @pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,COUNT(Documento) AS SCANT,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS  P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('COM','DEI')  AND  TipOrd=@pmTipOrd AND NumOrden=@pmNumOrden AND IdCiaOrd=@pmIdCiaOrd
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND  Servcios=0  
 GROUP BY  K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexOdcEnt @pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,COUNT(Documento) AS SCANT,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS  P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('ENT','DVE')  AND  TipOrd=@pmTipOrd AND NumOrden=@pmNumOrden AND IdCiaOrd=@pmIdCiaOrd
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND  Servcios=0  
 GROUP BY  K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexOrd @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Item,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,TipDocDev,NumDocDev
 FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPar @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmTipDocDev VARCHAR(3)
,@pmNumDocDev INT,@pmIdProducto VARCHAR(16),@pmItem INT
 AS 
 SELECT SUM(Entradas) AS SEnt,SUM(Salidas) AS SSal
 FROM Trn_Kardex
 WHERE TipDoc=@pmTipDoc AND IdCia=@pmIdCia AND TipDocDev=@pmTipDocDev AND NumDocDev=@pmNumDocDev
 AND IdProducto LIKE ISNULL(@pmIdProducto ,'%')
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPbas @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmIdProducto VARCHAR(16)
 AS 
 SELECT Item,K.IdProducto AS CdProducto,DescripProd,K.IdBodega AS CdBodega,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,VrUnitario,VrPrecio,VrCostProm
,VrBruto,K.Referencia AS Refncia,Descripcion,CdProdEquiv,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase
,ListaPrec,VrBase,TipoRef,Combo,Lotes,Seriales,Tanques
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND ItemCombo=@pmItem AND EsProdBase=1 AND EsCombo=0 AND CdProdEquiv=@pmIdProducto
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPedFac @pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,COUNT(Documento) AS SCANT,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS  P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('FCO','FO1','FO2','FO3','FO4','FO5','DFO','DO1','DO2','DO3','DO4','DO5','FCR','FC1','FC2','FC3','FC4','FC5','DFC','DF1','DF2','DF3','DF4','DF5')
  AND  TipOrd=@pmTipOrd AND NumOrden=@pmNumOrden AND IdCiaOrd=@pmIdCiaOrd AND  Servcios=0
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') 
 GROUP BY  K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPedOcc @pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmTipDoc VARCHAR(3),@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,COUNT(Documento) AS SCANT,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS  P ON K.IdProducto=P.IdProducto
 INNER JOIN Trn_OComb AS O ON K.TipDoc=O.TipDoc AND K.Documento=O.OCargue AND K.IdCia=O.IdCia
 WHERE TipOrd=@pmTipOrd AND NumOrden=@pmNumOrden AND IdCiaOrd=@pmIdCiaOrd
 AND K.TipDoc=@pmTipDoc AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND Anulado=0 AND Servcios=0 AND Tanques<>0
 GROUP BY  K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPedSal @pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,COUNT(Documento) AS SCANT,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS  P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('SAL','DVS')  AND  TipOrd=@pmTipOrd AND NumOrden=@pmNumOrden AND IdCiaOrd=@pmIdCiaOrd
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND  Servcios=0  
 GROUP BY  K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPre @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT K.IdProducto AS CodProducto,DescripProd,TipoRef,VrCosto,VrCostPmd,IdTarIva,Tarifa,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,CdDctCom
,SUM(Entradas) AS STOTENT
 FROM Trn_Kardex  AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto INNER JOIN Tablapor AS T ON P.IdTarIva=T.IdTarifa
 WHERE TipDoc=@pmTipDoc  AND  Documento BETWEEN @pmDocumentoIni  AND @pmDocumentoFin
 AND IdCia=@pmIdCia
 GROUP BY K.IdProducto,DescripProd,TipoRef,VrCosto,VrCostPmd,IdTarIva,Tarifa,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,CdDctCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexPro @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CodProducto,DescripProd,Descripcion,ExtciaAct,VrCosto,VrCostPmd,K.IdBodega AS CodBodega,Bodega,CdTanque
,Entradas,Salidas,VrUnitario,VrUnitario*Entradas AS SCOSENT,VrUnitario*Salidas AS SCOSSAL
,VrPrecio,VrPrecio*Entradas AS SVALENT,VrPrecio*Salidas AS SVALSAL
,VrCostProm,VrBruto,CdProdEquiv,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase
,ExtciaMin,ExtciaMax,Seriales,Lotes,Combo,Tanques
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND TipoRef<>'SERVICIO'
 ORDER BY  K.IdProducto,K.IdBodega,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexProCc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdSubgrupo,K.IdProducto AS tmIdProducto,DescripProd,TipoRef,IdProv,CdCCosto,CdSubCos,CodTarIca
,SUM(VrCostoEnt) AS SCOSENT,SUM(VrCostoSal) AS SCOSSAL
,SUM(VrPrecio*Entradas) AS SVALENT,SUM(VrPrecio*Salidas) AS SVALSAL
,SUM(VrBruto*Entradas) AS SBRUENT,SUM(VrBruto*Salidas) AS SBRUSAL
,SUM(CASE WHEN  Salidas>0 THEN VrIvaSal ELSE VrIvaEnt END) AS SIVA
,SUM(CASE WHEN  Salidas>0 THEN VrDctoSal ELSE VrDctoEnt END) AS SDCT
,SUM(CASE WHEN  Salidas>0 THEN VrReteSal ELSE VrReteEnt END) AS SRET
,SUM(CASE WHEN  Salidas>0 THEN VrIcaSal ELSE VrIcaEnt END) AS SICA
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc  AND Documento=@pmDocumento AND IdCia=@pmIdCia
 GROUP BY  IdSubgrupo,K.IdProducto,DescripProd,TipoRef,IdProv,CdCCosto,CdSubCos,CodTarIca
 ORDER BY IdSubgrupo,K.IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexReqSal @pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,COUNT(Documento) AS SCANT,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS  P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('SAI','DSI')  AND  TipOrd=@pmTipOrd AND NumOrden=@pmNumOrden AND IdCiaOrd=@pmIdCiaOrd
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND  Servcios=0  
 GROUP BY  K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexSob @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT CdLocal,K.IdProducto AS CdProducto,DescripProd,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL
,SUM(VrPrecio*Entradas) AS SVALENT,SUM(VrPrecio*Salidas) AS SVALSAL
,SUM(VrDctoEnt) AS SDCTENT,SUM(VrDctoSal) AS SDCTSAL
,SUM(Sobretasa*Entradas) AS SSOBENT,SUM(Sobretasa*Salidas) AS SSOBSAL
,SUM(TasaNac*Entradas) AS SNACENT,SUM(TasaNac*Salidas) AS SNACSAL
,SUM(TasaDep*Entradas) AS SDEPENT,SUM(TasaDep*Salidas) AS SDEPSAL
,SUM(TasaMun*Entradas) AS SMUNENT,SUM(TasaMun*Salidas) AS SMUNSAL
,SUM(Soldicom*Entradas) AS SSOLENT,SUM(Soldicom*Salidas) AS SSOLSAL
,SUM(ImpGlobal*Entradas) AS SGLOENT,SUM(ImpGlobal*Salidas) AS SGLOSAL
,SUM(VrBase*Entradas) AS SBASEENT,SUM(VrBase*Salidas) AS SBASESAL
,MAX(Sobretasa) AS MAXSOB,MAX(TasaNac) AS MAXNAC,MAX(TasaDep) AS MAXDEP
,MAX(TasaMun) AS MAXMUN
,MAX(VrPrecio) AS MAXPRECIO,MAX(VrBase) AS MAXBASE,MAX(VrBruto) AS MAXBRUTO
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc IN ('FCR','FC1','FC2','FC3','FC4','FC5','DFC','DF1','DF2','DF3','DF4','DF5','PLA','DVP','PAS','DNP')
 AND  Fecha BETWEEN @pmFechaIni  AND @pmFechaFin
 AND Tanques<>0 AND NoVentas>0
 GROUP BY CdLocal,K.IdProducto,DescripProd
 ORDER BY CdLocal,K.IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexSobtasa @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null,@pmCdLocal VARCHAR(8)=Null,@pmIdDep VARCHAR(8)=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
 AS 
 SELECT K.IdTercero AS NitCliente,RazonSocial,CdAgencia,Agencia,CodAgencia,K.IdCia AS CdCia,Compania
,CdLocal,Localidad,LK.IdDep AS CodDep,Departamento,K.IdProducto AS  CdProducto,DescripProd,PM.IdUnd AS CdUnid,Unidad
,SUM(Entradas) AS TotalEnt,SUM(Salidas) AS TotalSal,SUM((Entradas*VrPrecio)-VrDctoEnt) AS VrTotalEnt,SUM((Salidas*VrPrecio)-VrDctoSal) AS VrTotalSal
,SUM(Sobretasa*Entradas) AS SobTasaEnt,SUM(Sobretasa*Salidas) AS SobTasaSal
,SUM(TasaNac*Entradas) AS TasaNacEnt,SUM(TasaNac*Salidas) AS TasaNacSal
,SUM(TasaDep*Entradas) AS TasaDepEnt,SUM(TasaDep*Salidas) AS TasaDepSal
,SUM(TasaMun*Entradas) AS TasaMunEnt,SUM(TasaMun*Salidas) AS TasaMunSal
,SUM(Soldicom*Entradas) AS SoldicomEnt,SUM(Soldicom*Salidas) AS SoldicomSal
,SUM(ImpGlobal*Entradas) AS ImpGlobalEnt,SUM(ImpGlobal*Salidas) AS ImpGlobalSal
,SUM(VrIvaEnt) AS TotalIvaEnt,SUM(VrIvaSal) AS TotalIvaSal,SUM(VrCostoEnt) AS CostoEnt,SUM(VrCostoSal) AS CostoSal
,MAX(VrPrecio) AS PrecioUnit,MAX(TarifaIva) AS TarifIva,MAX(Sobretasa) AS TarifSobtasa
,MAX(TasaNac) AS TarifNac,MAX(TasaDep) AS TarifDep,MAX(TasaMun) AS TarifMun,MAX(Soldicom) AS TarifSol,MAX(ImpGlobal) AS TarifIGlobal
,MAX(VrBruto) AS PrecioBase,MAX(VrBase) AS PrecioLista
,TipoId,Dv,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,DescripAbrv
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN UndMed AS U ON PM.IdUnd=U.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Companias AS CN ON K.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON K.IdTercero=T.IdTercero
 LEFT JOIN Localidades AS LK ON K.CdLocal=LK.IdLocal LEFT JOIN Departamentos AS DP ON LK.IdDep=DP.IdDep 
 LEFT JOIN Agencias  AS A ON K.CdAgencia=A.IdAgencia
 WHERE NoVentas IN (1,2,5,6) AND Tanques<>0 AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND EsProdBase<1 
 AND (Sobretasa>0 OR Soldicom>0 OR ImpGlobal>0)
 AND K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND K.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND CdLocal LIKE ISNULL(@pmCdLocal,'%') AND LK.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 GROUP BY K.IdTercero,RazonSocial,CdAgencia,Agencia,CodAgencia,K.IdCia,Compania
,CdLocal,Localidad,LK.IdDep,Departamento,K.IdProducto,DescripProd,PM.IdUnd,Unidad
,TipoId,Dv,TipoRef,PM.IdSubgrupo,Subgrupo,S.IdGrupo,Grupo,G.IdLinea,Linea,DescripAbrv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexSub @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdSubgrupo,K.IdProducto AS tmIdProducto,DescripProd,TipoRef,Combo,EsProdBase AS tmEsProdBase
,IdProv,COUNT(Item) AS SCANT,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
,SUM(VrCostoEnt) AS SCOSENT,SUM(VrCostoSal) AS SCOSSAL
,SUM(VrPrecio*Entradas) AS SVALENT,SUM(VrPrecio*Salidas) AS SVALSAL
,SUM(VrBruto*Entradas) AS SBRUENT,SUM(VrBruto*Salidas) AS SBRUSAL
,SUM(Entradas*ImpGlobal) AS SGLOENT,SUM(Salidas*ImpGlobal) AS SGLOSAL
,SUM(Sobretasa*Entradas) AS SSOBENT,SUM(Sobretasa*Salidas) AS SSOBSAL
,SUM(TasaNac*Entradas) AS SNACENT,SUM(TasaNac*Salidas) AS SNACSAL
,SUM(TasaDep*Entradas) AS SDEPENT,SUM(TasaDep*Salidas) AS SDEPSAL
,SUM(TasaMun*Entradas) AS SMUNENT,SUM(TasaMun*Salidas) AS SMUNSAL
,SUM(Soldicom*Entradas) AS SSOLENT,SUM(Soldicom*Salidas) AS SSOLSAL
,SUM(CASE WHEN  Salidas>0 THEN VrIvaSal ELSE VrIvaEnt END) AS SIVA
,SUM(CASE WHEN  Salidas>0 THEN VrDctoSal ELSE VrDctoEnt END) AS SDCT
,SUM(CASE WHEN  Salidas>0 THEN VrReteSal ELSE VrReteEnt END) AS SRET
,SUM(CASE WHEN  Salidas>0 THEN VrIcaSal ELSE VrIcaEnt END) AS SICA
,SUM(OtroImpto*Entradas) AS SOTRENT,SUM(OtroImpto*Salidas) AS SOTRSAL
,SUM(Rec_Costo*Entradas) AS SRCOSENT,SUM(Rec_Costo*Salidas) AS SRCOSSAL
--Para los ajustes con cantidad en cero (0)
,SUM(VrUnitario) AS SCOSAJU,SUM(OtroImpto) AS SDVEAJU
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 GROUP BY  IdSubgrupo,K.IdProducto,DescripProd,TipoRef,Combo,EsProdBase,IdProv
 ORDER BY IdSubgrupo,K.IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexSubc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdSubgrupo,K.IdProducto AS tmIdProducto,DescripProd
,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
,SUM(VrCostoEnt) AS SCOSENT,SUM(VrCostoSal) AS SCOSSAL
,SUM(VrPrecio*Entradas) AS SVALENT,SUM(VrPrecio*Salidas) AS SVALSAL
,SUM(CASE WHEN  Salidas>0 THEN VrIvaSal ELSE VrIvaEnt END) AS SIVA
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND EsCombo<>0 AND Combo<>0 AND TipoRef<>'SERVICIO'  AND  EsProdBase=0
 GROUP BY IdSubgrupo,K.IdProducto,DescripProd
 ORDER BY IdSubgrupo,K.IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexTan @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CodProducto,DescripProd,Descripcion,ExtciaAct,VrCosto,VrCostPmd,K.IdBodega AS CodBodega,CdTanque
,Entradas,Salidas,VrUnitario,VrUnitario*Entradas AS SCOSENT,VrUnitario*Salidas AS SCOSSAL
,VrPrecio,VrPrecio*Entradas AS SVALENT,VrPrecio*Salidas AS SVALSAL
,VrCostProm,VrBruto,CdProdEquiv,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase
,ExtciaMin,ExtciaMax,Seriales,Lotes,Combo,TipoRef
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND  Tanques<>0
 ORDER BY  K.IdProducto,CdTanque,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardexTdoc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT K.IdProducto AS CodProducto,DescripProd,SUM(Entradas) AS STOTENT,SUM(Salidas) AS STOTSAL,SUM(Unidades) AS STOTUNI
 FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY K.IdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryKardex_Cr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipDoc VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmnAnnoAnt INT=Null,@pmnMesAnt INT=Null
 AS 
 SELECT  K.IdProducto AS CdProducto,DescripProd,TipDoc,Documento,K.IdCia AS CdCia,Compania,Fecha,Item,K.IdBodega AS CdBodega,Bodega
,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMed,VrUnitario,VrCostoEnt,VrCostoSal,VrCostProm,VrPrecio,VrPrecio*(Entradas+Salidas) AS VrTotal,TarifaDct,VrDctoEnt,VrDctoSal
,TarifaIva,VrIvaEnt,VrIvaSal,TarifaRet,VrReteEnt,VrReteSal,TarifaIca,VrIcaEnt,VrIcaSal,Unidades,K.Descripcion AS KarDescripcion,K.Referencia AS Referncia,Referencia2,FecOrden
,K.IdConcepto AS CdConcepto,Concepto,K.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,CdAgencia,Agencia,CodAgencia,K.IdVend AS NitVend,VN.RazonSocial AS Vendedor,Comision
,CdOperario,OP.RazonSocial AS NomOperario,ComisnOper,pVehiculo,CdLocal,LK.Localidad AS KarCiudad,CdCCosto,CCosto,CdSubCos,SubCosto,TipOrd,NumOrden,IdCiaOrd,Factura,Remision,IdCiaRem,Cotizacion,IdCiaCot
,TipDocDev,NumDocDev,CdUbic,NumLote,FechLote,Comptmntos,CdMngra,NumInicial,NumFinal,Sobretasa,TasaNac,TasaDep,TasaMun
,Soldicom,ImpGlobal,OtroImpto,Rec_Costo,ListaPrec,VrBruto,VrBase,Servcios,NoVentas,EsCombo,EsProdBase,ItemCombo
,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,TimeSys,K.IdUsuario AS IdUsuari,Usuario,TipoDoc
--Datos del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono
--datos del producto
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,Tamano,Color,PM.UndMed AS CdUndMed,SU.Unidad AS ProdUnidad,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,IvaInc,IdTarIva,Tarifa,Simbolo
,Seriales,Lotes,Combo,Tanques,DescripLong,DescripAbrv,Precio1,Precio2,Precio3,Precio4,Precio5,IdProv,PV.RazonSocial AS NomProveedor
,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,PM.Inactivo AS ProdInactivo
--Información del saldo anterior
,SaldoAntProd,CostoAntProd,SaldoAntBod,CostoAntBod
 FROM Trn_Kardex AS K INNER JOIN Companias AS CN ON K.IdCia=CN.IdCia 
 INNER JOIN Sys_TiposDoc AS TD ON K.TipDoc=TD.IdDoc
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed   INNER JOIN Terceros AS PV ON PM.IdProv=PV.IdTercero
 INNER JOIN  Conceptos AS C ON K.IdConcepto=C.IdConcepto 
 INNER JOIN Terceros AS T ON K.IdTercero=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON K.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Terceros AS VN ON K.IdVend=VN.IdTercero
 LEFT JOIN Agencias  AS A ON K.CdAgencia=A.IdAgencia
 LEFT JOIN CentroCosto AS CC ON K.CdCCosto=CC.IdCCosto LEFT JOIN SubCentros AS SC ON K.CdSubCos=SC.IdSubCos
 LEFT JOIN  Localidades AS LK ON K.CdLocal=LK.IdLocal 
 LEFT JOIN Terceros AS OP ON K.CdOperario=OP.IdTercero
 LEFT JOIN Tablapor AS TI ON PM.IdTarIva=TI.IdTarifa
--Saldos anteriores
 LEFT JOIN (SELECT IdProducto,SUM(SaldoAnt+Entradas-Salidas) AS SaldoAntProd,SUM(CostoAnt+CostoEnt-CostoSal) AS CostoAntProd 
 FROM  AcuInventario
 WHERE  nAnno=@pmnAnnoAnt AND nMes=@pmnMesAnt
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 GROUP BY IdProducto) AS SP ON K.IdProducto=SP.IdProducto
--Saldos por bodega
 LEFT JOIN (SELECT IdProducto,IdBodega,SUM(SaldoAnt+Entradas-Salidas) AS SaldoAntBod,SUM(CostoAnt+CostoEnt-CostoSal) AS CostoAntBod
 FROM  AcuInventario
 WHERE  nAnno=@pmnAnnoAnt AND nMes=@pmnMesAnt
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 GROUP BY IdProducto,IdBodega) AS SB ON K.IdProducto=SB.IdProducto AND K.IdBodega=SB.IdBodega
 WHERE TipDoc NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC')  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%')  AND  K.IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND K.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND K.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
 ORDER BY DescripProd,Fecha,TimeSys
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLineas @pmIdLinea VARCHAR(8)
 AS 
 SELECT IdLinea,Linea,TipoInvent,Inactivo
 FROM Lineas
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLineasDso @pmTipoInvent VARCHAR(10)=Null
 AS 
 SELECT IdLinea,Linea,Linea+ ' '+IdLinea AS DsLin
 FROM Lineas
 WHERE Inactivo=0 AND TipoInvent LIKE ISNULL(@pmTipoInvent,'%')
 ORDER BY Linea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLineasLta @pmTipoInvent VARCHAR(10)=Null
 AS 
  SELECT IdLinea,Linea,TipoInvent
  FROM Lineas
  WHERE Inactivo=0 AND TipoInvent LIKE ISNULL(@pmTipoInvent,'%')
  ORDER BY Linea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLocalidades @pmIdLocal VARCHAR(8)
 AS 
 SELECT IdLocal,Localidad,IdDep,TipLocal,Dist_Kmt,Time_hh,DistSede,TimeSede,CelRow,CelCol,CodAlias,Inactivo
 FROM Localidades
 WHERE IdLocal=@pmIdLocal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLocalidadesDep @pmIdLocal VARCHAR(8)
 AS 
 SELECT IdLocal,Localidad,L.IdDep AS CodDep,Departamento,TipLocal
 FROM Localidades AS L INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE IdLocal=@pmIdLocal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLocalidadesDso @pmIdDep VARCHAR(8)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdLocal,Localidad,L.IdDep AS CodDep,Departamento,TipLocal
, Localidad+ '  ('+ Departamento + ')' AS NomCiudad 
 FROM Localidades AS L INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE  L.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND (L.Inactivo=ISNULL(@pmInactivo,0) or  L.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Localidad,Departamento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLocalidadesLta @pmIdDep VARCHAR(8)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdLocal,Localidad,L.IdDep AS CodDep,Departamento,TipLocal,Dist_Kmt,Time_hh
,DistSede,TimeSede,CelRow,CelCol,CodAlias,L.Inactivo AS Inactvo
 FROM Localidades AS L INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE  L.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND (L.Inactivo=ISNULL(@pmInactivo,0) or  L.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Localidad
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLocalidadesNom @pmLocalidad VARCHAR(50)
 AS 
 SELECT IdLocal,Localidad,IdDep,TipLocal
 FROM Localidades
 WHERE Localidad=@pmLocalidad
 ORDER BY IdLocal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLog_Nomina @pmIdNom VARCHAR(4),@pmIdPeriodo VARCHAR(8)
 AS 
 SELECT IdPeriodo,IdNom,Cerrado,IntCtaCte,LogCtaCte
 FROM Log_Nomina
 WHERE IdNom LIKE ISNULL(@pmIdNom,'%') AND IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 ORDER BY IdPeriodo,IdNom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLog_Periodos @pmAnno INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdCia,Anno,Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic,CierreAnno
 FROM Log_Periodos
 WHERE IdCia=@pmIdCia  AND Anno=@pmAnno
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLog_PeriodosLta @pmAnno INT,@pmIdCia CHAR(2)
 AS 
 SELECT L.IdCia AS CdCia,Compania,Anno,Ene,Feb,Mar,Abr,May,Jun,Jul,Ago,Sep,Oct,Nov,Dic,CierreAnno
 FROM Log_Periodos AS L INNER JOIN Companias AS C ON L.IdCia=C.IdCia
 WHERE (Anno>=ISNULL(@pmAnno,0)  AND Anno<=ISNULL(@pmAnno,2147483647))
 AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER  BY L.IdCia,Anno
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotMov @pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,NumLote,IdProducto,IdBodega,CdUbic,Entradas,Salidas,Fecha,FecVence,Linea
 FROM Trn_LotMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotMovGru @pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCia CHAR(2)
 AS 
 SELECT NumLote,M.IdProducto AS CodProducto,DescripProd,M.IdBodega AS CdBodega,CdUbic
,SUM(Entradas) AS SENT,SUM(Salidas) AS SSAL
 FROM  Trn_LotMov  AS M INNER JOIN ProdMcias AS P ON M.IdProducto=P.IdProducto
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 GROUP BY NumLote,M.IdProducto,DescripProd,M.IdBodega,CdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotMovLta @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmLinea INT=Null
 AS 
 SELECT NumLote,FecVence,Linea,Entradas,Salidas,M.IdProducto AS CdProducto,DescripProd,M.IdBodega AS CdBodega
,Bodega,CdUbic,Fecha,Item,TipDoc,Documento,IdCia
 FROM Trn_LotMov  AS M INNER JOIN ProdMcias AS P ON M.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON M.IdBodega=B.IdBodega
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
 AND M.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND M.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND (Linea>=ISNULL(@pmLinea,0)  AND Linea<=ISNULL(@pmLinea,2147483647))
 ORDER BY NumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotMov_Cr @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT M.TipDoc AS TipoDoc,Documento,M.IdCia AS CdCia,Item,M.NumLote AS Num_Lote,M.IdProducto AS CdProducto,DescripProd,M.IdBodega AS CdBodega,Bodega
,CdUbic,Entradas,Salidas,M.Fecha AS FechaDoc,M.FecVence AS Fec_Vence,Linea
--datos de lotes
,L.Fecha AS LotFecha,L.FecVence AS LotFecVence,SaldoActual,NitProv,RazonSocial,L.TipDoc AS TipoDocEnt,Entrada
 FROM Trn_LotMov  AS M INNER JOIN Trn_Lotes AS L ON M.NumLote=L.NumLote AND M.IdProducto=L.IdProducto
 AND M.IdCia=L.IdCia AND M.IdBodega=L.IdBodega
 INNER JOIN ProdMcias AS P ON M.IdProducto=P.IdProducto INNER JOIN Bodegas AS B ON M.IdBodega=B.IdBodega
 LEFT JOIN Terceros AS T ON L.NitProv=T.IdTercero 
 WHERE M.TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin  AND M.IdCia=@pmIdCia
 AND M.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotes @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
,@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10)
 AS 
 SELECT NumLote,IdProducto,IdCia,IdBodega,IdUbic,Fecha,FecVence,SaldoActual,NitProv,TipDoc,Entrada,TimeSys
 FROM Trn_Lotes
 WHERE NumLote=@pmNumLote AND IdProducto=@pmIdProducto AND IdCia=@pmIdCia
 AND IdBodega=@pmIdBodega AND IdUbic LIKE ISNULL(@pmIdUbic,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotesLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmFecVenceIni SMALLDATETIME,@pmFecVenceFin SMALLDATETIME,@pmNumLote VARCHAR(30)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdBodega VARCHAR(4)=Null,@pmProdInac BIT=Null
 AS 
 SELECT NumLote,Fecha,FecVence,SaldoActual,L.IdProducto AS CdProducto,DescripProd,L.IdBodega AS CdBodega,Bodega
,L.IdUbic AS IdUbica,NitProv,RazonSocial,TipDoc,Entrada,IdCia,TimeSys
 FROM Trn_Lotes AS L INNER JOIN ProdMcias AS P  ON L.IdProducto=P.IdProducto
  INNER JOIN Bodegas AS B ON L.IdBodega=B.IdBodega
 LEFT JOIN Terceros AS T ON L.NitProv=T.IdTercero
 WHERE NumLote LIKE ISNULL(@pmNumLote,'%') AND L.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND L.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME))) 
 AND (FecVence>=ISNULL(@pmFecVenceIni,CAST('19100101' AS SMALLDATETIME)) AND FecVence<=ISNULL(@pmFecVenceFin,CAST('20781230' AS SMALLDATETIME)))
 AND (P.Inactivo=ISNULL(@pmProdInac,0) or P.Inactivo=ISNULL(@pmProdInac,1))
 ORDER BY L.IdProducto,L.IdBodega,FecVence,NumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotesPro @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)=Null
,@pmIdBodega VARCHAR(4)=Null,@pmIdUbic VARCHAR(10)=Null
 AS 
 SELECT NumLote,IdProducto,IdCia,IdBodega,IdUbic,Fecha,FecVence,SaldoActual,NitProv,TipDoc,Entrada,TimeSys
 FROM Trn_Lotes
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdBodega LIKE ISNULL(@pmIdBodega,'%') AND IdUbic LIKE ISNULL(@pmIdUbic,'%')
 ORDER BY IdBodega,IdCia,FecVence

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotesSal @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
,@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10)=Null
 AS 
 SELECT SUM(SaldoActual) AS SSALDO
 FROM Trn_Lotes
 WHERE NumLote=@pmNumLote AND IdProducto=@pmIdProducto AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdBodega LIKE ISNULL(@pmIdBodega,'%') AND IdUbic LIKE ISNULL(@pmIdUbic,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryLotes_Cr @pmFecha SMALLDATETIME,@pmSaldoActual DECIMAL(14,4),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmFecVenceIni SMALLDATETIME,@pmFecVenceFin SMALLDATETIME,@pmNumLote VARCHAR(30)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdBodega VARCHAR(4)=Null,@pmProdInac BIT=Null
,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null
 AS 
 SELECT L.IdCia AS CdCia,Compania,L.IdProducto AS CdProducto,DescripProd,NumLote,L.IdBodega AS CdBodega,Bodega
,Fecha,FecVence,DATEDIFF(day,@pmFecha,FecVence) AS DiasVence,SaldoActual,NitProv,RazonSocial,TipDoc,Entrada,L.IdUbic AS CdUbic,TimeSys
--datos del producto
,Referencia,CodBarras,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
,Tamano,P.IdUnd AS CdUnid,Unidad,Seriales,Combo,DescripLong,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5
,FecUltcom,FecUltVta,P.Inactivo AS Pro_Inactivo
 FROM Trn_Lotes AS L INNER JOIN ProdMcias AS P  ON L.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON L.IdBodega=B.IdBodega  INNER JOIN Companias AS CI ON L.IdCia=CI.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd 
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 LEFT JOIN Terceros AS T ON L.NitProv=T.IdTercero
 WHERE SaldoActual>@pmSaldoActual AND  NumLote LIKE ISNULL(@pmNumLote,'%')
 AND L.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND L.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND L.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND SG.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%') AND P.IdMarca LIKE ISNULL(@pmIdMarca,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME))) 
 AND (FecVence>=ISNULL(@pmFecVenceIni,CAST('19100101' AS SMALLDATETIME)) AND FecVence<=ISNULL(@pmFecVenceFin,CAST('20781230' AS SMALLDATETIME)))
 AND (P.Inactivo=ISNULL(@pmProdInac,0) or P.Inactivo=ISNULL(@pmProdInac,1))
 ORDER BY L.IdProducto,L.IdBodega,FecVence,NumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcas @pmIdMarca VARCHAR(4)
 AS 
 SELECT IdMarca,Marca,TipoMarca,CodigoMT,Inactivo
 FROM Marcas
 WHERE IdMarca=@pmIdMarca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcasLin @pmIdLinea VARCHAR(10)
 AS 
 SELECT IdLinea,LineaVeh,IdMarca,CodMT,Inactivo
 FROM MarcasLin
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcasLinDso @pmIdMarca VARCHAR(4)=Null
 AS 
 SELECT IdLinea,LineaVeh,LineaVeh+IdLinea+' ('+Marca+')' AS DsLin
 FROM MarcasLin AS L INNER JOIN Marcas AS M ON L.IdMarca=M.IdMarca
 WHERE L.Inactivo=0 AND L.IdMarca LIKE ISNULL(@pmIdMarca,'%')
 ORDER BY Marca,LineaVeh
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcasLinLta @pmIdMarca VARCHAR(4)=Null
 AS 
 SELECT IdLinea,LineaVeh,L.IdMarca AS CdMarca,Marca,CodMT,TipoMarca
 FROM MarcasLin AS L INNER JOIN Marcas AS M ON L.IdMarca=M.IdMarca
 WHERE L.Inactivo=0 AND L.IdMarca LIKE ISNULL(@pmIdMarca,'%')
 ORDER BY Marca,LineaVeh
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMarcasLta  @pmTipoMarca VARCHAR(20)=Null
 AS 
 SELECT IdMarca,Marca,TipoMarca,CodigoMT
 FROM Marcas
 WHERE Inactivo=0  AND ( TipoMarca='GENERAL'  OR  TipoMarca LIKE ISNULL(@pmTipoMarca,'%') )
 ORDER BY Marca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayAnotaciones @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT Item,Fecha,Detalle,IdUsuario,NContrato,IdCia
 FROM Trn_MayAnotaciones
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayAnotacionesItm @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_MayAnotaciones
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonCuentas @pmIdBonif VARCHAR(4),@pmTipoCue INT,@pmCdTipo VARCHAR(4)
 AS 
 SELECT IdBonif,TipoCue,CdTipo,IdCuenta
 FROM MayBonCuentas
 WHERE IdBonif=@pmIdBonif AND TipoCue=@pmTipoCue AND CdTipo=@pmCdTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonCuentasLta @pmIdBonif VARCHAR(4)=Null
 AS 
 SELECT C.IdBonif AS CdBonif,TipoBonif,TipoCue,CdTipo,C.IdCuenta AS CdCuenta,NomCuenta
 FROM MayBonCuentas AS C INNER JOIN MayTiposBonif  AS B ON C.IdBonif=B.IdBonif
 INNER JOIN  Puc AS P ON C.IdCuenta=P.IdCuenta
 WHERE C.IdBonif LIKE ISNULL(@pmIdBonif ,'%')
 ORDER BY C.IdBonif,TipoCue
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonCuentasTip @pmIdBonif VARCHAR(4),@pmCdTipo VARCHAR(4)
 AS 
 SELECT IdBonif,TipoCue,CdTipo,IdCuenta
 FROM MayBonCuentas
 WHERE IdBonif=@pmIdBonif  AND (CdTipo=@pmCdTipo  OR CdTipo='0')
 ORDER BY TipoCue,CdTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonif @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT NContrato,IdCia,Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdProducto,CdSubgrupo
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonifCant @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT  COUNT(Item) AS SCANT
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonifDet @pmNContrato INT,@pmIdCia CHAR(2),@pmTipoProd VARCHAR(20)=Null
 AS 
 SELECT Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdSubgrupo
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  AND CdSubgrupo<>'0'
 AND TipoProd LIKE ISNULL(@pmTipoProd,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonifItm @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonifLta @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,TipoProd,VrInicial,VrFinal,Unidad,Periodo,Tarifa,TipoTarifa
,CdSubgrupo,Subgrupo,NContrato,IdCia
 FROM Trn_MayBonif  AS  B  LEFT JOIN SubGrupos AS S ON B.CdSubgrupo=S.IdSubgrupo
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayBonifSub @pmNContrato INT,@pmIdCia CHAR(2),@pmCdSubgrupo VARCHAR(8)=Null
,@pmTipoProd VARCHAR(20)=Null
 AS 
 SELECT Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdSubgrupo
 FROM Trn_MayBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
  AND CdSubgrupo LIKE ISNULL(@pmCdSubgrupo,'%')
  AND TipoProd LIKE ISNULL(@pmTipoProd,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContDoc @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT NContrato,IdCia,Item,IdDoc,FechaRad,Dependencia,Estado,FechaAprob,Comentarios,CdUsuario
 FROM Trn_MayContDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContDocDep @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT Dependencia,COUNT(Item) AS SCANT
 FROM Trn_MayContDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 GROUP BY Dependencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContDocItm @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM Trn_MayContDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContDocLta @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,Dependencia,D.IdDoc AS CdDoc,NombreDoc,FechaRad,Estado,Comentarios,FechaAprob
,CdUsuario,NContrato,IdCia
 FROM Trn_MayContDoc AS D INNER JOIN MayTiposDoc AS T ON D.IdDoc=T.IdDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContDocPen @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM Trn_MayContDoc
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Estado<>'APROBADO'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContratos @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT NContrato,IdCia,Fecha,IdCliente,IdAgencia,IdTipo,IdBonif,ConvBonif,CuotaFija,CondPago,DiasGracia,ModPlazo,Plazo
,FechaVence,ModRecursos,VrInversion,VrInventario,VrCuota,ModCuota,IdBandera,IdVend,AprobJunta,FechaAprob,ObservAprob
,Observacion,IdEstado,FechaInicio,CuotaMin,TipoAprob,NumInterno,InvCombus,CuotaBase,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
,FechaFirma,CdAprobNeg,ArchivoAprob,ClaseContrato,DescTipoCont,GarantHipoteca,GarantPrenda,GarantPoliza,GarantBanco,GarantPagare
,GarantOtro,CdTipoBien,VrAvaludo,NroEscritura,CdTipoPoliza,DescPoliza,VigencPoliza,FecVencPoliza,ValorPoliza,EdoGarantias
,DescGarantOtro,InverContab,InverContrac,VolContMes,VolContTotal,VolViabMes,VolViabTotal,InvCtbObra,InvCtbCapital,InvCtbEfectivo,InvCtbImagen
,InvCtbMntmto,FecDesemb,InvContObra,InvContCapital,InvContEfectivo,InvContImagen,CrtlAprovInv,CrtlAprovImg,CdTipoBloq,EdoDocmtos,DescEdoDoc,RentabInicial,GraciaBonif
 FROM Trn_MayContratos
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContratosAct  @pmIdCia CHAR(2)=Null
 AS 
 SELECT C.NContrato AS NumContrato,IdCia,Fecha,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia
,IdTipo,C.IdBonif AS CdBonif,TipoBonif,AboCartera,AboPstmo,ConvBonif,CuotaFija,CondPago,C.DiasGracia AS Dias_Gracia,ModPlazo,C.Plazo AS PlazoCont
,FechaVence,VrCuota,ModCuota,C.IdBandera AS CdBandera,C.IdVend AS CdVend,C.IdEstado AS CdEstado,FechaInicio,CuotaMin,TipoAprob,NumInterno,InvCombus,CuotaBase
--datos del cliente
,CL.IdPlazo AS CliCdPlazo,PZ.Plazo AS CliPlazo,PZ.NVmto AS CliNVce,PZ.DiasPago AS CliDiasPago
,CL.IdVend AS CliVend,VrCupo,VrSaldo,CL.IdEstado AS CliEstado
--datos de agencia
,A.IdPlazo AS AgeCdPlazo,PA.Plazo AS AgePlazo,PA.NVmto AS AgeNVce,PA.DiasPago AS AgeDiasPago
,A.IdVend AS AgeCdVend,VrCupoCre,VrSaldoAct,Referencia,A.IdEstado AS AgeEstado,A.Inactivo AS AgeInactivo
 FROM Trn_MayContratos AS C INNER JOIN MayTiposBonif AS B ON C.IdBonif=B.IdBonif
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero 
 INNER JOIN TercCliente AS CL ON C.IdCliente=CL.IdClie
 INNER JOIN Plazos AS PZ ON CL.IdPlazo=PZ.IdPlazo
 LEFT JOIN  Agencias   AS A ON C.IdAgencia=A.IdAgencia
 LEFT JOIN Plazos AS PA ON A.IdPlazo=PA.IdPlazo
 WHERE   C.IdEstado<>'0002' AND C.IdEstado<>'9999' AND AprobJunta>0 
 AND ( ConvBonif<>0 OR CuotaFija<>0) AND CL.Inactivo=0
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia,C.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContratosCli @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmNContrato INT
 AS 
 SELECT NContrato,IdCia,Fecha,IdCliente,IdAgencia,IdTipo,IdBonif
 FROM Trn_MayContratos
 WHERE IdCliente=@pmIdCliente AND IdEstado IN ('0001','0002') AND (IdAgencia=@pmIdAgencia OR IdAgencia='0')
 AND NContrato<>@pmNContrato
 ORDER BY IdCia,NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContratosId @pmIdCia CHAR(2)
 AS 
 SELECT MAX(NContrato) AS MXITM
 FROM Trn_MayContratos
 WHERE  IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContratosLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmNContratoIni INT=Null,@pmNContratoFin INT=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdTipo VARCHAR(4)=Null
,@pmIdBonif VARCHAR(4)=Null,@pmConvBonif BIT=Null,@pmAprobJuntaIni INT=Null,@pmAprobJuntaFin INT=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT C.NContrato AS NumContrato,C.IdCia AS CdCia,Compania,Fecha,FechaInicio,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia,C.IdTipo AS CdTipo,TipContrato,C.IdBonif AS CdBonif,TipoBonif
,ConvBonif,CuotaFija,CondPago,C.DiasGracia AS Dias_Gracia,ModPlazo,Plazo,FechaVence,ModRecursos,VrInversion,VrInventario,VrCuota,CuotaMin,CuotaBase,ModCuota,C.IdBandera AS CdBandera,TipoBandera
,C.IdVend AS CdVend,V.RazonSocial AS Vendedor,TipoAprob,AprobJunta,FechaAprob,ObservAprob,C.Observacion AS Observ,NumInterno,InvCombus
,FechaFirma,CdAprobNeg,ArchivoAprob,ClaseContrato,DescTipoCont,GarantHipoteca,GarantPrenda,GarantPoliza,GarantBanco,GarantPagare
,GarantOtro,CdTipoBien,VrAvaludo,NroEscritura,CdTipoPoliza,DescPoliza,VigencPoliza,FecVencPoliza,ValorPoliza,EdoGarantias
,DescGarantOtro,InverContab,InverContrac,C.VolContMes AS VlmenContMes,C.VolContTotal AS VlmenContTotal,C.VolViabMes AS VlmenViabMes,C.VolViabTotal AS VlmenViabTotal,C.InvCtbObra AS VrCtbObra,C.InvCtbCapital AS VrCtbCapital
,C.InvCtbEfectivo AS VrCtbEfectivo,C.InvCtbImagen AS VrCtbImagen,C.InvCtbMntmto AS VrCtbMnto,C.FecDesemb AS FechDesemb,C.InvContObra AS VrContObra,C.InvContCapital AS VrContCaptial,C.InvContEfectivo AS VrContEfectivo,C.InvContImagen AS VrContImagen
,C.CrtlAprovInv AS ConCrtlInv,C.CrtlAprovImg AS ConCrtlImag,CdTipoBloq,EdoDocmtos,DescEdoDoc,RentabInicial,GraciaBonif
,C.IdEstado AS CdEstado,Estado,NColor,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
 FROM Trn_MayContratos AS C INNER JOIN MayTipos AS TC ON C.IdTipo=TC.IdTipo
 INNER JOIN MayTiposBonif AS B ON C.IdBonif=B.IdBonif INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia  INNER JOIN  MayEstados AS E ON C.IdEstado=E.IdEstado
 INNER JOIN  TiposBan AS TB ON C.IdBandera=TB.IdBandera INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero  LEFT JOIN  Agencias   AS A ON C.IdAgencia=A.IdAgencia
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND C.NContrato BETWEEN ISNULL(@pmNContratoIni,0) AND ISNULL(@pmNContratoFin,2147483647)
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND C.IdTipo LIKE ISNULL(@pmIdTipo,'%') AND C.IdBonif LIKE ISNULL(@pmIdBonif,'%')
 AND (ConvBonif=ISNULL(@pmConvBonif,0) or ConvBonif=ISNULL(@pmConvBonif,1))
 AND AprobJunta BETWEEN ISNULL(@pmAprobJuntaIni,-1) AND ISNULL(@pmAprobJuntaFin,2147483647)
 AND C.IdEstado  LIKE ISNULL(@pmIdEstado,'%') AND C.IdVend LIKE ISNULL(@pmIdVend,'%')
 ORDER BY C.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayContratos_Cr  @pmNContratoIni INT=Null,@pmNContratoFin INT=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdTipo VARCHAR(4)=Null,@pmIdBonif VARCHAR(4)=Null,@pmConvBonif BIT=Null,@pmAprobJuntaIni INT=Null,@pmAprobJuntaFin INT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 SELECT C.NContrato AS NumContrato,C.IdCia AS CdCia,Compania,Fecha,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia,C.IdTipo AS CdTipo,TipContrato,C.IdBonif AS CdBonif,TipoBonif
,ConvBonif,CuotaFija,CondPago,C.DiasGracia AS Dias_Gracia,ModPlazo,Plazo,FechaVence,ModRecursos,VrInversion,VrInventario,VrCuota,ModCuota,C.IdBandera AS CdBandera,TipoBandera
,C.IdVend AS CdVend,V.RazonSocial AS Vendedor,AprobJunta,FechaAprob,ObservAprob,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,NColor,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS CdUsuario,Usuario
,FechaInicio,CuotaMin,TipoAprob,NumInterno,InvCombus,CuotaBase
--datos de bonificaciones
,Item,TipoProd,VrInicial,VrFinal,Unidad,Tarifa,TipoTarifa,Periodo,CdProducto,DescripProd,CdSubgrupo,Subgrupo
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_MayContratos AS C INNER JOIN MayTipos AS TC ON C.IdTipo=TC.IdTipo
 INNER JOIN MayTiposBonif AS B ON C.IdBonif=B.IdBonif INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN Companias AS CN ON C.IdCia=CN.IdCia  INNER JOIN  MayEstados AS E ON C.IdEstado=E.IdEstado
 INNER JOIN  TiposBan AS TB ON C.IdBandera=TB.IdBandera INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero 
 INNER JOIN Trn_MayBonif AS MB ON C.NContrato=MB.NContrato AND C.IdCia=MB.IdCia
 INNER JOIN TercCliente AS CLI ON C.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Agencias   AS A ON C.IdAgencia=A.IdAgencia
 LEFT JOIN SubGrupos AS SG ON MB.CdSubgrupo=SG.IdSubgrupo
 LEFT JOIN ProdMcias AS PM ON MB.CdProducto=PM.IdProducto
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE  C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.NContrato BETWEEN ISNULL(@pmNContratoIni,0) AND ISNULL(@pmNContratoFin,2147483647)
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND C.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND C.IdTipo LIKE ISNULL(@pmIdTipo,'%') AND C.IdBonif LIKE ISNULL(@pmIdBonif,'%')
 AND (ConvBonif=ISNULL(@pmConvBonif,0) or ConvBonif=ISNULL(@pmConvBonif,1))
 AND AprobJunta BETWEEN ISNULL(@pmAprobJuntaIni,-1) AND ISNULL(@pmAprobJuntaFin,2147483647)
 AND C.IdEstado  LIKE ISNULL(@pmIdEstado,'%') AND C.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY C.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayEstados @pmIdEstado VARCHAR(4)
 AS 
 IF @pmIdEstado IS NULL
  SELECT IdEstado,Estado,NColor
  FROM MayEstados
  WHERE Inactivo=0
 ORDER BY  IdEstado
 ELSE
  SELECT IdEstado,Estado,NColor,Inactivo
  FROM MayEstados
  WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayInventario @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT NContrato,IdCia,Item,IdArticulo,Detalle,Cantidad,VrUnitario,VrTotal,TipoArticulo,Referencia,IdTipo
,TipDoc,Documento,IdCiaDoc,CantEntrega,FecEntrega,Estado
,CdProd,NumReq,IdCiaReq,CantReq,NumOrden,IdCiaOrd,CantOrden
 FROM Trn_MayInventario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
  AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayInventarioItm @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM  FROM Trn_MayInventario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayInventarioLta @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,I.IdArticulo AS CodArticulo,Detalle,Cantidad,UndMed,VrUnitario,VrTotal,TipoArticulo,Referencia
,I.IdTipo AS CdTipo,TipContrato,Estado,NumReq,IdCiaReq,CantReq,NumOrden,IdCiaOrd,CantOrden
,TipDoc,Documento,IdCiaDoc,CantEntrega,FecEntrega,NContrato,IdCia,CdProd,Descrpcion
 FROM Trn_MayInventario AS I INNER JOIN MayItemsInv AS R ON I.IdArticulo=R.IdArticulo
 INNER JOIN MayTipos AS T ON I.IdTipo=T.IdTipo
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia  
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayInventarioTot @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT COUNT(Item) AS SCOUNT,SUM(Cantidad) AS SCANT,SUM(VrTotal) AS SVALTOT
,SUM(CantEntrega) AS SCANENT,SUM(CantReq) AS SCANTREQ,SUM(CantOrden) AS SCANTODC
 FROM Trn_MayInventario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayItemsInv @pmIdArticulo VARCHAR(16)
 AS 
 SELECT IdArticulo,Descrpcion,UndMed,GrupoArt,CdProducto,Inactivo
 FROM MayItemsInv
 WHERE IdArticulo=@pmIdArticulo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayItemsInvGru
 AS 
 SELECT GrupoArt,count(IdArticulo) AS SCANT
 FROM MayItemsInv
 WHERE Inactivo=0
 GROUP BY GrupoArt
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayItemsInvLta @pmInactivo BIT=Null
 AS 
 SELECT IdArticulo,Descrpcion,I.UndMed AS Umed,Unidad,GrupoArt,CdProducto,DescripProd,I.Inactivo AS Inactiv
 FROM MayItemsInv AS I INNER JOIN   Sys_Um AS U ON I.UndMed=U.UndMed
 LEFT JOIN  ProdMcias AS P ON I.CdProducto=P.IdProducto
 WHERE  (I.Inactivo=ISNULL(@pmInactivo,0) or I.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Descrpcion

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqDetalleLta @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT D.NContrato AS NumContrato,IdCiaCon,Item,D.IdSubgrupo AS CdSubgrupo,Subgrupo,Combustible,VenGalones,Unidades,UnidDev
,Unidades-UnidDev AS UnidNeto,VenTotal,ValorTotal,ValorDev,ValorTotal-ValorDev AS VrTotal,Intervalo,Margen,Subsidio,Tarifa,TipoTarifa,VrLiquida,LogDetalle
,FechaInicio,IdCliente,RazonSocial,CN.IdAgencia AS Id_Agencia,Agencia
 FROM Trn_MayLiqDetalle AS D  INNER JOIN Trn_MayContratos AS CN ON D.NContrato=CN.NContrato AND D.IdCiaCon=CN.IdCia
 INNER JOIN  SubGrupos AS S ON D.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Terceros AS T ON CN.IdCliente=T.IdTercero
 LEFT JOIN Agencias AS A ON CN.IdAgencia=A.IdAgencia
 WHERE NLiquida=@pmNLiquida AND D.IdCia=@pmIdCia 
 ORDER BY D.NContrato,IdCiaCon,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqTotalesAbo @pmNLiquida INT,@pmIdCia CHAR(2),@pmTipoNcr VARCHAR(3)
 AS 
 SELECT Item,T.NContrato AS NumContrato,IdCiaCon,T.IdCliente AS NitCliente,RazonSocial,T.IdAgencia AS Id_Agencia,Agencia
,Unidades,VrBonif,VrAbono,VrCuotaFija,T.DiasGracia AS Dias_Gracia,NotaCred,IdCiaNot
,L.Fecha AS FechaLiq,TipCom,Comprobante,IdCiaCom,L.Observacion AS Observ
,CN.IdVend AS CdVend,TC.IdLocal AS CdCiuClie,A.IdLocal AS CdCiuAge
 FROM Trn_MayLiquida AS L INNER JOIN Trn_MayLiqTotales  AS T ON L.NLiquida=T.NLiquida AND L.IdCia=T.IdCia
 INNER JOIN Terceros AS TC ON T.IdCliente=TC.IdTercero
 LEFT JOIN Trn_MayContratos AS CN ON T.NContrato=CN.NContrato AND T.IdCiaCon=CN.IdCia
 LEFT JOIN Agencias AS A ON T.IdAgencia=A.IdAgencia
 WHERE L.NLiquida=@pmNLiquida AND L.IdCia=@pmIdCia AND Estado<>2 AND VrAbono>0
 AND TipoNcr=@pmTipoNcr AND NotaCred<=0
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqTotalesAbp @pmNLiquida INT,@pmIdCia CHAR(2),@pmTipoNcr VARCHAR(3)
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM  Trn_MayLiqTotales 
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia AND TipoNcr=@pmTipoNcr AND NotaCred<=0 AND VrAbono>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqTotalesLog @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,T.NContrato AS NumContrato,IdCiaCon,T.IdCliente AS NitCliente,RazonSocial,T.IdAgencia AS Id_Agencia,Agencia
,Unidades,VrBonif,VrAbono,VrCuotaFija,T.DiasGracia AS Dias_Gracia,NotaCred,IdCiaNot,LogLiquida,L.Fecha AS FechaLiq
,IdTipo,IdBonif,CN.IdVend AS CdVend,TC.IdLocal AS CdCiuClie,A.IdLocal AS CdCiuAge
 FROM Trn_MayLiquida AS L INNER JOIN Trn_MayLiqTotales  AS T ON L.NLiquida=T.NLiquida AND L.IdCia=T.IdCia
 INNER JOIN Terceros AS TC ON T.IdCliente=TC.IdTercero
 LEFT JOIN Trn_MayContratos AS CN ON T.NContrato=CN.NContrato AND T.IdCiaCon=CN.IdCia
 LEFT JOIN Agencias AS A ON T.IdAgencia=A.IdAgencia
 WHERE L.NLiquida=@pmNLiquida AND L.IdCia=@pmIdCia AND Estado=0
 AND (VrBonif<>0 OR VrCuotaFija>0) 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqTotalesLta @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,T.IdCliente AS NitCliente,TC.RazonSocial AS NombreCliente,T.IdAgencia AS Id_Agencia,Agencia
,Unidades,VrBonif,VrAbono,T.DiasGracia AS Dias_Gracia,L.Fecha AS FechaLiq,LogLiquida
--datos del contrato
,T.NContrato AS NumContrato,CN.Fecha AS FechaContrato,FechaInicio,FechaVence,IdCiaCon,CN.IdTipo AS CdTipo,TipContrato,CN.IdBonif AS CdBonif,TipoBonif
,CN.IdVend AS CdVend,V.RazonSocial AS Vendedor,CN.IdBandera AS CdBandera,TipoBandera
,TipCom,Comprobante,IdCiaCom,L.Observacion AS Observ,TipoNcr,NotaCred,IdCiaNot,VrCuotaFija
--datos del cliente
,TC.TipoId AS TipIdClie,TC.Dv AS DvCliente,TC.NomCial AS NomCialClie,TC.SiglaRaz AS SiglaClie,TC.IdLocal AS CdCiuClie,LT.Localidad AS CiudadCliente
,DP.Departamento AS DptoClie,A.IdLocal AS CdCiuAge,LA.Localidad AS CiudadAgencia,DA.Departamento AS DptoAgencia
 FROM Trn_MayLiquida AS L INNER JOIN Trn_MayLiqTotales  AS T ON L.NLiquida=T.NLiquida AND L.IdCia=T.IdCia
 INNER JOIN Terceros AS TC ON T.IdCliente=TC.IdTercero
 INNER JOIN Localidades AS LT ON TC.IdLocal=LT.IdLocal  INNER JOIN Departamentos AS DP ON LT.IdDep=DP.IdDep 
 LEFT JOIN Trn_MayContratos AS CN ON T.NContrato=CN.NContrato AND T.IdCiaCon=CN.IdCia
 LEFT JOIN Agencias AS A ON T.IdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Terceros AS V ON CN.IdVend=V.IdTercero
 LEFT JOIN  MayTipos AS TCN ON CN.IdTipo=TCN.IdTipo
 LEFT JOIN  MayTiposBonif AS TB ON CN.IdBonif=TB.IdBonif
 LEFT JOIN  TiposBan AS B ON CN.IdBandera=B.IdBandera
 WHERE L.NLiquida=@pmNLiquida AND L.IdCia=@pmIdCia 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqTotalesNot @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM  Trn_MayLiqTotales 
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia AND NotaCred>0 AND VrAbono>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiqTotalesTot @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT SUM(Unidades) AS STOTUNI,SUM(VrBonif) AS STOTBON,SUM(VrAbono) AS STOTABO
,SUM(VrCuotaFija) AS STOTFIJ
 FROM  Trn_MayLiqTotales
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiquida @pmNLiquida INT,@pmIdCia CHAR(2)
 AS 
 SELECT NLiquida,IdCia,Fecha,Observacion,Estado,FecDev,TipCom,Comprobante,IdCiaCom,TimeSys,IdUsuario
 FROM Trn_MayLiquida
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiquidaEst @pmFecha SMALLDATETIME,@pmIdCia CHAR(2),@pmEstado INT
 AS 
 SELECT NLiquida,IdCia,Fecha,Estado,TipCom,Comprobante,IdCiaCom
 FROM Trn_MayLiquida
 WHERE  Fecha=@pmFecha  AND  IdCia=@pmIdCia AND Estado=@pmEstado
 ORDER BY NLiquida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiquidaFec @pmFecha SMALLDATETIME,@pmIdCia CHAR(2)
,@pmNLiquida INT
 AS 
 --Verifica si ya tiene liq. cerradas en el mes
 SELECT NLiquida,IdCia,Fecha,Estado,TipCom,Comprobante,IdCiaCom
 FROM Trn_MayLiquida
 WHERE Fecha=@pmFecha AND Estado=1 AND IdCia=@pmIdCia AND NLiquida<>@pmNLiquida
 ORDER BY NLiquida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiquidaId @pmIdCia CHAR(2)
 AS 
 SELECT MAX(NLiquida) AS MXID
 FROM Trn_MayLiquida
 WHERE   IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayLiquidaLta @pmIdCia CHAR(2)=Null,@pmEstado INT=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT NLiquida,L.IdCia AS CdCia,Compania,Fecha,Observacion,Estado,FecDev
,TipCom,Comprobante,IdCiaCom,L.IdUsuario AS CdUsuario,Usuario,TimeSys
 FROM Trn_MayLiquida AS L INNER JOIN Companias AS C ON L.IdCia=C.IdCia
 INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario
 WHERE L.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND (Estado>=ISNULL(@pmEstado,0)  AND Estado<=ISNULL(@pmEstado,2147483647))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY NLiquida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayPspto @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT NContrato,IdCia,Item,TipoProd,Cantidad,Unidad,CantMes,CdProducto,CdSubgrupo
 FROM Trn_MayPspto
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,-1)  AND Item<=ISNULL(@pmItem,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayPsptoCant @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT COUNT(Item) AS SCANT
 FROM Trn_MayPspto
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayPsptoItm @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT  MAX(Item) AS MXITM
 FROM Trn_MayPspto
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayPsptoLta @pmNContrato INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,TipoProd,Cantidad,Unidad,CantMes,CdSubgrupo,Subgrupo,NContrato,IdCia
 FROM Trn_MayPspto AS PS  LEFT JOIN SubGrupos AS S ON PS.CdSubgrupo=S.IdSubgrupo
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayTipos @pmIdTipo VARCHAR(4)
 AS 
 IF @pmIdTipo IS NULL
  SELECT IdTipo,TipContrato
  FROM MayTipos
  WHERE Inactivo=0
  ORDER BY IdTipo
 ELSE
  SELECT IdTipo,TipContrato,Inactivo
  FROM MayTipos
  WHERE IdTipo=@pmIdTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryMayTiposAprob @pmIdAprob VARCHAR(4)
 AS 
 IF @pmIdAprob IS NULL
  SELECT IdAprob,TipoAprob
  FROM MayTiposAprob
  WHERE Inactivo=0
 ORDER BY TipoAprob
 ELSE
  SELECT IdAprob,TipoAprob,Inactivo
  FROM MayTiposAprob
  WHERE IdAprob=@pmIdAprob
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryMayTiposBienes @pmIdTipBien VARCHAR(4)
 AS 
 IF @pmIdTipBien IS NULL
  SELECT IdTipBien,TipoBien
  FROM MayTiposBienes
  WHERE Inactivo=0
 ORDER BY TipoBien
 ELSE
  SELECT IdTipBien,TipoBien,Inactivo
  FROM MayTiposBienes
  WHERE IdTipBien=@pmIdTipBien

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayTiposBonif @pmIdBonif VARCHAR(4)
 AS 
 IF @pmIdBonif IS NULL
  SELECT IdBonif,TipoBonif,AboCartera,AboPstmo
  FROM MayTiposBonif
  WHERE Inactivo=0
  ORDER BY IdBonif
 ELSE
 SELECT IdBonif,TipoBonif,AboCartera,AboPstmo,Inactivo
 FROM MayTiposBonif
 WHERE IdBonif=@pmIdBonif
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMayTiposDoc @pmIdDoc VARCHAR(4)
 AS 
 IF @pmIdDoc IS NULL
  SELECT IdDoc,NombreDoc
  FROM MayTiposDoc
  WHERE Inactivo=0
  ORDER BY  IdDoc
 ELSE
  SELECT IdDoc,NombreDoc,Inactivo
  FROM MayTiposDoc
  WHERE IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryMayTiposPoliza @pmIdTipoPol VARCHAR(4)
 AS 
 IF @pmIdTipoPol IS NULL
 SELECT IdTipoPol,TipoPoliza
 FROM MayTiposPoliza
 WHERE Inactivo=0
 ORDER BY TipoPoliza
 ELSE
 SELECT IdTipoPol,TipoPoliza,Inactivo
 FROM MayTiposPoliza
 WHERE IdTipoPol=@pmIdTipoPol

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryMercancias @pmIdMercancia VARCHAR(16)
 AS 
 SELECT IdMercancia,DescripMcia,CodigoMcia,IdGrupo,UndMed,IdUnd,IdNat,IdMnjo,IdTmcia,Contenedor,IdProducto
,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Mercancias
 WHERE IdMercancia=@pmIdMercancia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryMercanciasLta @pmIdGrupo VARCHAR(10)=Null,@pmIdNat VARCHAR(4)=Null,@pmIdTmcia VARCHAR(4)=Null
,@pmInactivo BIT=Null
 AS 
 SELECT IdMercancia,DescripMcia,M.IdGrupo AS CdGrupo,GrupoMcia,M.UndMed AS Und_Med,M.IdUnd AS CdUndPre,Unidad,UM.IdEmp AS CdEmp
,M.IdNat AS CdNat,Natlzaprod,M.IdMnjo AS CdMnjo,ManejoMcia,M.IdTmcia AS CdTmcia,TipoMcia,Contenedor,M.IdProducto AS CdProducto,DescripProd,CodigoMcia
,M.IdEstado AS CdEstado,Estado,M.Inactivo AS Inactvo,M.FechaAdd AS FechaCrea,M.FechaUpdate AS FechaAct,M.IdUsuario AS CdUsuario,Usuario
 FROM Mercancias AS M INNER JOIN GruposMcia AS G ON M.IdGrupo=G.IdGrupo
 INNER JOIN UndMed AS UM ON M.IdUnd=UM.IdUnd    INNER JOIN TiposNat AS N ON M.IdNat=N.IdNat
 INNER JOIN TiposMnjo AS MM ON M.IdMnjo=MM.IdMnjo INNER JOIN TiposMcia AS TM ON M.IdTmcia=TM.IdTmcia
 INNER JOIN EstadoPro AS EP ON M.IdEstado=EP.IdEstado  INNER JOIN adm_Usuarios AS U ON M.IdUsuario=U.IdUsuario 
 LEFT JOIN ProdMcias AS P ON M.IdProducto=P.IdProducto
 WHERE M.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND M.IdNat LIKE ISNULL(@pmIdNat,'%') AND M.IdTmcia LIKE ISNULL(@pmIdTmcia,'%')
  AND (M.Inactivo=ISNULL(@pmInactivo,0) or  M.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY DescripMcia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryModulos @pmIdMod VARCHAR(3),@pmnModulo VARCHAR(10)=Null
 AS 
 SELECT IdMod,Consecutivo,Descripcion,Valor,nModulo
 FROM Sys_Modulos
 WHERE IdMod LIKE ISNULL(@pmIdMod,'%') AND nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY Consecutivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryModulosId @pmnModulo VARCHAR(10)=Null
 AS 
 SELECT MAX(Consecutivo) AS MXconce
 FROM Sys_Modulos
 WHERE nModulo LIKE ISNULL(@pmnModulo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAbonos @pmNPrestamo INT,@pmItem INT
 AS 
 SELECT NPrestamo,Item,IdEmpleado,FechaAbo,VrAbono,nCuotas,Dscto,IdPeriodo,Detalle,NLiquida
 FROM Trn_NomAbonos
 WHERE NPrestamo=@pmNPrestamo 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAbonosId @pmNPrestamo INT
 AS 
 SELECT MAX(Item) AS MXITEM
 FROM Trn_NomAbonos
 WHERE NPrestamo=@pmNPrestamo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAbonosLiq @pmDscto VARCHAR(3),@pmNLiquida INT,@pmIdEmpleado VARCHAR(16)=Null
AS 
 SELECT NPrestamo,SUM(VrAbono) AS SABONO,SUM(nCuotas) AS SCUOTAS
 FROM Trn_NomAbonos
 WHERE Dscto=@pmDscto AND NLiquida=@pmNLiquida  AND IdEmpleado LIKE  ISNULL(@pmIdEmpleado,'%')
 GROUP BY NPrestamo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAbonosPer @pmIdPeriodo VARCHAR(8),@pmDscto VARCHAR(3),@pmIdEmpleado VARCHAR(16)=Null
AS 
 SELECT NPrestamo,SUM(VrAbono) AS SABONO,SUM(nCuotas) AS SCUOTAS
 FROM Trn_NomAbonos
 WHERE IdPeriodo=@pmIdPeriodo AND Dscto LIKE ISNULL(@pmDscto,'%')  AND IdEmpleado LIKE  ISNULL(@pmIdEmpleado,'%')
 GROUP BY NPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAbonosPre @pmNPrestamo INT,@pmIdPeriodo VARCHAR(8)=Null,@pmDscto VARCHAR(3)=Null
,@pmNLiquida INT=Null
AS 
 SELECT SUM(VrAbono) AS SABONO,SUM(nCuotas) AS SCUOTAS
 FROM Trn_NomAbonos
 WHERE NPrestamo=@pmNPrestamo AND IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND Dscto LIKE ISNULL(@pmDscto,'%') 
 AND  (NLiquida>=ISNULL(@pmNLiquida,0)  AND NLiquida<=ISNULL(@pmNLiquida,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAlqDet @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT
 AS 
 SELECT TipoLiq,Numero,Item,IdEmpleado,NContrato,Apellido1,Apellido2,Nombre1,Nombre2,IdFonPen,IdFonSal,IdFonRie,IdCajCom,SalBasico,VrIBCPen,DiasPen,TarifPen,VrPension,VrApoFsp,VrApoFsu,VrApoVolAfi,VrApoVolApt
,VrNoReten,VrIBCSal,DiasSal,TarifSal,VrSalud,VrUpc,VrIncap,VrLicencia,NumAutInc,NumAutLic,VrIBCRie,DiasRie,TarifRie,VrRiesgos,VrIncIrp,NumAutIrp,IdInstala,VrIBCRef,VrIBCCajas,DiasCom,TarifCom,VrCajas,TarifSen,VrSena,TarifIcb
,VrICBF,TarifEsap,VrESAP,TarifEdu,VrMinEdu,TarifFsp,TarifFsu,CdFonAvp
 FROM Trn_NomAlqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAlqNov @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT
 AS 
 SELECT TipoLiq,Numero,Item,IdEmpleado,NovIng,NovRet,NovTde,NovTae,NovTdp,NovTap,NovVsp,NovVte,NovVst,NovSln,NovIge,NovLma,NovVac,NovAvp
,NovVct,NovIrp,CodNewPen,CodNewSal,CodNewIns,CodNewRie,CodNewGru,DiasNov
 FROM Trn_NomAlqNov
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAlqTot @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT
 AS 
 SELECT TipoLiq,Numero,Item,TipAdm,IdFondo,CodTipoApo,VrTotalCot,VrTotalFsp,VrTotalFsu,VrTotalAva,VrTotalAve,VrTotalUpc,VrTotalLic,VrTotalInc,VrTotalOtr,DiasMora
,TarifMora,VrIntMora,VrIMoraOtr,VrTotalSAF,VrSAFUpc,VrTotalPagar,VrTotalFon,TotalAfi,NumAutLic,NumAutInc,NumPlaSAF,TipoReg
 FROM Trn_NomAlqTot
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAlqTot_Cr @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmTipAdm VARCHAR(3)=Null
,@pmIdFondo VARCHAR(8)=Null
 AS 
 SELECT TipoLiq,Numero,Item,TipAdm,T.IdFondo AS CodFondo,Fondo,F.IdTercero AS NitFondo,RazonSocial,TipoId,Dv,CodTipoApo,VrTotalCot,VrTotalFsp,VrTotalFsu,VrTotalAva,VrTotalAve,VrTotalUpc
,VrTotalLic,VrTotalInc,VrTotalOtr,DiasMora,TarifMora,VrIntMora,VrIMoraOtr,VrTotalSAF,VrSAFUpc,VrTotalPagar,VrTotalFon,TotalAfi,NumAutLic,NumAutInc,NumPlaSAF,TipoReg
 FROM Trn_NomAlqTot AS T LEFT JOIN Fondos AS F ON T.IdFondo=F.IdFondo
 LEFT JOIN Terceros AS TF ON F.IdTercero=TF.IdTercero
 WHERE TipoLiq=@pmTipoLiq AND Numero LIKE ISNULL(@pmNumero,'%') AND TipAdm LIKE ISNULL(@pmTipAdm,'%')
 AND T.IdFondo LIKE ISNULL(@pmIdFondo,'%')
 ORDER BY Numero,TipoReg,TipAdm,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAportante @pmIdAportnte VARCHAR(16)
 AS 
 SELECT IdAportnte,RazonSocial,TipoId,DvNit,IdRepLegal,Direccion,IdLocal,Telefono,Fax,e_mail,ClaseApo,TipoEco,TipoPersna,CIIUDane,FecIniCndato
,TipAccCndato,FecFinCndato,CodOperInfo,TipoApo,FecAdd,FecUpdate,IdUsuario
 FROM NomAportante
 WHERE IdAportnte LIKE ISNULL(@pmIdAportnte,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAportanteReg
 AS 
 SELECT COUNT(IdAportnte) AS SCanApo
 FROM NomAportante

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAstncia @pmIdEmpleado VARCHAR(16),@pmFechaDia SMALLDATETIME
 AS 
 SELECT IdEmpleado,FechaDia,HHDiurno,HHNocturno,HEDiurno,HENocturno,TipoJrnada,EsDmncal,Horario,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomAstncia
 WHERE IdEmpleado=@pmIdEmpleado AND FechaDia=@pmFechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAstnciaLta @pmIdEmpleado VARCHAR(16)=Null,@pmFecInicio SMALLDATETIME=Null,@pmFecFinal SMALLDATETIME=Null
,@pmEsDmncal BIT=Null,@pmTipoJrnada VARCHAR(10)=Null
 AS 
 SELECT A.IdEmpleado AS IdEmplado,Apellidos,Nombres,FechaDia,HHDiurno,HHNocturno,HEDiurno,HENocturno,TipoJrnada,EsDmncal,Horario,TimeSys,A.FecUpdate AS FecUpd
,A.IdUsuario AS IdUsuari,Usuario,E.NContrato AS NumContrato,FecIngreso,FecRetiro,VrSalario,VrAuxTrans,C.IdNom AS CodNom,TipoNomina,pVehiculo
 FROM Trn_NomAstncia AS A INNER JOIN Empleados AS E ON A.IdEmpleado=E.IdEmpleado INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 LEFT JOIN Emp_Contrato AS C ON E.NContrato=C.NContrato LEFT JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 WHERE  FechaDia BETWEEN @pmFecInicio AND @pmFecFinal AND A.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
  AND TipoJrnada LIKE ISNULL(@pmTipoJrnada,'%')
 AND (EsDmncal=ISNULL(@pmEsDmncal,0) or EsDmncal=ISNULL(@pmEsDmncal,1))
 ORDER BY Apellidos,Nombres,FechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAstnciaTip @pmTipo CHAR(1),@pmIdEmpleado VARCHAR(16),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME
 AS 
 IF @pmTipo='D' --descansos
  SELECT Count(FechaDia) AS Cant,SUM(HHDiurno) AS STotHHD,SUM(HHNocturno) AS STotHHN,SUM(HEDiurno) AS STotHED ,SUM(HENocturno) AS STotHEN
  FROM Trn_NomAstncia
  WHERE IdEmpleado=@pmIdEmpleado AND  FechaDia BETWEEN @pmFecInicio AND @pmFecFinal 
   AND TipoJrnada IN ('DESCANSO','DSCSINCOMP')
 ELSE --SIN COMPENSATORIOS
  SELECT Count(FechaDia) AS Cant,SUM(HHDiurno) AS STotHHD,SUM(HHNocturno) AS STotHHN,SUM(HEDiurno) AS STotHED ,SUM(HENocturno) AS STotHEN
  FROM Trn_NomAstncia
  WHERE IdEmpleado=@pmIdEmpleado AND  FechaDia BETWEEN @pmFecInicio AND @pmFecFinal 
   AND TipoJrnada IN ('SINCOMP','DSCSINCOMP')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAstnciaTot @pmIdEmpleado VARCHAR(16)=Null,@pmFecInicio SMALLDATETIME=Null,@pmFecFinal SMALLDATETIME=Null
,@pmEsDmncal BIT=Null,@pmTipoJrnada VARCHAR(10)=Null
 AS 
 SELECT Count(FechaDia) AS Cant,SUM(HHDiurno) AS STotHHD,SUM(HHNocturno) AS STotHHN,SUM(HEDiurno) AS STotHED ,SUM(HENocturno) AS STotHEN
 FROM Trn_NomAstncia
 WHERE  FechaDia BETWEEN @pmFecInicio AND @pmFecFinal AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND TipoJrnada LIKE ISNULL(@pmTipoJrnada,'%')
 AND (EsDmncal=ISNULL(@pmEsDmncal,0) or EsDmncal=ISNULL(@pmEsDmncal,1))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAutoliq @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20)
 AS 
 SELECT TipoLiq,Numero,Fecha,PerAutoliq,PerAutoSal,FechaPago,IdAportnte,Presentacion,CodCia,CodFondo,CodArp,CodOper,EsCorr,PerCorr,NumCorr,TotalEmp,VrNomina,VrPension,VrSalud,VrRiesgos,VrCajas,VrSena,VrIcbf,VrEsap,VrMinEduc,TotalAfipen,TotalAfiSal
,TotalAfiRie,TotalAfiCaj,TotalAfiSen,TotalAfiIcb,TotalAfiEsa,TotalAfiEduc,TotAdmPen,TotAdmSal,TotAdmRie,TotAdmCom,Observacion,LogAdmon,RedoMiles,Anulado,IdEstado,TimeSys,FecUpdate,IdUsuario,TipoPlanilla
 FROM Trn_NomAutoliq
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAutoliqDso @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipoLiq VARCHAR(3)=Null,@pmNumero VARCHAR(20)=Null
,@pmPerAutoliq VARCHAR(6)=Null,@pmCodFondo VARCHAR(8)=Null,@pmEsCorr BIT=Null,@pmIdEstado VARCHAR(4)=Null,@pmPresentacion CHAR(1)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipoLiq,Numero,Fecha,PerAutoliq,PerAutoSal,FechaPago,A.IdAportnte AS NitAportnte,RazonSocial,Presentacion,CodCia,CodFondo,F.Fondo AS NomFondo,CodArp,FR.Fondo AS FondoArp,CodOper,EsCorr,PerCorr,NumCorr,TotalEmp,VrNomina,VrPension,VrSalud,VrRiesgos
,VrCajas,VrSena,VrIcbf,VrEsap,VrMinEduc,TotalAfipen,TotalAfiSal,TotalAfiRie,TotalAfiCaj,TotalAfiSen,TotalAfiIcb,TotalAfiEsa,TotalAfiEduc,TotAdmPen,TotAdmSal,TotAdmRie,TotAdmCom,Observacion
,LogAdmon,RedoMiles,Anulado,A.IdEstado AS IdEstdo,Estado,TipoPlanilla,TimeSys,A.FecUpdate AS Fec_Updte,A.IdUsuario AS IdUsuari,Usuario
 FROM Trn_NomAutoliq AS A INNER JOIN NomAportante AS NA ON A.IdAportnte=NA.IdAportnte
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 LEFT JOIN Fondos AS F ON A.CodFondo=F.IdFondo LEFT JOIN Fondos AS FR ON A.CodArp=FR.IdFondo
 WHERE TipoLiq LIKE ISNULL(@pmTipoLiq,'%') AND Numero LIKE ISNULL(@pmNumero,'%') AND PerAutoliq LIKE ISNULL(@pmPerAutoliq,'%')
 AND CodFondo LIKE ISNULL(@pmCodFondo,'%') AND A.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND Presentacion LIKE ISNULL(@pmPresentacion,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (EsCorr=ISNULL(@pmEsCorr,0) or EsCorr=ISNULL(@pmEsCorr,1))
 ORDER BY TipoLiq,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAutoliqPer @pmTipoLiq VARCHAR(3),@pmPerAutoliq VARCHAR(6),@pmNumero VARCHAR(20),@pmPresentacion CHAR(1)=Null
,@pmCodCia CHAR(2)=Null,@pmCodFondo VARCHAR(8)=Null,@pmEsCorr BIT=Null
 AS 
 SELECT TipoLiq,Numero,Fecha,PerAutoliq,PerAutoSal,FechaPago,Presentacion,CodCia,CodFondo,EsCorr
 FROM Trn_NomAutoliq
 WHERE TipoLiq=@pmTipoLiq AND PerAutoliq=@pmPerAutoliq AND Anulado=0 AND Numero<>@pmNumero AND Presentacion LIKE ISNULL(@pmPresentacion,'%')
 AND CodCia LIKE ISNULL(@pmCodCia,'%%') AND CodFondo LIKE ISNULL(@pmCodFondo,'%')
 AND  (EsCorr=ISNULL(@pmEsCorr,0) or EsCorr=ISNULL(@pmEsCorr,1))
 ORDER BY  Fecha,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomAutoliq_Cr @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmPerAutoliq VARCHAR(6)=Null
AS
 SELECT  A.TipoLiq AS TipoPla,A.Numero AS NumPla,Fecha,PerAutoliq,PerAutoSal,FechaPago,A.IdAportnte AS NitAportnte,NA.RazonSocial AS NomAportante,Presentacion,A.CodCia AS CodSucur,Compania,CodSucsal,DireccSuc,TelefSuc,CI.IdLocal AS CodCiuSuc
,LC.Localidad AS CiudadSuc,LC.IdDep AS CodDepSuc,CodFondo,F.Fondo AS NomFondo,CodArp,FRP.Fondo AS FondoArp,CodOper,EsCorr,PerCorr,NumCorr,TotalEmp,VrNomina,A.VrPension AS Total_Pen,A.VrSalud AS Total_Sal,A.VrRiesgos AS Total_Rie,A.VrCajas AS Total_Caj
,A.VrSena AS Total_Sen,A.VrIcbf AS Total_ICB,A.VrEsap AS Total_ESA,VrMinEduc,TotalAfipen,TotalAfiSal,TotalAfiRie,TotalAfiCaj,TotalAfiSen,TotalAfiIcb,TotalAfiEsa,TotalAfiEduc,TotAdmPen,TotAdmSal,TotAdmRie,TotAdmCom,A.Observacion AS Observ,LogAdmon,RedoMiles
,Anulado,A.IdEstado AS IdEstdo,Estado,TimeSys,A.FecUpdate AS Fec_Update,A.IdUsuario AS IdUsuari,Usuario
--datos del aportante
,NA.TipoId AS Apo_TipID,DvNit,IdRepLegal,RLG.RazonSocial AS NomRepLegal,RLG.TipoId AS RLegTID,RLG.Dv AS RLegDV,NA.Direccion AS Apo_dirccion,NA.IdLocal AS Apo_CodCiu,LA.Localidad AS Apo_NomCiu,LA.IdDep AS Apo_CodDep,Departamento,NA.Telefono AS Apo_tel,NA.Fax AS Apo_Fax,NA.e_mail AS Apo_email
,ClaseApo,TipoEco,TipoPersna,CIIUDane,FecIniCndato,TipAccCndato,FecFinCndato,CodOperInfo,TipoApo
--datos del detalle
,D.Item AS DetItem,D.IdEmpleado AS IdEmpldo,D.NContrato AS NumContrato,Apellido1,Apellido2,Nombre1,Nombre2,D.IdFonPen AS IdFdoPen,FP.Fondo AS FondoPen,TFP.TipoId AS Pen_TipID,FP.IdTercero AS Pen_Nit,TFP.Dv AS Pen_DV,TFP.RazonSocial AS Pen_Nombre
,IdFonSal,FS.Fondo AS FondoSal,TFS.TipoId AS Sal_TipID,FS.IdTercero AS Sal_Nit,TFS.Dv AS Sal_Dv,TFS.RazonSocial AS Sal_Nombre,IdFonRie,FR.Fondo AS FondoRie,TFR.TipoId AS Rie_TipID,FR.IdTercero AS Rie_nit,TFR.Dv AS Rie_Dv,TFR.RazonSocial AS Rie_nombre
,IdCajCom,FC.Fondo AS FondoCc,TFC.TipoId AS CC_TipId,FC.IdTercero AS CC_Nit,TFC.Dv AS CC_DV,TFC.RazonSocial AS cc_Nombre,SalBasico,VrIBCPen,DiasPen,TarifPen,D.VrPension AS VlrPension,VrApoFsp,VrApoFsu,VrApoVolAfi,VrApoVolApt,VrNoReten,VrIBCSal,DiasSal,TarifSal,D.VrSalud AS VlrSalud,D.VrUpc AS VlrUpc
,VrIncap,VrLicencia,NumAutInc,NumAutLic,VrIBCRie,DiasRie,TarifRie,D.VrRiesgos AS VlrRiesgos,VrIncIrp,NumAutIrp,D.IdInstala AS CodInstla,I.Instlacion AS CentroTrabaj,I.CodCiu AS CiuInstala
,VrIBCRef,VrIBCCajas,DiasCom,TarifCom,D.VrCajas AS VlrCajas,TarifSen,D.VrSena AS VlrSena,TarifIcb,D.VrICBF AS Vlricbf,TarifEsap,D.VrESAP AS VlrESAP,TarifEdu,VrMinEdu,TarifFsp,TarifFsu
--datos de novedades
,NovIng,NovRet,NovTde,NovTae,NovTdp,NovTap,NovVsp,NovVte,NovVst,NovSln,NovIge,NovLma,NovVac,NovAvp,NovVct,NovIrp,CodNewPen,FNP.Fondo AS NewFonPen,CodNewSal,FNS.Fondo AS NewFonEPS,CodNewIns,NI.Instlacion AS NewCentroTrabj,CodNewRie,CRN.Tarifa AS New_TarRie,CodNewGru
,NGR.TarfaTrab AS NewTarfTrab,NGR.TarfaEmp AS NewTarfEmp,DiasNov
--datos del empleado
,Apellidos,Nombres,E.Codigo AS EmpCodigo,E.TipoId AS Emp_TipID,E.Dv AS Emp_Dv,E.IdLugarCed AS Emp_IdLugrCed,L.Localidad AS Lugar_Ced,L.IdDep AS Emp_CodDep,E.Direccion AS Dirccion,E.Telefono AS Emp_Telfono,E.TelMovil AS Emp_TelMovil,E.e_mail AS Emp_Email,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,NumCuenta
,E.IdBanco AS CodBanco,EsCondtor,pVehiculo,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,Licencia,CatLicencia,VigLicencia,TallaCam,TallaPan,TallaZap,IdEstCivil,EstCivil,NHijos,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDepe,Dependencia,C.IdCCosto AS CodCcto,CCosto,C.IdSubCos AS CodScost,SubCosto,C.IdNom AS CodTipNom,TipoNomina,C.IdClase AS CodClaRie,CR.ClaseRiesgo AS ClasRiesgo
,CR.Tarifa AS TarifaRie,VrSalario,VrAuxTrans,C.VrUpc AS Con_VrUpc,SalMinimo,SalIntegral,FPnomina,C.TipoLiquida AS TipLiq,FecAfiSeg,C.IdCot AS CodCotz,Cotizante,C.IdGrupo AS CodGrup,GAR.TarfaTrab AS Con_TrfaTrab,GAR.TarfaEmp AS TrfaEmp,TipoTrabj
,ATFinMes,SegFinMes,C.IdEstado AS Con_IdEstdo,C.Inactivo AS Con_inactivo,IdJornada,DenyPEN,DenyEPS,DenyARP,DenyCaj,D.CdFonAvp AS CodFondoAvp,E.CdFonAvp AS Emp_FondoAVP,TipoPlanilla,SubTipoCot
 FROM Trn_NomAutoliq AS A INNER JOIN Trn_NomAlqDet AS D ON A.TipoLiq=D.TipoLiq AND A.Numero=D.Numero 
 INNER JOIN  NomAportante AS NA ON A.IdAportnte=NA.IdAportnte  INNER JOIN Terceros AS RLG ON NA.IdRepLegal=RLG.IdTercero
 INNER JOIN EstadoDoc AS ED ON A.IdEstado=ED.IdEstado 
 INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario INNER JOIN Localidades AS LA ON NA.IdLocal=LA.IdLocal
 INNER JOIN Departamentos AS DE ON LA.IdDep=DE.IdDep
 INNER JOIN Empleados AS E ON D.IdEmpleado=E.IdEmpleado INNER JOIN Fondos AS FP ON D.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON D.IdFonSal=FS.IdFondo INNER JOIN Fondos AS FR ON D.IdFonRie=FR.IdFondo
 INNER JOIN Fondos  AS FC ON D.IdCajCom=FC.IdFondo INNER JOIN Terceros AS TFP ON FP.IdTercero=TFP.IdTercero
 INNER JOIN Terceros AS TFS ON FS.IdTercero=TFS.IdTercero  INNER JOIN Terceros AS TFR ON FR.IdTercero=TFR.IdTercero
 INNER JOIN Terceros AS TFC ON FC.IdTercero=TFC.IdTercero  INNER JOIN Instalaciones AS I ON D.IdInstala=I.IdInstala
 INNER JOIN Emp_Contrato AS C ON D.NContrato=C.NContrato  INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep 
 INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom  INNER JOIN TiposCot AS TCZ ON C.IdCot=TCZ.IdCot
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  INNER JOIN EstadoCiv AS EC ON E.IdEstCivil=EC.IdEstado
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase 
 LEFT JOIN GruposAR AS GAR ON C.IdGrupo=GAR.IdGrupo
 LEFT JOIN Trn_NomAlqNov AS DN ON D.TipoLiq=DN.TipoLiq AND D.Numero=DN.Numero AND D.Item=DN.Item
 LEFT JOIN Fondos AS F ON A.CodFondo=F.IdFondo LEFT JOIN Fondos AS FRP ON A.CodArp=FRP.IdFondo
 LEFT JOIN Companias AS CI ON A.CodCia=CI.IdCia LEFT JOIN Localidades AS LC ON CI.IdLocal=LC.IdLocal
 LEFT JOIN Fondos AS FNP ON DN.CodNewPen=FNP.IdFondo  LEFT JOIN Fondos AS FNS ON DN.CodNewSal=FNS.IdFondo
 LEFT JOIN Instalaciones AS NI ON DN.CodNewIns=NI.IdInstala LEFT JOIN ClaseRie AS CRN ON DN.CodNewRie=CRN.IdClase
 LEFT JOIN GruposAR AS NGR ON DN.CodNewGru=NGR.IdGrupo
 WHERE A.TipoLiq=@pmTipoLiq AND A.Numero LIKE ISNULL(@pmNumero,'%') AND PerAutoliq LIKE ISNULL(@pmPerAutoliq,'%')
 ORDER BY A.Numero,Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCAdmon @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT IdEmpleado,Numero,Fecha,NContrato,ClaseAdmon,FecIniNov,NNovedad,IdFondo,IdFonNew,Observacion,Anulado,TimeSys,IdUsuario
 FROM Trn_NomCAdmon
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCAdmonCla @pmIdEmpleado VARCHAR(16),@pmClaseAdmon VARCHAR(10)
 AS 
 SELECT IdEmpleado,Numero,Fecha,NContrato,ClaseAdmon,FecIniNov,NNovedad,IdFondo,IdFonNew,Anulado
 FROM Trn_NomCAdmon
 WHERE IdEmpleado=@pmIdEmpleado  AND Anulado=0 AND ClaseAdmon=@pmClaseAdmon
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCAdmonId @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomCAdmon
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCAdmon_Cr   @pmIdEmpleado VARCHAR(16)=Null,@pmNumero INT=Null,@pmClaseAdmon VARCHAR(10)=Null,@pmIdFondo VARCHAR(8)=Null
,@pmIdFonNew VARCHAR(8)=Null,@pmAnulado BIT=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT CA.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Numero,Fecha,CA.NContrato AS NumCtrato,ClaseAdmon,FecIniNov,NNovedad,CA.IdFondo AS IdFonAnt,FAN.Fondo AS FondoAnt
,IdFonNew,FNE.Fondo AS FondoNuevo,CA.Observacion AS Observ,Anulado,TimeSys,CA.IdUsuario AS IdUsuari,Usuario
 --datos del empleado
,Codigo,E.IdProf AS CodProf,Profesion,IdFonPen,FP.Fondo AS FondoPen,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdFonCes,FC.Fondo AS FondoCes,IdCajaCom,CC.Fondo AS CajaComp
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion
,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,VrSalario,VrAuxTrans,SalMinimo,SalIntegral
,C.IdEstado AS IdEstdo,Estado,C.Inactivo AS Con_inactivo 
 FROM Trn_NomCAdmon AS CA INNER JOIN Empleados AS E ON CA.IdEmpleado=E.IdEmpleado INNER JOIN Fondos AS FAN ON CA.IdFondo=FAN.IdFondo
 INNER JOIN Fondos AS FNE ON CA.IdFonNew=FNE.IdFondo  INNER JOIN adm_Usuarios AS U ON CA.IdUsuario=U.IdUsuario
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo  INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo
 LEFT JOIN  Emp_Contrato AS C ON CA.NContrato=C.NContrato  LEFT JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 LEFT JOIN Dependencias AS DP ON C.IdDep=DP.IdDep  LEFT JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
 LEFT JOIN TiposNom AS TN ON C.IdNom=TN.IdNom LEFT JOIN EstadoDoc AS EN ON C.IdEstado=EN.IdEstado 
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo LEFT JOIN Areas AS AR ON C.IdArea=AR.IdArea
 LEFT JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase 
 WHERE CA.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND ClaseAdmon LIKE ISNULL(@pmClaseAdmon,'%')
 AND CA.IdFondo LIKE ISNULL(@pmIdFondo,'%') AND IdFonNew LIKE ISNULL(@pmIdFonNew,'%')
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCausalesRet @pmIdCausa VARCHAR(4)
 AS 
 IF @pmIdCausa IS NULL
  SELECT IdCausa,CausalRet,Indmnizar
  FROM NomCausalesRet
  WHERE Inactivo=0
  ORDER BY  IdCausa
 ELSE
  SELECT IdCausa,CausalRet,Indmnizar,Inactivo
  FROM NomCausalesRet
  WHERE IdCausa=@pmIdCausa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCertf @pmIdEmpleado VARCHAR(16),@pmNumCertf VARCHAR(20)
 AS 
 SELECT IdEmpleado,NumCertf,nAnno,nMes,ClaseCert,VrCertf,Observacion,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomCertf
 WHERE IdEmpleado=@pmIdEmpleado AND NumCertf=@pmNumCertf
 ORDER BY NumCertf
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCertfLta @pmnAnno INT,@pmnMes INT=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmNumCertf VARCHAR(20)=Null,@pmObservacion VARCHAR(100)=Null,@pmClaseCert VARCHAR(10)=Null
 AS 
 SELECT C.IdEmpleado AS IdEmpledo,Apellidos,Nombres,NumCertf,nAnno,nMes,ClaseCert,VrCertf,C.Observacion AS Observ
,C.IdUsuario AS IdUsuari,Usuario,FecAdd,FecUpdate,NContrato
 FROM Trn_NomCertf AS C INNER JOIN Empleados AS E ON C.IdEmpleado=E.IdEmpleado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE nAnno=@pmnAnno AND  C.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND NumCertf  LIKE ISNULL(@pmNumCertf ,'%') AND C.Observacion LIKE ISNULL(@pmObservacion,'%')
 AND ClaseCert LIKE ISNULL(@pmClaseCert,'%')
 AND (nMes>=ISNULL(@pmnMes,0)  AND nMes<=ISNULL(@pmnMes,2147483647))
 ORDER BY C.IdEmpleado,nMes,NumCertf
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomCertfMes @pmIdEmpleado VARCHAR(16),@pmnAnno INT,@pmnMes INT
,@pmClaseCert VARCHAR(10)=Null
 AS 
 SELECT ClaseCert,SUM(VrCertf) AS STOTCER
 FROM Trn_NomCertf
 WHERE IdEmpleado=@pmIdEmpleado AND nAnno=@pmnAnno AND nMes=@pmnMes
 AND ClaseCert LIKE ISNULL(@pmClaseCert,'%')
 GROUP BY ClaseCert
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConAuto @pmIdReg INT
 AS 
 SELECT IdReg,IdConcepto,FecInicio,VrAplicar,Modalidad,OrigenBase,FrecAplica,CedEmpleado,CodNom,CodDep,CodCcto
,CodSubCos,CritSelect,FecFinal,Exceptuar,Observacion,Inactivo,NumPerMes,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomConAuto
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConAutoId 
 AS 
 SELECT MAX(IdReg) AS MXIDREG
 FROM Trn_NomConAuto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConAutoLta @pmIdConcepto VARCHAR(4)=Null,@pmCedEmpleado VARCHAR(16)=Null,@pmCodNom VARCHAR(4)=Null,@pmCodDep VARCHAR(4)=Null
 ,@pmCodCcto VARCHAR(16)=Null,@pmCodSubCos VARCHAR(16)=Null,@pmInactivo BIT=Null,@pmFecInicioIni SMALLDATETIME=Null,@pmFecInicioFin SMALLDATETIME=Null
,@pmFrecAplica VARCHAR(10)=Null,@pmFecFinalIni SMALLDATETIME=Null,@pmFecFinalFin SMALLDATETIME=Null
 AS 
 SELECT IdReg,A.IdConcepto AS CodConcept,Concepto,ClaseCon,FecInicio,VrAplicar,Modalidad,OrigenBase,FrecAplica,CedEmpleado,Apellidos,Nombres,NContrato,CodNom
,TipoNomina,CodDep,Dependencia,CodCcto,CCosto,CodSubCos,SubCosto,CritSelect,FecFinal,Exceptuar,A.Observacion AS Observ,A.Inactivo AS Inactvo,A.FecAdd AS Fecha_Add
,A.FecUpdate AS FecUpd,A.IdUsuario AS IdUsuari,Usuario,E.Inactivo AS Emp_Inactivo,NumPerMes
 FROM Trn_NomConAuto AS A INNER JOIN NomConceptos AS C ON A.IdConcepto=C.IdConcepto INNER JOIN adm_Usuarios AS U ON A.IdUsuario=U.IdUsuario
 LEFT JOIN Empleados AS E ON A.CedEmpleado=E.IdEmpleado  LEFT JOIN TiposNom AS TN ON A.CodNom=TN.IdNom
 LEFT JOIN Dependencias AS DP ON A.CodDep=DP.IdDep  LEFT JOIN CentroCosto AS CO ON A.CodCcto=CO.IdCCosto
 LEFT JOIN SubCentros AS SC ON A.CodSubCos=SC.IdSubCos
 WHERE A.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND CedEmpleado LIKE ISNULL(@pmCedEmpleado,'%') AND CodNom LIKE ISNULL(@pmCodNom,'%')
 AND CodDep LIKE ISNULL(@pmCodDep,'%') AND CodCcto LIKE ISNULL(@pmCodCcto,'%') AND CodSubCos LIKE ISNULL(@pmCodSubCos,'%')
 AND FrecAplica LIKE ISNULL(@pmFrecAplica,'%')
 AND (A.Inactivo=ISNULL(@pmInactivo,0) or A.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecInicio>=ISNULL(@pmFecInicioIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicio<=ISNULL(@pmFecInicioFin,CAST('20781230' AS SMALLDATETIME)))
-- AND (FecFinal>=ISNULL(@pmFecFinalIni,CAST('19100101' AS SMALLDATETIME)) AND FecFinal<=ISNULL(@pmFecFinalFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConAutoPen @pmFecFin SMALLDATETIME,@pmCedEmpleado VARCHAR(16),@pmCodNom VARCHAR(4)
,@pmCodDep VARCHAR(4),@pmCodCcto VARCHAR(16),@pmCodSubCos VARCHAR(16)
 AS 
 SELECT IdReg,IdConcepto,FecInicio,VrAplicar,Modalidad,OrigenBase,FrecAplica,CedEmpleado,CodNom,CodDep,CodCcto
,CodSubCos,CritSelect,FecFinal,Exceptuar,Observacion,Inactivo,NumPerMes,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomConAuto
 WHERE Inactivo=0 AND FecInicio<=@pmFecFin
 AND ( CedEmpleado=@pmCedEmpleado OR CedEmpleado IS NULL OR LEN(CedEmpleado)=0)
 AND ( CodNom=@pmCodNom OR CodNom  IS NULL OR LEN(CodNom)=0)
 AND ( CodDep=@pmCodDep  OR CodDep  IS NULL OR LEN(CodDep)=0)
 AND ( CodCcto=@pmCodCcto  OR CodCcto  IS NULL OR LEN(CodCcto)=0)
 AND ( CodSubCos=@pmCodSubCos  OR CodSubCos  IS NULL OR LEN(CodSubCos)=0)
 ORDER BY IdConcepto,IdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConCuentas @pmIdConcepto VARCHAR(4),@pmItem INT
 AS 
 SELECT IdConcepto,Item,NClase,IdCuenta,TipoMov,CodNom,CodDep,CodFon,NitTerc
 FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConCuentasCla @pmIdConcepto VARCHAR(4),@pmNClase INT
,@pmCodNom VARCHAR(4),@pmCodDep VARCHAR(4)
 AS 
 SELECT IdConcepto,Item,NClase,IdCuenta,TipoMov,CodNom,CodDep,CodFon,NitTerc
 FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto AND NClase=@pmNClase 
 AND (CodNom=@pmCodNom OR CodNom IS NULL OR LEN(CodNom)=0 )
 AND (CodDep=@pmCodDep OR CodDep IS NULL OR LEN(CodDep)=0)
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConCuentasCls @pmIdConcepto VARCHAR(4),@pmNClase INT
 AS 
 SELECT IdConcepto,Item,NClase,IdCuenta,TipoMov,CodNom,CodDep,CodFon,NitTerc
 FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto AND NClase=@pmNClase 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConCuentasFon @pmIdConcepto VARCHAR(4),@pmNClase INT,@pmCodFon VARCHAR(8)
 AS 
 SELECT IdConcepto,Item,NClase,IdCuenta,TipoMov,CodFon,NitTerc
 FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto AND NClase=@pmNClase  AND CodFon=@pmCodFon
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConCuentasId @pmIdConcepto VARCHAR(4)
 AS 
 SELECT MAX(Item) AS MXITEM
 FROM NomConCuentas
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConCuentasLta @pmIdConcepto VARCHAR(4)=Null,@pmCodNom VARCHAR(4)=Null,@pmCodDep VARCHAR(4)=Null
,@pmInactivo BIT=Null
 AS 
 SELECT NC.IdConcepto  AS CodConcept,Concepto,Item,NClase,NC.IdCuenta AS CodCuenta,NomCuenta,TipoMov
,CodNom,TipoNomina,CodDep,Dependencia,CodFon,Fondo,ClaseCon,Unidad,Tarifa,Pdtermnado,NitTerc
 FROM NomConCuentas AS NC INNER JOIN NomConceptos AS C ON NC.IdConcepto=C.IdConcepto
 INNER JOIN Puc AS P ON NC.IdCuenta=P.IdCuenta 
 LEFT JOIN TiposNom AS TN ON NC.CodNom=TN.IdNom
 LEFT JOIN Dependencias AS D ON NC.CodDep=D.IdDep
 LEFT JOIN Fondos AS F ON NC.CodFon=F.IdFondo
 WHERE NC.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND CodNom LIKE ISNULL(@pmCodNom,'%')
 AND CodDep LIKE ISNULL(@pmCodDep,'%')
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY NC.IdConcepto,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConOpciones @pmIdConcepto VARCHAR(4),@pmIdOpc INT=Null
 AS 
 SELECT IdConcepto,IdOpc,NomOpcion,Asignado
 FROM NomConOpciones
 WHERE IdConcepto=@pmIdConcepto 
 AND (IdOpc>=ISNULL(@pmIdOpc,0)  AND IdOpc<=ISNULL(@pmIdOpc,2147483647))
 ORDER BY IdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConOpcionesId @pmIdConcepto VARCHAR(4),@pmIdOpc INT,@pmAsignado BIT
 AS 
 SELECT count(IdOpc) AS SCANT -- IdConcepto,IdOpc,NomOpcion,Asignado
 FROM NomConOpciones
 WHERE IdConcepto=@pmIdConcepto  AND IdOpc=@pmIdOpc AND Asignado=@pmAsignado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptos @pmIdConcepto VARCHAR(4)
 AS 
 SELECT IdConcepto,Concepto,ClaseCon,Unidad,Tarifa,BaseTrfa,Pdtermnado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM NomConceptos
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptosDsc
 AS 
 SELECT IdConcepto,Concepto,Concepto + ' ' + IdConcepto  AS DsCon
 FROM NomConceptos
 WHERE  Inactivo=0 AND ClaseCon IN ('DEVENGADO','DEDUCIDO')  
 AND IdConcepto NOT IN ('AXT','DNR','DSR','DCP','DCS','FSP','FSU','IGE','IRP','LIC','RET','BAS','VAC')
 ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptosDso @pmClaseCon VARCHAR(10)=Null,@pmPdtermnado BIT=Null,@pmInactivo BIT=Null
,@pmGrupo CHAR(1)=Null
 AS 
 IF @pmGrupo='N'--NOMINA
  SELECT IdConcepto,Concepto,Concepto + ' ' + IdConcepto  AS DsCon
  FROM NomConceptos
  WHERE ClaseCon IN ('DEVENGADO','DEDUCIDO')  
  AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
  AND  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
  ORDER BY Concepto
 ELSE 
  BEGIN 
  IF @pmGrupo='V' --NOVEDADES
   SELECT IdConcepto,Concepto,Concepto + ' ' + IdConcepto  AS DsCon
   FROM NomConceptos
   WHERE IdConcepto IN ('DNR','DSR','IGE','IRP','LIC','SAN','VAC','STC','AVP')  AND ClaseCon LIKE ISNULL(@pmClaseCon,'%')
   AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
   AND  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
   ORDER BY Concepto
  ELSE
   SELECT IdConcepto,Concepto,Concepto + ' ' + IdConcepto  AS DsCon
   FROM NomConceptos
   WHERE ClaseCon LIKE ISNULL(@pmClaseCon,'%') 
   AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
   AND  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
  ORDER BY Concepto
  END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptosLiq @pmPdtermnado BIT=Null,@pmInactivo BIT=Null
 AS 
  SELECT IdConcepto,Concepto,Concepto + ' ' + IdConcepto  AS DsCon
  FROM NomConceptos
  WHERE ( ClaseCon IN ('DEVENGADO','DEDUCIDO')  OR IdConcepto IN ('CES','PRI','VAC','INT') )
  AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
  AND  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
  ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptosLta @pmClaseCon VARCHAR(10)=Null,@pmPdtermnado BIT=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdConcepto,Concepto,ClaseCon,Unidad,Tarifa,BaseTrfa,Pdtermnado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM NomConceptos
 WHERE ClaseCon LIKE ISNULL(@pmClaseCon,'%')
  AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
  AND  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptosPre 
 AS 
  SELECT C.IdConcepto AS IdConcept,Concepto,Concepto + ' ' + C.IdConcepto  AS DsCon
  FROM NomConceptos AS C INNER JOIN NomConOpciones AS O ON C.IdConcepto=O.IdConcepto
  WHERE Inactivo=0 AND IdOpc=13 AND Asignado<>0
  ORDER BY Concepto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomConceptosTar @pmIdConcepto VARCHAR(4)=Null,@pmPdtermnado BIT=Null
 AS 
 SELECT IdConcepto,Concepto,Tarifa,Pdtermnado
 FROM NomConceptos
 WHERE IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND  (Pdtermnado=ISNULL(@pmPdtermnado,0) or Pdtermnado=ISNULL(@pmPdtermnado,1))
 ORDER BY IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDefault @pmnAnno INT,@pmIdOpc VARCHAR(3)
 AS 
 SELECT nAnno,IdOpc,Descripcion,Valor
 FROM NomDefault
 WHERE nAnno=@pmnAnno AND IdOpc LIKE ISNULL(@pmIdOpc,'%')
 ORDER BY IdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalle @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmItem INT
 AS 
 SELECT IdPeriodo,KeyRegistro,Item,IdEmpleado,IdConcepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,Unidad,ClaseLiq,VrOrigen,Tarifa,DiasCalc
,DiasNov,CodFondo,NPrestamo,NCuota,OrigCargue,VrBaseLiq
 FROM Trn_NomDetalle
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleAcu @pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmFecIni SMALLDATETIME
,@pmFecFin SMALLDATETIME,@pmIdConcepto VARCHAR(4)=Null,@pmConcBas BIT=Null
 AS 
 IF @pmConcBas=1 --SOLO CONCEPTOS DEL BASICO
  SELECT SUBSTRING(N.IdPeriodo,1,4) AS nAnno,SUBSTRING(N.IdPeriodo,5,2) AS nMes,SUM(CantDevg) AS SCANDEV,SUM(VrTotDevg) AS STOTDEV
,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
  FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON  N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
  WHERE N.IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato AND FecInicial>=@pmFecIni AND FecFinal<=@pmFecFin
  AND  ClaseLiq IN ('NOMINA','NOVEDAD')  AND VrBaseLiq>0 AND IdConcepto IN ('BAS','LIC','IGE','IRP','DSR','VAC')
  AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
  GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2)
 ELSE -- CONCEPTOS DEVENGADOS
  SELECT SUBSTRING(N.IdPeriodo,1,4) AS nAnno,SUBSTRING(N.IdPeriodo,5,2) AS nMes,SUM(CantDevg) AS SCANDEV,SUM(VrTotDevg) AS STOTDEV
,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
  FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON  N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
  WHERE N.IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato AND FecInicial>=@pmFecIni AND FecFinal<=@pmFecFin
  AND  ClaseLiq IN ('NOMINA','NOVEDAD')  AND VrBaseLiq>0 AND IdConcepto IN ('BAS','AXT','LIC','IGE','IRP','HHD','HHN','DSR','VAC','HFD','HFN')
  AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
  GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleAux @pmIdEmpleado VARCHAR(16),@pmNContrato INT
,@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
 AS 
    SELECT SUBSTRING(N.IdPeriodo,1,4) AS nAnno,SUBSTRING(N.IdPeriodo,5,2) AS nMes,VrATransMes
    ,SUM(CantDevg) AS SCANDEV,SUM(VrTotDevg) AS STOTDEV,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV
   ,SUM(VrBaseLiq) AS SBASELIQ
    FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON  N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
    WHERE N.IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato
    AND FecInicial>=@pmFecIni AND FecFinal<=@pmFecFin
    AND IdConcepto='AXT' AND  ClaseLiq IN ('NOMINA','NOVEDAD')  AND VrBaseLiq>0
    GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2),VrATransMes
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleBas @pmMes VARCHAR(6),@pmKeyRegistro VARCHAR(30),@pmIdEmpleado VARCHAR(16)
,@pmIdConcepto VARCHAR(4),@pmIdPeriodo VARCHAR(8),@pmTipBas VARCHAR(3),@pmClaseLiq VARCHAR(10),@pmPerMax INT
 AS 
 IF @pmTipBas='SD' --SALARIO BASICO
 SELECT IdConcepto,SUM(CantDevg) AS SCANDEV,SUM(CantDed) AS SCANDED,SUM(VrTotDevg) AS STOTDEV,SUM(VrTotDed) AS STOTDED
 ,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
 FROM Trn_NomDetalle 
 WHERE KeyRegistro=@pmKeyRegistro  AND IdEmpleado=@pmIdEmpleado AND SUBSTRING(IdPeriodo,1,6)=@pmMes
 AND IdPeriodo<>@pmIdPeriodo  AND CAST(SUBSTRING(IdPeriodo,7,2) AS INT)<@pmPerMax
 AND  IdConcepto NOT IN ('BAS','LIC','IGE','IRP','HHD','HHN','DSR','VAC')  AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
 GROUP BY IdConcepto

 ELSE --TOTAL DEVENGADO

 SELECT IdConcepto, SUM(CantDevg) AS SCANDEV,SUM(CantDed) AS SCANDED,SUM(VrTotDevg) AS STOTDEV,SUM(VrTotDed) AS STOTDED
 ,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
 FROM Trn_NomDetalle 
 WHERE KeyRegistro=@pmKeyRegistro  AND IdEmpleado=@pmIdEmpleado AND SUBSTRING(IdPeriodo,1,6)=@pmMes
 AND IdPeriodo<>@pmIdPeriodo  AND CAST(SUBSTRING(IdPeriodo,7,2) AS INT)<@pmPerMax
 AND ClaseLiq IN ('NOMINA','NOVEDAD')  AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 GROUP BY IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleDia @pmMes VARCHAR(6),@pmKeyRegistro VARCHAR(30),@pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4)
,@pmIdPeriodo VARCHAR(8),@pmClaseLiq VARCHAR(10)=Null
 AS 
 SELECT SUM(DiasCalc) AS SDiasCalc,SUM(DiasNov) AS SDiasNov,SUM(DiasCalc-DiasNov) AS DiasNeto
 FROM Trn_NomDetalle
 WHERE  KeyRegistro=@pmKeyRegistro AND IdEmpleado=@pmIdEmpleado AND IdConcepto=@pmIdConcepto
 AND SUBSTRING(IdPeriodo,1,6)=@pmMes AND IdPeriodo<>@pmIdPeriodo AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleDsp @pmIdPeriodo VARCHAR(8)=Null,@pmIdEmpleado VARCHAR(16)=Null
 AS 
-- listado de carga inicial de promedios
 SELECT D.IdEmpleado AS CodEmpleado,Apellidos,Nombres,D.IdPeriodo AS IdPerdo,Item,IdConcepto,Detalle,VrBaseLiq,N.NContrato AS NumCont
,FecInicial,FecFinal,N.KeyRegistro AS KeyRegtro,VrDevengado,TimeSys,N.IdUsuario AS IdUsuari
 FROM Trn_NomDetalle AS D INNER JOIN Trn_Nomina AS N ON D.IdPeriodo=N.IdPeriodo AND D.KeyRegistro=N.KeyRegistro
 INNER JOIN Empleados AS E ON D.IdEmpleado=E.IdEmpleado 
 WHERE NLiqRet=-1 AND  D.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')  AND D.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY Apellidos,Nombres,D.IdPeriodo,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleHex  @pmIdPeriodo VARCHAR(8),@pmMes VARCHAR(6),@pmIdEmpleado VARCHAR(16)=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmEsRetiro BIT=Null
 AS 
 SELECT  N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContrato,N.IdDep AS CodDepe,N.IdInstala AS CdInstala,N.IdCCosto AS CodCcosto
,N.IdSubCos AS CdSubcen,N.IdNom AS TipNom,N.VrSalario AS VrBasico,VrATransMes
,N.pVehiculo AS PlacaVeh,EsRetiro,IdArea,TipoLiq,D.IdConcepto AS CodConcepto,Concepto
,SUM(CantDevg) AS SCantDev,SUM(VrTotDevg) AS STotDev,SUM(CantDed) AS SCantDed,SUM(VrTotDed) AS STotDed,SUM(VrOrigen) AS STotBase
,SUM(DiasCalc-DiasNov) AS TotalDias,SUM(VrBaseLiq) AS STotLiq
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE ClaseLiq<>'PROVISION' AND ClaseLiq<>'APORTE'  AND SUBSTRING(N.IdPeriodo,1,6)=@pmMes
 AND ClaseCon='DEVENGADO'
 AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom like ISNULL(@pmIdNom,'%')  AND  N.IdDep LIKE ISNULL(@pmIdDep,'%') 
 AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND IdArea LIKE ISNULL(@pmIdArea,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 GROUP BY N.IdEmpleado,Apellidos,Nombres,N.NContrato,N.IdDep,N.IdInstala,N.IdCCosto
,N.IdSubCos,N.IdNom,N.VrSalario,VrATransMes,N.pVehiculo,EsRetiro,IdArea,TipoLiq,D.IdConcepto,Concepto
 ORDER BY  N.IdEmpleado,N.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetallePer @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30)=Null,@pmIdNom VARCHAR(4)=Null
 AS 
 SELECT D.IdEmpleado AS CedEmpleado,Apellidos,Nombres,Item,D.IdConcepto AS IdConcept,Detalle,ClaseCon,CantDevg,VrUnitario,VrTotDevg,CantDed,VrTotDed
,D.Unidad AS Undad,VrOrigen,D.Tarifa AS Tarfa,VrBaseLiq,DiasCalc,DiasNov,DiasCalc-DiasNov AS DiasNeto,ClaseLiq,NPrestamo,NCuota,OrigCargue,CodFondo,D.KeyRegistro AS Kyregistro
 FROM Trn_NomDetalle AS D INNER JOIN Empleados AS E ON D.IdEmpleado=E.IdEmpleado
 INNER JOIN  NomConceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN Trn_Nomina AS N ON D.IdPeriodo=N.IdPeriodo AND D.KeyRegistro=N.KeyRegistro
 WHERE D.IdPeriodo=@pmIdPeriodo AND D.KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%') AND IdNom like ISNULL(@pmIdNom,'%')
 ORDER BY Apellidos,Nombres,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetallePmd @pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmModoPmd CHAR(1),@pmFecIni SMALLDATETIME
,@pmFecFin SMALLDATETIME,@pmIdConcepto VARCHAR(4)=Null
 AS 
 IF @pmModoPmd='F' --SALARIOS FIJO
  SELECT SUBSTRING(N.IdPeriodo,1,4) AS nAnno,SUBSTRING(N.IdPeriodo,5,2) AS nMes,IdConcepto,SUM(CantDevg) AS SCANDEV,SUM(VrTotDevg) AS STOTDEV
,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
  FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON  N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
  WHERE N.IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato AND FecInicial>=@pmFecIni AND FecFinal<=@pmFecFin
  AND  ClaseLiq IN ('NOMINA','NOVEDAD')  AND VrBaseLiq>0 AND IdConcepto NOT IN ('BAS','AXT','LIC','IGE','IRP','HHD','HHN','DSR','VAC')
  GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2),IdConcepto
 ELSE 
  BEGIN
   IF @pmModoPmd='B' --SALARIO BASICO
    SELECT SUBSTRING(N.IdPeriodo,1,4) AS nAnno,SUBSTRING(N.IdPeriodo,5,2) AS nMes,IdConcepto,SUM(CantDevg) AS SCANDEV,SUM(VrTotDevg) AS STOTDEV
   ,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
    FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON  N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
    WHERE N.IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato AND FecInicial>=@pmFecIni AND FecFinal<=@pmFecFin
   AND  ClaseLiq IN ('NOMINA','NOVEDAD')  AND VrBaseLiq>0 AND IdConcepto IN ('BAS','AXT','LIC','IGE','IRP','HHD','HHN','DSR','VAC')
   GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2),IdConcepto
  ELSE --SALARIO VARIABLE
   SELECT SUBSTRING(N.IdPeriodo,1,4) AS nAnno,SUBSTRING(N.IdPeriodo,5,2) AS nMes, IdConcepto,SUM(CantDevg) AS SCANDEV,SUM(VrTotDevg) AS STOTDEV
  ,SUM(DiasCalc) AS SDIASCAL,SUM(DiasNov) AS SDIASNOV,SUM(VrBaseLiq) AS SBASELIQ
   FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON  N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
   WHERE N.IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato AND FecInicial>=@pmFecIni AND FecFinal<=@pmFecFin
   AND  ClaseLiq IN ('NOMINA','NOVEDAD') AND VrBaseLiq>0 AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
   GROUP BY  SUBSTRING(N.IdPeriodo,1,4),SUBSTRING(N.IdPeriodo,5,2),IdConcepto
  END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleRcon @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdPeriodo VARCHAR(8)=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto  VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmClaseLiq VARCHAR(10)=Null,@pmClaseCon VARCHAR(10)=Null,@pmNPrestamo INT=Null
 AS 
 SELECT N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumCntrato,D.IdConcepto AS CodConcept,Concepto,ClaseCon,SUBSTRING(N.IdPeriodo,1,6)  AS nMES
 ,SUM(CantDevg) AS SCantDevg,SUM(VrTotDevg) AS STotDevg,SUM(CantDed) AS SCantDed,SUM(VrTotDed) AS STotDed,SUM(VrOrigen) AS STotBase
,SUM(DiasCalc) AS SCanDia,SUM(DiasNov) AS SDiasNov,SUM(DiasCalc-DiasNov) AS SDiasNeto,SUM(VrBaseLiq) AS SBaseLiq
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN  Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND  N.KeyRegistro=D.KeyRegistro
 INNER JOIN NomConceptos AS NC ON D.IdConcepto=NC.IdConcepto
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdArea LIKE ISNULL(@pmIdArea,'%')
 AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
 AND ClaseCon LIKE ISNULL(@pmClaseCon,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-2)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
 GROUP BY  N.IdEmpleado,Apellidos,Nombres,N.NContrato,D.IdConcepto,Concepto,ClaseCon,SUBSTRING(N.IdPeriodo,1,6)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleRes @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdPeriodo VARCHAR(8)=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto  VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmClaseLiq VARCHAR(10)=Null,@pmClaseCon VARCHAR(10)=Null,@pmNPrestamo INT=Null
 AS 
 SELECT N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumCntrato,N.KeyRegistro AS KeyReg,N.IdPeriodo AS NumPeriodo,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido
,N.IdNom AS CodNmna,TipoNomina,N.IdDep AS CodDep,Dependencia,N.IdCCosto AS CodCcto,CCosto,N.IdSubCos AS CodSubcos,SubCosto,N.IdInstala AS CodIstala,Instlacion
,N.VrSalario AS SalBasico,N.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS IdEstdo,ED.Estado AS Nom_Estado,EsRetiro,NLiqRet,TipoLiq,TimeSys
,N.FecUpdate AS Fec_Update,N.IdUsuario AS IdUsuari,Usuario
--datos del detalle
,Item,D.IdConcepto AS CodConcept,Concepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,ClaseCon,D.Unidad AS Undad,ClaseLiq,VrOrigen,D.Tarifa AS Tarfa,DiasCalc,DiasNov,CodFondo,FCO.Fondo AS NomFondo
,NPrestamo,NCuota,OrigCargue,VrBaseLiq
--Datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,FC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS VlrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,SegFinMes,DchDotacion,DchCafeteria
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,TipoTrabj,C.IdGrupo AS CodGrup
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN TiposNom AS TN ON N.IdNom=TN.IdNom INNER JOIN Dependencias AS DP ON N.IdDep=DP.IdDep
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto INNER JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN Instalaciones AS I ON N.IdInstala=I.IdInstala  INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco 
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo 
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS FC ON E.IdCajaCom=FC.IdFondo 
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN  Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND  N.KeyRegistro=D.KeyRegistro
 INNER JOIN NomConceptos AS NC ON D.IdConcepto=NC.IdConcepto
 LEFT JOIN TiposCom AS TCM ON N.TipCom=TCM.IdCom  LEFT JOIN Fondos AS FCO ON D.CodFondo=FCO.IdFondo
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
 AND ClaseCon LIKE ISNULL(@pmClaseCon,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-2)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
 ORDER BY  N.IdEmpleado,N.IdPeriodo,D.IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleResN @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdPeriodo VARCHAR(8)=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto  VARCHAR(16)=Null,@pmEsRetiro BIT=Null
,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmClaseLiq VARCHAR(10)=Null,@pmClaseCon VARCHAR(10)=Null
 AS 
SELECT N.IdEmpleado AS IdEmple,N.NContrato AS NumContrato,FecInicial,FecFinal,N.IdPeriodo AS IdPerdo,IdArea,N.IdDep AS nDepe,N.IdInstala AS nInstala
,N.IdCCosto AS CodCcosto,N.IdSubCos AS CodSubcentro,N.IdNom AS CdNom,N.VrSalario AS vSalario,VrATransMes,N.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom
,N.Observacion AS Observ,EsRetiro,NLiqRet,TipoLiq
--Detales
,D.IdConcepto AS CdConcepto,Item,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,D.Unidad AS Und,ClaseLiq,VrOrigen,D.Tarifa AS Tarif,DiasCalc,DiasNov,CodFondo,NPrestamo,NCuota,VrBaseLiq
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN  Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND  N.KeyRegistro=D.KeyRegistro
 INNER JOIN NomConceptos AS NC ON D.IdConcepto=NC.IdConcepto
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND IdArea LIKE ISNULL(@pmIdArea,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%')  AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
 AND ClaseCon LIKE ISNULL(@pmClaseCon,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 ORDER BY  FecInicial, N.IdEmpleado,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleRtot @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdPeriodo VARCHAR(8)=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto  VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmClaseLiq VARCHAR(10)=Null,@pmClaseCon VARCHAR(10)=Null,@pmNPrestamo INT=Null
 AS 
 SELECT SUM(CantDevg) AS SCantDevg,SUM(VrTotDevg) AS STotDevg,SUM(CantDed) AS SCantDevg,SUM(VrTotDed) AS STotDed,SUM(VrOrigen) AS STotBase
,SUM(DiasCalc) AS SCantDias,SUM(DiasNov) AS SDiasNoRem,SUM(DiasCalc-DiasNov) AS SDiasNet,SUM(VrBaseLiq) AS STotLiq
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN  Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND  N.KeyRegistro=D.KeyRegistro
 INNER JOIN NomConceptos AS NC ON D.IdConcepto=NC.IdConcepto
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdArea LIKE ISNULL(@pmIdArea,'%')
 AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
 AND ClaseCon LIKE ISNULL(@pmClaseCon,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-2)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomDetalleTar @pmMes VARCHAR(6),@pmIdPeriodo VARCHAR(8)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmClaseLiq VARCHAR(10)=Null
 AS 
 SELECT D.IdConcepto AS CodConcepto,Concepto,CN.Tarifa AS TrifaDft,D.Tarifa AS Tarfa,SUM(VrTotDevg) AS STotDev,SUM(VrTotDed) AS StotDed
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D  ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE SUBSTRING(N.IdPeriodo,1,6)=@pmMes AND NLiqRet>=0 AND D.IdConcepto IN ('DCP','DCS','PEN','RIE','SAL','FSP','FSU','SEN','ICB','COM')
 AND N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND ClaseLiq LIKE ISNULL(@pmClaseLiq,'%')
 GROUP BY  D.IdConcepto,Concepto,CN.Tarifa,D.Tarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomFijos @pmIdEmpleado VARCHAR(16),@pmnAnno INT,@pmnMes INT
 AS 
 SELECT IdEmpleado,nAnno,nMes,Tarifa,VrBase,VrSalarios,VrPrima,VrVacacnes,VrSIntExcto,VrOtrosIng,VrIngresos,VrPensiones,VrSalud,VrDedcido,VrExcento,VrCertEdu,VrCertVda
,VrCertAFC,VrCertfcado,VrTotalBase,Periodos,OrignPorc,FecAdd,FecUpdate,IdUsuario
 FROM Trn_NomFijos
 WHERE IdEmpleado=@pmIdEmpleado AND nAnno=@pmnAnno AND nMes=@pmnMes
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomFijosLta @pmIdEmpleado VARCHAR(16)=Null,@pmnAnno INT=Null,@pmnMes INT=Null
 AS 
 SELECT F.IdEmpleado AS IdEmpledo,Apellidos,Nombres,nAnno,nMes,Tarifa,VrBase,VrSalarios,VrPrima,VrVacacnes,VrSIntExcto,VrOtrosIng,VrIngresos,VrPensiones,VrSalud,VrDedcido,VrExcento
,VrCertEdu,VrCertVda,VrCertAFC,VrCertfcado,VrTotalBase,Periodos,OrignPorc,Codigo,NContrato,FecUltIng,F.IdUsuario AS IdUsuari,Usuario,FecAdd,FecUpdate
 FROM Trn_NomFijos AS F INNER JOIN Empleados AS E ON F.IdEmpleado=E.IdEmpleado
 INNER JOIN adm_Usuarios AS U ON F.IdUsuario=U.IdUsuario
 WHERE F.IdEmpleado like ISNULL(@pmIdEmpleado,'%')
 AND (nAnno>=ISNULL(@pmnAnno,0)  AND nAnno<=ISNULL(@pmnAnno,2147483647))
 AND (nMes>=ISNULL(@pmnMes,0)  AND nMes<=ISNULL(@pmnMes,2147483647))
 ORDER BY Apellidos,Nombres,nAnno,nMes
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiqDet @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmItem INT
 AS 
 SELECT TipoLiq,Numero,Item,IdConcepto,Detalle,FecUltLiq,FecInicial,FecFinal,VrBase,Cantidad,VrLiquida,VrDeducido,FecIniPmd
,FecFinPmd,DiasBase,DiasNoRem,Tarifa,NPrestamo,NCuota,LogPmdio,IdEmpleado,NContrato,pVehiculo,IdDep
,IdCCosto,IdSubCos,IdNom,VrSalario,VrParcial
 FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiqDetEmp @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT IdEmpleado,NContrato,FecUltLiq,FecInicial,FecFinal, COUNT(Item) AS SITEM,SUM(VrLiquida) AS SValLiq,SUM(VrDeducido) AS SValDed
 FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 GROUP BY  IdEmpleado,NContrato,FecUltLiq,FecInicial,FecFinal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiqDetTEmp @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmIdEmpleado VARCHAR(16)
,@pmNContrato INT
 AS 
 SELECT SUM(VrBase) AS SBASE,SUM(Cantidad) AS SCANT,SUM(VrLiquida) AS STOTLIQ,SUM(VrDeducido) AS STOTDED
 FROM Trn_NomLiqDet
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiqDetTnm  @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmIdNom VARCHAR(4)=Null
AS
 SELECT D.IdNom AS CodNom,TipoNomina,TipoLiquida,COUNT(Item) AS SITEM
 FROM Trn_NomLiqDet  AS D INNER JOIN TiposNom AS T ON D.IdNom=T.IdNom
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND D.IdNom LIKE ISNULL(@pmIdNom,'%')
 GROUP BY D.IdNom,TipoNomina,TipoLiquida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiqDet_Cr  @pmTipoLiq VARCHAR(3),@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmNumero INT=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmSemstre INT=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmNContrato INT=Null,@pmIdConcepto VARCHAR(4)=Null
AS
 SELECT Q.TipoLiq AS Tip_Liqda,Q.Numero AS NumLiquida,Fecha,Semstre,VrTotal,VrTotDed,VrAplicado,TipCom,TipoCom,Comprobante,IdCiaCom,Q.Observacion AS Observ,Anulado,Q.IdEstado AS IdEstdo,ED.Estado AS Liq_estado,TimeSys,Q.FecUpdate AS FecUpd,Q.IdUsuario AS IdUsuari,Usuario
--detalle de liquidaicion
,D.IdEmpleado AS IdEmpldo,Apellidos,Nombres,D.NContrato AS NumContrato,D.pVehiculo AS PlacaVeh,D.IdDep AS CodDep,Dependencia,D.IdCCosto AS CodCcto,CCosto,D.IdSubCos AS CodSubcen,SubCosto,D.IdNom AS CodNom,TipoNomina,D.VrSalario AS Liq_SalBasico
,Item,D.IdConcepto AS CodConcepto,Concepto,Detalle,ClaseCon,FecUltLiq,FecInicial,FecFinal,VrBase,Cantidad,VrLiquida,VrDeducido,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,D.Tarifa AS Tarfa,NPrestamo,NCuota,LogPmdio,Unidad
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen,IdFonEps,FS.Fondo AS FondoEps
,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,IdFonCes,FC.Fondo AS FondoCes,EsCondtor,E.pVehiculo AS Emp_Vehiculo,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,FecUltCes,FecUltPri,FecUltVac,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdInstala AS CodInst,Instlacion,C.IdJornada AS CodJnada,Jornada,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga,TipoTrabj
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModPromdio,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,DenyCes,DenyPri,DenyVac,BasePrest
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,VrParcial
 FROM Trn_NomLiquida AS Q INNER JOIN Trn_NomLiqDet AS D ON Q.TipoLiq=D.TipoLiq AND Q.Numero=D.Numero
 INNER JOIN adm_Usuarios AS U ON Q.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON Q.IdEstado=ED.IdEstado
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  INNER JOIN  Empleados AS E ON D.IdEmpleado=E.IdEmpleado
 INNER JOIN Dependencias AS DP ON D.IdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON D.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON D.IdNom=TN.IdNom 
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON D.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 LEFT JOIN TiposCom AS TM ON Q.TipCom=TM.IdCom 
 WHERE Q.TipoLiq=@pmTipoLiq  AND D.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND D.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND D.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND Q.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (Q.Numero>=ISNULL(@pmNumero,0)  AND Q.Numero<=ISNULL(@pmNumero,2147483647))
 AND (Semstre >=ISNULL(@pmSemstre ,0)  AND Semstre <=ISNULL(@pmSemstre,2147483647))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (D.NContrato >=ISNULL(@pmNContrato ,0)  AND D.NContrato <=ISNULL(@pmNContrato,2147483647))
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY Q.Numero,Apellidos,Nombres,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquida @pmTipoLiq VARCHAR(3),@pmNumero INT
 AS 
 SELECT TipoLiq,Numero,Fecha,Semstre,VrTotal,VrTotDed,VrAplicado,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomLiquida
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaBan @pmTipoLiq VARCHAR(3),@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null
AS
SELECT L.TipoLiq AS Tliq,D.IdEmpleado AS IdEmpldo,Apellidos,Nombres,TipoId,Codigo,IdLocal,E.IdClase AS CdClase,ClaseCuenta,NumCuenta
,E.IdBanco AS CdBanco,Banco,CodEntidad,CdLocCue,e_mail,Telefono,D.NContrato AS NumContrato,IdConcepto
,E.Direccion AS EmpDireccion,TelMovil,Cargo 
,SUM(VrLiquida) AS STOTLIQ,SUM(VrDeducido) AS STOTDED,SUM(VrParcial) AS STOTPARC,SUM(VrLiquida-VrDeducido) AS SNETLIQ
 FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
 INNER JOIN Empleados AS E ON D.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON D.NContrato=C.NContrato
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 WHERE  Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND Anulado=0  AND FPnomina='TRANS' 
 --AND IdConcepto IN ('CES','PRI','INT')
 AND L.TipoLiq LIKE ISNULL(@pmTipoLiq,'%')  AND D.IdNom LIKE ISNULL(@pmIdNom,'%') AND D.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') 
 GROUP BY L.TipoLiq,D.IdEmpleado,Apellidos,Nombres,TipoId,Codigo,IdLocal,E.IdClase,ClaseCuenta,NumCuenta
 ,E.IdBanco,Banco,CodEntidad,CdLocCue,e_mail,Telefono,D.NContrato,IdConcepto,E.Direccion,TelMovil,Cargo
 ORDER BY D.IdEmpleado,L.TipoLiq
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaCes @pmTipoLiq VARCHAR(3),@pmIdEmpleado VARCHAR(16),@pmFechLiq SMALLDATETIME
 AS 
 SELECT L.TipoLiq AS TipLiq,L.Numero AS NumLiq,Fecha,VrTotal,VrTotDed,VrAplicado,VrParcial,Anulado,IdEstado
 FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
 WHERE L.TipoLiq=@pmTipoLiq  AND Anulado=0 AND IdEmpleado=@pmIdEmpleado
 AND Fecha>=@pmFechLiq
 ORDER BY L.Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaCmp @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
 AS 
 SELECT  TipCom,Comprobante,IdCiaCom,COUNT(Numero) AS SCANT 
 FROM  Trn_NomLiquida
 WHERE TipoLiq='PRI'  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND Comprobante>0
 GROUP BY TipCom,Comprobante,IdCiaCom

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaEmp @pmTipoLiq VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdEmpleado VARCHAR(16),@pmNContrato INT=Null
 AS 
 SELECT L.TipoLiq AS TipLiq,L.Numero AS NumLiq,Fecha,IdEmpleado,NContrato,SUM(VrBase) AS STOTBASE,SUM(Cantidad) AS SDIAS,SUM(VrLiquida) AS SVLIQ
,SUM(VrDeducido) AS SVDED,SUM(DiasBase) AS SDiasBase,SUM(DiasNoRem) AS SDiasNoRem,SUM(VrParcial) AS SVRPAR
 FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
 WHERE L.TipoLiq=@pmTipoLiq AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (NContrato>=ISNULL(@pmNContrato,0)  AND NContrato<=ISNULL(@pmNContrato,2147483647))
 GROUP BY L.TipoLiq,L.Numero,Fecha,IdEmpleado,NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaFec @pmTipoLiq VARCHAR(3),@pmFecha SMALLDATETIME,@pmNumero INT
 AS 
 SELECT TipoLiq,Numero,Fecha,Semstre,IdEstado,TipCom,Comprobante,IdCiaCom
 FROM Trn_NomLiquida
 WHERE TipoLiq=@pmTipoLiq AND Anulado=0 AND Fecha>@pmFecha  AND Numero<>@pmNumero
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaId  @pmTipoLiq VARCHAR(3)
AS
 SELECT MAX(Numero) AS MXNUM FROM Trn_NomLiquida
 WHERE TipoLiq=@pmTipoLiq
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaIng @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmNContrato INT=Null,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null,@pmIdConcepto VARCHAR(4)=Null
AS
 SELECT Q.TipoLiq AS Tip_Liqda,Q.Numero AS NumLiquida,Fecha,D.IdEmpleado AS IdEmpldo,Apellidos,Nombres,D.NContrato AS NumContrato,D.pVehiculo AS PlacaVeh,D.IdDep AS CodDep
,D.IdCCosto AS CodCcto,D.IdSubCos AS CodSubcen,D.IdNom AS CodNom,D.VrSalario AS Liq_SalBasico,Item,D.IdConcepto AS CodConcepto,Concepto,CN.Tarifa AS TrfaDft,Detalle,ClaseCon,FecUltLiq,FecInicial,FecFinal
,VrBase,Cantidad,VrLiquida,VrDeducido,DiasBase,DiasNoRem,D.Tarifa AS Tarfa,NPrestamo,IdTipCon,IdArea,IdInstala,C.Inactivo AS Con_inactivo,E.Inactivo AS Emp_Inactvo,IdFonCes,VrParcial
 FROM Trn_NomLiquida AS Q INNER JOIN Trn_NomLiqDet AS D ON Q.TipoLiq=D.TipoLiq AND Q.Numero=D.Numero
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  INNER JOIN  Empleados AS E ON D.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON D.NContrato=C.NContrato
 WHERE  Fecha  BETWEEN @pmFecIni AND @pmFecFin AND Anulado=0 AND D.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND IdArea LIKE ISNULL(@pmIdArea,'%')  AND D.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND IdInstala LIKE ISNULL(@pmIdInstala,'%') AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%') AND D.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (D.NContrato >=ISNULL(@pmNContrato ,0)  AND D.NContrato <=ISNULL(@pmNContrato,2147483647))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 ORDER BY D.IdEmpleado,Q.TipoLiq,Q.Numero,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaPar @pmTipoLiq VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdEmpleado VARCHAR(16),@pmNContrato INT=Null
 AS 
 SELECT IdConcepto,Detalle,SUM(VrBase) AS STOTBASE,SUM(Cantidad) AS SDIAS,SUM(VrLiquida) AS SVLIQ,SUM(VrDeducido) AS SVDED
,SUM(DiasBase) AS SDiasBase,SUM(DiasNoRem) AS SDiasNoRem,SUM(VrParcial) AS SVRPAR
 FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
 WHERE L.TipoLiq=@pmTipoLiq AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND VrTotal>VrAplicado
 AND  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (NContrato>=ISNULL(@pmNContrato,0)  AND NContrato<=ISNULL(@pmNContrato,2147483647))
 GROUP BY IdConcepto,Detalle
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaPtot @pmTipoLiq VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdEmpleado VARCHAR(16),@pmNContrato INT=Null,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT IdConcepto,SUM(VrBase) AS STOTBASE,SUM(Cantidad) AS SDIAS,SUM(VrLiquida) AS SVLIQ,SUM(VrDeducido) AS SVDED
,SUM(DiasBase) AS SDiasBase,SUM(DiasNoRem) AS SDiasNoRem,SUM(VrParcial) AS SVRPAR
 FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
 WHERE L.TipoLiq=@pmTipoLiq AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (NContrato>=ISNULL(@pmNContrato,0)  AND NContrato<=ISNULL(@pmNContrato,2147483647))
 GROUP BY IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaTip  @pmTipoLiq VARCHAR(3),@pmFecha SMALLDATETIME,@pmSemstre INT=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdEmpleado VARCHAR(16)=Null
AS
 SELECT L.TipoLiq AS TLiq,L.Numero AS NumLiq,Fecha
  FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
  WHERE L.TipoLiq=@pmTipoLiq AND Fecha=@pmFecha AND Anulado=0 AND  IdDep LIKE ISNULL(@pmIdDep,'%') 
  AND IdNom LIKE ISNULL(@pmIdNom,'%')   AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (Semstre>=ISNULL(@pmSemstre,0)  AND Semstre<=ISNULL(@pmSemstre,2147483647))
 ORDER BY L.Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquidaUcp @pmTipoLiq VARCHAR(3),@pmIdEmpleado VARCHAR(16)
 AS 
 SELECT L.TipoLiq AS TipLiq,L.Numero AS NumLiq,Fecha,VrTotal,VrTotDed,VrAplicado,Anulado,IdEstado,VrParcial
 FROM Trn_NomLiquida AS L INNER JOIN Trn_NomLiqDet AS D ON L.TipoLiq=D.TipoLiq AND L.Numero=D.Numero
 WHERE L.TipoLiq=@pmTipoLiq  AND Anulado=0 AND IdEmpleado=@pmIdEmpleado
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLiquida_Cr @pmTipoLiq VARCHAR(3),@pmNumero INT=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 SELECT Q.TipoLiq AS Tip_Liqda,Q.Numero AS NumLiquida,Fecha,Semstre,VrTotal,VrTotDed,VrAplicado,TipCom,TipoCom,Comprobante,IdCiaCom,Q.Observacion AS Observ,Anulado,Q.IdEstado AS IdEstdo,ED.Estado AS Liq_estado,TimeSys,Q.FecUpdate AS FecUpd,Q.IdUsuario AS IdUsuari,Usuario
--detalle de liquidaicion
,D.IdEmpleado AS IdEmpldo,Apellidos,Nombres,D.NContrato AS NumContrato,D.pVehiculo AS PlacaVeh,D.IdDep AS CodDep,Dependencia,D.IdCCosto AS CodCcto,CCosto,D.IdSubCos AS CodSubcen,SubCosto,D.IdNom AS CodNom,TipoNomina,D.VrSalario AS Liq_SalBasico
,Item,D.IdConcepto AS CodConcepto,Concepto,Detalle,ClaseCon,FecUltLiq,FecInicial,FecFinal,VrBase,Cantidad,VrLiquida,VrDeducido,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,D.Tarifa AS Tarfa,NPrestamo,NCuota,LogPmdio,Unidad
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdInstala AS CodInst,Instlacion,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes
,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada
,FecUltCes,FecUltPri,FecUltVac,NumVeh,TipoAdmon,VehPropio,VrParcial
 FROM Trn_NomLiquida AS Q INNER JOIN Trn_NomLiqDet AS D ON Q.TipoLiq=D.TipoLiq AND Q.Numero=D.Numero
 INNER JOIN adm_Usuarios AS U ON Q.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON Q.IdEstado=ED.IdEstado
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  INNER JOIN  Empleados AS E ON D.IdEmpleado=E.IdEmpleado
 INNER JOIN Dependencias AS DP ON D.IdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON D.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON D.IdNom=TN.IdNom 
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON D.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 LEFT JOIN TiposCom AS TM ON Q.TipCom=TM.IdCom LEFT JOIN NomVehiculos AS VH ON D.pVehiculo=VH.IdVehiculo
 WHERE Q.TipoLiq=@pmTipoLiq 
 AND (Q.Numero>=ISNULL(@pmNumero,0)  AND Q.Numero<=ISNULL(@pmNumero,2147483647))
 AND D.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND D.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND D.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Q.Numero,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLogVeh @pmIdEmpleado VARCHAR(16),@pmNumReg INT
 AS 
 SELECT IdEmpleado,NumReg,Fecha,pVehAnterior,NitPropAnt,NitCondAnt,pVehNew,NitPropNew,Observacion,TimeSys,IdUsuario
 FROM Trn_NomLogVeh
 WHERE IdEmpleado=@pmIdEmpleado
 AND (NumReg>=ISNULL(@pmNumReg,0)  AND NumReg<=ISNULL(@pmNumReg,2147483647))
 ORDER BY NumReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLogVehDso @pmIdEmpleado VARCHAR(16)=Null,@pmNumReg INT=Null,@pmFechaIni SMALLDATETIME=Null
,@pmFechaFin SMALLDATETIME=Null,@pmpVehNew VARCHAR(10)=Null
 AS 
 SELECT LV.IdEmpleado AS IdEmpldo,Apellidos,Nombres,NumReg,Fecha,pVehAnterior,NitPropAnt,NitCondAnt,pVehNew,NitPropNew,LV.Observacion AS Observ
,TimeSys,LV.IdUsuario AS IdUsuari,Usuario,NContrato
 FROM Trn_NomLogVeh AS LV INNER JOIN Empleados AS E ON LV.IdEmpleado=E.IdEmpleado INNER JOIN adm_Usuarios AS U ON LV.IdUsuario=U.IdUsuario
 WHERE LV.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND pVehNew LIKE ISNULL(@pmpVehNew,'%')
 AND (NumReg>=ISNULL(@pmNumReg,0)  AND NumReg<=ISNULL(@pmNumReg,2147483647))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,NumReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomLogVehId @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(NumReg) AS MXNUM
 FROM Trn_NomLogVeh
 WHERE IdEmpleado=@pmIdEmpleado

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNov @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16),@pmItem INT
 AS 
 SELECT Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrTotal,Tarifa,VrOrigen,IdPeriodo,IdEmpleado,TimeSys,FecUpdate,IdUsuario,OrigCargue
 FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovCon @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16) 
,@pmIdConcepto VARCHAR(4),@pmOrigCargue VARCHAR(10)=Null
 AS 
 SELECT Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrTotal,Tarifa,VrOrigen,IdPeriodo,IdEmpleado
,TimeSys,FecUpdate,IdUsuario,OrigCargue
 FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado
 AND IdConcepto=@pmIdConcepto
 AND OrigCargue LIKE ISNULL(@pmOrigCargue,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovId @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Item) AS MXITEM
 FROM Trn_NomNov
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovTot @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)
,@pmDeveng BIT
 AS 
 IF @pmDeveng <>0 --devengados 
  SELECT Count(Item) AS SITEM,SUM(Cantidad) AS SCANT,SUM(VrTotal) AS STOTAL,SUM(VrOrigen) AS SVBASE
  FROM Trn_NomNov AS N  INNER JOIN NomConceptos AS C ON N.IdConcepto=C.IdConcepto
  WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado AND ClaseCon<>'DEDUCIDO'
 ELSE
  SELECT Count(Item) AS SITEM,SUM(Cantidad) AS SCANT,SUM(VrTotal) AS STOTAL,SUM(VrOrigen) AS SVBASE
  FROM Trn_NomNov AS N  INNER JOIN NomConceptos AS C ON N.IdConcepto=C.IdConcepto
  WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado AND ClaseCon='DEDUCIDO'

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNov_Cr @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT IdPeriodo,N.IdEmpleado AS IdEmpledo,Apellidos,Nombres,E.NContrato AS NumContrato,Item,N.IdConcepto AS IdConcept,Concepto,Detalle,Cantidad,VrUnitario,VrTotal,N.Tarifa AS TrfaBase,VrOrigen,ClaseCon,Unidad,TimeSys
,N.FecUpdate AS FecUpd,N.IdUsuario AS IdUsuari,Usuario,OrigCargue
--Datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion,C.IdCCosto AS CodCcto,CCosto
,C.IdSubCos AS CodSubcen,SubCosto,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes
,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado AS IdEstdo,Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada
 FROM Trn_NomNov AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN  NomConceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 LEFT JOIN  Emp_Contrato AS C ON E.NContrato=C.NContrato  LEFT JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 LEFT JOIN Dependencias AS DP ON C.IdDep=DP.IdDep  LEFT JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
 LEFT JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto  LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposNom AS TN ON C.IdNom=TN.IdNom LEFT JOIN EstadoDoc AS EN ON C.IdEstado=EN.IdEstado 
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo LEFT JOIN Areas AS AR ON C.IdArea=AR.IdArea
 LEFT JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase LEFT JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 WHERE IdPeriodo=@pmIdPeriodo AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY  N.IdEmpleado,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedad @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT IdEmpleado,Numero,IdConcepto,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,IdNov,Remnrado
,Observacion,CodInstla,CodClsRie,NumAutza,CodNewFon,CodOldFon,Inactivo,VrCotVolAfi,VrCotVolApo,VrNoRetenido
,TipoIncap,FechaDcto,VrDescuento,TipoBaseLiq,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadApo @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmIdNov VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT FecInicio,FecFinal,SUM(CantDias) AS SCanDia,SUM(VrCotVolAfi) AS SVolAfi,SUM(VrCotVolApo) AS SVolApo,SUM(VrNoRetenido) AS SNoReten
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado AND Inactivo=0  AND FecFinal>=@pmFecIni  AND  FecInicio<=@pmFecFin
 AND IdNov LIKE ISNULL(@pmIdNov,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 GROUP BY FecInicio,FecFinal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadCon @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null,@pmRemnrado BIT=Null,@pmIdOpc INT=Null
 AS 
 SELECT IdEmpleado,Numero,N.IdConcepto AS CodConcept,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,IdNov,CodInstla,CodClsRie,NumAutza,CodNewFon
,CodOldFon,VrCotVolAfi,VrCotVolApo,VrNoRetenido,TipoIncap,Remnrado,Observacion,IdUsuario,Inactivo,FechaDcto,VrDescuento,TipoBaseLiq
 FROM Trn_NomNovedad AS N INNER JOIN NomConOpciones AS O ON N.IdConcepto=O.IdConcepto
 WHERE IdEmpleado=@pmIdEmpleado  AND Inactivo=0 AND CantDias>0 AND FecFinal>=@pmFecIni AND FecInicio<=@pmFecFin
 AND Asignado<>0 AND  (IdOpc>=ISNULL(@pmIdOpc,0)  AND IdOpc<=ISNULL(@pmIdOpc,2147483647))
 AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND (Remnrado=ISNULL(@pmRemnrado,0) or Remnrado=ISNULL(@pmRemnrado,1))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadEmp @pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdNov VARCHAR(4)=Null,@pmRemnrado BIT=Null
 AS 
 SELECT IdEmpleado,Numero,N.IdConcepto AS CodConcepto,Concepto,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,IdNov,CodInstla
,CodClsRie,NumAutza,CodNewFon,CodOldFon,VrCotVolAfi,VrCotVolApo,VrNoRetenido,TipoIncap,Remnrado,N.Inactivo AS Inactvo,FechaDcto,VrDescuento,TipoBaseLiq
 FROM Trn_NomNovedad AS N INNER JOIN NomConceptos AS C ON N.IdConcepto=C.IdConcepto
 WHERE IdEmpleado=@pmIdEmpleado  AND N.Inactivo=0 AND CantDias>0 AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND IdNov LIKE ISNULL(@pmIdNov,'%') 
 AND (Remnrado=ISNULL(@pmRemnrado,0) or Remnrado=ISNULL(@pmRemnrado,1))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadFec @pmIdEmpleado VARCHAR(16),@pmFecha SMALLDATETIME,@pmModalidad VARCHAR(10)=Null
,@pmRemnrado BIT=Null
 AS 
 SELECT IdEmpleado,Numero,IdConcepto,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,IdNov,CodInstla,CodClsRie,NumAutza,CodNewFon,CodOldFon,Remnrado
,VrCotVolAfi,VrCotVolApo,VrNoRetenido,TipoIncap,Observacion,FechaDcto,VrDescuento,TipoBaseLiq,Inactivo,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomNovedad
 WHERE Inactivo=0 AND CantDias>0 AND IdConcepto<>'AVP' AND IdEmpleado=@pmIdEmpleado AND @pmFecha BETWEEN FecInicio AND FecFinal 
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')   AND (Remnrado=ISNULL(@pmRemnrado,0) or Remnrado=ISNULL(@pmRemnrado,1))
ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadId @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNumero
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadLic @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
--total de incapacidad y licencias por descontar
 AS 
 SELECT Numero,IdConcepto,Modalidad,IdNov,VrDescuento,NumAutza,FechaDcto,TipoIncap
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado  AND Inactivo=0 AND FecInicio<=@pmFecFin AND CantDias>0
  AND  FechaDcto BETWEEN @pmFecIni AND @pmFecFin AND IdConcepto  IN ('IGE','IRP','LIC')
  AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%') 
 ORDER BY IdConcepto,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadLta @pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmIdEmpleado VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmModalidad VARCHAR(10)=Null,@pmNumero INT=Null,@pmIdNov VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmRemnrado BIT=Null
 AS 
 SELECT N.IdEmpleado AS IdEmpledo,Apellidos,Nombres,Numero,N.IdConcepto AS CodConcepto,Concepto,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,N.IdNov AS CodNov,Novedad,Remnrado
,N.Observacion AS Observ,CodInstla,CodClsRie,CR.Tarifa AS NewTarfARP,NumAutza,CodOldFon,CodNewFon,Fondo,VrCotVolAfi,VrCotVolApo,VrNoRetenido,TipoIncap,TipoBaseLiq
,N.Inactivo AS Inactvo,TimeSys,FecUpdate,N.IdUsuario AS IdUsuari,Usuario,FechaDcto,VrDescuento
 FROM Trn_NomNovedad AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN NomConceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN TiposNov AS TN ON N.IdNov=TN.IdNov INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 LEFT JOIN ClaseRie AS CR ON N.CodClsRie=CR.IdClase LEFT JOIN Fondos AS F ON N.CodNewFon=F.IdFondo
 WHERE FecInicio BETWEEN @pmFecInicio AND @pmFecFinal AND  N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')  AND N.IdNov LIKE ISNULL(@pmIdNov,'%')
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
 AND (N.Inactivo=ISNULL(@pmInactivo,0) or N.Inactivo=ISNULL(@pmInactivo,1))
 AND (Remnrado=ISNULL(@pmRemnrado,0) or Remnrado=ISNULL(@pmRemnrado,1))
 ORDER BY Apellidos,Nombres,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadPen @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmIdConcepto VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null,@pmRemnrado BIT=Null,@pmCantDias INT=0
 AS 
 SELECT IdEmpleado,Numero,IdConcepto,FecInicio,FecFinal,CantDias,DiaInicio,Modalidad,IdNov,CodInstla
,CodClsRie,NumAutza,CodOldFon,CodNewFon,VrCotVolAfi,VrCotVolApo,VrNoRetenido,TipoIncap
,Remnrado,FechaDcto,VrDescuento,TipoBaseLiq,Observacion,IdUsuario,Inactivo
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado  AND Inactivo=0 AND CantDias>@pmCantDias AND FecFinal>=@pmFecIni AND FecInicio<=@pmFecFin
 AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND (Remnrado=ISNULL(@pmRemnrado,0) or Remnrado=ISNULL(@pmRemnrado,1))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomNovedadTra @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT IdNov,CodOldFon,CodNewFon,SUM(CantDias) AS SCanDia
 FROM Trn_NomNovedad
 WHERE IdEmpleado=@pmIdEmpleado AND Inactivo=0 AND CantDias>0 AND  FecInicio BETWEEN @pmFecIni AND @pmFecFin
 AND IdNov IN ('TAE','TAP')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 GROUP BY IdNov,CodOldFon,CodNewFon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrest @pmNPrestamo INT
 AS 
 SELECT NPrestamo,Fecha,IdEmpleado,IdConcepto,VrPrestamo,NumCuotas,VrCuota,FecInicio,FrecDcto,Exceptuar,TotPrestamo
,TotAbonado,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,Cxpagar,DsctNom,DsctPri,DsctVac,DsctEspPer,DsctEspVal
,VrTmAbono,NumPerMes,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomPrest
 WHERE NPrestamo=@pmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestAbo @pmNPrestamo INT,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT NPrestamo,IdEmpleado,VrPrestamo,NumCuotas,VrCuota,TotPrestamo,TotAbonado,VrTmAbono
 FROM Trn_NomPrest
 WHERE IdEmpleado like ISNULL(@pmIdEmpleado,'%')
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestEmp @pmIdEmpleado VARCHAR(16),@pmFecInicio SMALLDATETIME,@pmDsctPri INT,@pmDsctVac INT
 AS 
 SELECT NPrestamo,Fecha,IdEmpleado,IdConcepto,VrPrestamo,NumCuotas,VrCuota,FecInicio,FrecDcto,Exceptuar,TotPrestamo
,TotAbonado,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,Cxpagar,DsctNom,DsctPri,DsctVac,DsctEspPer,DsctEspVal
,VrTmAbono,NumPerMes,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomPrest
 WHERE IdEmpleado=@pmIdEmpleado AND Anulado=0 AND DsctNom>0  AND FecInicio<=@pmFecInicio AND DsctPri>=@pmDsctPri AND DsctVac>=@pmDsctVac
  AND (VrSaldo>=0.8 or (VrSaldo+VrTmAbono)>=0.8)
 ORDER BY FecInicio,NPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestId
 AS 
 SELECT MAX(NPrestamo) AS MXPSTM
 FROM Trn_NomPrest
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestLta @pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmSaldoIni DECIMAL(14,2),@pmSaldoFin DECIMAL(14,2),@pmIdEmpleado VARCHAR(16)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmFrecDcto VARCHAR(10)=Null,@pmAnulado BIT=Null,@pmNPrestamo INT=Null
 AS 
 SELECT NPrestamo,Fecha,P.IdEmpleado AS IdEmpledo,Apellidos,Nombres,P.IdConcepto AS IdConcept,Concepto,VrPrestamo,NumCuotas,VrCuota,FecInicio,FrecDcto,Exceptuar,TotPrestamo
,TotAbonado,TotPrestamo-TotAbonado AS VrSaldoAct,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,P.Observacion AS Observ,Cxpagar,DsctNom,DsctPri,DsctVac,DsctEspPer,DsctEspVal,TimeSys,P.FecUpdate AS FecUpd,P.IdUsuario AS IdUsuari,Usuario
,E.NContrato AS NumContrato,FecIngreso,FecRetiro,VrSalario,VrAuxTrans,CE.IdNom AS CodNom,TipoNomina,pVehiculo,NumPerMes
 FROM Trn_NomPrest AS P INNER JOIN Empleados AS E ON P.IdEmpleado=E.IdEmpleado INNER JOIN NomConceptos AS C ON P.IdConcepto=C.IdConcepto
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Emp_Contrato AS CE ON E.NContrato=CE.NContrato LEFT JOIN TiposNom AS TN ON CE.IdNom=TN.IdNom
 WHERE Fecha BETWEEN @pmFecInicio AND @pmFecFinal  AND VrSaldo BETWEEN @pmSaldoIni AND @pmSaldoFin AND P.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND FrecDcto LIKE ISNULL(@pmFrecDcto,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
 ORDER BY NPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestPen @pmIdEmpleado VARCHAR(16),@pmNPreExste INT
 AS 
 SELECT NPrestamo,Fecha,IdEmpleado,IdConcepto,VrPrestamo,NumCuotas,VrCuota,FecInicio,FrecDcto,Exceptuar,TotPrestamo
,TotAbonado,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,Cxpagar,NumPerMes
 FROM Trn_NomPrest
 WHERE Anulado=0 AND VrSaldo>=0.8  AND  IdEmpleado=@pmIdEmpleado  AND NPrestamo<>@pmNPreExste
 ORDER BY NPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestSal @pmIdEmpleado VARCHAR(16),@pmFecInicio SMALLDATETIME,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT NPrestamo,Fecha,TotPrestamo,TotAbonado,VrSaldo,VrPrestamo,NumCuotas,VrCuota,DsctNom,FecInicio,FrecDcto
,P.IdConcepto AS CodConcepto,Concepto,Exceptuar,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,P.Observacion AS Observ,Cxpagar
,DsctPri,DsctVac,DsctEspPer,DsctEspVal,P.IdEmpleado AS CedEmpleado,Apellidos,Nombres,NumPerMes
 FROM Trn_NomPrest AS P INNER JOIN Empleados AS E ON P.IdEmpleado=E.IdEmpleado
 INNER JOIN NomConceptos AS N ON P.IdConcepto=N.IdConcepto
 WHERE P.IdEmpleado=@pmIdEmpleado AND Anulado=0 AND DsctNom>0  AND FecInicio<=@pmFecInicio AND VrSaldo>=0.8
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY FecInicio,NPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrestSdo @pmIdEmpleado VARCHAR(16),@pmFecInicio SMALLDATETIME
 AS 
 SELECT COUNT(NPrestamo) AS CANPRE,SUM(VrPrestamo) AS SVALPRE,SUM(NumCuotas) AS Scuotas,SUM(TotPrestamo) AS STOTPRE
,SUM(TotAbonado) AS STOTABO
 FROM Trn_NomPrest
 WHERE IdEmpleado=@pmIdEmpleado AND Anulado=0 AND DsctNom>0  AND FecInicio<=@pmFecInicio
  AND (VrSaldo>=0.8 or (VrSaldo+VrTmAbono)>=0.8)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrest_Cra @pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmSaldoIni DECIMAL(14,2),@pmSaldoFin DECIMAL(14,2),@pmIdEmpleado VARCHAR(16)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmFrecDcto VARCHAR(10)=Null,@pmAnulado BIT=Null,@pmNPrestamo INT=Null
 AS 
 SELECT P.NPrestamo AS NumPrest,Fecha,P.IdEmpleado AS IdEmpledo,Apellidos,Nombres,P.IdConcepto AS IdConcept,Concepto,VrPrestamo,NumCuotas,VrCuota,FecInicio,FrecDcto,Exceptuar,TotPrestamo
,TotAbonado,TotPrestamo-TotAbonado AS VrSaldoAct,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,P.Observacion AS Observ,Cxpagar,DsctNom,DsctPri,DsctVac,DsctEspPer,DsctEspVal,TimeSys,P.FecUpdate AS FecUpd,P.IdUsuario AS IdUsuari,Usuario
,E.NContrato AS NumContrato,FecIngreso,FecRetiro,VrSalario,VrAuxTrans,CE.IdNom AS CodNom,TipoNomina,pVehiculo
,Item,FechaAbo,VrAbono,nCuotas,Dscto,IdPeriodo,Detalle,NLiquida,NumPerMes
 FROM Trn_NomPrest AS P INNER JOIN Empleados AS E ON P.IdEmpleado=E.IdEmpleado INNER JOIN NomConceptos AS C ON P.IdConcepto=C.IdConcepto
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario LEFT JOIN Trn_NomAbonos AS PA ON P.NPrestamo=PA.NPrestamo
 LEFT JOIN Emp_Contrato AS CE ON E.NContrato=CE.NContrato LEFT JOIN TiposNom AS TN ON CE.IdNom=TN.IdNom
 WHERE Fecha BETWEEN @pmFecInicio AND @pmFecFinal  AND VrSaldo BETWEEN @pmSaldoIni AND @pmSaldoFin AND P.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND FrecDcto LIKE ISNULL(@pmFrecDcto,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (P.NPrestamo>=ISNULL(@pmNPrestamo,0)  AND P.NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
 ORDER BY P.NPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomPrmSaludLta @pmIdEmpleado VARCHAR(16)=Null,@pmnAnno INT=Null
 AS 
 SELECT N.IdEmpleado AS CdEmpleado,Apellidos,Nombres,nAnno,VrBase,nMeses,VrPromedio
 FROM Trn_NomPrmSalud AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 WHERE N.IdEmpleado like ISNULL(@pmIdEmpleado,'%')
 AND (nAnno>=ISNULL(@pmnAnno,0)  AND nAnno<=ISNULL(@pmnAnno,2147483647))
 ORDER BY Apellidos,Nombres,nAnno

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomProrrogas @pmIdEmpleado VARCHAR(16),@pmNumReg INT
 AS 
 SELECT IdEmpleado,NumReg,NContrato,Fecha,FecAnterior,Indefinido,CdTipCon,CdCargo,CdDep,CdArea,CdCCosto,CdSubCos
,IdTipCon,IdCargo,IdArea,IdDep,IdCCosto,IdSubCos,Observacion,TimeSys,IdUsuario
 FROM Trn_NomProrrogas
 WHERE IdEmpleado=@pmIdEmpleado AND NumReg=@pmNumReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomProrrogasId @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(NumReg)  AS MXITM
 FROM Trn_NomProrrogas
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomProrrogasLta @pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmInactivo BIT=Null
,@pmEmp_Inactivo BIT=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT P.IdEmpleado AS IdEmplado,Apellidos,Nombres,NumReg,P.NContrato AS NumContrato,FecAnterior,Fecha,P.Indefinido AS Indefnido,CdTipCon,TCA.TipoContrato AS TipoConAnterior
,P.IdTipCon AS IdTcont,TC.TipoContrato AS TipoConNuevo,CdCargo,CGA.Cargo AS CargoAnterior,P.IdCargo AS IdCarg,CG.Cargo AS CargoNuevo,CdArea,ARA.Area AS AreaAnterior
,P.IdArea AS Id_Area,AR.Area AS AreaNueva,CdDep,DA.Dependencia AS DepeAnterior,P.IdDep AS IdDepe,D.Dependencia AS DepeNueva
,CdCCosto,COA.CCosto AS CentroAnterior,P.IdCCosto AS IdCenCost,CO.CCosto AS CentroNuevo,CdSubCos,SCA.SubCosto AS SubCenAnterior,P.IdSubCos AS IdSubCen,SC.SubCosto AS SubCenNuevo
,P.Observacion AS Observ,TimeSys,P.IdUsuario AS IdUsuari,Usuario
--inf. de contratos
,FecIngreso,C.Indefinido AS Con_Indefinido,C.IdJornada AS IdJrnada,Jornada,C.IdInstala AS IdInstla,Instlacion,C.IdCia AS CodCia,Compania
,C.IdClase AS CodClaRie,ClaseRiesgo,Tarifa,TipoTrabj,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipoLiq,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,C.Inactivo AS Inactvo
--empleados
--,E.Direccion AS Dirccion,E.IdLocal AS CodCiu,L.Localidad AS Ciudad,Telefono,TelMovil,e_mail,VvdaPropia,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,IdEstCivil,EstCivil,NHijos,TallaCam,TallaPan,TallaZap,UsaGafas,E.IdProf AS CodProf,Profesion
--,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,E.IdLugar AS CodLugar,LugarLic,CatLicencia,FecLicencia,VigLicencia,IdLugarCed,LC.Localidad AS LugarCed,Conyugue,ConyFecNac,ConyTrabaja
--,ConyEmpresa,ConyCargo,ConyFing,ConyTel,E.IdClase AS CodClase,ClaseCuenta,NumCuenta,E.IdBanco AS CodBco,Banco,IdFonPen,FP.Fondo AS FondPen,IdFonEps,FS.Fondo AS FondEps,IdFonArp,FR.Fondo AS FondArp,IdFonCes,FC.Fondo AS FondCes
--,IdCajaCom,CC.Fondo AS CajaComp,FecVincula,FecUltAum,FecUltCes,FecUltPri,FecUltVac,FecUltDot,FecUltExm,FecUltCap,NCAnterior,FecIngAnt,FecRetAnt,EsCondtor,pVehiculo
--,ET.Estado AS Emp_estado,E.Inactivo AS Emp_Inactivo,TipoId,Codigo,ET.NColor AS EmpEstcolor,E.FechaAdd AS EmpFecAdd,E.FechaUpdate AS EmpFecupd,E.IdUsuario AS Emp_IdUsuario,UE.Usuario AS Emp_Usuario

 FROM Trn_NomProrrogas AS P INNER JOIN Empleados AS E ON P.IdEmpleado=E.IdEmpleado
 INNER JOIN Emp_Contrato AS C  ON P.NContrato=C.NContrato
 INNER JOIN TiposCon AS TC ON P.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON P.IdCargo=CG.IdCargo 
 INNER JOIN Dependencias AS D ON P.IdDep=D.IdDep
 INNER JOIN Areas AS AR ON P.IdArea=AR.IdArea
 INNER JOIN CentroCosto AS CO ON P.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON P.IdSubCos=SC.IdSubCos
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 LEFT JOIN TiposCon AS TCA ON P.CdTipCon=TCA.IdTipCon
 LEFT JOIN Cargos AS CGA ON P.CdCargo=CGA.IdCargo 
 LEFT JOIN Dependencias AS DA ON P.CdDep=DA.IdDep
 LEFT JOIN Areas AS ARA ON P.CdArea=ARA.IdArea
 LEFT JOIN CentroCosto AS COA ON P.CdCCosto=COA.IdCCosto
 LEFT JOIN SubCentros AS SCA ON P.CdSubCos=SCA.IdSubCos

-- INNER JOIN Localidades AS L ON E.IdLocal=L.IdLocal INNER JOIN Localidades AS LC ON E.IdLugarCed=LC.IdLocal
-- INNER JOIN EstadoCiv AS ECV ON E.IdEstCivil=ECV.IdEstado INNER JOIN ExpLicencias AS LL ON E.IdLugar=LL.IdLugar
-- INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 --INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 --INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 --INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase  INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
--INNER JOIN EstadoTer AS ET ON E.IdEstado=ET.IdEstado INNER JOIN adm_Usuarios AS UE ON E.IdUsuario=UE.IdUsuario
 WHERE P.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  
 AND (P.NContrato>=ISNULL(@pmNContrato,0)  AND P.NContrato<=ISNULL(@pmNContrato,2147483647)) 
 AND P.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')  AND P.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 ORDER BY Apellidos,Nombres,NumReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRangosFsu @pmIdRango INT
AS
 SELECT     IdRango, VrInicial, VrFinal, Tarifa 
 FROM  NomRangosFsu
 WHERE IdRango=@pmIdRango

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRangosFsuId 
AS
 SELECT  MAX(IdRango) AS MXRANGO,MAX(VrFinal) AS MXFINAL
 FROM  NomRangosFsu
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRangosFsuLta 
AS
 SELECT   IdRango, VrInicial, VrFinal, Tarifa 
 FROM  NomRangosFsu
 ORDER BY IdRango

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRet @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT IdEmpleado,Numero,Fecha,NContrato,IdCausa,FecInicial,FecFinal,DiasTrab,DiasNoRem,VrTotDev,VrTotDed,VrTotCes,VrTotPri,VrTotVac,VrTotInt,VrTotInd,VrParCes,VrParInt,VrNeto,VrBaseCes
,VrBaseVac,IdPeriodo,KeyReg,NPerVac,VrSalario,IdDep,IdCCosto,IdSubCos,IdNom,TipoLiq,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,Anulado,IdEstado,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRetAct @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT IdEmpleado,Numero,Fecha,NContrato,IdCausa,FecInicial,FecFinal,DiasTrab,DiasNoRem
 FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado AND Anulado=0 AND Numero<>@pmNumero
 ORDER BY  Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryNomRetBan @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null
AS
SELECT R.IdEmpleado AS IdEmpldo,Apellidos,Nombres,TipoId,Codigo,IdLocal,E.IdClase AS CdClase,ClaseCuenta,NumCuenta
,E.IdBanco AS CdBanco,Banco,CodEntidad,CdLocCue,E.Direccion AS EmpDireccion,TelMovil,e_mail,Telefono,R.NContrato AS NumContrato
,Cargo,VrTotDev,VrTotDed,VrTotCes,VrTotPri,VrTotVac,VrTotInt,VrTotInd,VrParCes,VrParInt,VrNeto,VrBaseCes,VrBaseVac
 FROM Trn_NomRet AS R INNER JOIN Empleados AS E ON R.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON R.NContrato=C.NContrato
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 WHERE  Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND Anulado=0  AND FPnomina='TRANS' 
 AND R.IdNom LIKE ISNULL(@pmIdNom,'%') AND R.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%') AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') 
 ORDER BY  Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRetCmp @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT  TipCom,Comprobante,IdCiaCom,COUNT(Numero) AS SCANT 
 FROM Trn_NomRet
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND Comprobante>0
 AND  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 GROUP BY TipCom,Comprobante,IdCiaCom

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRetDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT
 AS 
 SELECT IdEmpleado,Numero,Item,IdConcepto,Detalle,Cantidad,VrUnitario,VrDevngado,VrDeducido,Tarifa,VrBase,DiasBase,DiasNoRem,FecUltLiq,FecDesde,FecHasta,FecIniPmd,FecFinPmd
,GrupoLiq,NPrestamo,NCuota,LogPmdio,ClaseLiq,CodFondo,VrBaseLiq
 FROM Trn_NomRetDet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRetDetLiq @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT IdEmpleado,Numero,Item,IdConcepto,Detalle,FecUltLiq,FecDesde,FecHasta,ClaseLiq
 FROM Trn_NomRetDet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero AND ClaseLiq='LIQUDACION'
 ORDER BY IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRetId @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomRet
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRetIng @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmNContrato INT=Null,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null
 AS 
 SELECT R.IdEmpleado AS IdEmpldo,Apellidos,Nombres,R.NContrato AS NumContto,MONTH(R.Fecha) AS Nmes,YEAR(R.Fecha) AS NumAnno,R.IdDep AS CodDep,C.IdInstala AS CodInstala
,R.IdCCosto AS CodCcto,R.IdSubCos AS CodSubcc,R.IdNom AS CodNom,R.pVehiculo AS PlacaVeh,R.VrSalario  AS Salbasico,TipoLiq,IdArea,IdTipCon
,D.IdConcepto AS CodConcepto,Concepto
,SUM(Cantidad) AS SCANT,SUM(VrDevngado) AS STotDev,SUM(VrDeducido) AS STotDed,SUM(VrBase) AS SValBase
,SUM(DiasBase) AS DIAS_BASE,SUM(D.DiasNoRem) AS SCanNoRem,SUM(DiasBase-D.DiasNoRem) AS DiasNeto
,SUM(VrBaseLiq) AS SbaseLiq
 FROM Trn_NomRet AS R INNER JOIN  Trn_NomRetDet AS  D ON R.IdEmpleado=D.IdEmpleado AND R.Numero=D.Numero
 INNER JOIN Empleados AS E ON R.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON R.NContrato=C.NContrato
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE Fecha BETWEEN @pmFecIni  AND @pmFecFin  AND ClaseLiq='LIQUDACION'
 AND R.IdNom LIKE ISNULL(@pmIdNom,'%')  AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 AND R.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')   AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND R.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  
 AND (R.NContrato>=ISNULL(@pmNContrato,0)  AND R.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 GROUP BY  R.IdEmpleado,Apellidos,Nombres,R.NContrato,MONTH(R.Fecha),YEAR(R.Fecha),R.IdDep,C.IdInstala
,R.IdCCosto,R.IdSubCos,R.IdNom,R.pVehiculo,R.VrSalario,TipoLiq,IdArea,IdTipCon,D.IdConcepto,Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRet_Cr @pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdCausa VARCHAR(4)=Null
,@pmNumero INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdPeriodo VARCHAR(8)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmNContrato INT=Null
 AS 
 SELECT R.IdEmpleado AS IdEmpldo,Apellidos,Nombres,R.Numero AS NumLiquida,Fecha,R.NContrato AS Ncontrato,R.IdCausa AS IdCauRet,CausalRet,FecInicial,FecFinal,DiasTrab,R.DiasNoRem AS Dias_Norem,VrTotDev,VrTotDed,VrTotCes
,VrTotPri,VrTotVac,VrTotInt,VrTotInd,VrParCes,VrParInt,VrNeto,VrBaseCes,VrBaseVac,IdPeriodo,KeyReg,NPerVac,R.VrSalario AS Liq_Salbasico,R.IdDep AS CodDep,Dependencia,R.IdCCosto AS CodCcost,CCosto,R.IdSubCos AS CodSubcos,SubCosto
,R.IdNom AS CodNom,TipoNomina,TipoLiq,R.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,R.Observacion AS Observ,Anulado,R.IdEstado AS Liq_IdEstdo,ED.Estado AS Liq_Estado,TimeSys,R.FecUpdate AS FecUpd,R.IdUsuario AS IdUsuari,Usuario
,Item,D.IdConcepto AS CodConcepto,Concepto,ClaseCon,Unidad,Detalle,Cantidad,VrUnitario,VrDevngado,VrDeducido,D.Tarifa  AS Tarfa,VrBase,DiasBase,D.DiasNoRem AS DiasNov,FecUltLiq,FecDesde,FecHasta,FecIniPmd,FecFinPmd
,GrupoLiq,NPrestamo,NCuota,LogPmdio,ClaseLiq,CodFondo,FD.Fondo AS Det_Fondo,VrBaseLiq
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,IdFonCes,FC.Fondo AS FondoCes,EsCondtor,E.pVehiculo AS Emp_Vehiculo,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar
,LugarLic,TallaCam,TallaPan,TallaZap,FecUltCes,FecUltPri,FecUltVac,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,Duracion,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdInstala AS CodInst,Instlacion,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,ModPromdio,SegFinMes,DchDotacion,DchCafeteria,DenyCes,DenyPri,DenyVac,BasePrest
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada
,NumVeh,TipoAdmon,VehPropio
 FROM Trn_NomRet AS R  INNER JOIN  Empleados AS E ON R.IdEmpleado=E.IdEmpleado  INNER JOIN NomCausalesRet AS CAR ON R.IdCausa=CAR.IdCausa
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON R.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON R.IdNom=TN.IdNom 
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo  INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON R.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 LEFT JOIN Trn_NomRetDet AS D ON R.IdEmpleado=D.IdEmpleado AND R.Numero=D.Numero
 LEFT JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto LEFT JOIN Fondos AS FD ON D.CodFondo=FD.IdFondo
 LEFT JOIN TiposCom AS TM ON R.TipCom=TM.IdCom   LEFT JOIN NomVehiculos AS VH ON R.pVehiculo=VH.IdVehiculo
 WHERE R.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')   AND R.IdCausa LIKE ISNULL(@pmIdCausa,'%') AND R.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND R.IdNom LIKE ISNULL(@pmIdNom,'%')  AND IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND (R.Numero>=ISNULL(@pmNumero,0)  AND  R.Numero<=ISNULL(@pmNumero,2147483647))
AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
AND (R.NContrato>=ISNULL(@pmNContrato,0)  AND  R.NContrato<=ISNULL(@pmNContrato,2147483647))
ORDER BY R.IdEmpleado,R.Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomRet_Crr @pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdCausa VARCHAR(4)=Null
,@pmNumero INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdPeriodo VARCHAR(8)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmNContrato INT=Null
 AS 
 SELECT R.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Numero,Fecha,R.NContrato AS Ncontrato,R.IdCausa AS IdCauRet,CausalRet,FecInicial,FecFinal,DiasTrab,DiasNoRem,VrTotDev,VrTotDed,VrTotCes
,VrTotPri,VrTotVac,VrTotInt,VrTotInd,VrParCes,VrParInt,VrNeto,VrBaseCes,VrBaseVac,IdPeriodo,KeyReg,NPerVac,R.VrSalario AS Liq_Salbasico,R.IdDep AS CodDep,Dependencia,R.IdCCosto AS CodCcost,CCosto,R.IdSubCos AS CodSubcos,SubCosto
,R.IdNom AS CodNom,TipoNomina,TipoLiq,R.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,R.Observacion AS Observ,Anulado,R.IdEstado AS Liq_IdEstdo,ED.Estado AS Liq_Estado,TimeSys,R.FecUpdate AS FecUpd,R.IdUsuario AS IdUsuari,Usuario
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,IdFonCes,FC.Fondo AS FondoCes,EsCondtor,E.pVehiculo AS Emp_Vehiculo,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar
,LugarLic,TallaCam,TallaPan,TallaZap,FecUltCes,FecUltPri,FecUltVac,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,Duracion,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdInstala AS CodInst,Instlacion,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,ModPromdio,SegFinMes,DchDotacion,DchCafeteria,DenyCes,DenyPri,DenyVac,BasePrest
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada
,NumVeh,TipoAdmon,VehPropio
 FROM Trn_NomRet AS R  INNER JOIN  Empleados AS E ON R.IdEmpleado=E.IdEmpleado  INNER JOIN NomCausalesRet AS CAR ON R.IdCausa=CAR.IdCausa
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON R.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON R.IdNom=TN.IdNom 
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo  INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON R.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 LEFT JOIN TiposCom AS TM ON R.TipCom=TM.IdCom  LEFT JOIN NomVehiculos AS VH ON  R.pVehiculo=VH.IdVehiculo
 WHERE R.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')   AND R.IdCausa LIKE ISNULL(@pmIdCausa,'%') AND R.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND R.IdNom LIKE ISNULL(@pmIdNom,'%')  AND IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND (Numero>=ISNULL(@pmNumero,0)  AND  Numero<=ISNULL(@pmNumero,2147483647))
AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
AND (R.NContrato>=ISNULL(@pmNContrato,0)  AND  R.NContrato<=ISNULL(@pmNContrato,2147483647))
ORDER BY R.IdEmpleado,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomSueldos @pmNumero INT,@pmIdEmpleado VARCHAR(16)
 AS 
 SELECT Numero,IdEmpleado,Fecha,NContrato,VrSueAnterior,VrSueNuevo,VrAtAnterior,VrAtNuevo,MetodoUp,Reemplzado
,FecUltAumto,CritSelect,Observacion,TimeSys,IdUsuario
 FROM Trn_NomSueldos
 WHERE Numero=@pmNumero AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomSueldosId @pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomSueldos
 WHERE  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomSueldos_Cr  @pmIdEmpleado VARCHAR(16)=Null,@pmNumero INT=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT Numero,Fecha,S.IdEmpleado AS IdEmpldo,Apellidos,Nombres,S.NContrato AS NumCtrato,VrSueAnterior,VrSueNuevo,VrAtAnterior,VrAtNuevo,MetodoUp,Reemplzado
,FecUltAumto,CritSelect,S.Observacion AS Observ,TimeSys,S.IdUsuario AS IdUsuari,Usuario
--datos del empleado
,E.IdProf AS CodProf,Profesion
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion,C.IdNom AS CodNom,TipoNomina
,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,C.IdEstado AS Con_IdEstdo,Estado,C.Inactivo AS Con_inactivo
 FROM Trn_NomSueldos AS S INNER JOIN Empleados AS E ON S.IdEmpleado=E.IdEmpleado INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf 
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario 
 INNER JOIN  Emp_Contrato AS C ON S.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon 
 INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala 
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea 
 WHERE S.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (Numero>=ISNULL(@pmNumero,0)  AND  Numero<=ISNULL(@pmNumero,2147483647))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Numero,Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomTablaRet @pmnAnno INT,@pmnIntrvalo INT=Null
 AS 
 SELECT nAnno,nIntrvalo,VrInicial,VrFinal,Tarifa,VrRetncion,TarExcdnte,VrBaseExc,FechaAdd,FechaUpdate,IdUsuario
 FROM NomTablaRet
 WHERE nAnno=@pmnAnno  AND (nIntrvalo>=ISNULL(@pmnIntrvalo,0)  AND nIntrvalo<=ISNULL(@pmnIntrvalo,2147483647))
 ORDER BY nIntrvalo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomTablaRetInv @pmnAnno INT,@pmValbase MONEY
 AS 
 SELECT nAnno,nIntrvalo,VrInicial,VrFinal,Tarifa,VrRetncion,TarExcdnte,VrBaseExc
 FROM NomTablaRet
 WHERE nAnno=@pmnAnno AND @pmValbase>VrInicial AND @pmValbase<=VrFinal
 ORDER BY nIntrvalo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomTablaRetItm @pmnAnno INT
 AS 
 SELECT MAX(nIntrvalo) AS MXITM,MAX(VrFinal) AS MXFINAL
 FROM NomTablaRet
 WHERE nAnno=@pmnAnno
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVac @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT IdEmpleado,Numero,Fecha,NContrato,FecInicial,FecFinal,Cantidad,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,DiasVac,NNovedad,NPeriodos,CantRem
,VrRemndo,VrTotDed,VrSalario,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,LogPmdio,Anulado,IdEstado,TimeSys,FecUpdate,IdUsuario
 FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacBan @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null
AS
SELECT V.IdEmpleado AS IdEmpldo,Apellidos,Nombres,TipoId,Codigo,IdLocal,E.IdClase AS CdClase,ClaseCuenta,NumCuenta
,E.IdBanco AS CdBanco,Banco,CodEntidad,CdLocCue,e_mail,Telefono,V.NContrato AS NumContrato
,E.Direccion AS EmpDireccion,TelMovil,Cargo
,Cantidad,VrLiquida,VrBase,VrRemndo,VrTotDed,(VrLiquida+VrRemndo)-VrTotDed  AS SNETVAC
 FROM Trn_NomVac AS V INNER JOIN Empleados AS E ON V.IdEmpleado=E.IdEmpleado  
 INNER JOIN  Emp_Contrato AS C ON V.NContrato=C.NContrato
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 WHERE  Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND Anulado=0  AND FPnomina='TRANS' 
 AND C.IdNom LIKE ISNULL(@pmIdNom,'%') AND C.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') 
 ORDER BY  Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacCmp @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT  TipCom,Comprobante,IdCiaCom,COUNT(Numero) AS SCANT 
 FROM  Trn_NomVac
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0 AND Comprobante>0
 AND  IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 GROUP BY TipCom,Comprobante,IdCiaCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT
 AS 
 SELECT IdEmpleado,Numero,Item,IdConcepto,Detalle,Cantidad,VrDevgado,VrDeducido,Tarifa,VrBaseLiq,VrCertfcado,VrPension
,VrSalud,NPrestamo,NCuota,IdPeriodo
 FROM Trn_NomVacDet
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacDetCon @pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmMes VARCHAR(6)
,@pmIdPeriodo VARCHAR(8)=Null,@pmNContrato INT=Null
 AS 
 SELECT SUM(VD.Cantidad) AS SCANT,SUM(VrDevgado) AS SDEVNG,SUM(VrDeducido) AS STOTDED,SUM(VrBaseLiq) AS STOTBAS
,SUM(VrCertfcado) AS STOTCER,SUM(VrPension) AS STOTPEN,SUM(VrSalud) AS STOTSALUD
 FROM Trn_NomVacDet AS VD INNER JOIN Trn_NomVac AS V ON VD.IdEmpleado=V.IdEmpleado AND VD.Numero=V.Numero
 WHERE VD.IdEmpleado=@pmIdEmpleado AND  IdConcepto=@pmIdConcepto AND Anulado=0 AND SUBSTRING(IdPeriodo,1,6)=@pmMes
 AND  IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') 
 AND (NContrato>=ISNULL(@pmNContrato,0)  AND NContrato<=ISNULL(@pmNContrato,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacDetRes @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmInactivo BIT=Null
,@pmEmp_Inactivo BIT=Null,@pmFecVacIni SMALLDATETIME=Null,@pmFecVacFin SMALLDATETIME=Null
 AS 
 SELECT Q.IdEmpleado AS IdEmpldo,Q.Numero AS NumLiquida,Q.NContrato AS NumContrato,Fecha,FecInicial,FecFinal,Q.Cantidad AS CantDias
,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,DiasVac,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,Q.VrSalario AS Liq_SalBasico
,Q.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom,Q.Observacion AS Observ
--detalle de liq vac
,Item,IdConcepto,Detalle,D.Cantidad AS Cant,VrDevgado,VrDeducido,Tarifa,VrBaseLiq,VrCertfcado,VrPension,NPrestamo,NCuota,IdPeriodo,VrSalud
--datos del contrato
,IdArea,IdDep,IdInstala,IdNom,TipoLiquida,IdCCosto,IdSubCos,C.VrSalario AS SalBasico,VrAuxTrans
 FROM Trn_NomVac AS Q  INNER JOIN  Empleados AS E ON Q.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON Q.NContrato=C.NContrato
 LEFT JOIN Trn_NomVacDet AS D ON Q.IdEmpleado=D.IdEmpleado AND Q.Numero=D.Numero
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND Q.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND IdDep LIKE ISNULL(@pmIdDep,'%') AND IdArea LIKE ISNULL(@pmIdArea,'%')
 AND IdNom LIKE ISNULL(@pmIdNom,'%')
 AND IdInstala LIKE ISNULL(@pmIdInstala,'%') AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND (Q.NContrato >=ISNULL(@pmNContrato ,0)  AND Q.NContrato <=ISNULL(@pmNContrato,2147483647))
 AND (FecInicial>=ISNULL(@pmFecVacIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicial<=ISNULL(@pmFecVacFin,CAST('20781230' AS SMALLDATETIME)))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 ORDER BY IdEmpldo,Q.Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacDet_Cr @pmIdEmpleado VARCHAR(16)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmNumero INT=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmNContrato INT=Null,@pmFecVacIni SMALLDATETIME=Null
,@pmFecVacFin SMALLDATETIME=Null,@pmIdConcepto VARCHAR(4)=Null
 AS 
 SELECT Q.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Q.Numero AS NumLiquida,Fecha,Q.NContrato AS NumContrato,FecInicial,FecFinal,Q.Cantidad AS CantDias,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem
,DiasVac,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,Q.VrSalario AS Liq_SalBasico,Q.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,Q.Observacion AS Observ,LogPmdio,Anulado
,Q.IdEstado AS IdEstdo,ED.Estado AS Liq_estado,TimeSys,Q.FecUpdate AS FecUpd,Q.IdUsuario AS IdUsuari,Usuario
--detalle de liq vac
,Item,D.IdConcepto AS CodConcepto,Concepto,Detalle,ClaseCon,D.Cantidad AS Cant,VrDevgado,VrDeducido,D.Tarifa AS Tarfa,VrBaseLiq,VrCertfcado,VrPension,NPrestamo,NCuota,IdPeriodo
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen,IdFonEps,FS.Fondo AS FondoEps
,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,E.pVehiculo AS Emp_Vehiculo,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,FecUltCes,FecUltPri,FecUltVac,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion,C.IdJornada AS CodJnada,Jornada
,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipLiq,C.IdCCosto AS CodCcto,CCosto,C.IdSubCos AS CodSubcen,SubCosto,Asistencia,NoDevenga,TipoTrabj
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModPromdio,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,DenyCes,DenyPri,DenyVac,BasePrest
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,VrSalud
 FROM Trn_NomVac AS Q INNER JOIN Trn_NomVacDet AS D ON Q.IdEmpleado=D.IdEmpleado AND Q.Numero=D.Numero
 INNER JOIN adm_Usuarios AS U ON Q.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON Q.IdEstado=ED.IdEstado
 INNER JOIN  Empleados AS E ON Q.IdEmpleado=E.IdEmpleado  INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON Q.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 LEFT JOIN TiposCom AS TM ON Q.TipCom=TM.IdCom 
 WHERE  Q.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND C.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND Q.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (Q.Numero>=ISNULL(@pmNumero,0)  AND Q.Numero<=ISNULL(@pmNumero,2147483647))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (Q.NContrato >=ISNULL(@pmNContrato ,0)  AND Q.NContrato <=ISNULL(@pmNContrato,2147483647))
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (FecInicial>=ISNULL(@pmFecVacIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicial<=ISNULL(@pmFecVacFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,Q.Numero,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacId @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacIng @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmNContrato INT=Null,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null
AS 
 SELECT Q.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Q.Numero AS NumLiquida,Fecha,Q.NContrato AS NumContrato,FecInicial,FecFinal,Q.Cantidad AS CantDias,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem
,DiasVac,CantRem,VrRemndo,VrTotDed,Q.VrSalario AS Liq_SalBasico,Q.pVehiculo AS PlacaVeh
,Item,D.IdConcepto AS CodConcepto,Concepto,Detalle,ClaseCon,CN.Tarifa AS TarifDft,D.Cantidad AS Cant,VrDevgado,VrDeducido,D.Tarifa AS Tarfa,VrBaseLiq,VrCertfcado,VrPension,VrSalud,NPrestamo
,IdTipCon,IdArea,IdDep,IdInstala,IdNom,TipoLiquida,IdCCosto,IdSubCos,C.VrSalario AS SalBasico,VrAuxTrans,E.Inactivo AS Emp_Inactvo,C.Inactivo AS Con_inactivo,IdFonPen,IdFonEps
 FROM Trn_NomVac AS Q INNER JOIN Empleados AS E ON Q.IdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON Q.NContrato=C.NContrato
 LEFT JOIN  Trn_NomVacDet AS D ON Q.IdEmpleado=D.IdEmpleado AND Q.Numero=D.Numero
 LEFT JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  
 WHERE  FecInicial  BETWEEN  @pmFecIni AND @pmFecFin
 AND IdNom LIKE ISNULL(@pmIdNom,'%')  AND IdArea LIKE ISNULL(@pmIdArea,'%')
 AND IdDep LIKE ISNULL(@pmIdDep,'%') AND IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')   AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND Q.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND (Q.NContrato>=ISNULL(@pmNContrato,0)  AND Q.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 ORDER BY Q.IdEmpleado,Q.Numero,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVacNom @pmIdEmpleado VARCHAR(16),@pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME
,@pmNContrato INT=Null
 AS 
 SELECT IdEmpleado,Numero,Fecha,NContrato,FecInicial,FecFinal,Cantidad,VrLiquida,VrBase
 FROM Trn_NomVac
 WHERE IdEmpleado=@pmIdEmpleado AND Anulado=0 AND FecInicial<=@pmFecFin AND FecFinal>=@pmFecIni
 AND (NContrato>=ISNULL(@pmNContrato,0)  AND NContrato<=ISNULL(@pmNContrato,2147483647))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVac_Cr @pmIdEmpleado VARCHAR(16),@pmNumero INT
 AS 
 SELECT V.IdEmpleado AS IdEmpldo,Apellidos,Nombres,V.Numero AS NumReg,Fecha,V.NContrato AS NumContrato,FecInicial,FecFinal,V.Cantidad AS DiasCal,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem,DiasVac
,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,V.VrSalario AS Vac_VrBasico,V.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,V.Observacion AS Observ,LogPmdio,Anulado,V.IdEstado AS Vac_IdEstdo,ED.Estado AS Vac_Estado
,TimeSys,V.FecUpdate AS FecUpd,V.IdUsuario AS IdUsuari,Usuario
--datos del detalle de liquidacion
,Item,D.IdConcepto AS CodConcepto,Concepto,ClaseCon,Detalle,D.Cantidad AS Cant,Unidad,VrDevgado,VrDeducido,D.Tarifa AS Tarfa,VrBaseLiq,VrCertfcado,VrPension,NPrestamo,NCuota,IdPeriodo
--datos del empleado 
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion,C.IdCCosto AS CodCcto,CCosto
,C.IdSubCos AS CodSubcen,SubCosto,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes
,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada
,NumVeh,TipoAdmon,VehPropio,VrSalud
 FROM Trn_NomVac  AS V INNER JOIN  Empleados AS E ON V.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON V.NContrato=C.NContrato
 INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario  INNER JOIN EstadoDoc AS ED ON V.IdEstado=ED.IdEstado
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom 
 INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 LEFT JOIN  Trn_NomVacDet  AS D ON V.IdEmpleado=D.IdEmpleado AND V.Numero=D.Numero  LEFT JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 LEFT JOIN TiposCom AS TM ON V.TipCom=TM.IdCom
 LEFT JOIN NomVehiculos AS VH ON V.pVehiculo=VH.IdVehiculo
 WHERE  V.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (V.Numero>=ISNULL(@pmNumero,0)  AND V.Numero<=ISNULL(@pmNumero,2147483647))
ORDER BY   V.Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVac_Crr @pmIdEmpleado VARCHAR(16)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmNumero INT=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null,@pmNContrato INT=Null,@pmFecVacIni SMALLDATETIME=Null,@pmFecVacFin SMALLDATETIME=Null
 AS 
 SELECT Q.IdEmpleado AS IdEmpldo,Apellidos,Nombres,Numero,Fecha,Q.NContrato AS NumContrato,FecInicial,FecFinal,Cantidad,VrLiquida,VrBase,FecIniPmd,FecFinPmd,DiasBase,DiasNoRem
,DiasVac,NNovedad,NPeriodos,CantRem,VrRemndo,VrTotDed,Q.VrSalario AS Liq_SalBasico,Q.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,Q.Observacion AS Observ,LogPmdio,Anulado
,Q.IdEstado AS IdEstdo,ED.Estado AS Liq_estado,TimeSys,Q.FecUpdate AS FecUpd,Q.IdUsuario AS IdUsuari,Usuario
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen,IdFonEps,FS.Fondo AS FondoEps
,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,E.pVehiculo AS Emp_Vehiculo,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,FecUltCes,FecUltPri,FecUltVac,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion,C.IdJornada AS CodJnada,Jornada
,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipLiq,C.IdCCosto AS CodCcto,CCosto,C.IdSubCos AS CodSubcen,SubCosto,Asistencia,NoDevenga,TipoTrabj
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS SalBasico,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ModPromdio,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,DenyCes,DenyPri,DenyVac,BasePrest
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo
 FROM Trn_NomVac AS Q INNER JOIN adm_Usuarios AS U ON Q.IdUsuario=U.IdUsuario  
 INNER JOIN EstadoDoc AS ED ON Q.IdEstado=ED.IdEstado  INNER JOIN  Empleados AS E ON Q.IdEmpleado=E.IdEmpleado  
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON Q.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom
 LEFT JOIN TiposCom AS TM ON Q.TipCom=TM.IdCom 
 WHERE  Q.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND C.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND C.IdDep LIKE ISNULL(@pmIdDep,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%') AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND C.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND Q.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (Q.NContrato >=ISNULL(@pmNContrato ,0)  AND Q.NContrato <=ISNULL(@pmNContrato,2147483647))
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (FecInicial>=ISNULL(@pmFecVacIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicial<=ISNULL(@pmFecVacFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Apellidos,Nombres,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomVehiculos @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT IdVehiculo,NumVeh,ClaseVeh,TipoAdmon,GrupoProp,VehPropio,CedCond,Conductor
 FROM NomVehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomina @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30)
 AS 
 SELECT IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido,NContrato,IdDep,IdInstala,IdCCosto,IdSubCos,IdNom
,VrSalario,VrATransMes,pVehiculo,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,EsRetiro,NLiqRet,TipoLiq,TimeSys,FecUpdate,IdUsuario
 FROM Trn_Nomina
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%')
 ORDER BY KeyRegistro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaApo @pmMes VARCHAR(6),@pmIdPeriodo VARCHAR(8)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmCodFondo VARCHAR(8)=Null,@pmIdCia CHAR(2)=Null
AS 
 SELECT N.KeyRegistro AS KeyReg,N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContto,FecInicial,FecFinal,N.IdDep AS CodDep,N.IdInstala AS CodInstala,N.IdCCosto AS CodCcto
,N.IdSubCos AS CodSubcc,N.IdNom AS CodNom,N.pVehiculo AS PlacaVeh,N.VrSalario AS Salbasico,EsRetiro,NLiqRet,IdFonPen,IdFonEps,IdFonArp,IdFonCes,IdCajaCom,FecIngreso,FecRetiro,IdArea,C.IdClase AS CodClaRie
,CR.Tarifa AS TarRie,TipoTrabj,VrAuxTrans,VrUpc,SalIntegral,SalMinimo,C.Inactivo AS Con_Inctivo,D.IdConcepto AS CodConcepto,Concepto,ClaseCon,CN.Tarifa AS TarifDft,ClaseLiq,CantDevg,CantDed,VrTotDevg,VrTotDed,VrOrigen,D.Tarifa AS Tarfa
,DiasCalc,DiasNov,DiasCalc-DiasNov AS DiasNeto,CodFondo,IdCot
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase  INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE  SUBSTRING(N.IdPeriodo,1,6)=@pmMes AND NLiqRet>=0 AND D.IdConcepto IN ('PEN','RIE','SAL','FSP','FSU','SEN','ICB','COM','DCP','DCS','AVP','AVA')
 AND N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%') AND N.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND IdArea LIKE ISNULL(@pmIdArea,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%')   AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND CodFondo LIKE ISNULL(@pmCodFondo,'%')
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 ORDER BY  N.IdEmpleado,N.NContrato,D.IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaAux @pmIdEmpleado VARCHAR(16),@pmNContrato INT
,@pmnMes VARCHAR(2),@pmnAnno VARCHAR(4)
 AS 
 SELECT IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal,NContrato,VrSalario,VrATransMes
 FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato
 AND SUBSTRING(IdPeriodo,5,2)=@pmnMes AND  SUBSTRING(IdPeriodo,1,4)=@pmnAnno 
 ORDER BY IdPeriodo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaBan @pmMes VARCHAR(6),@pmIdPeriodo VARCHAR(8),@pmIdNom VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null,@pmInactivo BIT=Null
 AS 
 SELECT N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,TipoId,Codigo,IdLocal,E.IdClase AS CdClase,ClaseCuenta,NumCuenta
,E.IdBanco AS CdBanco,Banco,CodEntidad,CdLocCue,E.Direccion AS EmpDireccion,TelMovil,e_mail,Telefono,N.NContrato AS NumContrato,TipoLiq,Cargo
,SUM(VrTotDevg) AS STOTDEV,SUM(VrTotDed) AS STOTDED,SUM(VrOrigen) AS SVALBAS
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco INNER JOIN ClaseCta AS CTA ON E.IdClase=CTA.IdClase
 LEFT JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 WHERE SUBSTRING(N.IdPeriodo,1,6)=@pmMes  AND FPnomina='TRANS' AND ClaseLiq IN ('NOMINA','NOVEDAD')
 AND N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY  N.IdEmpleado,Apellidos,Nombres,TipoId,Codigo,IdLocal,E.IdClase,ClaseCuenta,NumCuenta
,E.IdBanco,Banco,CodEntidad,CdLocCue,E.Direccion,TelMovil,e_mail,Telefono,N.NContrato,TipoLiq,Cargo
 ORDER BY Apellidos,Nombres,N.IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaCa @pmIdPeriodo VARCHAR(8),@pmIdNom VARCHAR(4),@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT KeyRegistro,N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NCtto,TipCom,Comprobante,IdCiaCom,N.IdEstado AS IdEstdo
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 WHERE IdPeriodo=@pmIdPeriodo  AND IdNom=@pmIdNom AND EsRetiro=0 AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY  KeyRegistro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaCed @pmIdEmpleado VARCHAR(16)
 AS 
 SELECT IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal
 FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaCip @pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmFecha SMALLDATETIME
 AS 
 SELECT IdPeriodo,KeyRegistro,IdEmpleado,FecInicial,FecFinal,NContrato,EsRetiro,NLiqRet
 FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato AND NLiqRet<>-1 AND FecFinal<=@pmFecha
 ORDER BY IdPeriodo,FecInicial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaCon @pmIdEmpleado VARCHAR(16),@pmNContrato INT
 AS 
 SELECT IdPeriodo,IdEmpleado,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido,NContrato
,TipCom,Comprobante,IdCiaCom,IdEstado,EsRetiro,NLiqRet
 FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato
 ORDER BY IdPeriodo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaDia @pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmnMes VARCHAR(6)
 AS 
 SELECT  SUM(Cantidad) AS SDIAS,SUM(CantNoRem) AS SNOREM,SUM(Cantidad-CantNoRem) AS SDIASNET
 FROM Trn_Nomina
 WHERE IdEmpleado=@pmIdEmpleado AND NContrato=@pmNContrato 
 AND SUBSTRING(IdPeriodo,1,6)=@pmnMes

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaDso @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdNom VARCHAR(4)=Null
 AS 
 SELECT KeyRegistro,N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,IdPeriodo,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido,N.NContrato AS NumContrato,N.IdNom AS CodNom,TipoNomina
,N.IdDep AS CodDep,Dependencia,N.IdCCosto AS CodCcto,CCosto,N.IdSubCos AS CodSubcos,SubCosto,IdInstala
,VrSalario,N.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom,N.Observacion AS  Observ,N.IdEstado AS IdEstdo,Estado,EsRetiro,NLiqRet,TipoLiq,VrATransMes,N.IdUsuario AS IdUsuari,Usuario,TimeSys,N.FecUpdate AS FecUpdte
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN TiposNom AS TN ON N.IdNom=TN.IdNom INNER JOIN Dependencias AS D ON N.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto INNER JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 WHERE IdPeriodo=@pmIdPeriodo AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') AND N.IdNom LIKE ISNULL(@pmIdNom,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 ORDER BY  N.IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaGcon  @pmIdPeriodo VARCHAR(8),@pmMes VARCHAR(6),@pmKeyRegistro VARCHAR(30)=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
 AS 
 SELECT N.IdPeriodo AS NumPdo,N.KeyRegistro AS KeyReg,N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,D.IdConcepto AS CodConcepto,Concepto,ClaseCon,ClaseLiq
 ,SUM(CantDevg) AS SCantDev,SUM(VrTotDevg) AS STotDev,SUM(CantDed) AS SCantDed,SUM(VrTotDed) AS STotDed,SUM(VrOrigen) AS STotBase
,SUM(DiasCalc-DiasNov) AS TotalDias,SUM(VrBaseLiq) AS STotLiq
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE ClaseLiq<>'PROVISION' AND ClaseLiq<>'APORTE'  AND SUBSTRING(N.IdPeriodo,1,6)=@pmMes
 AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')  AND N.KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%')
 AND N.IdNom like ISNULL(@pmIdNom,'%')  AND  N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND IdArea LIKE ISNULL(@pmIdArea,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-1)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 GROUP BY N.IdPeriodo,N.KeyRegistro,N.IdEmpleado,Apellidos,Nombres,D.IdConcepto,Concepto,ClaseCon,ClaseLiq
 ORDER BY N.IdPeriodo,N.KeyRegistro,N.IdEmpleado,D.IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaIng @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmNContrato INT=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null,@pmIdConcepto VARCHAR(4)=Null
AS 
 SELECT N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContto,SUBSTRING(N.IdPeriodo,1,6) AS Nmes,N.IdDep AS CodDep,N.IdInstala AS CodInstala
,N.IdCCosto AS CodCcto,N.IdSubCos AS CodSubcc,N.IdNom AS CodNom,N.pVehiculo AS PlacaVeh,N.VrSalario AS Salbasico,NLiqRet,TipoLiq,IdArea,IdTipCon
,D.IdConcepto AS CodConcepto,Concepto,ClaseLiq,SUM(Cantidad) AS Scant,SUM(CantNoRem) AS SCanNoRem,SUM(VrBaseLiq) AS SbaseLiq
,SUM(CantDevg) AS SCanDev,SUM(CantDed) AS SCanDed,SUM(VrTotDevg) AS STotDev,SUM(VrTotDed) AS STotDed,SUM(VrOrigen) AS SValBase,MAX(D.Tarifa) AS Tarfa
,SUM(DiasCalc) AS SDiasCalc, SUM(DiasNov) AS SDiasNov,SUM(DiasCalc-DiasNov) AS DiasNeto
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin  AND ClaseLiq IN ('NOMINA','NOVEDAD','LIQUDACION')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%')  AND IdArea LIKE ISNULL(@pmIdArea,'%')
 AND N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')   AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND D.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-2)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 GROUP BY N.IdEmpleado,Apellidos,Nombres,N.NContrato,SUBSTRING(N.IdPeriodo,1,6),N.IdDep,N.IdInstala,N.IdCCosto,N.IdSubCos
,N.IdNom,N.pVehiculo,N.VrSalario,NLiqRet,TipoLiq,IdArea,IdTipCon,D.IdConcepto,Concepto,ClaseLiq
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaInt @pmIdPeriodo VARCHAR(8),@pmIdNom VARCHAR(4),@pmEsRetiro BIT=Null
 AS 
 SELECT N.IdPeriodo AS IdPdo,N.KeyRegistro AS IdKeyReg,N.IdEmpleado AS IdEmpldo,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido,N.NContrato AS NCntto,IdDep,IdInstala,IdCCosto
,IdSubCos,IdNom,VrSalario,N.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom,EsRetiro,NLiqRet,TipoLiq,Apellidos,Nombres,EsCondtor,E.pVehiculo AS VehiculoDef
--detalle de la nomina
,Item,D.IdConcepto AS IdConcep,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,ClaseLiq,VrOrigen,D.Tarifa AS Tarfa,DiasCalc,DiasNov,CodFondo,NPrestamo,NCuota,OrigCargue,VrBaseLiq
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D  ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN  NomConceptos AS C ON D.IdConcepto=C.IdConcepto
 WHERE N.IdPeriodo=@pmIdPeriodo AND IdNom like ISNULL(@pmIdNom,'%')
 AND  (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 ORDER BY N.IdEmpleado,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaNov @pmMes VARCHAR(6),@pmIdPeriodo VARCHAR(8)=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmEsRetiro BIT=Null,@pmIdConcepto VARCHAR(4)=Null
AS 
 SELECT N.IdEmpleado AS IdEmpldo,IdConcepto, SUM(CantDevg) AS SCantDev,SUM(CantDed) AS SCantDed
,SUM(VrTotDevg) AS SValDev,SUM(VrTotDed) AS SValDed,SUM(VrOrigen) AS SValBase,SUM(VrBaseLiq) AS SValLiquida
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 WHERE  SUBSTRING(N.IdPeriodo,1,6)=@pmMes AND NLiqRet>=0 AND IdConcepto IN ('IGE','IRP','LIC') AND ClaseLiq='NOVEDAD'
 AND N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 GROUP BY N.IdEmpleado,IdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaPag  @pmIdPeriodo VARCHAR(8),@pmMes VARCHAR(6),@pmKeyRegistro VARCHAR(30)=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
 AS 
 SELECT  N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContrato,Item,D.IdConcepto AS CodConcepto,Detalle,ClaseCon,CantDevg,D.Unidad AS Undad,VrUnitario,VrTotDevg,CantDed,VrTotDed,NPrestamo,NCuota
,VrOrigen,D.Tarifa AS TarifaBase,DiasCalc,DiasNov,DiasCalc-DiasNov AS TotalDias,CodFondo,FD.Fondo AS Det_Fondo,ClaseLiq,OrigCargue,Concepto,VrBaseLiq
,N.IdPeriodo AS IdPerdo,FecInicial,FecFinal,N.KeyRegistro AS KeyRegstro
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  LEFT JOIN Fondos AS FD ON D.CodFondo=FD.IdFondo
 WHERE ClaseLiq<>'PROVISION' AND ClaseLiq<>'APORTE' AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')  AND N.KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%')
 AND SUBSTRING(N.IdPeriodo,1,6)=@pmMes
 AND N.IdNom like ISNULL(@pmIdNom,'%')  AND  N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND IdArea LIKE ISNULL(@pmIdArea,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-1)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 ORDER BY Apellidos,Nombres,N.IdPeriodo,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaRes @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdPeriodo VARCHAR(8)=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto  VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null
 AS 
 SELECT N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumCntrato,KeyRegistro,IdPeriodo,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido
,N.IdNom AS CodNmna,TipoNomina,N.IdDep AS CodDep,Dependencia,N.IdCCosto AS CodCcto,CCosto,N.IdSubCos AS CodSubcos,SubCosto,N.IdInstala AS CodIstala,Instlacion
,N.VrSalario AS SalBasico,N.pVehiculo AS PlacaVeh,TipCom,TipoCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS IdEstdo,ED.Estado AS Nom_Estado,EsRetiro,NLiqRet,TipoLiq,TimeSys
,N.FecUpdate AS Fec_Update,N.IdUsuario AS IdUsuari,Usuario
--Datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,FC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,C.VrSalario AS VlrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,SegFinMes,DchDotacion,DchCafeteria
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,TipoTrabj,C.IdGrupo AS CodGrup
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado
 INNER JOIN TiposNom AS TN ON N.IdNom=TN.IdNom INNER JOIN Dependencias AS D ON N.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto INNER JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN Instalaciones AS I ON N.IdInstala=I.IdInstala  INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco 
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo 
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS FC ON E.IdCajaCom=FC.IdFondo 
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado  LEFT JOIN TiposCom AS TCM ON N.TipCom=TCM.IdCom 
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND  IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 AND C.IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-2)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
 ORDER BY  N.IdEmpleado,IdPeriodo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaRtot @pmFecIni SMALLDATETIME,@pmFecFin SMALLDATETIME,@pmIdPeriodo VARCHAR(8)=Null
,@pmIdEmpleado VARCHAR(16)=Null,@pmNContrato INT=Null,@pmIdArea VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmIdNom VARCHAR(4)=Null
,@pmIdInstala VARCHAR(4)=Null,@pmIdCCosto  VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdTipCon VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
,@pmInactivo BIT=Null,@pmEmp_Inactivo BIT=Null
 AS 
 SELECT SUM(Cantidad) AS SCant,SUM(VrDevengado) AS TotalDev,SUM(VrDeducido) AS TotalDed,SUM(N.VrSalario) AS ToalSalBasico,SUM(VrAuxTrans) AS TotalAuxtrans
 FROM Trn_Nomina AS N INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 WHERE FecInicial>=@pmFecIni  AND FecFinal<=@pmFecFin AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')  AND  IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdArea LIKE ISNULL(@pmIdArea,'%')
 AND IdTipCon LIKE ISNULL(@pmIdTipCon,'%')
 AND (N.NContrato>=ISNULL(@pmNContrato,0)  AND N.NContrato<=ISNULL(@pmNContrato,2147483647))
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-2)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (E.Inactivo=ISNULL(@pmEmp_Inactivo,0) or E.Inactivo=ISNULL(@pmEmp_Inactivo,1))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaTEmp  @pmIdPeriodo VARCHAR(8),@pmMes VARCHAR(6),@pmKeyRegistro VARCHAR(30)=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdArea VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmIdInstala VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
 AS 
 SELECT  Apellidos,Nombres,N.IdEmpleado AS IdEmpldo,N.NContrato AS NumContrato,sum(VrTotDevg) AS VrTotalDevg,sum(VrTotDed) AS VrTotalDed,sum(VrBaseLiq) AS VrTotOtros
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado  INNER JOIN Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto  LEFT JOIN Fondos AS FD ON D.CodFondo=FD.IdFondo
 WHERE ClaseLiq<>'PROVISION' AND ClaseLiq<>'APORTE' AND  N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')  AND N.KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%') 
 AND SUBSTRING(N.IdPeriodo,1,6)=@pmMes
 AND N.IdNom like ISNULL(@pmIdNom,'%') AND  N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND IdArea LIKE ISNULL(@pmIdArea,'%') AND N.IdInstala LIKE ISNULL(@pmIdInstala,'%')
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-1)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 GROUP BY Apellidos,Nombres,N.IdEmpleado,N.NContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaVeha @pmIdPeriodo VARCHAR(8),@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
 AS 
 SELECT N.pVehiculo AS PlacaVeh,N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContrato,N.IdCCosto AS CdCcosto,E.pVehiculo AS Emp_Veh,TipoLiq
,SUM(VrTotDevg) AS STOTDEV,SUM(VrTotDed) AS STOTDED,SUM(VrOrigen) AS SVALBAS
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 WHERE N.IdPeriodo=@pmIdPeriodo AND FPnomina='OTRO' AND EsCondtor<>0 AND ClaseLiq IN ('NOMINA','NOVEDAD')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%')
 GROUP BY   N.pVehiculo,N.IdEmpleado,Apellidos,Nombres,N.NContrato,N.IdCCosto,E.pVehiculo,TipoLiq
 ORDER BY  N.pVehiculo,N.IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNominaVehc @pmIdPeriodo VARCHAR(8),@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
 AS 
 SELECT N.pVehiculo AS PlacaVeh,N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContrato,N.IdCCosto AS CdCcosto,E.pVehiculo AS Emp_Veh,TipoLiq
,ClaseLiq,SUM(VrTotDevg) AS STOTDEV,SUM(VrTotDed) AS STOTDED,SUM(VrOrigen) AS SVALBAS
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 WHERE N.IdPeriodo=@pmIdPeriodo AND TipoTrabj=3 AND EsCondtor<>0 AND ClaseLiq IN ('NOMINA','NOVEDAD','PROVISION','APORTE')
 AND N.IdNom LIKE ISNULL(@pmIdNom,'%') AND N.IdDep LIKE ISNULL(@pmIdDep,'%')
 GROUP BY   N.pVehiculo,N.IdEmpleado,Apellidos,Nombres,N.NContrato,N.IdCCosto,E.pVehiculo,TipoLiq,ClaseLiq
 ORDER BY  N.pVehiculo,N.IdEmpleado,ClaseLiq
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomina_Cpn  @pmIdPeriodo VARCHAR(8),@pmMes VARCHAR(6),@pmKeyRegistro VARCHAR(30)=Null,@pmIdEmpleado VARCHAR(16)=Null
,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
 AS 
 SELECT  N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContrato,N.IdPeriodo AS IdPerdo,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido
,N.IdDep AS CodDep,Dependencia,N.IdInstala AS CodInst,Instlacion,N.IdCCosto AS CodCcto,CCosto,N.IdSubCos AS CodSubcen,SubCosto,N.IdNom AS CodNom,TipoNomina,Asistencia,NoDevenga
,TipoLiq,N.VrSalario AS VrSalBasico,N.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS Nom_IdEstdo,EN.Estado AS Nom_Estado,EsRetiro,NLiqRet
,N.KeyRegistro AS KeyRegstro,TimeSys,N.FecUpdate AS FecUpd,N.IdUsuario AS IdUsuari,Usuario
--datos detalle de nomina
,Item,D.IdConcepto AS CodConcepto,Concepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,D.Unidad AS Undad,ClaseLiq,VrOrigen,D.Tarifa AS TarifaBase,DiasCalc,DiasNov,CodFondo,FD.Fondo AS Det_Fondo
,NPrestamo,NCuota,OrigCargue,VrBaseLiq,ClaseCon
--Datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,SegFinMes,DchDotacion,DchCafeteria
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,TipoTrabj
,NumVeh,TipoAdmon,VehPropio,VrATransMes
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN Dependencias AS DP ON N.IdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON N.IdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON N.IdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON N.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EN ON N.IdEstado=EN.IdEstado 
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado  LEFT JOIN Fondos AS FD ON D.CodFondo=FD.IdFondo
 LEFT JOIN NomVehiculos AS VH ON N.pVehiculo=VH.IdVehiculo
 WHERE  SUBSTRING(N.IdPeriodo,1,6)=@pmMes AND ClaseLiq IN ('NOMINA','NOVEDAD','LIQUDACION')
 AND N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')  AND N.KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%') 
 AND N.IdNom like ISNULL(@pmIdNom,'%') AND  N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-1)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 ORDER BY N.KeyRegistro,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNomina_Cr  @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30)=Null,@pmIdEmpleado VARCHAR(16)=Null,@pmIdNom VARCHAR(4)=Null,@pmIdDep VARCHAR(4)=Null
,@pmEsRetiro BIT=Null,@pmNLiqRet INT=Null
 AS 
 SELECT  N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.NContrato AS NumContrato,N.IdPeriodo AS IdPerdo,FecInicial,FecFinal,Cantidad,CantNoRem,VrDevengado,VrDeducido
,N.IdDep AS CodDep,Dependencia,N.IdInstala AS CodInst,Instlacion,N.IdCCosto AS CodCcto,CCosto,N.IdSubCos AS CodSubcen,SubCosto,N.IdNom AS CodNom,TipoNomina,Asistencia,NoDevenga
,TipoLiq,N.VrSalario AS VrSalBasico,N.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS Nom_IdEstdo,EN.Estado AS Nom_Estado,EsRetiro,NLiqRet
,N.KeyRegistro AS KeyRegstro,TimeSys,N.FecUpdate AS FecUpd,N.IdUsuario AS IdUsuari,Usuario
--datos detalle de nomina
,Item,D.IdConcepto AS CodConcepto,Concepto,Detalle,CantDevg,CantDed,VrUnitario,VrTotDevg,VrTotDed,D.Unidad AS Undad,ClaseLiq,VrOrigen,D.Tarifa AS TarifaBase,DiasCalc,DiasNov,CodFondo,FD.Fondo AS Det_Fondo
,NPrestamo,NCuota,OrigCargue,VrBaseLiq,ClaseCon
--Datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,SegFinMes,DchDotacion,DchCafeteria
,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,TipoTrabj
,NumVeh,TipoAdmon,VehPropio,VrATransMes
 FROM Trn_Nomina AS N INNER JOIN Trn_NomDetalle AS D ON N.IdPeriodo=D.IdPeriodo AND N.KeyRegistro=D.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN Dependencias AS DP ON N.IdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON N.IdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON N.IdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON N.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EN ON N.IdEstado=EN.IdEstado 
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN NomConceptos AS CN ON D.IdConcepto=CN.IdConcepto INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado  LEFT JOIN Fondos AS FD ON D.CodFondo=FD.IdFondo
 LEFT JOIN NomVehiculos AS VH ON N.pVehiculo=VH.IdVehiculo
 WHERE N.IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%')  AND N.KeyRegistro LIKE ISNULL(@pmKeyRegistro,'%') AND N.IdNom like ISNULL(@pmIdNom,'%')
 AND  N.IdDep LIKE ISNULL(@pmIdDep,'%') AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND (NLiqRet>=ISNULL(@pmNLiqRet,-1)  AND NLiqRet<=ISNULL(@pmNLiqRet,2147483647))
 AND (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))
 ORDER BY N.KeyRegistro,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotaProd @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,IdCCosto,IdSubCos,Modalidad,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_NotaProd
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotaProdLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmNumNotaIni INT=Null,@pmNumNotaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT NumNota,IdCia,Fecha,N.IdConcepto AS CdConcepto,Concepto,N.IdTercero AS NitTercero,RazonSocial,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,N.IdCCosto AS CdCcosto,CCosto,IdSubCos
,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario
 FROM Trn_NotaProd AS N INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON  N.IdCCosto=CC.IdCCosto
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND NumNota BETWEEN ISNULL(@pmNumNotaIni,0) AND ISNULL(@pmNumNotaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND N.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotaProdOrd  @pmNumOrden INT,@pmIdCia CHAR(2),@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT NumNota,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrCostos,CantEnt,CantSal,NumOrden,Modalidad
 FROM Trn_NotaProd
 WHERE  NumOrden=@pmNumOrden  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotaProdRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,NumNota,N.IdCia AS CdCia,Compania,Fecha,N.IdConcepto AS CdConcepto,Concepto,N.IdTercero AS NitTercero,RazonSocial,VrSubTotal,VrCostos
,CantEnt,CantSal,NumOrden,N.IdCCosto AS CdCcosto,CCosto,N.IdSubCos AS CdSubCent,SubCosto,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,N.FecUpdate AS Fec_Update,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
 FROM Trn_NotaProd AS N  INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND N.IdTercero LIKE ISNULL(@pmIdTercero,'%')  AND N.IdCCosto LIKE ISNULL(@pmIdCCosto ,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY N.IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotaProd_Cr @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT N.TipDoc AS Tip_Doc,TipoDoc,NumNota,N.IdCia AS CdCia,Compania,N.Fecha AS FechaDoc,N.IdConcepto AS CdConcepto,Concepto
,N.IdTercero AS NitTercero,RazonSocial,VrSubTotal,VrCostos,CantEnt,CantSal,N.NumOrden AS Num_Orden,N.IdCCosto AS CdCcosto,CC.CCosto AS NomCentCosto
,N.IdSubCos AS CdSubCent,SC.SubCosto AS SubcCosto,Modalidad,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,N.TimeSys AS Fec_Add,N.FecUpdate AS Fec_Update,IdCiaCrea,N.IdUsuario AS CdUsuario,Usuario,Leyenda
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,VrReteEnt,VrReteSal,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,K.NumOrden AS KarNumOrden,IdCiaOrd,Cotizacion,IdCiaCot
,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_NotaProd AS N  INNER JOIN Trn_Kardex AS K ON N.TipDoc=K.TipDoc AND N.NumNota=K.Documento AND N.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd  
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposCom AS TC ON N.TipCom=TC.IdCom
 WHERE N.TipDoc=@pmTipDoc AND NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
 ORDER BY NumNota,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCosto,VrSancion,VrOtros,VrNeto
,VrAplicado,VrAnticipo,Cantidad,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos
,IdVend,TarifaCom,CodTarCom,CdCajero,IdLocal,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdProducto,CdForma,NumCheque,CdBanco
,MulPlazos,IdPlazo,PrestCaja,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Notas
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasAcu @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrNeto) AS STOTNET
 FROM Trn_Notas
 WHERE  TipDoc='NCR' AND YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY  IdCliente,IdAgencia,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBuses @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,FecVence,IdConcepto,IdCliente,IdAgencia,IdVehiculo,IdConductor,VehPropio,ValorTotal,ValorIva,ValorReteFte,ValorReteIca,ValorCosto,ValorSancion,ValorNeto,ValorAplicado,IdVendedor
,Comision,IdTarCom,MulPlazos,IdPlazo,PrestCaja,IntMora,NumCheque,IdBanco,IdCCosto,IdClase,IdCargo,Referencia,TipDcm,Documento,IdCiaDoc,BaseIva,BaseRet,TarifaIva,TarifaRet,IdTarIva,IdTarRet,TipCom,Comprobante
,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_NotasBuses
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesCR_Cr @pmTipDoc VARCHAR(3), @pmNumNotaIni INT,@pmNumNotaFin INT, @pmIdCia CHAR(2)
AS
 SELECT  N.TipDoc AS TipoNota,TipoDoc,NumNota, N.IdCia AS CodCia,Compania, N.Fecha AS FechaNota,N.IdConcepto AS IdConcept,C.Concepto AS Concept, N.IdCliente AS NitPropietario,NP.RazonSocial AS Propietario, N.IdAgencia,N.IdVehiculo AS PlacaVeh, N.IdConductor AS CedConductor,NC.RazonSocial AS Conductor
, N.VehPropio AS VehEspropio, ValorTotal, ValorIva, ValorReteFte, ValorReteIca, ValorCosto, ValorSancion, ValorNeto, ValorAplicado, N.IdVendedor, N.Comision, IdTarCom,PrestCaja, IntMora,NumCheque,N.IdBanco, N.IdCCosto AS IdCCost,CCosto,N.Referencia AS RefNota
,TipDcm, N.Documento AS NDocumnt, N.IdCiaDoc AS CiaDocumnt, BaseIva, BaseRet, TarifaIva, TarifaRet, IdTarIva, IdTarRet, N.TipCom AS IdTipCom,TM.TipoCom AS TipoComprobante, Comprobante, IdCiaCom, N.Observacion AS Observ, N.IdEstado AS IdEstadDoc,ED.Estado AS EstadDoc, N.TimeSys, FecUpdate, IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario,Leyenda
,Item,RF.TipDoc AS TipoDocApl,RF.Documento AS NCausacionApl,RF.IdCiaDoc AS CiaDocApl,ItemDoc,RF.IdCargo AS CodCargo,F.Concepto AS Cargo,RF.IdClase AS CodClase,ClaseCuenta,ValorAbono,Detalle,FecPago
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM Trn_NotasBuses  AS N INNER JOIN Trn_RecFactura AS RF ON N.TipDoc=RF.TipRec AND N.NumNota=RF.Recibo AND N.IdCia=RF.IdCia
 INNER JOIN Sys_TiposDoc  AS TD ON N.TipDoc=TD.IdDoc  INNER JOIN Companias  AS CI ON N.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON N.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON N.IdCliente=NP.IdTercero   INNER JOIN Terceros AS NC ON N.IdConductor=NC.IdTercero
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto  INNER JOIN Fijos AS F ON RF.IdCargo=F.IdCargo
 INNER JOIN BusClaseCue AS CU ON  RF.IdClase=CU.IdClase
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON N.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN TiposCom AS TM ON N.TipCom=TM.IdCom
 WHERE N.TipDoc=@pmTipDoc AND NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin   AND N.IdCia=@pmIdCia
ORDER BY N.IdCia,NumNota,Item


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasBusesCaj @pmTipDoc VARCHAR(3),@pmFecha SMALLDATETIME
, @pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
AS
 SELECT  TipDoc,IdCia,IdUsuario,SUM(ValorNeto) AS STotNet
 FROM   Trn_NotasBuses WHERE PrestCaja<>0 AND Fecha=@pmFecha AND  TipDoc like ISNULL(@pmTipDoc,'%')  AND  IdCia like ISNULL(@pmIdCia ,'%%')
 AND IdUsuario like ISNULL(@pmIdUsuario,'%')
 GROUP BY TipDoc,IdCia,IdUsuario

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesDb_Cr @pmTipDoc VARCHAR(3), @pmNumNotaIni INT,@pmNumNotaFin INT, @pmIdCia CHAR(2)
AS
 SELECT  N.TipDoc AS TipoNota,TipoDoc,NumNota, N.IdCia AS CodCia,Compania, Fecha, N.FecVence AS FecVenceNota,N.IdConcepto AS IdConcept,C.Concepto AS Concept, N.IdCliente AS NitPropietario,NP.RazonSocial AS Propietario, N.IdAgencia,N.IdVehiculo AS PlacaVeh, N.IdConductor AS CedConductor,NC.RazonSocial AS Conductor
, N.VehPropio AS VehEspropio, ValorTotal, ValorIva, ValorReteFte, ValorReteIca, ValorCosto, ValorSancion, ValorNeto, ValorAplicado, N.IdVendedor, N.Comision, IdTarCom, MulPlazos, N.IdPlazo AS IdPlzo,Plazo,DiasPago,NVmto, PrestCaja, IntMora,NumCheque, N.IdBanco AS CodBanco,Banco, N.IdCCosto AS IdCCost,CCosto,N.Referencia AS RefNota
,TipDcm, N.Documento AS NDocumnt, IdCiaDoc, BaseIva, BaseRet, TarifaIva, TarifaRet, IdTarIva, IdTarRet, N.TipCom AS IdTipCom,TM.TipoCom AS TipoComprobante, Comprobante, IdCiaCom, N.Observacion AS Observ, N.IdEstado AS IdEstadDoc,ED.Estado AS EstadDoc, TimeSys, FecUpdate, IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario,Leyenda
,Item,VF.FecVence AS FecVenceFac,ValorFactura,ValorAbono,VF.IdCargo AS IdCarg,F.Concepto AS NomCargo,VF.IdClase AS CodClase,ClaseCuenta,FecUltPago,Factura,VF.Referencia AS RefVce
,Detalle,TipRec,Recibo,IdCiaRec,ValorAtcpo
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM Trn_NotasBuses  AS N  INNER JOIN Trn_CausacVence AS VF ON N.TipDoc = VF.TipDoc AND N.NumNota= VF.Documento AND N.IdCia =VF.IdCia
 INNER JOIN Sys_TiposDoc  AS TD ON N.TipDoc=TD.IdDoc  INNER JOIN Companias  AS CI ON N.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON N.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario  INNER JOIN Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo 
 INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS NP ON N.IdCliente=NP.IdTercero 
 INNER JOIN Terceros AS NC ON N.IdConductor=NC.IdTercero INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN  Bancos AS B ON N.IdBanco=B.IdBanco  INNER JOIN Fijos AS F ON VF.IdCargo=F.IdCargo
 INNER JOIN BusClaseCue AS CU ON VF.IdClase=CU.IdClase
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON N.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN TiposCom AS TM ON N.TipCom=TM.IdCom
WHERE N.TipDoc=@pmTipDoc AND NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin   AND N.IdCia=@pmIdCia
ORDER BY N.IdCia,NumNota,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesLta @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCliente VARCHAR(16),@pmIdVehiculo VARCHAR(10)

AS
 SELECT  NumNota, IdCia, Fecha, FecVence,N.IdConcepto AS IdConcept,Concepto, IdCliente,T.RazonSocial AS Cliente, IdAgencia, IdVehiculo, IdConductor,O.RazonSocial AS Conductor,VehPropio, ValorTotal, ValorIva, ValorReteFte, 
                      ValorReteIca, ValorCosto, ValorSancion, ValorNeto, ValorAplicado, IdVendedor,V.RazonSocial AS Vendedor,N.Comision AS Comison, IdTarCom, MulPlazos, N.IdPlazo AS IdPlaz,DiasPago AS Dias, PrestCaja, IntMora, 
                      NumCheque,N.IdBanco AS IdBanc,Banco, IdCCosto, N.IdClase AS IdClas , IdCargo, Referencia, TipDcm, Documento, IdCiaDoc, BaseIva, BaseRet, TarifaIva, TarifaRet, IdTarIva, 
                      IdTarRet, TipCom, Comprobante, IdCiaCom, N.Observacion AS Observ, N.IdEstado AS IdEstad,Estado,TimeSys, FecUpdate, IdCiaCrea,N.IdUsuario AS IdUsuari
 FROM   Trn_NotasBuses AS N INNER JOIN Conceptos AS C ON N.IdConcepto=C.IdConcepto
  INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero  INNER JOIN Terceros AS O ON N.IdConductor=O.IdTercero
  LEFT JOIN Terceros AS V ON N.IdVendedor=V.IdTercero  LEFT JOIN Plazos AS P ON N.IdPlazo=P.IdPlazo
  LEFT JOIN Bancos AS B ON N.IdBanco=B.IdBanco LEFT JOIN EstadoDoc AS E ON N.IdEstado=E.IdEstado
 WHERE TipDoc=@pmTipDoc AND  Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY NumNota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesNMes @pmAnno INT,@pmMes INT=Null, @pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdClase VARCHAR(4)=Null, @pmTipDoc VARCHAR(3)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT IdCliente,IdVehiculo,IdCia,IdClase,Month(Fecha) AS NMes,SUM(ValorTotal) AS SvalTot,SUM(ValorNeto) AS SValNet
 FROM   Trn_NotasBuses 
 WHERE Year(Fecha)=@pmAnno AND Month(Fecha)=ISNULL(@pmMes,Month(Fecha))  AND  IdCia like ISNULL(@pmIdCia,'%%') 
 AND IdCliente like ISNULL(@pmIdCliente,'%') AND IdClase like ISNULL(@pmIdClase,'%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 GROUP BY  IdCliente,IdVehiculo,IdCia,IdClase,Month(Fecha)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesTotCaj  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmCdCajero VARCHAR(11)=Null
AS
 SELECT  TipDoc,SUM(ValorNeto) AS STotNet
 FROM   Trn_NotasBuses WHERE PrestCaja<>0 AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmCdCajero,'%')
 GROUP BY TipDoc


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesVFec @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdClase VARCHAR(4)=Null, @pmTipDoc VARCHAR(3)=Null
AS
 SELECT IdCliente,IdVehiculo,SUM(ValorTotal) AS SvalTot,SUM(ValorNeto) AS SValNet
 FROM   Trn_NotasBuses 
 WHERE Fecha  BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia like ISNULL(@pmIdCia,'%%') 
 AND IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdClase like ISNULL(@pmIdClase,'%') AND TipDoc like ISNULL(@pmTipDoc,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY IdCliente,IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryNotasBusesVc @pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
  SELECT  TipDoc, NumNota, IdCia, Fecha, FecVence, IdConcepto, IdCliente, IdAgencia, IdVehiculo, IdConductor, VehPropio, ValorTotal,ValorNeto, ValorAplicado
, IdVendedor, Comision,IdClase,IdCargo,Referencia,TipDcm, Documento, IdCiaDoc
 FROM Trn_NotasBuses 
 WHERE TipDoc='NC1' AND  IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY IdCia,NumNota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasBuses_Cr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdCargo VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmPrestCaja BIT=Null,@pmIntMora BIT=Null,@pmNumNotaIni INT=Null,@pmNumNotaFin INT=Null,@pmIdGrupo VARCHAR(4)=Null
AS
 SELECT  TipDoc, NumNota,N.IdCia AS CodCia,Compania, Fecha,MulPlazos, N.IdPlazo AS codPlazo,Plazo,DiasPago,NVmto, FecVence, N.IdConcepto AS IdConcept,C.Concepto AS ConceptDoc, IdCliente,NP.RazonSocial AS Propietario,N.IdVehiculo AS PlacaVeh, N.IdConductor AS CedConductor
, NC.RazonSocial AS Conductor,ValorTotal, ValorIva, ValorReteFte,ValorReteIca, ValorNeto, ValorAplicado,NumCheque, N.IdBanco AS CodBanco,Banco,N.IdClase AS IdClas,ClaseCuenta, N.IdCargo AS CodCargo,F.Concepto AS ConceptCargo, Referencia, TipDcm, Documento, IdCiaDoc
 ,TipCom,TipoCom, Comprobante, IdCiaCom,N.Observacion AS Observ, N.IdEstado AS IdEstad,ED.Estado AS EstadDoc,N.IdUsuario AS IdUsuari,Usuario,TipoDoc,TimeSys, FecUpdate, IdCiaCrea,PrestCaja, IntMora,N.IdCCosto AS IdCcost,CCosto,BaseIva, BaseRet, TarifaIva, TarifaRet, IdTarIva
,IdTarRet,N.VehPropio AS VhPropio
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_NotasBuses AS N INNER JOIN  Companias  AS CI ON N.IdCia=CI.IdCia
 INNER JOIN Conceptos AS C ON N.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON  N.IdUsuario=U.IdUsuario 
 INNER JOIN Vehiculos AS V ON N.IdVehiculo=V.IdVehiculo   INNER JOIN Terceros AS NP ON N.IdCliente=NP.IdTercero
 INNER JOIN Terceros AS NC ON N.IdConductor=NC.IdTercero  INNER JOIN BusClaseCue AS CU ON N.IdClase=CU.IdClase
 INNER JOIN Bancos AS B ON N.IdBanco=B.IdBanco INNER JOIN Fijos AS F ON N.IdCargo=F.IdCargo
 INNER JOIN Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN Sys_TiposDoc  AS TD ON N.TipDoc=TD.IdDoc
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON N.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar  LEFT JOIN TiposCom AS TM ON N.TipCom=TM.IdCom
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  NumNota BETWEEN ISNULL(@pmNumNotaIni,0)  AND ISNULL(@pmNumNotaFin,2147483647)
 AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND N.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND N.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND N.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND N.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND N.IdUsuario LIKE ISNULL(@pmIdUsuario,'%') 
 AND (PrestCaja=ISNULL(@pmPrestCaja,0) or PrestCaja=ISNULL(@pmPrestCaja,1))
 AND (IntMora=ISNULL(@pmIntMora,0) or IntMora=ISNULL(@pmIntMora,1))
 AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY TipDoc,N.IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasCaj @pmFecha SMALLDATETIME,@pmIdCia CHAR(2),@pmCdCajero VARCHAR(11)
 AS 
 SELECT TipDoc,IdCia,CdCajero,SUM(VrSubTotal) AS SSUBTOT,SUM(VrNeto) AS SNETO,SUM(VrOtros) AS STOTCAJ
 FROM Trn_Notas 
 WHERE PrestCaja<>0 AND Fecha=@pmFecha AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND CdCajero LIKE ISNULL(@pmCdCajero,'%')
 GROUP BY TipDoc,IdCia,CdCajero
 ORDER BY IdCia,CdCajero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasDoc @pmTipDoc VARCHAR(3),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2)
,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,NumNota,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FechaVence,VrNeto
,TipCom,Comprobante,IdCiaCom
 FROM Trn_Notas
 WHERE TipDoc=@pmTipDoc  AND TipDcm=@pmTipDcm AND Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm
AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmNotaIni INT=Null,@pmNotaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT NumNota,IdCia,Fecha,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrCosto,VrSancion,VrOtros,VrNeto
,VrAplicado,VrAnticipo,Cantidad,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv,CodTarIva,CodTarRet,CodTarIca,CodTarRiv,N.IdCCosto AS CodCcosto,CCosto,IdSubCos,IdVend,V.RazonSocial AS Vendedor
,TarifaCom,CodTarCom,CdCajero,N.IdLocal AS CodCiudad,Localidad,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,FecDcm,CdProducto,Modalidad,PrestCaja
,CdForma,NumCheque,CdBanco,MulPlazos,IdPlazo,OrigenAdd,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Notas AS N  INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON N.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON N.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario 
 INNER JOIN   CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON N.IdLocal=L.IdLocal
 WHERE TipDoc=@pmTipDoc   AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND NumNota BETWEEN ISNULL(@pmNotaIni,0) AND ISNULL(@pmNotaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdCCosto  VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocal VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,NumNota,N.IdCia AS CdCia,Compania,Fecha,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,N.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,CodAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,N.VrCosto AS ValCosto,VrSancion,VrOtros,VrNeto
,VrAplicado,VrAnticipo,Cantidad,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,N.IdCCosto AS CdCentCosto,CCosto,N.IdSubCos AS CdSubCentro,SubCosto,N.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom
,N.IdLocal AS NotCdCiudad,LN.Localidad AS NotCiudad,N.Referencia AS NotReferencia,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,TipDcm
,Documento,IdCiaDcm,FecDcm,CdProducto,DescripProd,CdForma,NumCheque,CdBanco,BN.Banco AS NomBanco,MulPlazos,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,PrestCaja,Modalidad,OrigenAdd
,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,N.FecUpdate AS Fec_Update,IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Notas AS N INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON N.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON N.IdAgencia=A.IdAgencia  INNER JOIN Localidades  AS LN ON N.IdLocal=LN.IdLocal
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON N.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN ProdMcias  AS PM ON N.CdProducto=PM.IdProducto  LEFT JOIN Bancos AS BN ON N.CdBanco=BN.IdBanco
 LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos LEFT JOIN Terceros AS TC ON N.CdConductor=TC.IdTercero
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND N.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND N.IdVend LIKE ISNULL(@pmIdVend,'%') 
 AND N.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY N.IdCia,NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotasTotCaj @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmCdCajero VARCHAR(11)=Null
 AS 
 SELECT TipDoc,SUM(VrSubTotal) AS SSUBTOT,SUM(VrNeto) AS SNETO,SUM(VrOtros) AS STOTCAJ
 FROM Trn_Notas 
 WHERE PrestCaja<>0 AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND CdCajero LIKE ISNULL(@pmCdCajero,'%')
 GROUP BY TipDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryNotas_Cr @pmTipDoc VARCHAR(3),@pmNumNotaIni INT,@pmNumNotaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT N.TipDoc AS Tip_Doc,TipoDoc,NumNota,N.IdCia AS CdCia,Compania,N.Fecha AS FechaDoc,N.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,N.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,CodAgencia,FechaVence,VrSubTotal,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,N.VrCosto AS VrTotCosto,VrSancion,VrOtros,VrNeto
,VrAplicado,VrAnticipo,Cantidad,BaseImp,BaseRet,BaseIca,BaseRiv,TarifaIva,TarifaRet,TarifaIca,TarifaRiv
,N.IdCCosto AS CdCentCosto,CC.CCosto AS NotCenCosto,N.IdSubCos AS CdSubCentro,SC.SubCosto AS NotSubCentro,N.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom
,N.IdLocal AS NotCdCiudad,LN.Localidad AS NotCiudad,N.Referencia AS NotReferencia,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,TipDcm,N.Documento AS NumDocmento,IdCiaDcm,FecDcm,CdProducto,DescripProd
,CdForma,N.NumCheque AS NroCheque,CdBanco,BN.Banco AS NomBanco,MulPlazos,N.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,PrestCaja,Modalidad,OrigenAdd
,N.TipCom AS CodTipoComp,TipoCom,N.Comprobante AS NumComprob,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,N.TimeSys AS Fec_Add,N.FecUpdate AS Fec_Update
,N.IdCiaCrea AS CodCiaCrea,N.IdUsuario AS IdUsuari,Usuario,Leyenda
--detalle del comprobante
,Item,D.IdCuenta AS CdCuenta,NomCuenta,Detalle,VrDebito,VrCredito,D.IdTercero AS NitTercero,DT.RazonSocial AS NomTercero,IdVehiculo,D.IdCCosto AS DetCodCentCost,DC.CCosto AS DetCentCost
,D.IdSubCos AS DetCodSubCentro,DS.SubCosto  AS DetSubCentro,VrBase,TarifaBase,D.TipDoc AS DetTipoDoc,D.Documento AS DetNumDoc,IdCiaDoc,TipFac,Factura,IdCiaFac,ItemFac
,FecVence,D.NumCheque AS DetNumCheque,TipoAplica,NitOtros,Referncia
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Notas AS N INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON N.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON N.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON N.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON N.IdAgencia=A.IdAgencia  INNER JOIN Localidades  AS LN ON N.IdLocal=LN.IdLocal
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON N.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN ProdMcias  AS PM ON N.CdProducto=PM.IdProducto  LEFT JOIN Bancos AS BN ON N.CdBanco=BN.IdBanco
 LEFT JOIN TiposCom AS TCM ON N.TipCom=TCM.IdCom  LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS TC ON N.CdConductor=TC.IdTercero
 LEFT JOIN  Trn_ComDetalle AS D ON N.TipCom=D.TipCom AND N.Comprobante=D.Comprobante AND N.IdCiaCom=D.IdCia
 LEFT JOIN Puc AS P ON D.IdCuenta=P.IdCuenta LEFT JOIN Terceros AS DT ON D.IdTercero=DT.IdTercero
 LEFT JOIN CentroCosto AS DC ON D.IdCCosto=DC.IdCCosto  LEFT JOIN SubCentros AS DS ON D.IdSubCos=DS.IdSubCos
 WHERE N.TipDoc=@pmTipDoc AND NumNota BETWEEN @pmNumNotaIni AND @pmNumNotaFin AND N.IdCia=@pmIdCia
 ORDER BY  NumNota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOComb @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,OCargue,IdCia,Fecha,FechaDesp,FechaVence,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto
,VrSobretasa,VrImpGlobal,VrNeto,Cantidad,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocal,LugarEnv,DiasEntraga,IdVehiculo,IdConductor,NitEmpTrans,EmpTrans
,CdRuta,Referncia,IdForma,DetallePago,MulPlazos,IdPlazo,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,Factura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo,Aprobacion,FechaAprob
,DetalleAprob,CdUsuAprob,OrigenAdd,ZonaFrontera,Anulado,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_OComb
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombDet @pmOCargue INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,K.IdProducto AS CdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Salidas,VrPrecio,Salidas*VrPrecio AS VrSubTotal,Comptmntos
,VrBruto,K.ListaPrec AS LtaPrec,VrBase,K.Referencia AS Referncia,Unidades,K.IdVend AS NitVend,V.RazonSocial AS Vendedor,Comision,K.CodTarCom AS CdTarifCom
,DescripProd,Servcios,Tanques,O.Fecha AS FecOCargue,FechaDesp,FechaVence,IdCliente,T.RazonSocial AS Cliente,IdAgencia,DirEnvio,LugarEnv
,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor,NitEmpTrans,EmpTrans,Referncia,Referencia2,Pedido,IdCiaPed,Guia,IdCiaGui,O.Factura AS NumFactura,IdCiaFac,FechaFact,O.Observacion AS Observ
 FROM Trn_OComb AS O  INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCargue=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN Terceros AS V ON K.IdVend=V.IdTercero
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS CD ON O.IdConductor=CD.IdTercero
 WHERE OCargue=@pmOCargue AND O.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombFac  @pmFactura INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT OCargue,IdCia,Fecha,T.RazonSocial AS NomCliente,IdCliente,IdAgencia,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor,FechaDesp,FechaVence,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto
,Cantidad,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio,O.IdLocal AS CdCiudad,Localidad,LugarEnv,DiasEntraga,O.IdConcepto AS CdConcepto,Concepto,NitEmpTrans,EmpTrans,CdRuta,Referncia,IdForma,DetallePago,MulPlazos,IdPlazo
,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,Factura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo,Aprobacion,FechaAprob,DetalleAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev
,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.IdUsuario AS IdUsuari,Usuario,TimeSys,IdCiaCrea,FecUpdate,TipDoc
 FROM Trn_OComb AS O  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON O.IdVend=V.IdTercero
 INNER JOIN Terceros AS CD ON O.IdConductor=CD.IdTercero
 INNER JOIN  Conceptos AS CN ON O.IdConcepto=CN.IdConcepto
 INNER JOIN Localidades AS L ON O.IdLocal=L.IdLocal
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  Anulado=0 AND Factura<=@pmFactura
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')   AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 ORDER BY IdCia,OCargue
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombFact @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmOrdenIni INT=Null,@pmOrdenFin INT=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdProducto VARCHAR(16)=Null,@pmFechaFacIni SMALLDATETIME=Null,@pmFechaFacFin SMALLDATETIME=Null
 AS 
 SELECT  OCargue,O.IdCia AS CdCia,Compania,O.Fecha AS FechaOrd,FechaDesp,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantOrden,SUM(Unidades) AS Unidades
--datos de la factura
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas
,TotalUnidades,FacTarifSob,FacTarifSol,FacTarifIglobal
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor
,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,O.IdVehiculo AS PlacaVeh,Referncia,Aprobacion,FechaAprob,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui
,O.Observacion AS Observ,DetallePago,O.IdEstado AS CdEstado,Estado
,MAX(VrPrecio) AS OrdValorUnit,SUM(Salidas*VrPrecio) AS OrdValorTotal,MAX(Sobretasa) AS TarifSobtasa,MAX(Soldicom) AS TarifSoldicom,MAX(ImpGlobal) AS TarifIglobal
 FROM  Trn_OComb AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero  INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado 
 INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia 
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCargue=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT  TipOrd,NumOrden,IdCiaOrd,K.TipDoc AS TipoFact,K.Documento AS NumFactura,K.IdCia AS CdCiaFac,KC.Compania AS NomCiaFac
,K.Fecha AS FecFactura,Remision,IdCiaRem,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd
,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaEnt) AS IvaEntradas
,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoEnt) AS DctoEntradas,SUM(VrDctoSal) AS DctoSalidas,SUM(K.Unidades) AS TotalUnidades
,MAX(Sobretasa) AS FacTarifSob,MAX(Soldicom) AS FacTarifSol,MAX(ImpGlobal) AS FacTarifIglobal
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE NoVentas BETWEEN 1  AND 4  AND TipOrd='OCC'  AND  NumOrden>0 
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaFacIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFacFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY TipOrd,NumOrden,IdCiaOrd,K.TipDoc,K.Documento,K.IdCia,KC.Compania,K.Fecha,Remision,IdCiaRem,K.IdProducto,PM.DescripProd) AS FA 
 ON O.TipDoc=FA.TipOrd AND O.OCargue=FA.NumOrden AND O.IdCia=FA.IdCiaOrd AND K.IdProducto=FA.CdProducto
 WHERE  O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%') AND O.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND OCargue  BETWEEN ISNULL(@pmOrdenIni,0) AND ISNULL(@pmOrdenFin,2147483647)
 GROUP BY  OCargue,O.IdCia,Compania,O.Fecha,FechaDesp,FechaVence,K.IdProducto,DescripProd
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas
,TotalUnidades,FacTarifSob,FacTarifSol,FacTarifIglobal
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,O.IdVend,VN.RazonSocial
,O.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,LugarEnv,O.IdVehiculo,Referncia,Aprobacion,FechaAprob,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui
,O.Observacion,DetallePago,O.IdEstado,Estado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombGui  @pmGuia INT,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT OCargue,IdCia,Fecha,T.RazonSocial AS NomCliente,IdCliente,IdAgencia,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor
,FechaDesp,FechaVence,O.IdConcepto AS CdConcepto,Concepto,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto
,Cantidad,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio,O.IdLocal AS CdCiudad,Localidad,LugarEnv,DiasEntraga,NitEmpTrans,EmpTrans,CdRuta,Referncia,IdForma,DetallePago,MulPlazos,IdPlazo
,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,Factura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo,Aprobacion,FechaAprob,DetalleAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev
,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.IdUsuario AS IdUsuari,Usuario,TimeSys,IdCiaCrea,FecUpdate,TipDoc
 FROM Trn_OComb AS O  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON O.IdVend=V.IdTercero
 INNER JOIN Terceros AS CD ON O.IdConductor=CD.IdTercero
 INNER JOIN  Conceptos AS CN ON O.IdConcepto=CN.IdConcepto
 INNER JOIN Localidades AS L ON O.IdLocal=L.IdLocal
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  Anulado=0 AND Factura<=0
 AND Guia<=@pmGuia
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')   AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 ORDER BY IdCia,OCargue
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombGuia @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmOrdenIni INT=Null,@pmOrdenFin INT=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdProducto VARCHAR(16)=Null,@pmFechaGuiIni SMALLDATETIME=Null,@pmFechaGuiFin SMALLDATETIME=Null
,@pmFacturaIni INT=Null,@pmFacturaFin INT=Null
 AS 
 SELECT  OCargue,O.IdCia AS CdCia,Compania,O.Fecha AS FechaOrd,FechaDesp,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantOrden,SUM(Unidades) AS Unidades
--datos de la guía
,NumGuia,CdCiaGui,NomCiaGui,FecGuia,CantSalidas,TotalUnidades,FechaDesGui,GuiLugar,HorasVig,GuiTipFact,GuiFactura,GuiCiaFac,GuiFechaFac
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor
,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,O.IdVehiculo AS PlacaVeh,Referncia,Aprobacion,FechaAprob,TipPed,Pedido,IdCiaPed,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact
,O.Observacion AS Observ,DetallePago,O.IdEstado AS CdEstado,Estado
,MAX(VrPrecio) AS OrdValorUnit,SUM(Salidas*VrPrecio) AS OrdValorTotal,MAX(Sobretasa) AS TarifSobtasa,MAX(Soldicom) AS TarifSoldicom,MAX(ImpGlobal) AS TarifIglobal
 FROM  Trn_OComb AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero  INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado 
 INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia 
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCargue=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN  (SELECT  TipOrd,NumOrden,IdCiaOrd,K.Documento AS NumGuia,K.IdCia AS CdCiaGui,KC.Compania AS NomCiaGui,K.Fecha AS FecGuia,K.IdProducto AS CdProducto
,PM.DescripProd AS NombreProd,SUM(Salidas) AS CantSalidas,SUM(Unidades) AS TotalUnidades,G.FechaDesp AS FechaDesGui,G.LugarEnv AS GuiLugar,HorasVig
,G.TipFac AS GuiTipFact,G.Factura AS GuiFactura,G.IdCiaFac AS GuiCiaFac,G.FechaFact AS GuiFechaFac
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN Trn_Guia AS G ON K.TipDoc=G.TipDoc AND K.Documento=G.Guia AND K.IdCia=G.IdCia
 WHERE  K.TipDoc='GUI' AND TipOrd='OCC' AND NumOrden>0 AND  Anulado=0 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (G.Fecha>=ISNULL(@pmFechaGuiIni,CAST('19100101' AS SMALLDATETIME)) AND G.Fecha<=ISNULL(@pmFechaGuiFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY  TipOrd,NumOrden,IdCiaOrd,K.Documento,K.IdCia,KC.Compania,K.Fecha,K.IdProducto,PM.DescripProd,FechaDesp,G.LugarEnv,HorasVig
,G.TipFac,G.Factura,G.IdCiaFac,G.FechaFact) AS GU
  ON O.TipDoc=GU.TipOrd AND O.OCargue=GU.NumOrden AND O.IdCia=GU.IdCiaOrd AND K.IdProducto=GU.CdProducto
  WHERE  O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%') AND O.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND OCargue  BETWEEN ISNULL(@pmOrdenIni,0) AND ISNULL(@pmOrdenFin,2147483647)
 AND O.Factura BETWEEN ISNULL(@pmFacturaIni,-1) AND ISNULL(@pmFacturaFin,2147483647)
 GROUP BY  OCargue,O.IdCia,Compania,O.Fecha,FechaDesp,FechaVence,K.IdProducto,DescripProd
,NumGuia,CdCiaGui,NomCiaGui,FecGuia,CantSalidas,TotalUnidades,FechaDesGui,GuiLugar,HorasVig,GuiTipFact,GuiFactura,GuiCiaFac,GuiFechaFac
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,O.IdVend,VN.RazonSocial
,O.IdLocal,LE.Localidad,LE.IdDep,DE.Departamento,LugarEnv,O.IdVehiculo,Referncia,Aprobacion
,FechaAprob,TipPed,Pedido,IdCiaPed,TipFac,O.Factura,IdCiaFac,FechaFact
,O.Observacion,DetallePago,O.IdEstado,Estado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombLta  @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmOCargueIni INT=Null,@pmOCargueFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT OCargue,IdCia,Fecha,FechaDesp,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto
,Cantidad,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio,O.IdLocal AS CdCiudad,Localidad,LugarEnv,DiasEntraga,IdVehiculo,IdConductor,CD.RazonSocial AS Conductor,NitEmpTrans,EmpTrans,CdRuta,Referncia,IdForma,DetallePago,MulPlazos,IdPlazo
,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,Factura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo,Aprobacion,FechaAprob,DetalleAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev
,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,ZonaFrontera,O.IdUsuario AS IdUsuari,Usuario,TimeSys,IdCiaCrea,FecUpdate,TipDoc
 FROM Trn_OComb AS O  INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON O.IdVend=V.IdTercero
 INNER JOIN Terceros AS CD ON O.IdConductor=CD.IdTercero
 INNER JOIN  Conceptos AS CN ON O.IdConcepto=CN.IdConcepto
 INNER JOIN Localidades AS L ON O.IdLocal=L.IdLocal
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND OCargue BETWEEN ISNULL(@pmOCargueIni,0) AND ISNULL(@pmOCargueFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,OCargue
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCombRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocal VARCHAR(8)=Null
,@pmIdConductor VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,OCargue,O.IdCia AS CdCia,Compania,Fecha,FechaDesp,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto
,Cantidad,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,DiasEntraga,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,TC.RazonSocial AS Conductor,LibMilitar,ClaseLib,FechaLib,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,NumTarjeta,VigTarjeta
,O.NitEmpTrans,EmpTrans,CdRuta,Ruta,Referncia,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos
,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,Factura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo
,Aprobacion,FechaAprob,DetalleAprob,CdUsuAprob,UA.Usuario AS UsuarioAprob,OrigenAdd,ZonaFrontera,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
,TimeSys,O.FecUpdate AS Fech_Update,IdCiaCrea
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,CLI.NumCuenta AS NumeroCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--vehiculo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarcaVeh,MV.Marca AS MarcaVeh,Modelo,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,CarrCapac,V.UndCapc AS UnidCapacidad,Comptmtos,CapComp
 FROM  Trn_OComb AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN Terceros AS TC ON O.IdConductor=TC.IdTercero
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN adm_Usuarios AS UA ON O.CdUsuAprob=UA.IdUsuario
 LEFT JOIN TercCndtores AS TCD ON O.IdConductor=TCD.IdConductor
 LEFT JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo
 LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca
  WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND O.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND O.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,OCargue
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOComb_Cr @pmTipDoc VARCHAR(3),@pmOCargueIni INT,@pmOCargueFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc AS Tip_Doc,TipoDoc,OCargue,O.IdCia AS CdCia,Compania,O.Fecha AS FechaDoc,FechaDesp,FechaVence
,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia
,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto
,Cantidad,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,O.IdLocal AS CdCiudad,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,LugarEnv,DiasEntraga,O.IdVehiculo AS PlacaVeh,O.IdConductor AS CedConductor,TC.RazonSocial AS Conductor,LibMilitar,ClaseLib,FechaLib,CertJudicial,FecJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,NumTarjeta,VigTarjeta
,O.NitEmpTrans,EmpTrans,CdRuta,Ruta,Referncia,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos
,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,TipPed,Pedido,IdCiaPed,TipGui,Guia,IdCiaGui,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact,AutzaMora,AutzaCupo
,Aprobacion,FechaAprob,DetalleAprob,CdUsuAprob,UA.Usuario AS UsuarioAprob,OrigenAdd,ZonaFrontera,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,Leyenda
,O.TimeSys AS Fec_Add,O.FecUpdate AS Fech_Update,IdCiaCrea
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,CLI.NumCuenta AS NumeroCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--vehiculo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarcaVeh,MV.Marca AS MarcaVeh,Modelo,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,CarrCapac,V.UndCapc AS UnidCapacidad,Comptmtos,CapComp
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,K.Descripcion AS KarDescripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,K.Remision AS KarRemision,K.IdCiaRem AS CdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev
,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,K.ListaPrec AS KarLtaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,M.Marca AS MarcaProd,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM  Trn_OComb AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCargue=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN Terceros AS TC ON O.IdConductor=TC.IdTercero
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON O.IdLocal=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN adm_Usuarios AS UA ON O.CdUsuAprob=UA.IdUsuario
 LEFT JOIN TercCndtores AS TCD ON O.IdConductor=TCD.IdConductor
 LEFT JOIN Vehiculos AS V ON O.IdVehiculo=V.IdVehiculo
 LEFT JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 LEFT JOIN Marcas AS MV ON V.IdMarca=MV.IdMarca
  WHERE O.TipDoc=@pmTipDoc AND OCargue BETWEEN @pmOCargueIni AND @pmOCargueFin AND O.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY OCargue,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCompra @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,OCompra,IdCia,Fecha,FechaVence,IdConcepto,IdProv,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobtasa,VrImpGlobal,VrNeto
,VrRetencion,VrReteICA,TarifaRet,TarifaIca,CodTarRet,CodTarIca,Cantidad,IdEmpresa,DirEntrega,IdLocEtga,DiasEntraga,NitContac,NomContac,TelContac,emlContac
,IdCCosto,IdSubCos,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans
,EmpTrans,LiqFletes,pVehiculo,CdConductor,TipEnt,Entrada,IdCiaEnt,NumAutoriza,Modalidad,Vigencia,OrigenAdd,Anulado,FecDev,Observacion,VrReteIVA,CodTarRiv
,NumAprob,FecAprob,CdUsuAprob,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_OCompra
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCompraDet @pmOCompra INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,CdTanque,Entradas,VrPrecio AS CostoBruto,Entradas*VrPrecio AS TotalCosto,TarifaDct,VrDctoEnt
,VrUnitario AS CostoNeto,Entradas*VrUnitario AS TotalCostNeto,TarifaIva,VrIvaEnt,CdCCosto,CdSubCos,K.Referencia AS Referncia,Unidades
,CodTarDct,CodTarIva,ImpGlobal,Servcios,Tanques,DescripProd,O.Fecha AS FecOrden,DiasEntraga,FechaVence,O.IdProv AS NitProvee,T.RazonSocial AS Proveedor
,Modalidad,Vigencia,IdCCosto,IdSubCos,TipEnt,Entrada,IdCiaEnt,K.CodTarRet AS CdTarifRet,K.TarifaRet AS TarifRet,VrReteEnt,K.CodTarIca AS CdTarifIca,K.TarifaIca AS TarifIca,VrIcaEnt
 FROM  Trn_OCompra AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCompra=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN Terceros AS T ON O.IdProv=T.IdTercero
 WHERE OCompra=@pmOCompra AND O.IdCia=@pmIdCia
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryOCompraEnt  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmOCompraIni INT=Null,@pmOCompraFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null
,@pmIdEmpresa VARCHAR(16)=Null,@pmIdLocEtga VARCHAR(8)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaEntIni SMALLDATETIME=Null,@pmFechaEntFin SMALLDATETIME=Null
 AS 
 SELECT  OCompra,O.IdCia AS CdCia,Compania,O.Fecha AS FechaOrden,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Entradas) AS CantOrden,SUM(Unidades) AS Unidades
--datos de entradas y dev.
,TipDocEnt,E.Documento AS NumDocEnt,CdCiaEnt,NomCiaEnt,FechaEnt,CantEntradas,CantSalidas,CostoUnitEnt
,TotalCostoEnt,TotalCostoSal,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,Unidad,O.IdProv AS NitProvee,T.RazonSocial AS NomProvee,Modalidad,O.IdConcepto AS CdConcepto,Concepto,IdEmpresa,TE.RazonSocial AS NomEmpresa
,IdLocEtga,LE.Localidad AS CiudadEntrega,LE.IdDep AS CdDepEntrega,DE.Departamento AS DptoEntrega,Vigencia,NumAprob,O.IdEstado AS CdEstado,Estado,O.Observacion AS Observ
,MAX(VrUnitario) AS OrdCostoUnit,SUM(VrCostoEnt) AS OrdCostoEnt,SUM(VrCostoSal) AS OrdCostoSal,SUM(VrIvaEnt) AS OrdIva,SUM(VrDctoEnt) AS OrdDcto
,MAX(CASE K.TipOrd WHEN 'REQ' THEN K.NumOrden ELSE 0 END) AS Requisicion
 FROM  Trn_OCompra AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCompra=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia INNER JOIN Terceros AS T ON O.IdProv=T.IdTercero INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto  INNER JOIN Terceros AS TE ON O.IdEmpresa=TE.IdTercero
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN Localidades AS LE ON O.IdLocEtga=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT  TipOrd,NumOrden,IdCiaOrd,K.TipDoc AS TipDocEnt,Documento,K.IdCia AS CdCiaEnt,KC.Compania AS NomCiaEnt,K.Fecha AS FechaEnt,K.IdProducto AS CdProducto
,PM.DescripProd AS NombreProd,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrUnitario) AS CostoUnitEnt,SUM(VrCostoEnt) AS TotalCostoEnt,SUM(VrCostoSal) AS TotalCostoSal
,SUM(VrIvaEnt) AS IvaEntradas,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoEnt) AS DctoEntradas,SUM(VrDctoSal) AS DctoSalidas,SUM(Unidades) AS TotalUnidades
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE TipDoc IN ('ENT', 'DVE', 'COM', 'DEI') AND TipOrd='ODC' AND NumOrden >0 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaEntIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaEntFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY  TipOrd,NumOrden,IdCiaOrd,K.TipDoc,Documento,K.IdCia,Compania,K.Fecha,K.IdProducto,DescripProd) AS E 
  ON O.TipDoc=E.TipOrd AND O.OCompra=E.NumOrden AND O.IdCia=E.IdCiaOrd AND K.IdProducto=E.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND O.IdProv LIKE ISNULL(@pmIdProv,'%') AND IdEmpresa LIKE ISNULL(@pmIdEmpresa,'%')
 AND IdLocEtga LIKE ISNULL(@pmIdLocEtga,'%')  AND O.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND OCompra  BETWEEN ISNULL(@pmOCompraIni,0) AND ISNULL(@pmOCompraFin,2147483647)
 GROUP BY OCompra,O.IdCia,Compania,O.Fecha,FechaVence,K.IdProducto,DescripProd
,TipDocEnt,E.Documento,CdCiaEnt,NomCiaEnt,FechaEnt,CantEntradas,CantSalidas,CostoUnitEnt
,TotalCostoEnt,TotalCostoSal,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,Unidad,O.IdProv,T.RazonSocial,Modalidad,O.IdConcepto,Concepto,IdEmpresa,TE.RazonSocial
,IdLocEtga,LE.Localidad,LE.IdDep,DE.Departamento,Vigencia,NumAprob,O.IdEstado,Estado,O.Observacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCompraLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmOCompraIni INT=Null,@pmOCompraFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT OCompra,IdCia,Fecha,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdProv,T.RazonSocial AS Proveedor,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobtasa,VrImpGlobal,VrNeto
,VrRetencion,VrReteICA,TarifaRet,TarifaIca,CodTarRet,CodTarIca,VrReteIVA,CodTarRiv,Cantidad,IdEmpresa,E.RazonSocial AS Empresa,DirEntrega,IdLocEtga,DiasEntraga,NitContac,NomContac,TelContac,emlContac,IdCCosto,IdSubCos,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans
,EmpTrans,LiqFletes,pVehiculo,CdConductor,TipEnt,Entrada,IdCiaEnt,NumAutoriza,Modalidad,Vigencia,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ,NumAprob,FecAprob,CdUsuAprob
,O.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario
 FROM Trn_OCompra AS O INNER JOIN Terceros AS T ON O.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS E ON O.IdEmpresa=E.IdTercero
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND OCompra BETWEEN ISNULL(@pmOCompraIni,0) AND ISNULL(@pmOCompraFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,OCompra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOCompraPen @pmEntrada INT,@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT OCompra,O.IdCia AS CdCia,Compania,Fecha,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdProv,T.RazonSocial AS Proveedor,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobtasa,VrImpGlobal,VrNeto
,Cantidad,IdEmpresa,E.RazonSocial AS Empresa,DirEntrega,IdLocEtga,DiasEntraga,NitContac,NomContac,TelContac,emlContac,O.IdCCosto AS CdCenCto,O.IdSubCos AS CdSubCen,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans
,EmpTrans,LiqFletes,pVehiculo,CdConductor,TipEnt,Entrada,IdCiaEnt,NumAutoriza,Modalidad,Vigencia,OrigenAdd,O.Observacion AS Observ,NumAprob,FecAprob,CdUsuAprob,O.IdEstado AS CdEstado,Estado,O.IdUsuario AS IdUsuari,Usuario
 FROM Trn_OCompra AS O INNER JOIN Terceros AS T ON O.IdProv=T.IdTercero
 INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS E ON O.IdEmpresa=E.IdTercero
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Formaspago AS F ON O.IdForma=F.IdForma
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  Anulado=0
 AND (Modalidad='PERMANENTE' or Entrada<=@pmEntrada)
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdProv LIKE ISNULL(@pmIdProv,'%')
 AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY O.IdCia,OCompra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryOCompraRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdEmpresa VARCHAR(16)=Null,@pmIdLocEtga VARCHAR(8)=Null,@pmIdCCosto VARCHAR(16)=Null
,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT TipDoc,OCompra,O.IdCia AS CdCia,Compania,Fecha,FechaVence,O.IdConcepto AS CdConcepto,Concepto
,O.IdProv AS NitProvee,T.RazonSocial AS NomProvee,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobtasa,VrImpGlobal,VrReteIVA,VrNeto,Cantidad
,VrRetencion,VrReteICA,TarifaRet,TarifaIca,CodTarRet,CodTarIca,CodTarRiv,IdEmpresa,TE.RazonSocial AS NomEmpresa,DirEntrega
,IdLocEtga,LE.Localidad AS CiudadEntrega,LE.IdDep AS CdDepEntrega,DE.Departamento AS DptoEntrega,DiasEntraga,O.NitContac AS Nit_Contacto,O.NomContac AS NombContacto,O.TelContac AS Tel_Contacto,O.emlContac AS Eml_Contacto
,O.IdCCosto AS CdCentCosto,CCosto,O.IdSubCos AS CdSubcost,SubCosto,O.IdForma AS CdForma,FormaPago,DetallePago
,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,O.CdMney AS OrdMoney,NitEmpTrans,EmpTrans,O.LiqFletes AS Liq_Fletes,pVehiculo
,CdConductor,TCD.RazonSocial AS Conductor,TipEnt,Entrada,IdCiaEnt,NumAutoriza,Modalidad,Vigencia,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ
,NumAprob,FecAprob,CdUsuAprob,O.IdEstado AS CdEstado,Estado,TimeSys,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercIdCiu,L.Localidad AS TercCiudad,L.IdDep AS TercIdDep,D.Departamento AS TercDpto
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,TP.NitContac AS ProvNitContact,TP.NomContac AS ProvNomContacto,TP.TelContac AS ProvTelContact,TP.emlContac AS ProvEmailContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
 FROM  Trn_OCompra AS  O INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON O.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN Terceros AS TE ON O.IdEmpresa=TE.IdTercero  INNER JOIN CentroCosto AS CC ON O.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Localidades AS LE ON O.IdLocEtga=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TCD ON O.CdConductor=TCD.IdTercero
 LEFT JOIN SubCentros AS SC ON O.IdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON O.IdProv=TP.IdProv
 LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND O.IdProv LIKE ISNULL(@pmIdProv,'%') AND IdEmpresa LIKE ISNULL(@pmIdEmpresa,'%')
 AND IdLocEtga LIKE ISNULL(@pmIdLocEtga,'%') AND O.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,OCompra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryOCompra_Cr @pmTipDoc VARCHAR(3),@pmOCompraIni INT,@pmOCompraFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc AS Tip_Doc,TipoDoc,OCompra,O.IdCia AS CdCia,Compania,O.Fecha AS FechaDoc,FechaVence,O.IdConcepto AS CdConcepto,Concepto
,O.IdProv AS NitProvee,T.RazonSocial AS NomProvee,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobtasa,VrImpGlobal,VrNeto,Cantidad
,VrRetencion,VrReteICA,VrReteIVA,CodTarRiv,O.TarifaRet AS OrdTarifRet,O.TarifaIca AS OrdTarifIca,O.CodTarRet AS OrdCodTarRef,O.CodTarIca AS OrdCodTarIca,IdEmpresa,TE.RazonSocial AS NomEmpresa,DirEntrega
,IdLocEtga,LE.Localidad AS CiudadEntrega,LE.IdDep AS CdDepEntrega,DE.Departamento AS DptoEntrega,DiasEntraga,O.NitContac AS Nit_Contacto,O.NomContac AS NombContacto,O.TelContac AS Tel_Contacto,O.emlContac AS Eml_Contacto
,O.IdCCosto AS CdCentCosto,CC.CCosto AS CentCosto,O.IdSubCos AS CdSubcost,SC.SubCosto AS SubcCosto,O.IdForma AS CdForma,FormaPago,DetallePago
,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,O.CdMney AS CodMoney,NitEmpTrans,EmpTrans,O.LiqFletes AS Liq_Fletes,O.pVehiculo AS OrdPlacaVeh
,CdConductor,TCD.RazonSocial AS Conductor,TipEnt,Entrada,IdCiaEnt,NumAutoriza,Modalidad,Vigencia,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ
,NumAprob,FecAprob,CdUsuAprob,O.IdEstado AS CdEstado,Estado,O.TimeSys AS Fec_Add,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercIdCiu,L.Localidad AS TercCiudad,L.IdDep AS TercIdDep,D.Departamento AS TercDpto
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercEnte
--Información del proveedor
,TP.IdGrupo AS ProvGrupo,GrupoClie,TP.NitContac AS ProvNitContact,TP.NomContac AS ProvNomContacto,TP.TelContac AS ProvTelContact,TP.emlContac AS ProvEmailContac,CargContac,DirOrdComp,DiasEntga
,TP.IdClase AS CodClase,NumCuenta,TP.IdBanco AS CdBanco,Banco,Autoret
--detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS PlacaVeh,K.Referencia AS KarReferencia,Descripcion,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,Remision,IdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM  Trn_OCompra AS  O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.OCompra=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON O.IdProv=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN Terceros AS TE ON O.IdEmpresa=TE.IdTercero  INNER JOIN CentroCosto AS CC ON O.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN Localidades AS LE ON O.IdLocEtga=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TCD ON O.CdConductor=TCD.IdTercero
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON O.IdSubCos=SC.IdSubCos
 LEFT JOIN TercProvee AS TP ON O.IdProv=TP.IdProv
 LEFT JOIN GruposCli AS GP ON TP.IdGrupo=GP.IdGrupo
 LEFT JOIN Bancos AS BP ON TP.IdBanco=BP.IdBanco
 WHERE O.TipDoc=@pmTipDoc AND OCompra BETWEEN @pmOCompraIni AND @pmOCompraFin  AND O.IdCia=@pmIdCia
 ORDER BY OCompra,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryObjetosApp @pmIdObj VARCHAR(10)
 AS 
 SELECT IdObj,IdGrupo,SubModulo,IndObj,Nombre,Formulario,Permisos,NivelMinimo,NomArchivo
 FROM Sys_ObjetosApp
 WHERE IdObj=@pmIdObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryObjetosAppLta @pmNivelMinimo INT,@pmIdGrupo VARCHAR(6)=Null
,@pmSubModulo VARCHAR(3)=Null,@pmnModulo VARCHAR(10)=Null
 AS 
 SELECT O.IdGrupo AS IdGrup,GrupoObjeto,SubModulo,IndObj,IdObj,Nombre,Formulario,Permisos,NivelMinimo,NomArchivo
 FROM Sys_ObjetosApp AS O INNER JOIN Sys_GruposObj AS G ON O.IdGrupo=G.IdGrupo
 WHERE NivelMinimo<=@pmNivelMinimo AND O.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') 
 AND SubModulo LIKE ISNULL(@pmSubModulo,'%') AND nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY nModulo,O.IdGrupo,SubModulo,IndObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryObjetosAppSub @pmIdGrupo VARCHAR(6)=Null
 AS
 SELECT   SubModulo,Count(IndObj) AS CantInd
 FROM     Sys_ObjetosApp 
 WHERE IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 GROUP BY SubModulo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpciones @pmIdOpc VARCHAR(3),@pmnModulo VARCHAR(10)=Null
 AS 
 SELECT IdOpc,Opcion,TipoDato,Valor,NivUp,nModulo
 FROM adm_Opciones
 WHERE IdOpc LIKE ISNULL(@pmIdOpc,'%') AND nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY IdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpcionesLta @pmnModulo VARCHAR(10)=Null
 AS 
 SELECT IdOpc,Opcion,TipoDato,Valor,NivUp,nModulo
 FROM adm_Opciones
 WHERE nModulo='GENERAL' Or  nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY IdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedido @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Pedido,IdCia,Fecha,FechaVence,IdConcepto,IdCliente,IdAgencia,IdClieFact,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa
,VrImpGlobal,VrNeto,Cantidad,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,LugarEnvio,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma
,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans,AsignarVeh,pVehiculo,CdConductor,CdRuta,ListaPrec,RefPedido,Modalidad,Vigencia,NumAutoriza,NumAutCupo
,NumAutCheq,NumAprob,IdCiaApr,FecAprob,DetalleAprob,CdUsuAprob,TipFac,Factura,IdCiaFac,FechaFact,TipRem,Remision,IdCiaRem,FechaRem,OrigenAdd,Anulado
,FecDev,ZonaFrontera,TipoTrans,TipoOrden,TipoModifica,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Opedido
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoAct @pmIdCliente VARCHAR(16),@pmIdCia CHAR(2),@pmModalidad VARCHAR(10)
 AS 
 SELECT TipDoc,Pedido,IdCia,Fecha,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos
,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto,Factura,NumAprob
 FROM Trn_Opedido
 WHERE IdEstado='0001' AND IdCliente=@pmIdCliente AND Anulado=0 AND NumAprob>0 
 AND (Factura<=0 OR Vigencia='PERMANENTE')
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY Pedido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoDet @pmPedido INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,Salidas,VrPrecio,Salidas*VrPrecio AS VrSubTotal
,TarifaDct,VrDctoSal,TarifaIva,VrIvaSal,((Salidas*VrPrecio)-VrDctoSal)+VrIvaSal AS VrTotal,VrBruto,K.ListaPrec AS LtaPrec,VrBase,K.Referencia AS Referncia,Referencia2,Unidades
,CodTarDct,CodTarIva,K.IdVend AS NitVend,V.RazonSocial AS Vendedor,Comision,K.CodTarCom AS CdTarifCom,DescripProd,Servcios,Tanques,CdTanque,CdMoneda,VrTasaCamb
,O.Fecha AS FecPedido,FechaVence,DiasEntraga,IdCliente,T.RazonSocial AS Cliente,IdAgencia,Modalidad,Vigencia,O.Factura AS NumFactura,IdCiaFac,FechaFact
,O.Remision AS NumRemision,O.IdCiaRem AS CiaRem,FechaRem
 FROM  Trn_Opedido AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Pedido=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero
 INNER JOIN Terceros AS  V ON K.IdVend=V.IdTercero
 WHERE Pedido=@pmPedido AND O.IdCia=@pmIdCia
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoFac  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmPedidoIni INT=Null,@pmPedidoFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaFacIni SMALLDATETIME=Null,@pmFechaFacFin SMALLDATETIME=Null
,@pmTipoVentaIni INT=Null,@pmTipoVentaFin INT=Null
 AS 
 SELECT  Pedido,O.IdCia AS CdCia,Compania,O.Fecha AS FechaPed,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantPedido,SUM(Unidades) AS Unidades
--datos de factura
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,OrdCargue,CiaOrdCargue,FA.Remision AS FacRemision,FA.IdCiaRem AS FacCiaRem
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Modalidad,O.IdConcepto AS CdConcepto,Concepto
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Vigencia,NumAprob,O.Remision AS PedRemision,O.IdCiaRem AS PedCiaRem,O.IdEstado AS CdEstado,Estado,O.Observacion AS Observ,DetallePago
,MAX(VrPrecio) AS OrdValorUnit,SUM(Salidas*VrPrecio) AS OrdValorTotal,SUM(VrIvaSal) AS OrdIva,SUM(VrDctoSal) AS OrdDcto
 FROM Trn_Opedido AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia   INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado  INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Pedido=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT  CASE TipOrd WHEN 'PED' THEN TipOrd ELSE 'PED' END AS TipoPed,CASE TipOrd WHEN 'PED' THEN NumOrden ELSE Cotizacion END AS NumPedido
,CASE TipOrd WHEN 'PED' THEN IdCiaOrd ELSE IdCiaCot END AS IdCiaPedido,K.TipDoc AS TipoFact,K.Documento AS NumFactura,K.IdCia AS CdCiaFac,KC.Compania AS NomCiaFac
,K.Fecha AS FecFactura,CASE TipOrd WHEN 'OCC' THEN  NumOrden ELSE 0 END AS OrdCargue,CASE TipOrd WHEN 'OCC' THEN  IdCiaOrd ELSE '00' END AS CiaOrdCargue
,Remision,IdCiaRem,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd
,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaEnt) AS IvaEntradas
,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoEnt) AS DctoEntradas,SUM(VrDctoSal) AS DctoSalidas,SUM(K.Unidades) AS TotalUnidades
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE NoVentas BETWEEN ISNULL(@pmTipoVentaIni,1) AND ISNULL(@pmTipoVentaFin,4)  AND TipOrd IN ('PED','OCC') 
 AND (CASE TipOrd WHEN 'PED' THEN NumOrden ELSE Cotizacion END)>0 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaFacIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFacFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY  CASE TipOrd WHEN 'PED' THEN TipOrd ELSE 'PED' END,CASE TipOrd WHEN 'PED' THEN NumOrden ELSE Cotizacion END
,CASE TipOrd WHEN 'PED' THEN IdCiaOrd ELSE IdCiaCot END,K.TipDoc,K.Documento,K.IdCia,KC.Compania,K.Fecha,CASE TipOrd WHEN 'OCC' THEN  NumOrden ELSE 0 END
,CASE TipOrd WHEN 'OCC' THEN  IdCiaOrd ELSE '00' END,Remision,IdCiaRem,K.IdProducto,PM.DescripProd) AS FA 
 ON O.TipDoc=FA.TipoPed AND O.Pedido=FA.NumPedido AND O.IdCia=FA.IdCiaPedido AND K.IdProducto=FA.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Pedido  BETWEEN ISNULL(@pmPedidoIni,0) AND ISNULL(@pmPedidoFin,2147483647)
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY  Pedido,O.IdCia,Compania,O.Fecha,FechaVence,K.IdProducto,DescripProd
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,OrdCargue,CiaOrdCargue,FA.Remision,FA.IdCiaRem
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,Modalidad,O.IdConcepto,Concepto
,O.IdVend,VN.RazonSocial,O.IdLocEnv,LE.Localidad,LE.IdDep,DE.Departamento
,Vigencia,NumAprob,O.Remision,O.IdCiaRem,O.IdEstado,Estado,O.Observacion,DetallePago
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmPedidoIni INT=Null,@pmPedidoFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmVigencia VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS
 SELECT Pedido,IdCia,Fecha,FechaVence,P.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,IdClieFact,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto
,VrCargos,VrSobretasa,VrImpGlobal,VrNeto,Cantidad,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,LugarEnvio,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac
,IdForma,DetallePago,MulPlazos,IdPlazo,CdMney,TipoTrans,NitEmpTrans,EmpTrans,pVehiculo,AsignarVeh,CdConductor,CdRuta,ListaPrec,RefPedido,TipFac,Factura,IdCiaFac,FechaFact,TipRem,Remision,IdCiaRem,FechaRem
,NumAutoriza,NumAutCupo,NumAutCheq,Modalidad,Vigencia,NumAprob,IdCiaApr,FecAprob,CdUsuAprob,DetalleAprob,TipoOrden,TipoModifica,OrigenAdd,ZonaFrontera,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,P.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Opedido AS P INNER JOIN Terceros AS T ON P.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON P.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON P.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Pedido BETWEEN ISNULL(@pmPedidoIni,0) AND ISNULL(@pmPedidoFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND Vigencia LIKE ISNULL(@pmVigencia,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Pedido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoOcc  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmPedidoIni INT=Null,@pmPedidoFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaOrdIni SMALLDATETIME=Null,@pmFechaOrdFin SMALLDATETIME=Null
,@pmFacturaIni INT=Null,@pmFacturaFin INT=Null
 AS 
 SELECT  Pedido,O.IdCia AS CdCia,Compania,O.Fecha AS FechaPed,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantPedido,SUM(Unidades) AS Unidades
--datos de orden cargue
,OC.Documento AS OrdenCargue,CdCiaOcc,NomCiaOcc,FechaOcc,CantSalidas,VrPrecioUnit,IvaSalidas,DctoSalidas,TotalUnidades,TarifSobTasa,TarifSoldicom,TarifIglobal
,OccReferncia,Guia,IdCiaGui,OccNumFactura,OccCiaFact,OccFechaFact
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Modalidad,O.IdConcepto AS CdConcepto,Concepto
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Vigencia,NumAprob,O.Remision AS NRemision,O.IdCiaRem AS CdCiaRem,FechaRem,O.Factura AS NumFactura,IdCiaFac,FechaFact,O.IdEstado AS CdEstado,Estado,O.Observacion AS Observ,DetallePago
,MAX(VrPrecio) AS OrdValorUnit,SUM(Salidas*VrPrecio) AS OrdValorTotal,SUM(VrIvaSal) AS OrdIva,SUM(VrDctoSal) AS OrdDcto
 FROM Trn_Opedido AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia   INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado  INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Pedido=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT  TipOrd,NumOrden,IdCiaOrd,Documento,K.IdCia AS CdCiaOcc,KC.Compania AS NomCiaOcc,K.Fecha AS FechaOcc,K.IdProducto AS CdProducto
,PM.DescripProd AS NombreProd,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoSal) AS DctoSalidas
,SUM(Unidades) AS TotalUnidades,MAX(Sobretasa) AS TarifSobTasa,MAX(Soldicom) AS TarifSoldicom,MAX(ImpGlobal) AS TarifIglobal
,O.Referncia AS OccReferncia,Guia,IdCiaGui,O.Factura AS OccNumFactura,O.IdCiaFac AS OccCiaFact,O.FechaFact AS OccFechaFact
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN Trn_OComb AS O ON K.TipDoc=O.TipDoc AND K.Documento=O.OCargue AND K.IdCia=O.IdCia
 WHERE  K.TipDoc='OCC' AND TipOrd='PED' AND NumOrden >0 AND Salidas>0 AND Anulado=0 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (O.Fecha>=ISNULL(@pmFechaOrdIni,CAST('19100101' AS SMALLDATETIME)) AND O.Fecha<=ISNULL(@pmFechaOrdFin,CAST('20781230' AS SMALLDATETIME)))
 AND O.Factura BETWEEN ISNULL(@pmFacturaIni,-1) AND ISNULL(@pmFacturaFin,2147483647)
 GROUP BY  TipOrd,NumOrden,IdCiaOrd,Documento,K.IdCia,KC.Compania,K.Fecha,K.IdProducto,PM.DescripProd,O.Referncia,Guia,IdCiaGui,O.Factura,O.IdCiaFac,O.FechaFact ) AS OC
  ON O.TipDoc=OC.TipOrd AND O.Pedido=OC.NumOrden AND O.IdCia=OC.IdCiaOrd AND K.IdProducto=OC.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Pedido  BETWEEN ISNULL(@pmPedidoIni,0) AND ISNULL(@pmPedidoFin,2147483647)
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY Pedido,O.IdCia,Compania,O.Fecha,FechaVence,K.IdProducto,DescripProd
,OC.Documento,CdCiaOcc,NomCiaOcc,FechaOcc,CantSalidas,VrPrecioUnit,IvaSalidas,DctoSalidas,TotalUnidades,TarifSobTasa,TarifSoldicom,TarifIglobal
,OccReferncia,Guia,IdCiaGui,OccNumFactura,OccCiaFact,OccFechaFact,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,Modalidad
,O.IdConcepto,Concepto,O.IdVend,VN.RazonSocial,O.IdLocEnv
,LE.Localidad,LE.IdDep,DE.Departamento,Vigencia,NumAprob,O.Remision,O.IdCiaRem,FechaRem,O.Factura,IdCiaFac,FechaFact
,O.IdEstado,Estado,O.Observacion,DetallePago
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoPen @pmFactura INT,@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Pedido,P.IdCia AS CodCia,Compania,Fecha,DiasEntraga,FechaVence,P.IdConcepto AS CodConcepto,Concepto,IdCliente,T.RazonSocial AS Cliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto
,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,IdVend,V.RazonSocial AS Vendedor,DirEnvio,IdLocEnv,Localidad,NitContac,NomContac,TelContac,emlContac,CargoContac
,P.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,CdRuta,ListaPrec,TipFac,Factura,IdCiaFac,FechaFact
,TipRem,Remision,IdCiaRem,FechaRem,NumAutoriza,Modalidad,Vigencia,OrigenAdd,ZonaFrontera,TipoTrans,TipoOrden,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CodEstado,Estado,P.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Opedido AS P  INNER JOIN Terceros AS T ON  P.IdCliente=T.IdTercero
 INNER JOIN  Conceptos AS C ON P.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS V ON P.IdVend=V.IdTercero
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia
 INNER JOIN Formaspago AS F ON P.IdForma=F.IdForma
 INNER JOIN Localidades AS L ON P.IdLocEnv=L.IdLocal
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  Anulado=0
 AND (Modalidad='PERMANENTE' or Factura<=@pmFactura)
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY P.IdCia,Pedido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Pedido,O.IdCia AS CdCia,Compania,Fecha,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,IdClieFact,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto,Cantidad
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,LugarEnvio,DiasEntraga
,O.NitContac AS NitContacto,O.NomContac AS NomContacto,O.TelContac AS TelContacto,O.emlContac AS EmailContacto,CargoContac,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago
,NitEmpTrans,EmpTrans,AsignarVeh,O.pVehiculo AS PlacaVeh,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta,RefPedido,TipFac,Factura,IdCiaFac,FechaFact
,TipRem,Remision,IdCiaRem,FechaRem,NumAutoriza,Modalidad,Vigencia,NumAprob,IdCiaApr,FecAprob,CdUsuAprob,DetalleAprob,OrigenAdd,Anulado,FecDev,O.Observacion AS Observ
,O.IdEstado AS CdEstado,Estado,ZonaFrontera,TipoTrans,TipoOrden,TipoModifica,TimeSys,O.FecUpdate AS Fech_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Opedido AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON O.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,Pedido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoRem @pmRemision INT,@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Pedido,P.IdCia AS CodCia,T.RazonSocial AS Cliente,IdCliente,IdAgencia,pVehiculo,CdConductor,Fecha
,DiasEntraga,FechaVence,P.IdConcepto AS CodConcepto,Concepto,VrSubTotal,VrDescuento,VrImpuesto
,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,IdVend,V.RazonSocial AS Vendedor,DirEnvio,IdLocEnv,Localidad,NitContac,NomContac,TelContac,emlContac,CargoContac
,P.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,IdPlazo,CdMney,NitEmpTrans,EmpTrans,CdRuta,ListaPrec,Compania
,TipFac,Factura,IdCiaFac,FechaFact,NumAutoriza,Modalidad,Vigencia,OrigenAdd,Anulado,FecDev,P.Observacion AS Observ,P.IdEstado AS CodEstado,Estado,P.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Opedido AS P  INNER JOIN Terceros AS T ON  P.IdCliente=T.IdTercero
 INNER JOIN  Conceptos AS C ON P.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario 
 INNER JOIN Terceros AS V ON P.IdVend=V.IdTercero
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia
 INNER JOIN Formaspago AS F ON P.IdForma=F.IdForma
 INNER JOIN Localidades AS L ON P.IdLocEnv=L.IdLocal
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND  Anulado=0 AND Factura<=0
 AND (Modalidad='PERMANENTE' or Remision<=@pmRemision)
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY P.IdCia,Pedido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoRms  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmPedidoIni INT=Null,@pmPedidoFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaOrdIni SMALLDATETIME=Null,@pmFechaOrdFin SMALLDATETIME=Null
,@pmFacturaIni INT=Null,@pmFacturaFin INT=Null
 AS 
 SELECT  Pedido,O.IdCia AS CdCia,Compania,O.Fecha AS FechaPed,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantPedido,SUM(Unidades) AS Unidades
--datos de remision
,RM.Documento AS NRemision,CdCiaRem,NomCiaRem,FecRemision,CantSalidas,VrPrecioUnit,IvaSalidas,DctoSalidas
,TotalUnidades,FechaDesp,FecVceRem,RM.VrFletes AS VrFletesRem,RM.IdLocEnv AS CdCiudadRem,RM.Localidad AS CiudadRem,RM.NumAprob AS RemNumAprob,RemFactura,RM.IdCiaFac AS RemCiaFact,RM.FechaFact AS RemFechaFact
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Modalidad,O.IdConcepto AS CdConcepto,Concepto
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Vigencia,O.NumAprob AS PedNumAprob,O.Factura AS PedNFactura,O.IdCiaFac AS PedCiaFact,O.FechaFact AS PedFechaFac,O.IdEstado AS CdEstado,Estado,O.Observacion AS Observ,DetallePago
,MAX(VrPrecio) AS OrdValorUnit,SUM(Salidas*VrPrecio) AS OrdValorTotal,SUM(VrIvaSal) AS OrdIva,SUM(VrDctoSal) AS OrdDcto
 FROM Trn_Opedido AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia   INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado  INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Pedido=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT  TipOrd,NumOrden,IdCiaOrd,Documento,K.IdCia AS CdCiaRem,KC.Compania AS NomCiaRem,K.Fecha AS FecRemision,K.IdProducto AS CdProducto
,PM.DescripProd AS NombreProd,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoSal) AS DctoSalidas
,SUM(K.Unidades) AS TotalUnidades,FechaDesp,R.FechaVence AS FecVceRem,VrFletes,IdLocEnv,Localidad,NumAprob,R.Factura AS RemFactura,IdCiaFac,FechaFact
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN Trn_Remision AS R ON K.TipDoc=R.TipDoc AND K.Documento=R.Remision AND K.IdCia=R.IdCia
 LEFT JOIN  Localidades AS LE ON R.IdLocEnv=LE.IdLocal 
 WHERE  K.TipDoc='REM' AND TipOrd='PED' AND NumOrden>0 AND  Anulado=0 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (R.Fecha>=ISNULL(@pmFechaOrdIni,CAST('19100101' AS SMALLDATETIME)) AND R.Fecha<=ISNULL(@pmFechaOrdFin,CAST('20781230' AS SMALLDATETIME)))
 AND R.Factura BETWEEN ISNULL(@pmFacturaIni,-1) AND ISNULL(@pmFacturaFin,2147483647)
 GROUP BY  TipOrd,NumOrden,IdCiaOrd,Documento,K.IdCia,KC.Compania,K.Fecha,K.IdProducto,PM.DescripProd
 ,FechaDesp,FechaVence,VrFletes,IdLocEnv,Localidad,NumAprob,R.Factura,IdCiaFac,FechaFact) AS RM
  ON O.TipDoc=RM.TipOrd AND O.Pedido=RM.NumOrden AND O.IdCia=RM.IdCiaOrd AND K.IdProducto=RM.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Pedido  BETWEEN ISNULL(@pmPedidoIni,0) AND ISNULL(@pmPedidoFin,2147483647)
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY Pedido,O.IdCia,Compania,O.Fecha,FechaVence,K.IdProducto,DescripProd,RM.Documento
,CdCiaRem,NomCiaRem,FecRemision,CantSalidas,VrPrecioUnit,IvaSalidas,DctoSalidas,TotalUnidades,FechaDesp
,FecVceRem,RM.VrFletes,RM.IdLocEnv,RM.Localidad,RM.NumAprob,RemFactura,RM.IdCiaFac,RM.FechaFact
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,Modalidad,O.IdConcepto,Concepto,O.IdVend,VN.RazonSocial,O.IdLocEnv
,LE.Localidad,LE.IdDep,DE.Departamento,Vigencia,O.NumAprob,O.Remision,O.IdCiaRem,FechaRem,O.Factura,O.IdCiaFac,O.FechaFact
,O.IdEstado,Estado,O.Observacion,DetallePago
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedidoSal @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmPedidoIni INT=Null,@pmPedidoFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaSalIni SMALLDATETIME=Null,@pmFechaSalFin SMALLDATETIME=Null
 AS 
 SELECT  Pedido,O.IdCia AS CdCia,Compania,O.Fecha AS FechaPed,FechaVence,K.IdProducto AS CodProducto,DescripProd,SUM(Salidas) AS CantPedido,SUM(Unidades) AS Unidades
--datos de salidas
,TipoSalida,NumSalida,CdCiaSal,NomCiaSal,FecSalida,CdProducto,NombreProd,CantEntradas,CantSalidas,ValorUnit,CostoEntradas,CostoSalidas,IvaEntradas,IvaSalidas,TotalUnidades
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Modalidad,O.IdConcepto AS CdConcepto,Concepto
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,Vigencia,NumAprob,O.Remision AS PedRemision,O.IdCiaRem AS PedCiaRem,O.IdEstado AS CdEstado,Estado,O.Observacion AS Observ,DetallePago
,MAX(VrPrecio) AS OrdValorUnit,SUM(Salidas*VrPrecio) AS OrdValorTotal,SUM(VrIvaSal) AS OrdIva,SUM(VrDctoSal) AS OrdDcto
 FROM Trn_Opedido AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia   INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado  INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Pedido=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT TipOrd,NumOrden,IdCiaOrd,K.TipDoc AS TipoSalida,K.Documento AS NumSalida,K.IdCia AS CdCiaSal,KC.Compania AS NomCiaSal
,K.Fecha AS FecSalida,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd
,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrUnitario) AS ValorUnit,SUM(VrCostoEnt) AS CostoEntradas,SUM(VrCostoSal) AS CostoSalidas
,SUM(VrIvaEnt) AS IvaEntradas,SUM(VrIvaSal) AS IvaSalidas,SUM(K.Unidades) AS TotalUnidades
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE TipDoc IN ('SAL','DVS') AND TipOrd='PED' AND NumOrden>0  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaSalIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaSalFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY  TipOrd,NumOrden,IdCiaOrd,K.TipDoc,K.Documento,K.IdCia,KC.Compania,K.Fecha,K.IdProducto,PM.DescripProd) AS SD 
 ON O.TipDoc=SD.TipOrd AND O.Pedido=SD.NumOrden AND O.IdCia=SD.IdCiaOrd AND K.IdProducto=SD.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Pedido  BETWEEN ISNULL(@pmPedidoIni,0) AND ISNULL(@pmPedidoFin,2147483647)
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY  Pedido,O.IdCia,Compania,O.Fecha,FechaVence,K.IdProducto,DescripProd
,TipoSalida,NumSalida,CdCiaSal,NomCiaSal,FecSalida,CdProducto,NombreProd,CantEntradas,CantSalidas,ValorUnit,CostoEntradas,CostoSalidas,IvaEntradas,IvaSalidas,TotalUnidades
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,Modalidad,O.IdConcepto,Concepto
,O.IdVend,VN.RazonSocial,O.IdLocEnv,LE.Localidad,LE.IdDep,DE.Departamento
,Vigencia,NumAprob,O.Remision,O.IdCiaRem,O.IdEstado,Estado,O.Observacion,DetallePago
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryOpedido_Cr @pmTipDoc VARCHAR(3),@pmPedidoIni INT,@pmPedidoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT O.TipDoc AS Tip_Doc,TipoDoc,Pedido,O.IdCia AS CdCia,Compania,O.Fecha AS FechaDoc,FechaVence,O.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,IdClieFact,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrCargos,VrOtrDcto,VrSobretasa,VrImpGlobal,VrNeto,Cantidad
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,O.IdLocEnv AS IdCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio,DiasEntraga,LugarEnvio
,O.NitContac AS NitContacto,O.NomContac AS NomContacto,O.TelContac AS TelContacto,O.emlContac AS EmailContacto,CargoContac,O.IdForma AS CdForma,FormaPago,DetallePago,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago
,NitEmpTrans,EmpTrans,O.pVehiculo AS PlacaVeh,AsignarVeh,CdConductor,TC.RazonSocial AS Conductor,CdRuta,Ruta,RefPedido,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact
,TipRem,O.Remision AS NumRemision,O.IdCiaRem AS CdCiaRem,FechaRem,NumAutoriza,NumAutCupo,NumAutCheq,Modalidad,Vigencia,NumAprob,IdCiaApr,FecAprob,DetalleAprob,CdUsuAprob,OrigenAdd,ZonaFrontera
,TipoTrans,TipoOrden,TipoModifica,Anulado,FecDev,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.TimeSys AS Fech_Add,O.FecUpdate AS Fech_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,K.Remision AS KarRemision,K.IdCiaRem AS CdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev
,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,Unidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,K.ListaPrec AS KarLtaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_Opedido AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Pedido=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal  LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON O.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE O.TipDoc=@pmTipDoc AND Pedido BETWEEN @pmPedidoIni AND @pmPedidoFin AND O.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Pedido,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,IdUsuario
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosAcu @pmFecha SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT TipDoc,IdCia,IdUsuario,SUM(VrPagado-VrCambio) AS STOTPAG
 FROM Trn_Pagos
 WHERE Fecha=@pmFecha AND EsCaja<>0  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY TipDoc,IdCia,IdUsuario
 ORDER BY IdCia,IdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosChe @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND (IdForma IN ('CHE','BON','CON','TRA') OR IdForma LIKE 'CH%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosCns @pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdCia CHAR(2)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmIdForma VARCHAR(4)=Null,@pmNitCliente VARCHAR(16)=Null,@pmCdAgencia VARCHAR(16)=Null
,@pmDocIni INT=Null,@pmDocFin INT=Null
 AS 
 SELECT TipDoc,Documento,IdCia,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,P.IdBanco AS CdBanco,Banco,NumForma,Fecha,FecForma,CtaForma
,Beneficiario,Referncia1,Referncia2,NumAutoriza,NitCliente,RazonSocial,CdAgencia,CdLocal,Localidad,CdCta
,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,EsCaja,P.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Pagos AS P INNER JOIN Formaspago AS F ON P.IdForma=F.IdForma
 INNER JOIN  Bancos AS B ON P.IdBanco=B.IdBanco  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN Localidades AS L ON P.CdLocal=L.IdLocal
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero
 WHERE TipDoc IN ('REC','FCO','FO1','FO2','FO3','FO4','FO5','PLA','RCO','RC1','RC2' )
 AND ((TipRef='CNS' AND DocRef<=0) OR (TipRef='EGO' AND DocRef<=0) OR (TipRef<>'CNS' AND TipRef<>'EGO')  )
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND P.IdForma LIKE ISNULL(@pmIdForma,'%')
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%') AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')
 AND Documento BETWEEN ISNULL(@pmDocIni,0) AND ISNULL(@pmDocFin,2147483647)
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY IdCia,TipDoc,Documento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosDsd @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS CodBanco,Banco,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,Referncia2,NumAutoriza,NitCliente,CdAgencia,CdLocal,TipDenom,VrDenom,Cantidad
,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,P.IdUsuario AS IdUsuari,Usuario,TipDoc,Documento,IdCia,Fecha
 FROM Trn_Pagos AS P INNER JOIN Formaspago AS F ON P.IdForma=F.IdForma
 INNER JOIN  Bancos AS B ON P.IdBanco=B.IdBanco
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosFdvc  @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCiaDoc CHAR(2)
,@pmIdForma VARCHAR(4),@pmIdBanco VARCHAR(4),@pmNumForma VARCHAR(20)
 AS 
 SELECT  TipDev,Devolucion,D.IdCia AS CodCia,D.Fecha AS FechDev,Consignacion,IdCiaDoc,FecDoc
,Item,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,P.Referncia2 AS Refncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta
 FROM  Trn_DevCns AS D INNER JOIN  Trn_Pagos AS P ON D.TipDev=P.TipDoc AND D.Devolucion=P.Documento AND D.IdCia=P.IdCia
 WHERE D.TipDoc=@pmTipDoc  AND Consignacion=@pmConsignacion AND IdCiaDoc=@pmIdCiaDoc
 AND IdForma=@pmIdForma AND IdBanco=@pmIdBanco AND NumForma=@pmNumForma
 ORDER BY D.IdCia,Devolucion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosFor @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmIdBanco VARCHAR(4)
 AS 
 SELECT Item,Fecha,IdForma,Detalle,VrPagado,VrCambio,EsCaja,IdBanco,NitCliente,CdAgencia,NumForma,FecForma
,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef,ItemDoc,CdCta,IdUsuario
 FROM Trn_Pagos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND IdForma=@pmIdForma  AND NumForma=@pmNumForma AND IdBanco=@pmIdBanco
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagosTot @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT TipDoc,SUM(CASE EsCaja WHEN 1 THEN VrPagado-VrCambio ELSE 0 END)  AS  STOTCAJA
,SUM(CASE EsCaja WHEN 0 THEN VrPagado-VrCambio ELSE 0 END)  AS  STOTOTROS
 FROM Trn_Pagos
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 GROUP BY TipDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagos_Bus @pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdForma VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmEsCaja BIT=Null,@pmIdBanco VARCHAR(4)=Null
,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmNitCliente VARCHAR(16)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT P.TipDoc AS CdTipDoc,TipoDoc,Documento,P.IdCia AS CodCia,Compania,P.Fecha AS FechaRec,Item,P.IdForma AS CdForma,FormaPago,Detalle
,VrPagado,VrCambio,EsCaja,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NumForma,FecForma,CtaForma,Beneficiario
,NitCliente,T.RazonSocial AS NomCliente,CdAgencia,Referncia1,Referncia2,NumAutoriza,P.CdLocal AS CodCiudad,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza
,DF.Departamento AS DptoPlaza,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco,VrDenom,Cantidad,TipDenom
,TipRef,DocRef,IdCiaRef,ItemDoc,P.IdUsuario AS IdUsuari,Usuario
--datos de recibo de caja
,R.IdVehiculo AS PlacaVeh,NumVeh,R.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,Modalidad,TipCar,Cartulina,IdCiaCar,R.CentInicial AS CentenaIni,R.CentFinal AS CentenaFin
--Información del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDV,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,L.IdDep AS TercCdDep,D.Departamento AS TercDpto
 FROM  Trn_Pagos AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero 
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN  Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 LEFT JOIN Trn_RecBuses AS R ON P.TipDoc=R.TipDoc AND P.Documento=R.Recibo AND P.IdCia=R.IdCia
 LEFT JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo LEFT JOIN Terceros AS NC ON R.IdConductor =NC.IdTercero
 WHERE  P.Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.TipDoc IN ('RC1','OD1','CNS','DVC','RCO','DRC') AND P.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdForma  LIKE ISNULL(@pmIdForma,'%') AND P.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND  (EsCaja=ISNULL(@pmEsCaja,0) or EsCaja=ISNULL(@pmEsCaja,1))
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,0) AND ISNULL(@pmDocumentoFin,2147483647)
 AND  NitCliente LIKE ISNULL(@pmNitCliente,'%') AND P.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 ORDER BY P.TipDoc,P.IdCia,Documento,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagos_Cr @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,TipoDoc,Documento,P.IdCia AS CodCia,Compania,Fecha,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja
,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NitCliente,RazonSocial,CdAgencia,Agencia,CodAgencia,A.Referencia AS RefeAgencia,NumForma
,FecForma,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco,P.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS TerCodigo,NomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento,Telefono,e_mail
 FROM  Trn_Pagos AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN  Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 WHERE P.TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND P.IdCia=@pmIdCia
 ORDER BY Documento,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagos_Lta @pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdForma VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmEsCaja BIT=Null,@pmIdBanco VARCHAR(4)=Null
,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmNitCliente VARCHAR(16)=Null,@pmIdUsuario VARCHAR(11)=Null
 AS 
 SELECT TipDoc,TipoDoc,Documento,P.IdCia AS CodCia,Compania,Fecha,Item,P.IdForma AS CdForma,FormaPago,Detalle
,VrPagado,VrCambio,EsCaja,P.IdBanco AS CodBanco,B.Banco AS NomBanco,NumForma,FecForma,CtaForma,Beneficiario
,NitCliente,RazonSocial,CdAgencia,Agencia,CodAgencia,A.Referencia AS RefeAgencia,Referncia1,Referncia2,NumAutoriza,CdLocal,LF.Localidad AS CiudadPlaza,LF.IdDep AS CdDptoPlaza
,DF.Departamento AS DptoPlaza,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BC.Banco AS CtaBanco,VrDenom,Cantidad,TipDenom
,TipRef,DocRef,IdCiaRef,ItemDoc,P.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,TipoId,Dv,T.Codigo AS TerCodigo,NomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad
,L.IdDep AS TercCdDep,D.Departamento AS TercDpto,DocAnulado,NumDev,FecDev
 FROM  Trn_Pagos AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS B ON P.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero  LEFT JOIN Agencias  AS A ON P.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal  LEFT JOIN Departamentos AS DF ON LF.IdDep=DF.IdDep 
 LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN CtasCorrientes AS CTA ON P.CdCta=CTA.IdCta LEFT JOIN  Bancos AS BC ON CTA.IdBanco=BC.IdBanco
 LEFT JOIN (SELECT RC.TipDoc AS DocTipo,RC.Recibo AS DocNumero,RC.IdCia AS DocCia,RC.Anulado AS DocAnulado,NumDev,FecDev FROM Trn_Recibos AS RC 
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Factura,IdCia,Anulado,NumDev,FecDev FROM Trn_Factcon  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Consignacion,IdCia,Anulado,NumDev,FecDev FROM Trn_Consigna  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Recibo,IdCia,Anulado,NumDev,FecDev FROM Trn_CompCaja  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Planilla,IdCia,Anulado,NumDev,FecDev FROM Trn_EdsCortes  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  ) AS DOC ON P.TipDoc=DOC.DocTipo AND P.Documento=DOC.DocNumero  AND P.IdCia=DOC.DocCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdForma  LIKE ISNULL(@pmIdForma,'%') AND P.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND  (EsCaja=ISNULL(@pmEsCaja,0) or EsCaja=ISNULL(@pmEsCaja,1))
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,0) AND ISNULL(@pmDocumentoFin,2147483647)
 AND  NitCliente LIKE ISNULL(@pmNitCliente,'%') AND P.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 ORDER BY TipDoc,P.IdCia,Documento,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPagos_LtaTot @pmTipDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmIdForma VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmEsCaja BIT=Null,@pmIdBanco VARCHAR(4)=Null
,@pmDocumentoIni INT=Null,@pmDocumentoFin INT=Null,@pmNitCliente VARCHAR(16)=Null,@pmIdUsuario VARCHAR(11)=Null
,@pmAnulado BIT=Null
--******** NO SE ESTA UTILIZANDO
 AS 
 SELECT TipDoc,TipoDoc,COUNT(Documento) AS sCuenta,SUM(VrPagado) AS ValorTotal,SUM(VrCambio) AS TotalCambio,SUM(VrPagado-VrCambio) AS TotalPagado
,SUM(Cantidad) AS CantDenom,SUM(VrDenom*Cantidad) AS TotalDenom
 FROM  Trn_Pagos AS P INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc
 LEFT JOIN Terceros AS T ON P.NitCliente=T.IdTercero 
 LEFT JOIN (SELECT RC.TipDoc AS DocTipo,RC.Recibo AS DocNumero,RC.IdCia AS DocCia,RC.Anulado AS DocAnulado  FROM Trn_Recibos AS RC 
  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Factura,IdCia,Anulado FROM Trn_Factcon  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Consignacion,IdCia,Anulado FROM Trn_Consigna  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Recibo,IdCia,Anulado FROM Trn_CompCaja  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  UNION SELECT TipDoc,Planilla,IdCia,Anulado FROM Trn_EdsCortes  WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
  ) AS DOC ON P.TipDoc=DOC.DocTipo AND P.Documento=DOC.DocNumero  AND P.IdCia=DOC.DocCia
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND P.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND P.IdForma  LIKE ISNULL(@pmIdForma,'%') AND P.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND  (EsCaja=ISNULL(@pmEsCaja,0) or EsCaja=ISNULL(@pmEsCaja,1))
 AND Documento BETWEEN ISNULL(@pmDocumentoIni,0) AND ISNULL(@pmDocumentoFin,2147483647)
 AND  NitCliente LIKE ISNULL(@pmNitCliente,'%') AND P.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND  (DocAnulado=ISNULL(@pmAnulado,0) or DocAnulado=ISNULL(@pmAnulado,1))
 GROUP BY TipDoc,TipoDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPaises @pmIdPais VARCHAR(8)
 AS 
 SELECT IdPais,CodISO,NombrePais,IdMneda
 FROM adm_Paises
 WHERE IdPais=@pmIdPais
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPaisesIso @pmCodISO VARCHAR(3)
 AS 
 SELECT IdPais,CodISO,NombrePais,IdMneda
 FROM adm_Paises
 WHERE CodISO=@pmCodISO
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPaisesLta @pmIdPais VARCHAR(8)=Null
 AS 
 SELECT IdPais,CodISO,NombrePais,P.IdMneda AS CodMneda,Mneda,Simbolo,MneLocal,VrCambio
 FROM adm_Paises AS P INNER JOIN adm_monedas AS M ON P.IdMneda=M.IdMneda
 WHERE IdPais LIKE ISNULL(@pmIdPais,'%')
 ORDER BY NombrePais


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPeajesCat @pmIdCat VARCHAR(4)
 AS 
 IF @pmIdCat IS NULL
  SELECT IdCat,Catpeaje
  FROM PeajesCat
 WHERE Inactivo=0
 ORDER BY  IdCat
 ELSE
  SELECT IdCat,Catpeaje,Inactivo
  FROM PeajesCat
  WHERE IdCat=@pmIdCat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPeajesCatDso
 AS 
 SELECT IdCat,Catpeaje,IdCat+' '+Catpeaje AS DsCat
 FROM PeajesCat
 WHERE Inactivo=0
 ORDER BY  IdCat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPeajesClase @pmIdClase VARCHAR(4)
 AS 
 IF @pmIdClase IS NULL
 SELECT IdClase,ClasePeaje
 FROM PeajesClase
 WHERE Inactivo=0
 ORDER BY IdClase
 ELSE
 SELECT IdClase,ClasePeaje,Inactivo
 FROM PeajesClase
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPeajesTarf @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdCat,IdClase,VrTarifa
 FROM PeajesTarf
 WHERE IdCat=@pmIdCat AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPeajesTarfLta @pmIdCat VARCHAR(4)=Null,@pmIdClase VARCHAR(4)=Null
 AS 
 SELECT T.IdCat AS CdCat,Catpeaje,T.IdClase AS CdClase,ClasePeaje,VrTarifa
 FROM PeajesTarf AS T INNER JOIN PeajesCat AS C ON T.IdCat=C.IdCat
 INNER JOIN PeajesClase AS PC ON T.IdClase=PC.IdClase
 WHERE T.IdCat LIKE ISNULL(@pmIdCat,'%') AND T.IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY T.IdCat,T.IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPermisos @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10)=Null
 AS 
 SELECT IdUsuario,IdObj,Consecutivo,PerAdd,PerUpdate,PerDelete,PerAnular,PerRead,PerRename
 FROM adm_Permisos
 WHERE IdUsuario=@pmIdUsuario AND IdObj LIKE ISNULL(@pmIdObj,'%')
 ORDER BY IdObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPermisosLta @pmIdUsuario VARCHAR(11),@pmIdGrupo VARCHAR(6)=Null,@pmSubModulo VARCHAR(3)=Null
,@pmnModulo VARCHAR(10)=Null
AS
 SELECT P.IdUsuario AS IdUsuari,Usuario,P.IdObj AS IdObjto,Nombre,IndObj,PerAdd,PerUpdate,PerDelete,PerAnular,PerRead,PerRename,O.IdGrupo AS IdGrup,GrupoObjeto
,SubModulo,Formulario,Permisos,NivelMinimo,Consecutivo,U.IdGrupo AS NivelUsu
 FROM adm_Permisos AS P  INNER JOIN Sys_ObjetosApp AS O ON P.IdObj =O.IdObj
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Sys_GruposObj AS G ON O.IdGrupo=G.IdGrupo
 WHERE  P.IdUsuario=@pmIdUsuario  AND O.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')  AND SubModulo LIKE ISNULL(@pmSubmodulo,'%')
 AND nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY nModulo,O.IdGrupo,SubModulo,IndObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPermisosRoles @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10)=Null,@pmIdRole VARCHAR(3)=Null
 AS 
 SELECT IdUsuario,IdObj,IdRole,Asignado
 FROM adm_PermisosRoles
 WHERE IdUsuario=@pmIdUsuario AND IdObj LIKE ISNULL(@pmIdObj,'%') AND IdRole LIKE ISNULL(@pmIdRole,'%')
 ORDER BY IdObj,IdRole
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPermisosRoles_Cr @pmIdUsuario VARCHAR(11)=Null,@pmIdObj VARCHAR(10)=Null
 AS 
 SELECT P.IdUsuario AS IdUsuari,Usuario,P.IdObj AS CodObj,Nombre,O.IdGrupo AS IdGrup,SubModulo,P.IdRole AS IdFuncion,Funcion,Asignado
 FROM adm_PermisosRoles AS P INNER JOIN Sys_ObjetosApp AS O ON P.IdObj=O.IdObj
 INNER JOIN Sys_Roles AS R ON  P.IdObj=R.IdObj AND P.IdRole=R.IdRole
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE P.IdUsuario LIKE ISNULL(@pmIdUsuario,'%') AND P.IdObj LIKE ISNULL(@pmIdObj,'%') 
 ORDER BY Usuario,IndObj,P.IdRole
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPermisos_Cr @pmNivUsu INT,@pmIdUsuario VARCHAR(11)=Null,@pmIdGrupo VARCHAR(6)=Null
,@pmSubModulo VARCHAR(3)=Null,@pmnModulo VARCHAR(10)=Null
AS
 SELECT P.IdUsuario AS IdUsuari,Usuario,U.IdGrupo AS NivelUsu,IdEstacion,U.IdCargo AS CodCargo,Cargo,FechaAcc,U.Inactivo AS Inactvo
 ,P.IdObj AS IdObjto,Nombre,IndObj,PerAdd,PerUpdate,PerDelete,PerAnular,PerRead,PerRename,O.IdGrupo AS IdGrup,GrupoObjeto
,nModulo,SubModulo,Formulario,Permisos,NivelMinimo,Consecutivo
 FROM adm_Permisos AS P  INNER JOIN Sys_ObjetosApp AS O ON P.IdObj =O.IdObj
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN Sys_GruposObj AS G ON O.IdGrupo=G.IdGrupo
 INNER JOIN Cargos AS C ON U.IdCargo=C.IdCargo
 WHERE U.Inactivo=0 AND U.IdGrupo<=@pmNivUsu AND P.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')  AND O.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') 
 AND SubModulo LIKE ISNULL(@pmSubmodulo,'%')  AND nModulo LIKE ISNULL(@pmnModulo,'%')
 ORDER BY Usuario,nModulo,O.IdGrupo,SubModulo,IndObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryPicoPlaca @pmFecha SMALLDATETIME
 AS 
 IF @pmFecha IS NULL
  SELECT Fecha,UltimoDig
  FROM PicoPlaca
 ELSE
  SELECT Fecha,UltimoDig
  FROM PicoPlaca
  WHERE Fecha=@pmFecha


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPlantillasConcLta @pmIdPlantilla VARCHAR(4)=Null
AS
 SELECT  P.IdPlantilla AS CdPlantilla,DescPlantilla,CdTipCom,TipoCom,P.Inactivo AS Inactvo
 ,Item,Concepto,C.IdCuenta AS CdCuenta,NomCuenta,TipoMov,Tarifa, Requerido
 FROM Plantillas AS P LEFT JOIN  PlantillasConc AS C ON P.IdPlantilla=C.IdPlantilla
 LEFT JOIN TiposCom AS TC ON P.CdTipCom=TC.IdCom
 LEFT JOIN Puc AS PC ON C.IdCuenta=PC.IdCuenta
 WHERE P.IdPlantilla LIKE ISNULL(@pmIdPlantilla,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPlazos @pmIdPlazo VARCHAR(4)
 AS 
 IF @pmIdPlazo IS NULL
  SELECT IdPlazo,Plazo,NVmto,DiasPago
  FROM Plazos
  WHERE Inactivo=0
  ORDER BY IdPlazo
 ELSE
  SELECT IdPlazo,Plazo,NVmto,DiasPago,Inactivo
  FROM Plazos
  WHERE IdPlazo=@pmIdPlazo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPlazosDso
 AS 
 SELECT IdPlazo,Plazo,DiasPago,IdPlazo+' '+Plazo+' ('+DiasPago+')' AS DsPla
 FROM Plazos
 WHERE Inactivo=0
 ORDER BY IdPlazo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPolizas @pmNumPoliza VARCHAR(30)
 AS 
 SELECT NumPoliza,Fecha,Vigencia,NitCompania,Compania,NitTomador,Tomador,NitAseg,Asegurado
,NitBenef,Beneficiario,Inactivo,FechaAdd,FechaUpdate,IdUsuario,DescFirma
 FROM Polizas
 WHERE NumPoliza=@pmNumPoliza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPolizasLta
 AS 
 SELECT NumPoliza,Fecha,Vigencia,NitCompania,Compania,NitTomador,Tomador,NitAseg,Asegurado
,NitBenef,Beneficiario,DescFirma,P.IdUsuario AS IdUsuari,Usuario,FechaAdd,FechaUpdate
 FROM Polizas AS P INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE P.Inactivo=0
 ORDER BY NumPoliza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryPreCausacion @pmCausacionID INT,@pmIdCia CHAR(2)
 AS 
 SELECT CausacionID,IdCia,Fecha,Modalidad,TotalCapital,TotalInteres,TotalCuota,IdVehiculo,IdPropietario,TipDoc,PrestamoID,IdCiaDoc,NumCuota,Anulado,FecDev,TipCom,Comprobante,IdCiaCom,Observacion,TimeSys,IdUsuario
 FROM Trn_PreCausacion
 WHERE CausacionID=@pmCausacionID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPreCausacionAcp @pmCausacionID INT,@pmIdCia CHAR(2)
--aplicacion de anticipos
AS
  SELECT  RF.TipRec AS TRec, RF.Recibo AS Nrecibo, RF.IdCia AS CodCia,RF.IdVehiculo AS Placaveh,ValorAnticipo,R.ValorAbono AS AbonoAnt,SUM(RF.ValorAbono) AS TotalAbono
  FROM   Trn_PreCausacion AS C INNER JOIN Trn_PreCuotas AS CU ON C.CausacionID=CU.CausacionID
  INNER JOIN Trn_CausacVence AS VC ON CU.TipDoc=VC.TipDoc AND CU.PrestamoID=VC.Documento AND CU.IdCia=VC.IdCia AND CU.NumCuota=VC.Item
  INNER JOIN  Trn_RecFactura AS RF ON VC.TipDoc=RF.TipDoc AND VC.Documento=RF.Documento AND VC.IdCia=RF.IdCiaDoc AND VC.Item=RF.ItemDoc
  INNER JOIN Trn_RecBuses AS R ON RF.TipRec=R.TipDoc AND RF.Recibo=R.Recibo AND RF.IdCia=R.IdCia
  WHERE RF.TipDoc='PRE' AND C.CausacionID=@pmCausacionID  AND C.IdCia=@pmIdCia AND AplAntcpo<>0
  GROUP BY  RF.TipRec, RF.Recibo, RF.IdCia,RF.IdVehiculo,ValorAnticipo,R.ValorAbono

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryPreCausacionCuo @pmCausacionID INT,@pmIdCia CHAR(2)
AS
SELECT  CU.PrestamoID AS NumPrest,CU.IdCia AS CodCia,SUM(ValorCapital) AS tValorCapital,SUM(ValorInteres) AS tValorInteres ,SUM(ValorCuota) AS tValorCuota
FROM Trn_PreCuotas AS CU INNER JOIN  Trn_PreCausacion AS C ON CU.CausacionID=C.CausacionID
WHERE C.CausacionID=@pmCausacionID AND C.IdCia=@pmIdCia
GROUP BY CU.PrestamoID,CU.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryPreCausacionLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmAnulado BIT=Null
,@pmModalidad VARCHAR(10)=Null
AS
SELECT CausacionID, IdCia, Fecha, Modalidad, TotalCapital, TotalInteres, TotalCuota, C.IdVehiculo AS PlacaVeh,NumVeh,C.IdPropietario AS IdPropietari,RazonSocial
, TipDoc, PrestamoID, IdCiaDoc, NumCuota, Anulado, FecDev, TipCom, Comprobante, IdCiaCom, C.Observacion AS Observ, TimeSys, C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_PreCausacion AS C INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS T ON C.IdPropietario=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE  Fecha BETWEEN  @pmFechaIni AND @pmFechaFin  AND IdCia like ISNULL(@pmIdCia,'%%')
 AND Modalidad like ISNULL(@pmModalidad,'%')
 AND   (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,CausacionID


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPreCausacionVce @pmCausacionID INT,@pmIdCia CHAR(2)
AS
 SELECT C.CausacionID AS NumCausacion, C.IdCia AS CodCia,Fecha,Modalidad,VC.TipDoc AS TDoc,Documento,VC.IdCia AS CodCiaVce,Item,FecEmision
,VC.FecVence AS FecVce,ValorFactura,IdCliente,VC.IdVehiculo AS PlacaVeh,CU.PrestamoID AS PrstamoID, CU.IdCia AS CiaPrest,CU.NumCuota AS NroCuota
,ValorCapital, ValorInteres,ValorCuota
 FROM Trn_PreCausacion AS C INNER JOIN Trn_PreCuotas AS CU ON C.CausacionID=CU.CausacionID
 INNER JOIN Trn_CausacVence AS VC ON CU.TipDoc=VC.TipDoc AND CU.PrestamoID=VC.Documento AND CU.IdCia=VC.IdCia AND CU.NumCuota=VC.Item
 WHERE VC.TipDoc='PRE' AND C.CausacionID=@pmCausacionID  AND C.IdCia=@pmIdCia
 ORDER BY VC.IdCia,Documento,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPreCausacionVct @pmCausacionID INT,@pmIdCia CHAR(2)
AS
 SELECT VC.IdCliente AS NitCliente,VC.IdVehiculo AS PlacaVeh,VC.IdCia AS CodCia,SUM(ValorFactura) AS SValFac,SUM(ValorAbono) AS SValAbo
 FROM Trn_PreCausacion AS C INNER JOIN Trn_PreCuotas AS CU ON C.CausacionID=CU.CausacionID
 INNER JOIN Trn_CausacVence AS VC ON CU.TipDoc=VC.TipDoc AND CU.PrestamoID=VC.Documento AND CU.IdCia=VC.IdCia AND CU.NumCuota=VC.Item
 WHERE VC.TipDoc='PRE' AND C.CausacionID=@pmCausacionID  AND C.IdCia=@pmIdCia
 GROUP BY VC.IdCliente, VC.IdVehiculo,VC.IdCia
 ORDER BY VC.IdCliente, VC.IdVehiculo,VC.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPreCausacion_Cr @pmCausacionID INT=Null,@pmIdCia CHAR(2)=Null
AS
 SELECT C.CausacionID AS NumCausacion, C.IdCia AS CodCia,C.Fecha AS FechaCausa, Modalidad,CU.TipDoc AS TipPre, CU.PrestamoID AS PrstamoID, CU.IdCia AS CiaPrest, CU.NumCuota AS NroCuota
,FecCausa, FecVence, ValorCapital, ValorInteres,ValorCuota,FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres,P.IdConcepto AS IdConcept,Concepto,CxPagar
,Referencia,TotalCausado, P.TotalInteres AS TtalIntereses, UltimaCuota, Cancelado,P.Anulado AS PrestAnulado,P.IdEstado AS IdEstad,Estado,TotalCapital, C.TotalInteres AS TtalIntCausado, TotalCuota, C.TipCom AS IdTipCom
,TipoCom,C.Comprobante AS NumComp,C.IdCiaCom AS IdCiaCmp, C.Anulado AS Cau_anulado, C.FecDev AS Cau_FecAnulado,C.Observacion AS Observ,P.IdVehiculo AS PlacaVeh,NumVeh,P.IdPropietario AS IdPropietari,T.RazonSocial AS Propietario
,V.IdPropietario AS Veh_IdPropietario,NP.RazonSocial AS Veh_NomPropietario,C.TimeSys AS FecHoraCrea, C.IdUsuario AS IdUsuari,Usuario,Compania
 FROM Trn_PreCausacion AS C INNER JOIN Trn_PreCuotas AS CU ON C.CausacionID=CU.CausacionID
 INNER JOIN Trn_Prestamos AS P ON CU.TipDoc=P.TipDoc AND CU.PrestamoID=P.PrestamoID AND CU.IdCia=P.IdCia
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario INNER JOIN Companias  AS CI ON P.IdCia=CI.IdCia
 INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero
 INNER JOIN Terceros AS NP ON V.IdPropietario=NP.IdTercero
 INNER JOIN Conceptos AS CO ON P.IdConcepto=CO.IdConcepto INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 LEFT JOIN TiposCom AS TM ON C.TipCom=TM.IdCom
 WHERE  (C.CausacionID>=ISNULL(@pmCausacionID,0)  AND C.CausacionID<=ISNULL(@pmCausacionID,2147483647))
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY C.IdCia,C.CausacionID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryPreCausacion_Crr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmAnulado BIT=Null,@pmPrestamoID INT=Null,@pmNumCuota INT=Null,@pmTipoPlazo VARCHAR(10)=Null
,@pmTipoCuota VARCHAR(10)=Null,@pmCausacionID INT=Null
AS
SELECT C.CausacionID AS NumCausacion, C.IdCia AS CodCia,C.Fecha AS FechaCausa,Modalidad,TotalCapital,C.TotalInteres AS TInteresCausado,TotalCuota,CU.PrestamoID AS NumPrestamo,CU.IdCia AS CodCiaPre,CP.Compania AS NomCiaPrest
,CU.NumCuota AS NroCuota,FecCausa, CU.FecVence AS FechVence, ValorCapital, ValorInteres,ValorCuota,ValorAbono,FecUltPago, FecPrestamo, TipoPlazo, NumCuotas,TipoCuota,DiasCausa, DiasVence, ValorPrestamo, TasaInteres,TotalCausado, P.TotalInteres AS AcuIntereses, UltimaCuota
,Cancelado, P.IdConcepto AS IdConcept,Concepto,P.IdVehiculo AS PlacaVeh,NumVeh,P.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,P.VehPropio AS VhEsPropio,CxPagar,P.Referencia AS NFactura, P.Anulado AS Pre_Anulado, P.NumDev AS Pre_NumDev
,P.FecDev AS Pre_FecDev,P.TipCom AS Pre_TipCom,P.Comprobante AS Pre_NumCmp,P.IdCiaCom AS Pre_CiaCom,P.Observacion AS Pre_Observ,P.IdEstado AS IdEstad,Estado,C.Anulado AS CauAnulado, C.FecDev AS CauFecDev, C.TipCom AS CauTipCom
,TipoCom,C.Comprobante AS CauNumCmp,C.IdCiaCom AS CauCiaComp, C.Observacion AS CauObserv,CU.IdUsuario,Usuario,CI.Compania AS NomCiaCausa,C.TimeSys AS TmeSys
,V.IdPropietario,NP.RazonSocial AS Veh_propietario,V.IdConductor AS CedConductor,NC.RazonSocial AS Veh_Conductor
 FROM Trn_PreCausacion AS C INNER JOIN Trn_PreCuotas AS CU ON C.CausacionID=CU.CausacionID
 INNER JOIN Trn_Prestamos AS P ON CU.TipDoc=P.TipDoc AND CU.PrestamoID=P.PrestamoID AND CU.IdCia=P.IdCia
 INNER JOIN Trn_CausacVence AS VC ON CU.TipDoc=VC.TipDoc AND CU.PrestamoID=VC.Documento AND CU.IdCia=VC.IdCia AND CU.NumCuota=VC.Item
 INNER JOIN adm_Usuarios AS U ON CU.IdUsuario=U.IdUsuario INNER JOIN Companias  AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Companias  AS CP ON CU.IdCia=CP.IdCia  INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo 
 INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero  INNER JOIN Terceros AS NP ON V.IdPropietario=NP.IdTercero
 INNER JOIN Terceros AS NC ON V.IdConductor=NC.IdTercero
 INNER JOIN Conceptos AS CO ON P.IdConcepto=CO.IdConcepto INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 LEFT JOIN TiposCom AS TM ON C.TipCom=TM.IdCom
 WHERE C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND C.IdCia like ISNULL(@pmIdCia,'%%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND V.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') 
 AND (CU.PrestamoID>=ISNULL(@pmPrestamoID,0)  AND CU.PrestamoID<=ISNULL(@pmPrestamoID,2147483647))
 AND (C.Anulado=ISNULL(@pmAnulado,0) or C.Anulado=ISNULL(@pmAnulado,1))
 AND (CU.NumCuota>=ISNULL(@pmNumCuota,0)  AND CU.NumCuota<=ISNULL(@pmNumCuota,2147483647))
 AND TipoPlazo LIKE ISNULL(@pmTipoPlazo,'%')
 AND TipoCuota LIKE ISNULL(@pmTipoCuota,'%')
 AND (C.CausacionID>=ISNULL(@pmCausacionID,0)  AND C.CausacionID<=ISNULL(@pmCausacionID,2147483647))
 ORDER BY C.IdCia,C.CausacionID,CU.IdCia,CU.PrestamoID,CU.NumCuota
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryPreCuotas @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmNumCuota INT
 AS 
 SELECT TipDoc,PrestamoID,IdCia,NumCuota,FecCausa,FecVence,ValorCapital,ValorInteres,ValorCuota,CausacionID,IdUsuario
 FROM Trn_PreCuotas
 WHERE TipDoc=@pmTipDoc AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia 
 AND (NumCuota>=ISNULL(@pmNumCuota,-1)  AND NumCuota<=ISNULL(@pmNumCuota,2147483647))
 ORDER BY NumCuota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPreCuotasTot @pmTipDoc VARCHAR(3), @pmPrestamoID INT=Null, @pmIdCia CHAR(2)=Null
 AS
 SELECT C.TipDoc AS Tdoc,C.PrestamoID AS PrestID,C.IdCia AS CodCia,NumCuotas,IdEstado,ValorPrestamo,COUNT(NumCuota) AS SCanCuo,MAX(NumCuota) AS MxCuota,SUM(ValorCapital) AS STotCap
,SUM(ValorInteres) AS STotInt,SUM(ValorCuota) AS STotCuo
 FROM  Trn_PreCuotas AS C INNER JOIN Trn_Prestamos AS P ON C.TipDoc=P.TipDoc AND C.PrestamoID=P.PrestamoID AND C.IdCia=P.IdCia
 WHERE C.TipDoc=@pmTipDoc  AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')  
 AND (C.PrestamoID>=ISNULL(@pmPrestamoID,0)  AND C.PrestamoID<=ISNULL(@pmPrestamoID,2147483647))
 GROUP BY C.TipDoc,C.PrestamoID,C.IdCia,NumCuotas,IdEstado,ValorPrestamo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamos @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2)
AS
 SELECT   TipDoc, PrestamoID, IdCia, Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, IdConcepto, IdVehiculo, IdPropietario, IdConductor, VehPropio, CxPagar,Referencia, Anulado, NumDev
,FecDev, TipCom, Comprobante, IdCiaCom, Observacion, IdEstado, TimeSys, FecUpdate, IdCiaCrea, IdUsuario
 FROM  Trn_Prestamos
 WHERE  TipDoc=@pmTipDoc AND PrestamoID=@pmPrestamoID AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamosCau @pmFechaDia SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null
,@pmIdConcepto VARCHAR(4)=Null
AS
 SELECT   TipDoc, PrestamoID, IdCia, Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, IdConcepto, P.IdVehiculo AS PlacaVeh,NumVeh,TipoAfil,P.IdPropietario AS NitPropietario
,T.RazonSocial AS Propietario, P.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,P.VehPropio AS VhPropioPre, CxPagar,Referencia, Anulado, NumDev
,FecDev, TipCom, Comprobante, IdCiaCom, P.Observacion AS Observ, P.IdEstado AS IdEstad, TimeSys, FecUpdate, IdCiaCrea, P.IdUsuario AS IdUsuari
,V.IdPropietario AS IdPropVeh,V.IdConductor AS CedCondVeh
 FROM  Trn_Prestamos AS P INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero INNER JOIN Terceros AS NC ON P.IdConductor=NC.IdTercero
 WHERE  Anulado=0 AND Cancelado=0  AND P.IdEstado<>'0077' AND FecPrestamo<=@pmFechaDia AND FecInicio<=@pmFechaDia
 AND  IdCia like ISNULL(@pmIdCia,'%%') AND P.IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdConcepto like ISNULL(@pmIdConcepto,'%')
 ORDER BY P.IdVehiculo,IdCia,PrestamoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamosDft @pmIdVehiculo VARCHAR(10),@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
AS
 SELECT TipDoc, PrestamoID, IdCia, Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, IdConcepto, IdVehiculo, IdPropietario, IdConductor, VehPropio, CxPagar,Referencia, Anulado, NumDev
,FecDev, TipCom, Comprobante, IdCiaCom, Observacion, IdEstado, TimeSys, FecUpdate, IdCiaCrea, IdUsuario
 FROM  Trn_Prestamos 
 WHERE  Anulado=0 AND Cancelado=0  AND IdEstado<>'0077'  AND IdVehiculo=@pmIdVehiculo
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY IdCia,PrestamoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamosLta @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2)=Null,@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmAnulado BIT=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
 SELECT PrestamoID, IdCia, Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, P.IdConcepto AS IdConcep,Concepto,P.IdVehiculo AS IdVehic,NumVeh,P.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,P.IdConductor AS CedConductor
,NC.RazonSocial AS Conductor,P.VehPropio AS VhPropio,CxPagar, Referencia,Anulado, NumDev,FecDev, TipCom, Comprobante, IdCiaCom, P.Observacion AS Observ, P.IdEstado AS IdEstad,Estado, TimeSys, FecUpdate, IdCiaCrea, P.IdUsuario AS IdUsuari,Usuario 
 FROM  Trn_Prestamos AS P  INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto 
 INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero INNER JOIN Terceros AS NC ON P.IdConductor=NC.IdTercero
 INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
 INNER JOIN  EstadoDoc AS E ON P.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE  TipDoc=@pmTipDoc  AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND P.IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
ORDER BY IdCia,PrestamoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamosPen @pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdCia CHAR(2)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null,@pmIdConcepto VARCHAR(4)=Null
 AS
 SELECT  PrestamoID,P.IdCia AS CodCia,Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, P.IdConcepto AS IdConcept,Concepto, P.IdVehiculo AS PlacaVeh, P.IdPropietario AS IdPropietari,NP.RazonSocial AS Propietario,P.IdConductor AS CedConductor
,NC.RazonSocial AS Conductor,P.VehPropio AS VhPropio, CxPagar,Referencia,TipCom,TipoCom, Comprobante, IdCiaCom, P.Observacion AS Observ, P.IdEstado AS IdEstad,ED.Estado AS EstadDoc,P.IdUsuario AS IdUsuari,Usuario
,TipDoc,TipoDoc,Compania,IdCiaCrea,TimeSys, FecUpdate
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_Prestamos  AS P  INNER JOIN Sys_TiposDoc  AS TD ON P.TipDoc=TD.IdDoc   INNER JOIN Companias  AS CI ON P.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON P.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON P.IdConductor=NC.IdTercero
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
   INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa   LEFT JOIN TercCndtores AS CT ON  P.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN TiposCom AS TM ON P.TipCom=TM.IdCom
  WHERE  Anulado=0 AND Cancelado=0  AND P.IdEstado<>'0077'  AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND P.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') 
  AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%') AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
  ORDER BY P.IdPropietario,P.IdVehiculo,P.IdCia,PrestamoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamosTot @pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmCancelado BIT=Null
AS
 SELECT COUNT(PrestamoID) AS Cuenta,SUM(ValorPrestamo) AS tValorPrestamo,SUM(TotalCausado) AS tTotalCausado
 ,SUM(TotalInteres) AS tTotalInteres
 FROM  Trn_Prestamos
 WHERE IdVehiculo=@pmIdVehiculo AND Anulado=0 AND   IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND (Cancelado=ISNULL(@pmCancelado,0) or Cancelado=ISNULL(@pmCancelado,1))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamosVeh @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
AS
 SELECT PrestamoID, IdCia, Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, P.IdConcepto AS IdConcep,Concepto,P.IdVehiculo AS IdVehic,NumVeh,P.IdPropietario AS NitPropietario,T.RazonSocial AS Propietario,P.IdConductor AS CedConductor
,NC.RazonSocial AS Conductor,P.VehPropio AS VhPropio,CxPagar, Referencia,TipCom, Comprobante, IdCiaCom, P.Observacion AS Observ, P.IdEstado AS IdEstad,Estado, TimeSys, FecUpdate, IdCiaCrea, P.IdUsuario AS IdUsuari,Usuario 
 FROM  Trn_Prestamos AS P  INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto 
 INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero INNER JOIN Terceros AS NC ON P.IdConductor=NC.IdTercero
 INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
 INNER JOIN  EstadoDoc AS E ON P.IdEstado=E.IdEstado INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE  Anulado=0 AND Cancelado=0  AND P.IdEstado<>'0077'  AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND P.IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND P.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 ORDER BY IdCia,PrestamoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryPrestamos_Cr @pmTipDoc VARCHAR(3),@pmPrestamoIDIni INT=Null,@pmPrestamoIDFin INT=Null
,@pmIdCia CHAR(2)=Null
AS
 SELECT   TipDoc,TipoDoc, PrestamoID, P.IdCia AS CodCia,Compania, Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, P.IdConcepto AS IdConcept,Concepto, P.IdVehiculo AS PlacaVeh, P.IdPropietario AS IdPropietari,NP.RazonSocial AS Propietario,P.IdConductor AS CedConductor
,NC.RazonSocial AS Conductor,P.VehPropio AS VhPropio, CxPagar,Referencia, Anulado, NumDev,FecDev, TipCom,TipoCom, Comprobante, IdCiaCom, P.Observacion AS Observ, P.IdEstado AS IdEstad,ED.Estado AS EstadDoc, TimeSys, FecUpdate, IdCiaCrea, P.IdUsuario AS IdUsuari,Usuario
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_Prestamos  AS P  INNER JOIN Sys_TiposDoc  AS TD ON P.TipDoc=TD.IdDoc   INNER JOIN Companias  AS CI ON P.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON P.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON P.IdConductor=NC.IdTercero
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON P.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN TiposCom AS TM ON P.TipCom=TM.IdCom
  WHERE   P.TipDoc=@pmTipDoc  AND PrestamoID BETWEEN @pmPrestamoIDIni AND @pmPrestamoIDFin
 AND  P.IdCia like ISNULL(@pmIdCia,'%%')
 ORDER BY P.IdCia,PrestamoID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryPrestamos_Crr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmTipoPlazo VARCHAR(10)=Null,@pmTipoCuota VARCHAR(10)=Null,@pmDiasCausa VARCHAR(5)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
,@pmAnulado BIT=Null,@pmCancelado BIT=Null,@pmIdEstado VARCHAR(4)=Null
AS
 SELECT  PrestamoID,P.IdCia AS CodCia,Fecha, FecPrestamo, FecInicio, TipoPlazo, NumCuotas, DiasCausa, DiasVence, TipoCuota, ValorPrestamo, TasaInteres
,TotalCausado, TotalInteres, UltimaCuota, Cancelado, P.IdConcepto AS IdConcept,Concepto, P.IdVehiculo AS PlacaVeh, NumVeh,P.IdPropietario AS IdPropietari,NP.RazonSocial AS Propietario,P.IdConductor AS CedConductor
,NC.RazonSocial AS Conductor,P.VehPropio AS VhPropio, CxPagar,Referencia, Anulado, NumDev,FecDev, TipCom,TipoCom, Comprobante, IdCiaCom, P.Observacion AS Observ, P.IdEstado AS IdEstad,ED.Estado AS EstadDoc,P.IdUsuario AS IdUsuari,Usuario
,TipDoc,TipoDoc,Compania,IdCiaCrea,TimeSys, FecUpdate
,Marca,Modelo,TipoVehiculo,CV.Descripcion AS ClaseVeh,NomColor,EV.Estado AS EstadVeh,NumMotor,SerieChasis,V.IdGrupo AS IdGrup,GrupoProp,NumSoat,VigSoat,KmActual, ObligaTProd,V.FecIngreso AS FechaIngreso, V.FecRetiro AS FechaRetiro,NContrato
,V.IdAdmon AS CodAdmon,TA.TipoAdmon AS TipAdmonVeh,NP.TipoId AS TipoIdProp,NP.Dv AS DvProp,NP.Direccion AS DireccionProp,NP.Telefono AS TeleProp,NP.Fax AS Tele2Prop,NP.TelMovil AS CelularProp,NP.EsConductor AS EsConductr,NP.IdLocal AS CodCiuProp,LP.Localidad AS CiudadProp
,NC.IdLugarCed AS LugarCedulaCond,NC.Direccion AS DireccionCond,NC.Telefono AS TeleCond,NC.Fax AS Tele2Cond,NC.TelMovil AS CelularCond,NC.EsPropietario AS EsPropietar,NC.IdLocal AS CodCiuCond,LC.Localidad AS CiudadCond
 FROM  Trn_Prestamos  AS P  INNER JOIN Sys_TiposDoc AS TD ON P.TipDoc=TD.IdDoc   INNER JOIN Companias AS CI ON P.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON P.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON P.IdVehiculo=V.IdVehiculo
  INNER JOIN Terceros AS NP ON P.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON P.IdConductor=NC.IdTercero
  INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal  
  INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca   INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh  
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor  
  INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo  
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon   LEFT JOIN TiposCom AS TM ON P.TipCom=TM.IdCom
  LEFT JOIN TarifBuses AS CV ON V.CdTarifa=CV.IdTarifa
  WHERE  FecPrestamo BETWEEN @pmFechaIni AND @pmFechaFin AND  P.IdCia like ISNULL(@pmIdCia,'%%')  AND P.IdConcepto like ISNULL(@pmIdConcepto,'%')
  AND TipoPlazo like ISNULL(@pmTipoPlazo,'%') AND TipoCuota LIKE ISNULL(@pmTipoCuota,'%') AND DiasCausa LIKE ISNULL(@pmDiasCausa,'%')
 AND P.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND P.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND  (Cancelado=ISNULL(@pmCancelado,0) or Cancelado=ISNULL(@pmCancelado,1))
 ORDER BY P.IdCia,PrestamoID
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryPresupuesto @pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT Numero,IdCia,nAnno,nMes,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 ,TipoPsto,DetCentros
 FROM Trn_Presupuesto
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPresupuestoMes @pmIdCia CHAR(2),@pmnAnno INT,@pmnMes INT
 AS 
 SELECT Numero,IdCia,nAnno,nMes,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
,TipoPsto,DetCentros
 FROM Trn_Presupuesto
 WHERE  IdCia=@pmIdCia AND nAnno=@pmnAnno AND  nMes=@pmnMes
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPresupuestoPer  @pmnAnno INT,@pmnMesIni INT,@pmnMesFin INT,@pmIdCia CHAR(2)=Null
 AS 
 SELECT P.Numero AS NumPsto,P.IdCia AS CdCia,Compania,nAnno,nMes,Observacion,Item,D.IdCuenta AS CdCuenta,NomCuenta,D.IdCCosto AS CdCentro,CCosto
,D.IdSubCos AS CdSubcentro,SubCosto,VrPresupuesto,SaldoAnterior,TotalDebitos,TotalCreditos,Movimiento,CentroCosto,SubCentro,Naturaleza
,D.FecUpdate AS FecModifica,CdUsuario,UM.Usuario AS UsuarioModifica
,P.IdEstado AS CdEstado,Estado,TimeSys,P.FecUpdate AS Fec_Update,IdCiaCrea,P.IdUsuario AS Id_Usuario,U.Usuario AS UsuarioCrea
,TipoPsto,DetCentros
 FROM Trn_Presupuesto AS P INNER JOIN   Trn_PresDetalle AS D ON P.Numero=D.Numero AND P.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN   Puc  AS PC ON D.IdCuenta=PC.IdCuenta LEFT JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos LEFT JOIN adm_Usuarios AS UM ON D.CdUsuario=UM.IdUsuario
 WHERE nAnno=@pmnAnno AND nMes BETWEEN @pmnMesIni AND @pmnMesFin
 AND P.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY nMes
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPresupuesto_Cr @pmNumero INT,@pmIdCia CHAR(2)
 AS 
 SELECT P.Numero AS NumPsto,P.IdCia AS CdCia,Compania,nAnno,nMes,Observacion,Item,D.IdCuenta AS CdCuenta,NomCuenta,D.IdCCosto AS CdCentro,CCosto
,D.IdSubCos AS CdSubcentro,SubCosto,VrPresupuesto,SaldoAnterior,TotalDebitos,TotalCreditos,Movimiento,CentroCosto,SubCentro,Naturaleza
,D.FecUpdate AS FecModifica,CdUsuario,UM.Usuario AS UsuarioModifica
,P.IdEstado AS CdEstado,Estado,TimeSys,P.FecUpdate AS Fec_Update,IdCiaCrea,P.IdUsuario AS Id_Usuario,U.Usuario AS UsuarioCrea
,TipoPsto,DetCentros
 FROM Trn_Presupuesto AS P INNER JOIN   Trn_PresDetalle AS D ON P.Numero=D.Numero AND P.IdCia=D.IdCia
 INNER JOIN Companias AS CN ON P.IdCia=CN.IdCia INNER JOIN EstadoDoc AS ED ON P.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 INNER JOIN   Puc  AS PC ON D.IdCuenta=PC.IdCuenta LEFT JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos LEFT JOIN adm_Usuarios AS UM ON D.CdUsuario=UM.IdUsuario
 WHERE P.Numero=@pmNumero AND P.IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdAditivos @pmIdProducto VARCHAR(16),@pmIdAditivo VARCHAR(16)
 AS 
 SELECT IdProducto,IdAditivo,Cant,Unidad,CdBodega,CdTanq
 FROM ProdAditivos
 WHERE IdProducto=@pmIdProducto AND IdAditivo LIKE ISNULL(@pmIdAditivo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdAditivosLta @pmIdProducto VARCHAR(16)=Null,@pmIdAditivo VARCHAR(16)=Null
 AS 
 SELECT A.IdProducto AS CdProducto,P.DescripProd AS NombreProd,IdAditivo,PM.DescripProd AS ProdAditivo,Cant,Unidad,CdBodega,Bodega,CdTanq
 FROM ProdAditivos AS A INNER JOIN ProdMcias AS P ON A.IdProducto=P.IdProducto
 INNER JOIN ProdMcias AS PM ON A.IdAditivo=PM.IdProducto
 LEFT JOIN  Bodegas AS B ON A.CdBodega=B.IdBodega
 WHERE A.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdAditivo LIKE ISNULL(@pmIdAditivo,'%')
 ORDER BY P.DescripProd

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdCodigos  @pmCodigo VARCHAR(20)
 AS 
 SELECT Codigo,IdProducto
 FROM ProdCodigos
 WHERE Codigo=@pmCodigo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdCombo @pmIdProducto VARCHAR(16),@pmItem INT
 AS 
 SELECT IdProducto,Item,IdProdBas,Cant,FacMezcla,CdBodega,CdTanq,ExentoSob
 FROM ProdCombo
 WHERE IdProducto=@pmIdProducto
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdComboFac @pmIdProducto VARCHAR(16),@pmItem INT
 AS 
 SELECT SUM(Cant) AS SCANT
 FROM ProdCombo
 WHERE IdProducto=@pmIdProducto AND  Item<>@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdComboItm @pmIdProducto VARCHAR(16)
 AS 
 SELECT Max(Item) AS MXITM
 FROM ProdCombo
 WHERE IdProducto=@pmIdProducto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdComboLta @pmIdProducto VARCHAR(16)=Null,@pmIdProdBas VARCHAR(16)=Null
 AS 
 SELECT PC.IdProducto AS CdProd,P.DescripProd AS Prodcto,Item,IdProdBas,PB.DescripProd AS ProdBase
,Cant,FacMezcla,CdBodega,Bodega,CdTanq,P.Tanques AS ProdTanques,PB.Tanques AS ProBasTanq,ExentoSob
 FROM ProdCombo AS PC INNER JOIN ProdMcias AS P ON PC.IdProducto=P.IdProducto
 INNER JOIN ProdMcias AS PB ON PC.IdProdBas=PB.IdProducto
 LEFT JOIN  Bodegas AS B ON PC.CdBodega=B.IdBodega
 WHERE PC.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND IdProdBas LIKE ISNULL(@pmIdProdBas,'%')
 ORDER BY P.DescripProd,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdComboPro @pmIdProducto VARCHAR(16)
 AS 
 SELECT C.IdProducto AS CdProducto,Item,IdProdBas,Cant,FacMezcla,CdBodega,CdTanq
,DescripProd,TipoRef,IdSubgrupo,IdUnd,IdBodega,IdUbic
,VrCostPmd,Seriales,Lotes,Combo,Tanques,Precio1,Precio2,Precio3,Precio4,Precio5,ExentoSob
 FROM ProdCombo AS C INNER JOIN ProdMcias AS P ON C.IdProdBas=P.IdProducto
 WHERE C.IdProducto=@pmIdProducto
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdCostos @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,IdCia,CostoAnt,CostoUlt,CostoPmd,FecUltPmd
 FROM ProdCostos
 WHERE IdProducto=@pmIdProducto AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdCostosCia @pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null
,@pmIdSubgrupo VARCHAR(8)=Null,@pmTanques BIT=Null,@pmInactivo BIT=Null
 AS 
 SELECT P.IdProducto AS CdProducto,IdCia,CostoAnt,CostoUlt,CostoPmd
 FROM ProdCostos AS P INNER JOIN ProdMcias  AS PM ON P.IdProducto=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
  INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
  WHERE P.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND TipoRef LIKE ISNULL(@pmTipoRef,'%')  AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
  AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND PM.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo ,'%')
  AND (Tanques=ISNULL(@pmTanques,0) or Tanques=ISNULL(@pmTanques,1))
  AND (PM.Inactivo=ISNULL(@pmInactivo,0) or PM.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY P.IdProducto,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 SELECT IdProducto,IdClase,Item,IdLocal,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe
 FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImpFronLta @pmIdProducto VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null
 AS 
 SELECT PIM.IdProducto AS CdProducto,DescripProd,PIM.IdClase AS CdClase,NomClase,Item,PIM.IdLocal AS CdLocal,Localidad,Tarifa,TarifZfe,SimbTfa,CdTarifa
,RngFecha,FecInicial,FecFinal,LtaPre,PIM.Inactivo AS Inactvo,FecUpdate
 FROM ProdImpFron AS PIM  INNER JOIN ProdMcias AS P ON PIM.IdProducto=P.IdProducto
 INNER JOIN ClaseTar AS CT ON PIM.IdClase=CT.IdClase
 INNER JOIN Localidades AS L ON PIM.IdLocal=L.IdLocal
 WHERE PIM.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PIM.IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY  DescripProd,PIM.IdClase,PIM.IdLocal,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImpFronUlt @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
,@pmIdLocal VARCHAR(8)
 AS 
 SELECT IdProducto,IdClase,Item,IdLocal,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,TarifZfe
 FROM ProdImpFron
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase  AND IdLocal=@pmIdLocal
 AND Inactivo=0
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT
 AS 
 SELECT IdProducto,IdClase,Item,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,Inactivo,FecUpdate,TarifZfe
 FROM ProdImptos
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosFec @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
,@pmFecha SMALLDATETIME
 AS 
 SELECT IdProducto,IdClase,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,TarifZfe
 FROM ProdImptos
 WHERE  Inactivo=0 AND IdProducto=@pmIdProducto AND IdClase=@pmIdClase
 AND (RngFecha=0 OR (RngFecha<>0 AND @pmFecha BETWEEN FecInicial AND FecFinal))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosItm @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM ProdImptos
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosLta @pmIdProducto VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null
,@pmInactivo BIT=Null
 AS 
 SELECT PIM.IdProducto AS CodProd,DescripProd,PIM.IdClase AS CdClase,NomClase,Item,Tarifa,TarifZfe,SimbTfa,CdTarifa,RngFecha
,FecInicial,FecFinal,LtaPre,PIM.Inactivo AS Inactiv,FecUpdate
 FROM ProdImptos AS PIM  INNER JOIN ProdMcias AS P ON PIM.IdProducto=P.IdProducto
 INNER JOIN ClaseTar AS CT ON PIM.IdClase=CT.IdClase
 WHERE PIM.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PIM.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND (PIM.Inactivo=ISNULL(@pmInactivo,0) or PIM.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY  DescripProd,PIM.IdClase,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdImptosUlt @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdProducto,IdClase,Item,Tarifa,SimbTfa,CdTarifa,RngFecha,FecInicial,FecFinal,LtaPre,TarifZfe
 FROM ProdImptos
 WHERE  Inactivo=0 AND IdProducto=@pmIdProducto AND IdClase LIKE ISNULL(@pmIdClase ,'%')
 ORDER BY IdClase,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMcias @pmIdProducto VARCHAR(16)
 AS 
 SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,IdSubgrupo,IdMarca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,UndMed,IdUnd,IdUndP,CdUndS,CdUndE,CdUndEb
,IdEmp,IdNat,IdMnjo,IdTmcia,IdBodega,IdUbic,DesUbic,IdProv,GarProv,GarClie,CdDctCom,VrCostAnt,VrCosto,VrCostPmd,IdTarIva,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5,CdMon1,CdMon2,CdMon3
,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,CdTarIca,CdTarRet,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes
,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM ProdMcias
 WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasCat @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null
,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null,@pmIdProducto VARCHAR(16)=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null
 AS 
 SELECT G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,IdProducto,DescripProd,DescripAbrv,P.Inactivo AS Inactvo,Tanques,TipoRef,P.IdMarca AS CdMarc,Marca,P.IdEstado AS CdEstdo,Estado
,ExtciaAct
 FROM ProdMcias AS P INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN EstadoPro AS EP ON P.IdEstado=EP.IdEstado
 WHERE  TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND P.IdMarca LIKE ISNULL(@pmIdMarca,'%') AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Tanques=ISNULL(@pmTanques,0) or  Tanques=ISNULL(@pmTanques,1))
 AND (P.Inactivo=ISNULL(@pmInactivo,0) or  P.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Linea,Grupo,Subgrupo,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasCod @pmIdProducto VARCHAR(20),@pmCodBarras VARCHAR(20)
,@pmReferencia VARCHAR(20),@pmInactivo BIT=Null
 AS 
 SELECT ProdMcias.*
 FROM ProdMcias
 WHERE (IdProducto=@pmIdProducto OR CodBarras=@pmCodBarras OR Referencia=@pmReferencia)
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasCol
 AS 
 SELECT Color,COUNT(IdProducto)  AS CantProd
 FROM ProdMcias
 GROUP BY Color
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasDes @pmIdProducto VARCHAR(16)
 AS 
 SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,Linea,Grupo,Subgrupo,Marca,Color,Tamano,P.UndMed AS UndVolm
,UM.Unidad AS UndadMed,UP.Unidad AS EmpPrim,Empaque,Bodega,DescripLong,Estado,RazonSocial,Tarifa,Simbolo
 FROM ProdMcias AS P INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd  INNER JOIN UndMed AS UP ON P.IdUndP=UP.IdUnd
 INNER JOIN Empaques AS E ON P.IdEmp=E.IdEmp  INNER JOIN Bodegas AS B ON P.IdBodega=B.IdBodega
 INNER JOIN Terceros AS TP ON P.IdProv=TP.IdTercero
 INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa INNER JOIN EstadoPro AS EP ON P.IdEstado=EP.IdEstado
 WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasLkar  @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2),@pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null
,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null,@pmIdMarca VARCHAR(4)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmSeriales BIT=Null,@pmTanques BIT=Null
,@pmLotes BIT=Null,@pmInactivo BIT=Null,@pmCiaPre CHAR(2)=Null
 AS 
 SELECT K.IdProducto AS CdProducto,DescripProd,TipoRef,P.IdBodega AS CdBodega,IdTarIva,Tarifa,IvaInc,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,BaseMgn
,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,ExtciaAct,ExtciaAct,ExtciaMin,ExtciaMax,VrCostPmd,VrCosto,Precio1,Precio2,Precio3,Precio4,Precio5
--precios cia
,CiaCodProd,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,CiaIvaInc,CiaIdTarifIva,CiaTarifIva,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5
 ,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5
,SUM(Entradas) AS TotalEnt,SUM(Salidas) AS TotalSal,SUM(VrCostoEnt) AS TotalCostoEnt,SUM(VrCostoSal) AS TotalCostoSal
 FROM Trn_Kardex AS K INNER JOIN  ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa
 INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 LEFT JOIN (SELECT IdProducto AS CiaCodProd,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,IvaInc AS CiaIvaInc,CdTarIva AS CiaIdTarifIva,Tarifa AS CiaTarifIva
,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5
 FROM ProdPrecios AS PP LEFT JOIN Tablapor AS CI ON PP.CdTarIva=CI.IdTarifa WHERE IdCia=@pmCiaPre) AS PC ON K.IdProducto=PC.CiaCodProd
 WHERE TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND IdCia=@pmIdCia
 AND  TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND IdMarca LIKE ISNULL(@pmIdMarca,'%') AND P.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND IdProv LIKE ISNULL(@pmIdProv,'%') AND IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Seriales=ISNULL(@pmSeriales,0) or  Seriales=ISNULL(@pmSeriales,1))
 AND (Tanques=ISNULL(@pmTanques,0) or  Tanques=ISNULL(@pmTanques,1))
 AND (Lotes=ISNULL(@pmLotes,0) or  Lotes=ISNULL(@pmLotes,1))
 AND (P.Inactivo=ISNULL(@pmInactivo,0) or P.Inactivo=ISNULL(@pmInactivo,1))
 GROUP BY  K.IdProducto,DescripProd,TipoRef,P.IdBodega,IdTarIva,Tarifa,IvaInc,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,BaseMgn
,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,ExtciaAct,ExtciaAct,ExtciaMin,ExtciaMax,VrCostPmd,VrCosto,Precio1,Precio2,Precio3,Precio4,Precio5
,CiaCodProd,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,CiaIvaInc,CiaIdTarifIva,CiaTarifIva,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5
 ,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5
 ORDER BY DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasLpre @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmSeriales BIT=Null,@pmTanques BIT=Null
,@pmLotes BIT=Null,@pmInactivo BIT=Null,@pmCiaPre CHAR(2)=Null
 AS 
 SELECT IdProducto AS CdProducto,DescripProd,TipoRef,IdBodega,IdTarIva,Tarifa,IvaInc,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,BaseMgn
,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,ExtciaAct,ExtciaAct,ExtciaMin,ExtciaMax,VrCostPmd,VrCosto,Precio1,Precio2,Precio3,Precio4,Precio5
--precios cia
,CiaCodProd,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,CiaIvaInc,CiaIdTarifIva,CiaTarifIva,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5
 ,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5
 FROM ProdMcias AS P  INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa
 INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 LEFT JOIN (SELECT IdProducto AS CiaCodProd,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,IvaInc AS CiaIvaInc,CdTarIva AS CiaIdTarifIva,Tarifa AS CiaTarifIva
,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5
 FROM ProdPrecios AS PP LEFT JOIN Tablapor AS CI ON PP.CdTarIva=CI.IdTarifa WHERE IdCia=@pmCiaPre) AS PC ON P.IdProducto=PC.CiaCodProd
  WHERE TipoRef<>'INSUMO'  AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND IdMarca LIKE ISNULL(@pmIdMarca,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND IdProv LIKE ISNULL(@pmIdProv,'%') AND IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Seriales=ISNULL(@pmSeriales,0) or  Seriales=ISNULL(@pmSeriales,1))
 AND (Tanques=ISNULL(@pmTanques,0) or  Tanques=ISNULL(@pmTanques,1))
 AND (Lotes=ISNULL(@pmLotes,0) or  Lotes=ISNULL(@pmLotes,1))
 AND (P.Inactivo=ISNULL(@pmInactivo,0) or P.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasLta @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmSeriales BIT=Null,@pmTanques BIT=Null
 AS 
 SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo,P.IdMarca AS CdMarc,Marca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,P.UndMed AS UndVolm
,P.IdUnd AS CdUnd,UM.Unidad AS UndadMed,IdUndP,UP.Unidad AS EmpPrim,CdUndS,CdUndE,CdUndEb,P.IdEmp AS CdEmp,Empaque,P.IdNat AS CdNat,Natlzaprod,P.IdMnjo AS CdMnjo,ManejoMcia,P.IdTmcia AS CdTmcia,TipoMcia,P.IdBodega AS CdBodga,Bodega,IdUbic,DesUbic,IdProv,RazonSocial
,GarProv,GarClie,CdDctCom,VrCostAnt,VrCosto,VrCostPmd,IdTarIva,Tarifa,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5,P.CdTarIca AS CdTarfIca,P.CdTarRet AS CdTarfRet
,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,Cmntarios,PathFoto,CantTpv,P.IdEstado AS CdEstdo,Estado,P.Inactivo AS Inactvo,P.IdUsuario AS IdUsuari,Usuario,P.FechaAdd AS FecAdd,P.FechaUpdate AS FecUpd
 FROM ProdMcias AS P INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd  INNER JOIN UndMed AS UP ON P.IdUndP=UP.IdUnd
 INNER JOIN Empaques AS E ON P.IdEmp=E.IdEmp INNER JOIN TiposNat AS N ON P.IdNat=N.IdNat
 INNER JOIN TiposMnjo AS MM ON P.IdMnjo=MM.IdMnjo INNER JOIN TiposMcia AS TM ON P.IdTmcia=TM.IdTmcia
 INNER JOIN Bodegas AS B ON P.IdBodega=B.IdBodega INNER JOIN Terceros AS TP ON P.IdProv=TP.IdTercero
 INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa INNER JOIN EstadoPro AS EP ON P.IdEstado=EP.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario 
 WHERE P.Inactivo=0  AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND P.IdMarca LIKE ISNULL(@pmIdMarca,'%') AND P.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND IdProv LIKE ISNULL(@pmIdProv,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Seriales=ISNULL(@pmSeriales,0) or  Seriales=ISNULL(@pmSeriales,1))
 AND (Tanques=ISNULL(@pmTanques,0) or  Tanques=ISNULL(@pmTanques,1))
 ORDER BY DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasPre @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null
 AS 
 SELECT IdProducto,DescripProd,DescripAbrv,TipoRef,VrCosto,VrCostPmd,IdTarIva,Tarifa,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,BaseMgn,CdMgn1,CdMgn2,CdMgn3,CdMgn4,CdMgn5,CdDct1,CdDct2,CdDct3,CdDct4,CdDct5
,IdUnd,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo,IdMarca,IdBodega,IdProv,ExtciaMin,ExtciaMax,ExtciaAct
,IdEstado,P.Inactivo AS Inactiv,CdDctCom
 FROM ProdMcias AS P INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea  INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa 
 WHERE P.Inactivo=0  AND TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND IdMarca LIKE ISNULL(@pmIdMarca,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND IdProv LIKE ISNULL(@pmIdProv,'%') 
 ORDER BY DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasRef  @pmCodigo VARCHAR(20),@pmEsRef BIT,@pmTipoRef VARCHAR(10)=Null
 AS 
 IF @pmEsRef=1
   SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef
   FROM  ProdMcias
   WHERE Referencia=@pmCodigo AND  Inactivo=0 AND TipoRef LIKE ISNULL(@pmTipoRef,'%')
   ORDER BY IdProducto
 ELSE
   SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef
   FROM  ProdMcias
   WHERE CodBarras=@pmCodigo AND Inactivo=0 AND TipoRef LIKE ISNULL(@pmTipoRef,'%')
   ORDER BY IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasShr @pmIdProducto VARCHAR(16)
 AS 
 SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,DescripLong
,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,Inactivo
 FROM ProdMcias
 WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMciasVal 
 AS 
 SELECT IdProducto,DescripProd,TipoRef
 FROM ProdMcias
 WHERE  TipoRef='SERVICIO' AND ValesComb<>0  AND Inactivo=0
 ORDER BY  IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdMcias_Cr @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmSeriales BIT=Null,@pmTanques BIT=Null
,@pmInactivo BIT=Null,@pmIdCiaCos CHAR(2)=Null,@pmIdCiaSal CHAR(2)=Null
 AS 
 SELECT IdProducto,DescripProd,DescripAbrv,CodBarras,Referencia,TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo,P.IdMarca AS CdMarc,Marca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,P.UndMed AS UndVolm
,P.IdUnd AS CdUnd,UM.Unidad AS UndadMed,IdUndP,UP.Unidad AS EmpPrim,CdUndS,CdUndE,CdUndEb,P.IdEmp AS CdEmp,Empaque,P.IdNat AS CdNat,Natlzaprod,P.IdMnjo AS CdMnjo,ManejoMcia,P.IdTmcia AS CdTmcia,TipoMcia,P.IdBodega AS CdBodga,Bodega,Descrip,LtaPre AS LtaPreBod
,IdUbic,DesUbic,P.IdProv AS IdProvee,RazonSocial,GarProv,GarClie,CdDctCom,DCC.Tarifa AS TarifDctComp,VrCostAnt,CiaCostoAnt,VrCosto,CiaCostoUlt,VrCostPmd,CiaCostoPmd,IdTarIva,TI.Tarifa AS TarifIva,TI.Simbolo AS SimbTarfIva,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5,BaseMgn,CdMgn1,MG1.Tarifa AS TarifMarg1
,CdMgn2,MG2.Tarifa AS TarifMarg2,CdMgn3,MG3.Tarifa AS TarifMarg3,CdMgn4,MG4.Tarifa AS TarifMarg4,CdMgn5,MG5.Tarifa AS TarifMarg5,CdDct1,DC1.Tarifa AS TarifDct1,CdDct2,DC2.Tarifa AS TarifDct2,CdDct3,DC3.Tarifa AS TarifDct3,CdDct4,DC4.Tarifa AS TarifDct4,CdDct5,DC5.Tarifa AS TarifDct5
,P.CdTarIca AS CdTarfIca,TIC.Tarifa AS TarifIca,P.CdTarRet AS CdTarfRet,TRF.Tarifa AS TarifRet,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,ExtciaMin,ExtciaMax,ExtciaAct,CiaExistencia,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes
,Tanques,ValesComb,FecUltcom,FecUltVta,CodMcia,DescripLong,P.Cmntarios AS Prod_Observ,P.PathFoto AS ArchivoFoto,CantTpv,P.IdEstado AS CdEstdo,Estado,NColor,P.Inactivo AS Inactvo
--información del proveedor
,TipoId,Dv,NomCial,Direccion,TP.IdLocal AS CdCiudad,Localidad,Departamento,Telefono,Fax,e_mail,TP.IdSector AS CdSector,SectorEco,TP.IdRegimen AS CdRegimen,Regimen,TipEnte
,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,PV.IdGrupo AS IdGrupoClie,GrupoClie
,P.IdUsuario AS IdUsuari,Usuario,P.FechaAdd AS FecAdd,P.FechaUpdate AS FecUpd
 FROM ProdMcias AS P INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd  INNER JOIN UndMed AS UP ON P.IdUndP=UP.IdUnd
 INNER JOIN Empaques AS E ON P.IdEmp=E.IdEmp INNER JOIN TiposNat AS N ON P.IdNat=N.IdNat
 INNER JOIN TiposMnjo AS MM ON P.IdMnjo=MM.IdMnjo INNER JOIN TiposMcia AS TM ON P.IdTmcia=TM.IdTmcia
 INNER JOIN Bodegas AS B ON P.IdBodega=B.IdBodega INNER JOIN Terceros AS TP ON P.IdProv=TP.IdTercero
 INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa INNER JOIN EstadoPro AS EP ON P.IdEstado=EP.IdEstado
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario 
 INNER JOIN SectoresEco AS SE ON TP.IdSector=SE.IdSector INNER JOIN RegimenDian AS RG ON TP.IdRegimen=RG.IdRegimen
 INNER JOIN Localidades AS LP ON TP.IdLocal=LP.IdLocal  INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 LEFT JOIN TercProvee AS PV ON P.IdProv=PV.IdProv  LEFT JOIN GruposCli AS GP ON PV.IdGrupo=GP.IdGrupo
 LEFT JOIN  Tablapor AS MG1 ON P.CdMgn1=MG1.IdTarifa  LEFT JOIN  Tablapor AS MG2 ON P.CdMgn2=MG2.IdTarifa
 LEFT JOIN  Tablapor AS MG3 ON P.CdMgn3=MG3.IdTarifa  LEFT JOIN  Tablapor AS MG4 ON P.CdMgn4=MG4.IdTarifa
 LEFT JOIN  Tablapor AS MG5 ON P.CdMgn5=MG5.IdTarifa LEFT JOIN  Tablapor AS DCC ON P.CdDctCom=DCC.IdTarifa
 LEFT JOIN  Tablapor AS DC1 ON P.CdDct1=DC1.IdTarifa LEFT JOIN  Tablapor AS DC2 ON P.CdDct2=DC2.IdTarifa
 LEFT JOIN  Tablapor AS DC3 ON P.CdDct3=DC3.IdTarifa LEFT JOIN  Tablapor AS DC4 ON P.CdDct4=DC4.IdTarifa
 LEFT JOIN  Tablapor AS DC5 ON P.CdDct5=DC5.IdTarifa 
 LEFT JOIN  Tablapor AS TIC ON P.CdTarIca=TIC.IdTarifa LEFT JOIN  Tablapor AS TRF ON P.CdTarRet=TRF.IdTarifa
 --consulta de costos
 LEFT JOIN (SELECT IdProducto AS CiaProducto,CostoAnt AS CiaCostoAnt,CostoUlt AS CiaCostoUlt,CostoPmd  AS CiaCostoPmd
 FROM ProdCostos WHERE IdCia=@pmIdCiaCos)  AS CP ON P.IdProducto=CP.CiaProducto
--consulta de saldos
 LEFT JOIN (SELECT  IdProducto AS SalProducto,SUM(SaldoActual) AS CiaExistencia FROM ProdSaldos
 WHERE IdCia=@pmIdCiaSal AND IdBodega LIKE ISNULL(@pmIdBodega,'%') GROUP BY IdProducto)  AS SC ON P.IdProducto=SC.SalProducto
 WHERE  TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND P.IdMarca LIKE ISNULL(@pmIdMarca,'%') AND P.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND P.IdProv LIKE ISNULL(@pmIdProv,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Seriales=ISNULL(@pmSeriales,0) or  Seriales=ISNULL(@pmSeriales,1))
 AND (Tanques=ISNULL(@pmTanques,0) or  Tanques=ISNULL(@pmTanques,1))
 AND (P.Inactivo=ISNULL(@pmInactivo,0) or  P.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryProdPrecCom @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmNumero INT
 AS 
 SELECT IdProducto,IdCia,Numero,TipoTarif,nMes,nAnno,Tarifa,NitProv,Inactivo
 FROM ProdPrecCom
 WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdPrecComLta @pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT T.IdProducto AS CdProducto,DescripProd,T.IdCia AS CdCia,Compania,Numero,TipoTarif,nMes,nAnno
,Tarifa,NitProv,RazonSocial,T.Inactivo AS Inactvo
 FROM ProdPrecCom AS T INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto INNER JOIN Companias AS CI ON T.IdCia=CI.IdCia
 LEFT JOIN Terceros AS PV ON T.NitProv=PV.IdTercero
 WHERE T.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND T.IdCia LIKE ISNULL(@pmIdCia,'%%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdPrecios @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,IdCia,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,IvaInc,CdTarIva,CdMoney1
,CdMoney2,CdMoney3,CdMoney4,CdMoney5,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5
,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5,IdUnd,IdBodega
 FROM ProdPrecios
 WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdPreciosCia @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)=Null
 AS 
 SELECT IdProducto,IdCia,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5,IvaInc,CdTarIva,CdMoney1
,CdMoney2,CdMoney3,CdMoney4,CdMoney5,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5
,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5,IdUnd,IdBodega,Tarifa
 FROM ProdPrecios AS PP  LEFT JOIN Tablapor AS TI ON PP.CdTarIva=TI.IdTarifa
 WHERE IdProducto=@pmIdProducto AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdPreciosLta @pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT PP.IdProducto AS CdProducto,DescripProd,PP.IdCia AS CdCia,Compania,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5
,PP.IvaInc AS IvaIncluido,CdTarIva,Tarifa,PP.IdUnd AS CdUnidad,Unidad,PP.IdBodega AS CdBodega,Bodega,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5
,CdMargen1,CdMargen2,CdMargen3,CdMargen4,CdMargen5,BaseMargen,CdDcto1,CdDcto2,CdDcto3,CdDcto4,CdDcto5
 FROM ProdPrecios  AS PP INNER JOIN  ProdMcias AS P ON PP.IdProducto=P.IdProducto
 INNER JOIN Companias AS C ON PP.IdCia=C.IdCia  INNER JOIN UndMed AS UM ON PP.IdUnd=UM.IdUnd
 INNER JOIN Bodegas AS B ON PP.IdBodega=B.IdBodega
 LEFT JOIN Tablapor AS TI ON PP.CdTarIva=TI.IdTarifa
 WHERE PP.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY DescripProd,PP.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdPrecios_Cr   @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmTanques BIT=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT PP.IdProducto AS CdProducto,DescripProd,DescripAbrv,PP.IdCia AS CdCia,Compania,VrPrecio1,VrPrecio2,VrPrecio3,VrPrecio4,VrPrecio5
,PP.CdTarIva AS CodTarifIva,TI.Tarifa AS TarifIva,TI.Simbolo AS SimbTarfIva,PP.IvaInc AS IvaIncluido,BaseMargen
,CdMargen1,MG1.Tarifa AS TarifMarg1,CdMargen2,MG2.Tarifa AS TarifMarg2,CdMargen3,MG3.Tarifa AS TarifMarg3
,CdMargen4,MG4.Tarifa AS TarifMarg4,CdMargen5,MG5.Tarifa AS TarifMarg5
,CdDcto1,DC1.Tarifa AS TarifDcto1,CdDcto2,DC2.Tarifa AS TarifDcto2,CdDcto3,DC3.Tarifa AS TarifDcto3
,CdDcto4,DC4.Tarifa AS TarifDcto4,CdDcto5,DC5.Tarifa AS TarifDcto5
,P.CdTarIca AS CdTarfIca,TIC.Tarifa AS TarifIca,P.CdTarRet AS CdTarfRet,TRF.Tarifa AS TarifRet
,CdMoney1,CdMoney2,CdMoney3,CdMoney4,CdMoney5,CostoAnt AS CiaCostoAnt,CostoUlt AS CiaCostoUlt,CostoPmd AS CiaCostoPmd
,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct,CiaExistencia
,PP.IdUnd AS CdUnidad,Unidad,PP.IdBodega AS CdBodega,Bodega,Factor1,Factor2,Factor3,Factor4,Factor5
,CodBarras,Referencia,TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo,P.IdMarca AS CdMarc,Marca,Color,Tamano
,MedAlto,MedAncho,MedLargo,MedVolm,P.UndMed AS UndVolm,P.IdProv AS IdProvee,RazonSocial,CdDctCom,DCC.Tarifa AS TarifDctComp
,LtPreDef,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta,DescripLong,P.Cmntarios AS Prod_Observ,P.FechaAdd AS FecAdd
 FROM ProdPrecios  AS PP INNER JOIN  ProdMcias AS P ON PP.IdProducto=P.IdProducto
 INNER JOIN Companias AS C ON PP.IdCia=C.IdCia  INNER JOIN UndMed AS UM ON PP.IdUnd=UM.IdUnd
 INNER JOIN Bodegas AS B ON PP.IdBodega=B.IdBodega
 INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN Terceros AS TP ON P.IdProv=TP.IdTercero
 LEFT JOIN Tablapor AS TI ON PP.CdTarIva=TI.IdTarifa
 LEFT JOIN  Tablapor AS MG1 ON PP.CdMargen1=MG1.IdTarifa  LEFT JOIN  Tablapor AS MG2 ON PP.CdMargen2=MG2.IdTarifa
 LEFT JOIN  Tablapor AS MG3 ON PP.CdMargen3=MG3.IdTarifa  LEFT JOIN  Tablapor AS MG4 ON PP.CdMargen4=MG4.IdTarifa
 LEFT JOIN  Tablapor AS MG5 ON PP.CdMargen5=MG5.IdTarifa LEFT JOIN  Tablapor AS DCC ON P.CdDctCom=DCC.IdTarifa
 LEFT JOIN  Tablapor AS DC1 ON PP.CdDcto1=DC1.IdTarifa LEFT JOIN  Tablapor AS DC2 ON PP.CdDcto2=DC2.IdTarifa
 LEFT JOIN  Tablapor AS DC3 ON PP.CdDcto3=DC3.IdTarifa LEFT JOIN  Tablapor AS DC4 ON PP.CdDcto4=DC4.IdTarifa
 LEFT JOIN  Tablapor AS DC5 ON PP.CdDcto5=DC5.IdTarifa 
 LEFT JOIN  Tablapor AS TIC ON P.CdTarIca=TIC.IdTarifa LEFT JOIN  Tablapor AS TRF ON P.CdTarRet=TRF.IdTarifa
 LEFT JOIN ProdCostos AS CP ON  PP.IdProducto=CP.IdProducto AND PP.IdCia=CP.IdCia
 LEFT JOIN (SELECT  IdProducto AS SalProducto,IdCia AS SalCdCia,SUM(SaldoActual) AS CiaExistencia FROM ProdSaldos
 WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')  GROUP BY IdProducto,IdCia)  AS SC ON PP.IdProducto=SC.SalProducto AND PP.IdCia=SC.SalCdCia
 WHERE P.Inactivo=0 AND  TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND P.IdMarca LIKE ISNULL(@pmIdMarca,'%') 
 AND (Tanques=ISNULL(@pmTanques,0) or  Tanques=ISNULL(@pmTanques,1))
 AND PP.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY DescripProd,PP.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdProvee @pmIdProducto VARCHAR(16),@pmIdProv VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,IdProv,IdCia,Fecha,DiasGar,CdDct,VrCostCom,VrCostPmd,RefProv,Dcmento
 FROM ProdProvee
 WHERE IdProducto=@pmIdProducto AND IdProv=@pmIdProv AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdProveeLta @pmIdProducto VARCHAR(16)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT PV.IdProducto AS CodProd,DescripProd,PV.IdProv AS IdProvee,RazonSocial,PV.IdCia AS CdCia,Compania,Fecha,DiasGar
,CdDct,VrCostCom,PV.VrCostPmd AS VrCostoProm,RefProv,Dcmento
 FROM ProdProvee AS PV INNER JOIN ProdMcias AS P ON PV.IdProducto=P.IdProducto
 INNER JOIN Terceros AS T ON PV.IdProv=T.IdTercero
 INNER JOIN Companias AS C ON PV.IdCia=C.IdCia
 WHERE P.Inactivo=0 AND PV.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PV.IdProv LIKE ISNULL(@pmIdProv,'%')
  AND PV.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY DescripProd,RazonSocial,PV.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdProvee_Cr  @pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdProv VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmIdGrupoPrv VARCHAR(4)=Null,@pmInactivoPrv BIT=Null
AS
 SELECT PV.IdProducto AS CodProducto,DescripProd,PV.IdProv AS IdProvee,RazonSocial,PV.IdCia AS CdCia,Compania,Fecha,DiasGar
,PV.CdDct AS CodDct,TD.Tarifa AS TarifDcto,VrCostCom,PV.VrCostPmd AS VrCostoProm,RefProv,Dcmento
--datos del producto
,DescripAbrv,Referencia,TipoRef,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo,P.IdMarca AS CdMarc,Marca,Color,Tamano,P.IdUnd AS CdUnd,UM.Unidad AS UndadMed
,IdTarIva,TI.Tarifa AS TarifaIva,IvaInc,LtPreDef,Precio1,Precio2,Precio3,Precio4,Precio5,ExtciaMin,ExtciaMax,ExtciaAct,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,FecUltcom,FecUltVta
,DescripLong,P.IdEstado AS CdEstdo,Estado,P.Inactivo AS Inactvo
--datos del proveedor
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,Telefono,Fax,TP.IdGrupo AS CdGruClie,GrupoClie,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,TP.Inactivo AS Prov_Inactivo
 FROM ProdProvee AS PV INNER JOIN ProdMcias AS P ON PV.IdProducto=P.IdProducto
 INNER JOIN Terceros AS T ON PV.IdProv=T.IdTercero
 INNER JOIN Companias AS C ON PV.IdCia=C.IdCia
 INNER JOIN TercProvee AS TP ON PV.IdProv=TP.IdProv
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal
 INNER JOIN GruposCli AS GC ON TP.IdGrupo=GC.IdGrupo
 INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo 
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa INNER JOIN EstadoPro AS EP ON P.IdEstado=EP.IdEstado
 LEFT JOIN Tablapor AS TD ON PV.CdDct=TD.IdTarifa
 WHERE TipoRef LIKE ISNULL(@pmTipoRef,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%') AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND P.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')  AND P.IdMarca LIKE ISNULL(@pmIdMarca,'%')
 AND PV.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND PV.IdProv LIKE ISNULL(@pmIdProv,'%')
 AND PV.IdCia LIKE ISNULL(@pmIdCia,'%%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (P.Inactivo=ISNULL(@pmInactivo,0) or  P.Inactivo=ISNULL(@pmInactivo,1))
 AND TP.IdGrupo LIKE ISNULL(@pmIdGrupoPrv,'%')
 AND (TP.Inactivo=ISNULL(@pmInactivoPrv,0) or  TP.Inactivo=ISNULL(@pmInactivoPrv,1))
 ORDER BY DescripProd,RazonSocial,PV.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldoUbc @pmIdProducto VARCHAR(16),@pmIdUbic VARCHAR(10),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,IdUbic,IdCia,SaldoActual
 FROM ProdSaldoUbc
 WHERE IdProducto=@pmIdProducto AND IdUbic=@pmIdUbic AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldoUbcTot @pmIdProducto VARCHAR(16),@pmIdUbic VARCHAR(10),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,SUM(SaldoActual) AS SSALDO
 FROM ProdSaldoUbc
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND IdUbic LIKE ISNULL(@pmIdUbic,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldos @pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,IdBodega,IdCia,SaldoActual
 FROM ProdSaldos
 WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 ORDER BY IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldosInv @pmIdCia CHAR(2)=Null
 AS 
 SELECT S.IdProducto AS CdProducto,DescripProd,S.IdBodega AS CdBodega,IdCia,SaldoActual
 FROM ProdSaldos AS S INNER JOIN ProdMcias AS P ON S.IdProducto=P.IdProducto
 WHERE SaldoActual<>0 AND TipoRef<>'SERVICIO' AND Tanques=0 
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY S.IdProducto,S.IdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldosLta @pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdCia CHAR(2)=Null,@pmTipoRef VARCHAR(10)=Null,@pmIdLinea VARCHAR(8)=Null,@pmIdGrupo VARCHAR(8)=Null,@pmIdSubgrupo VARCHAR(8)=Null
,@pmIdMarca VARCHAR(4)=Null,@pmTanques BIT=Null
 AS 
 SELECT S.IdProducto AS CdProducto,DescripProd,S.IdBodega AS CdBodega,Bodega,S.IdCia AS CdCia,Compania,SaldoActual
,DescripAbrv,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
,P.IdUnd AS CdUnid,Unidad,ExtciaMin,ExtciaMax,CostoUlt,CostoPmd,Tanques,P.Inactivo AS ProInactivo
 FROM ProdSaldos  AS S INNER JOIN ProdMcias  AS P ON S.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON S.IdBodega=B.IdBodega  INNER JOIN Companias AS CI ON S.IdCia=CI.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 LEFT JOIN ProdCostos AS PC ON S.IdProducto=PC.IdProducto AND S.IdCia=PC.IdCia
 WHERE S.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND S.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND S.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND TipoRef LIKE ISNULL(@pmTipoRef,'%')
 ORDER BY DescripProd,S.IdBodega,S.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldosP @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 IF @pmIdCia  IS NULL
  SELECT  SUM(SaldoActual) AS SSALDO  FROM ProdSaldos  WHERE IdProducto=@pmIdProducto
 ELSE
  SELECT  SUM(SaldoActual) AS SSALDO  FROM ProdSaldos 
  WHERE IdProducto=@pmIdProducto  AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldosTot @pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto, SUM(SaldoActual) AS SSALDO
 FROM ProdSaldos
 WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdSaldos_Cr @pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdCia CHAR(2)=Null,@pmTipoRef VARCHAR(10)=Null
 AS 
 SELECT S.IdProducto AS CdProducto,DescripProd,S.IdBodega AS CdBodega,Bodega,S.IdCia AS CdCia,Compania,SaldoActual
,DescripAbrv,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
,P.IdUnd AS CdUnid,Unidad,ExtciaMin,ExtciaMax,CostoUlt,CostoPmd,Tanques,P.Inactivo AS ProInactivo
 FROM ProdSaldos  AS S INNER JOIN ProdMcias  AS P ON S.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON S.IdBodega=B.IdBodega  INNER JOIN Companias AS CI ON S.IdCia=CI.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 LEFT JOIN ProdCostos AS PC ON S.IdProducto=PC.IdProducto AND S.IdCia=PC.IdCia
 WHERE S.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND S.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND S.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND TipoRef LIKE ISNULL(@pmTipoRef,'%')
 ORDER BY DescripProd,S.IdBodega,S.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdTarf @pmTipo VARCHAR(3),@pmNumero INT
 AS 
 SELECT Tipo,Numero,FecInicial,FecFinal,CdCia,CdLinea,CdGrupo,CdSubgrupo,CdMarca,CdProducto,Tarifa,SimbTfa,LtaPre,CdTarifa,CdClie,CdAgencia
,CdVend,CdCiudad,CdZona,CdSzona,CdGruCli,OtrosCrit,Anulado,TimeSys,FecUpdate,IdUsuario
 FROM Trn_ProdTarf
 WHERE Tipo=@pmTipo AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdTarfFec @pmTipo VARCHAR(3),@pmFecha SMALLDATETIME,@pmCdProducto VARCHAR(16)
,@pmCdSubgrupo VARCHAR(8)=Null,@pmCdCia CHAR(2)=Null,@pmCdClie  VARCHAR(16)=Null,@pmCdVend VARCHAR(16)=Null
,@pmLtaPre CHAR(1)
 AS 
 SELECT Tipo,Numero,FecInicial,FecFinal,CdCia,CdLinea,CdGrupo,CdSubgrupo,CdMarca,CdProducto,Tarifa,SimbTfa,LtaPre,CdTarifa,CdClie
,CdAgencia,CdVend,CdCiudad,CdZona,CdSzona,CdGruCli,OtrosCrit,Anulado,TimeSys,FecUpdate,IdUsuario
 FROM Trn_ProdTarf
 WHERE Tipo=@pmTipo  AND Anulado=0 AND Tarifa>0 AND @pmFecha BETWEEN FecInicial AND FecFinal
 AND CdProducto IN (@pmCdProducto,'0') AND CdSubgrupo IN (@pmCdSubgrupo,'0')
 AND CdCia IN (@pmCdCia,'00') AND CdClie IN (@pmCdClie,'0') AND CdVend IN (@pmCdVend,'0')
 AND LtaPre IN (@pmLtaPre,'0')
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdTarfItm @pmTipo VARCHAR(3)
 AS 
 SELECT MAX(Numero) AS MXNUM
 FROM Trn_ProdTarf
 WHERE Tipo=@pmTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProdTarfLta @pmTipo VARCHAR(3)=Null,@pmCdSubgrupo VARCHAR(8)=Null,@pmCdProducto VARCHAR(16)=Null
,@pmCdClie VARCHAR(16)=Null,@pmFecIni SMALLDATETIME=Null,@pmFecFin SMALLDATETIME=Null
 AS 
 SELECT Tipo,Numero,FecInicial,FecFinal,Tarifa,SimbTfa,CdTarifa,CdSubgrupo,Subgrupo,CdProducto,DescripProd,CdClie,T.RazonSocial AS NombreClie,CdAgencia,Agencia
,LtaPre,CdCia,Compania,CdLinea,Linea,CdGrupo,Grupo,CdMarca,Marca,CdVend,V.RazonSocial AS NombreVend,CdCiudad,Localidad,CdZona,Zona,CdSzona,Subzona
,CdGruCli,GrupoClie,OtrosCrit,TimeSys,FecUpdate,TR.IdUsuario AS IdUsuari,Usuario
 FROM Trn_ProdTarf AS TR INNER JOIN adm_Usuarios AS U ON TR.IdUsuario=U.IdUsuario
 LEFT JOIN SubGrupos AS  S ON TR.CdSubgrupo=S.IdSubgrupo
 LEFT JOIN ProdMcias AS P ON TR.CdProducto=P.IdProducto
 LEFT JOIN Terceros AS T ON TR.CdClie=T.IdTercero
 LEFT JOIN Terceros AS V ON TR.CdVend=V.IdTercero
 LEFT JOIN Companias AS C ON TR.CdCia=C.IdCia
 LEFT JOIN Lineas AS L ON TR.CdLinea=L.IdLinea
 LEFT JOIN Grupos AS G ON TR.CdGrupo=G.IdGrupo
 LEFT JOIN Marcas AS M ON TR.CdMarca=M.IdMarca
 LEFT JOIN  Agencias AS A ON TR.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LC ON TR.CdCiudad=LC.IdLocal
 LEFT JOIN Zonas AS Z ON TR.CdZona=Z.IdZona
 LEFT JOIN Subzonas AS SZ ON TR.CdSzona=SZ.IdSzona
 LEFT JOIN GruposCli AS GC ON TR.CdGruCli=GC.IdGrupo
 WHERE Anulado=0 AND Tipo LIKE ISNULL(@pmTipo,'%') AND CdSubgrupo LIKE ISNULL(@pmCdSubgrupo,'%')
 AND CdProducto LIKE ISNULL(@pmCdProducto,'%') AND CdClie LIKE ISNULL(@pmCdClie,'%')
 AND (FecInicial>=ISNULL(@pmFecIni,CAST('19100101' AS SMALLDATETIME)) AND FecInicial<=ISNULL(@pmFecFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY Tipo,FecInicial,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryProfesiones @pmIdProf VARCHAR(4)
 AS 
 IF @pmIdProf IS NULL
  SELECT IdProf,Profesion
  FROM Profesiones
  WHERE Inactivo=0
  ORDER  BY Profesion
 ELSE
  SELECT IdProf,Profesion,Inactivo
  FROM Profesiones
  WHERE IdProf=@pmIdProf
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuc @pmIdCuenta VARCHAR(16)
 AS 
 IF @pmIdCuenta IS NULL
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,FechaAdd,FechaUpdate,IdUsuario,Afijos
  FROM Puc
  WHERE  Inactivo=0
  ORDER BY IdCuenta
 ELSE
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,FechaAdd,FechaUpdate,IdUsuario,Inactivo,Afijos
  FROM Puc
  WHERE IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPucAju @pmIdCuenta VARCHAR(16)=Null,@pmCueIni VARCHAR(16)=Null
,@pmCueFin VARCHAR(16)=Null
AS
 IF @pmCueIni IS NULL --NO RANGOS
  SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
 ,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,Afijos
  FROM  Puc
  WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta like ISNULL(@pmIdCuenta,'%')
  ORDER BY IdCuenta
 ELSE
  BEGIN
  IF @pmCueIni='LIKE'
   SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,Afijos
   FROM  Puc
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta LIKE @pmIdCuenta
   ORDER BY IdCuenta
  ELSE
   SELECT IdCuenta,NomCuenta,Mayor,Movimiento,Tercero,Vehiculo,Agencia,CentroCosto,SubCentro,Vencimiento,BaseGravable,Porcntaje,Naturaleza
   ,Ajustable,CuentaAjuDeb,CuentaAjuCre,FmtDian,ConDian,RenDec,Cheques,Referncias,CdTipDoc,Afijos
   FROM  Puc
   WHERE Movimiento<>0 AND Ajustable<>0 AND  IdCuenta BETWEEN @pmCueIni AND @pmCueFin
   ORDER BY IdCuenta
 END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPucSub  @pmIdCuenta VARCHAR(16)

AS
  SELECT  IdCuenta,NomCuenta   FROM  Puc  
 WHERE  SUBSTRING(IdCuenta ,1,LEN(@pmIdCuenta) )=@pmIdCuenta AND IdCuenta<>@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuestos @pmIdPto VARCHAR(4)
 AS 
 SELECT IdPto,PtoControl,TagPuesto,IdLocal,Ubicacion,Contacto,Telefono,TelMovil,Referencia,Inactivo
 FROM Puestos
 WHERE IdPto=@pmIdPto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuestosDso @pmIdLocal VARCHAR(8)=Null
 AS 
 SELECT IdPto,PtoControl+' '+IdPto+' ('+Localidad+' '+Departamento+')' AS DsPto
 FROM Puestos AS P INNER JOIN Localidades AS L ON P.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE P.Inactivo=0 AND P.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 ORDER BY  PtoControl
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuestosLta @pmIdLocal VARCHAR(8)=Null
 AS 
 SELECT IdPto,PtoControl,P.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDpto,Departamento,Ubicacion
,Contacto,Telefono,TelMovil,Referencia,TagPuesto
 FROM Puestos AS P INNER JOIN Localidades AS L ON P.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE P.Inactivo=0 AND P.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 ORDER BY  PtoControl
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuestosTag @pmTagPuesto VARCHAR(50)
 AS 
 SELECT IdPto,PtoControl,TagPuesto,IdLocal,Ubicacion,Contacto,Telefono,TelMovil,Referencia,Inactivo
 FROM Puestos
 WHERE TagPuesto=@pmTagPuesto
 ORDER BY IdPto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntBases @pmIdProgrma INT,@pmCdSubgrupo VARCHAR(8),@pmCdProducto VARCHAR(16)
 AS 
 SELECT IdProgrma,CdSubgrupo,CdProducto,Puntos,Monto
 FROM PuntBases
 WHERE IdProgrma=@pmIdProgrma AND CdSubgrupo=@pmCdSubgrupo AND CdProducto=@pmCdProducto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntBasesDet @pmIdProgrma INT
 AS 
 SELECT IdProgrma,CdSubgrupo,CdProducto,Puntos,Monto
 FROM PuntBases
 WHERE IdProgrma=@pmIdProgrma AND (CdSubgrupo<>'0'  OR  CdProducto<>'0')
 ORDER BY CdSubgrupo,CdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntBasesLta @pmIdProgrma INT,@pmCdSubgrupo VARCHAR(8)=Null
 AS 
 SELECT CdSubgrupo,Subgrupo,CdProducto,DescripProd,Puntos,Monto,IdProgrma
 FROM PuntBases AS B LEFT JOIN SubGrupos AS S ON B.CdProducto=S.IdSubgrupo
 LEFT JOIN ProdMcias AS P ON B.CdProducto=P.IdProducto
 WHERE  (IdProgrma>=ISNULL(@pmIdProgrma,0)  AND IdProgrma<=ISNULL(@pmIdProgrma,2147483647))
 AND CdSubgrupo LIKE ISNULL(@pmCdSubgrupo,'%')
 ORDER BY IdProgrma,CdSubgrupo,CdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntFormas @pmIdProgrma INT,@pmIdForma VARCHAR(4)
 AS 
 SELECT IdProgrma,IdForma,Puntos
 FROM PuntFormas
 WHERE IdProgrma=@pmIdProgrma AND IdForma LIKE ISNULL(@pmIdForma,'%')
 ORDER BY IdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntFormasLta @pmIdProgrma INT
 AS 
 SELECT P.IdForma AS CdForma,FormaPago,Puntos,IdProgrma
 FROM PuntFormas AS P INNER JOIN Formaspago AS F ON P.IdForma=F.IdForma
 WHERE (IdProgrma>=ISNULL(@pmIdProgrma,0)  AND IdProgrma<=ISNULL(@pmIdProgrma,2147483647))
 ORDER BY IdProgrma,P.IdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntPremios @pmIdProgrma INT,@pmItem INT
 AS 
 SELECT IdProgrma,Item,Descripcion,IdProducto,Puntos,VrBono
 FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntPremiosItm @pmIdProgrma INT
 AS 
 SELECT MAX(Item) AS MXITM
 FROM PuntPremios
 WHERE IdProgrma=@pmIdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntPremiosLta @pmIdProgrma INT
 AS 
 SELECT Item,Descripcion,P.IdProducto AS IdProdcto,DescripProd,Puntos,VrBono,IdProgrma
 FROM PuntPremios AS P INNER JOIN ProdMcias AS PD ON P.IdProducto=PD.IdProducto
 WHERE (IdProgrma>=ISNULL(@pmIdProgrma,0)  AND IdProgrma<=ISNULL(@pmIdProgrma,2147483647))
 ORDER BY IdProgrma,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntProg @pmIdProgrma INT
 AS 
 SELECT IdProgrma,FecInicial,FecFinal,MesesInact,MesesVcmto,Observacion,Anulado,TimeSys,FecUpdate,IdUsuario
 FROM PuntProg
 WHERE IdProgrma=@pmIdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntProgFec @pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME
,@pmIdProgrma INT
 AS 
 SELECT IdProgrma,FecInicial,FecFinal,MesesInact,MesesVcmto,Observacion,TimeSys,FecUpdate,IdUsuario
 FROM PuntProg
 WHERE Anulado=0 AND  FecInicial<@pmFecFinal  AND FecFinal>@pmFecInicial
 AND IdProgrma<>@pmIdProgrma
 ORDER BY IdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntProgItm 
 AS 
 SELECT MAX(IdProgrma) AS MXITM  FROM PuntProg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntProgLta @pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmAnulado BIT=Null
 AS 
 SELECT IdProgrma,FecInicial,FecFinal,MesesInact,MesesVcmto,Observacion,Anulado,TimeSys,FecUpdate,P.IdUsuario AS IdUsuari,Usuario
 FROM PuntProg AS P INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE (FecInicial>=ISNULL(@pmFecInicial,CAST('19100101' AS SMALLDATETIME)) AND FecInicial<=ISNULL(@pmFecFinal,CAST('20781230' AS SMALLDATETIME)))
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntProg_Cr @pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmAnulado BIT=Null
,@pmIdProgrmaIni INT=Null,@pmIdProgrmaFin INT=Null
 AS 
 SELECT P.IdProgrma AS Id_Programa,FecInicial,FecFinal,MesesInact,MesesVcmto,Observacion,Anulado,TimeSys,FecUpdate,P.IdUsuario AS IdUsuari,Usuario
,CdSubgrupo,Subgrupo,CdProducto,DescripProd,Puntos,Monto
 FROM PuntProg AS P INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 LEFT JOIN PuntBases AS PB ON P.IdProgrma=PB.IdProgrma
 LEFT JOIN SubGrupos AS S ON PB.CdProducto=S.IdSubgrupo
 LEFT JOIN ProdMcias AS PR ON PB.CdProducto=PR.IdProducto
  WHERE (FecInicial>=ISNULL(@pmFecInicial,CAST('19100101' AS SMALLDATETIME)) AND FecInicial<=ISNULL(@pmFecFinal,CAST('20781230' AS SMALLDATETIME)))
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND P.IdProgrma BETWEEN ISNULL(@pmIdProgrmaIni,0) AND ISNULL(@pmIdProgrmaFin,2147483647)
 ORDER BY P.IdProgrma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntTarjetas @pmNumTarjeta VARCHAR(20)
 AS 
 SELECT NumTarjeta,IdClie,Fecha,TotalPuntos,PuntRedim,FecUltCompra,Inactivo,FechaUpdate,IdUsuario
 FROM PuntTarjetas
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntTarjetasCli @pmIdClie VARCHAR(16)
 AS 
 SELECT NumTarjeta,IdClie,Fecha,TotalPuntos,PuntRedim,FecUltCompra
,Inactivo,FechaUpdate,IdUsuario
 FROM PuntTarjetas 
 WHERE IdClie=@pmIdClie AND  Inactivo=0 
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntTarjetasLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdClie VARCHAR(16)=Null,@pmInactivo BIT=Null
 AS 
 SELECT NumTarjeta,IdClie,RazonSocial,Fecha,TotalPuntos,PuntRedim,FecUltCompra,P.Inactivo AS Inactiv
,P.FechaUpdate AS FecUpdate,P.IdUsuario AS IdUsuari,Usuario
,TipoId,Dv,NomCial,SiglaRaz,Direccion,Telefono,T.IdLocal AS CdLocal,Localidad
 FROM PuntTarjetas AS P INNER JOIN Terceros AS T ON P.IdClie=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 WHERE (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND IdClie LIKE ISNULL(@pmIdClie,'%')
 AND (P.Inactivo=ISNULL(@pmInactivo,0) or P.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY NumTarjeta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntTarjetasNit @pmIdClie VARCHAR(16),@pmNumTarjeta VARCHAR(20)
 AS 
 SELECT NumTarjeta,IdClie,Fecha,TotalPuntos,PuntRedim,FecUltCompra,Inactivo
 FROM PuntTarjetas 
 WHERE IdClie=@pmIdClie AND  Inactivo=0  AND NumTarjeta<>@pmNumTarjeta
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Documento,IdCia,Item,Fecha,IdCliente,IdAgencia,VrBase,PuntFactura,PuntExtra,PuntRedim,IdProgrma,NumTarjeta,Detalles,Referncia
 FROM Trn_Puntos
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntosPrg  @pmIdProgrma INT
 AS 
 SELECT Fecha,IdCliente,IdAgencia,NumTarjeta
 FROM Trn_Puntos
 WHERE IdProgrma=@pmIdProgrma
 ORDER BY Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryPuntos_Cr  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdProgrma INT=Null
 AS 
 SELECT  M.IdProgrma AS NumPrograma,M.NumTarjeta  AS NTarjeta,M.IdCliente AS NitCliente,CL.RazonSocial AS NomCliente,M.IdAgencia AS CdAgencia,Agencia
,TipDoc,TipoDoc,Documento,M.IdCia AS CdCia,Compania,Item,M.Fecha AS FechaMov,VrBase,PuntFactura,PuntExtra,M.PuntRedim AS Punt_Redim,Detalles,Referncia
--datos de tarjeta
,T.IdClie AS TarjNit,CLT.RazonSocial AS TarjNomCliente,T.Fecha AS TarjFecha,TotalPuntos,T.PuntRedim AS TotalRedim,FecUltCompra,T.Inactivo AS TarjInactivo
--datos de programa
,FecInicial,FecFinal,MesesInact,MesesVcmto,P.Observacion AS Observ,P.Anulado AS ProgAnulado,P.TimeSys AS FecCreacion,P.IdUsuario AS CdUsuari,Usuario
--datos del cliente
,CL.TipoId AS CliTipoId,CL.Dv AS CliDV,CL.NomCial AS CliNomCial,CL.SiglaRaz CliSigalRaz,CL.Direccion AS CliDireccion,CL.Telefono AS CliTelefono
,CL.IdLocal AS CliCodLocal,Localidad,L.IdDep AS CdDep,Departamento,CL.Inactivo AS TercInactivo,CLI.IdEstado AS CliCdEstado,CLI.Inactivo AS CliInactivo
 FROM Trn_Puntos AS M INNER JOIN  Terceros AS CL ON M.IdCliente=CL.IdTercero  INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia
 INNER JOIN Localidades AS L ON CL.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN Agencias AS  A ON M.IdAgencia=A.IdAgencia LEFT JOIN TercCliente AS CLI ON M.IdCliente=CLI.IdClie
 LEFT JOIN PuntTarjetas AS T ON M.NumTarjeta=T.NumTarjeta LEFT JOIN Terceros AS CLT ON T.IdClie=CLT.IdTercero
 LEFT JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc
 LEFT JOIN  PuntProg AS P ON M.IdProgrma=P.IdProgrma LEFT JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE M.Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND ( M.IdProgrma>=ISNULL(@pmIdProgrma,0)  AND  M.IdProgrma<=ISNULL(@pmIdProgrma,2147483647))
 ORDER BY M.IdCliente,M.Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRadicaVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,nRadica,IdCia,Fecha,FechaVence,IdConcepto,IdTercero,TotalPagos,TotalCobros,Referencia,CxPagar,VrDescuento
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_RadicaVales
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRadicaValesLta  @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmnRadicaIni INT=Null,@pmnRadicaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT nRadica,IdCia,Fecha,FechaVence,R.IdConcepto AS CdConcepto ,Concepto,R.IdTercero AS NitTercero,RazonSocial,TotalPagos,TotalCobros,Referencia,CxPagar
,VrDescuento,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea
,R.IdUsuario AS IdUsuari,Usuario
 FROM Trn_RadicaVales AS R INNER JOIN Terceros AS T ON R.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND nRadica BETWEEN ISNULL(@pmnRadicaIni,0) AND ISNULL(@pmnRadicaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND R.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
ORDER BY IdCia,nRadica
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRadicaVales_Cr @pmTipDoc VARCHAR(3),@pmnRadicaIni INT,@pmnRadicaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS CdTipDoc,TipoDoc,R.nRadica AS NumRadica,R.IdCia AS CdCia,Compania,R.Fecha AS FechaDoc,FechaVence,R.IdConcepto AS CdConcepto ,Concepto,R.IdTercero AS NitTercero
,T.RazonSocial AS NomTercero,TotalPagos,TotalCobros,R.Referencia AS DocReferncia,CxPagar,VrDescuento,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,R.Anulado AS DocAnulado,R.NumDev AS DocNumDev,R.FecDev  AS DocFecDev
,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario,Leyenda
--detalles
,Item,TipoRango,IdRango,NumSerie,NumForma,V.Fecha AS FechaServ,HoraServ,IdCliente,CL.RazonSocial AS NomCliente,V.IdAgencia AS ValAgencia,Agencia,IdCuenta,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera
,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,PlacaVeh,CdConductor,NomConductor,CantPasaj,Usuarios,TipFac,NumFactura,IdCiaFac,FechaFac,V.Anulado AS ValAnulado,V.NumDev AS ValNumAnul,V.FecDev AS ValFechaDev
,V.Observacion AS ValObserv,FechaUp,CdUsuario
--datos del cliente
,CL.TipoId AS CliTipoId,CL.Dv AS CliDv,CL.Codigo AS CliCodigo,CL.NomCial AS CliNomCial,CL.Direccion AS CliDireccion,CL.IdLocal AS CliCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,CL.Telefono AS CliTelefono,CL.Fax AS CliFax,CL.e_mail AS CliEmail,CL.IdSector AS CdSector,SectorEco,CL.IdRegimen AS CdRegimen,Regimen,CL.TipEnte AS CliTipoEnte
 FROM Trn_RadicaVales AS R INNER JOIN Trn_Vales AS V ON R.TipDoc=V.TipDoc AND R.nRadica=V.nRadica AND R.IdCia=V.IdCia
 INNER JOIN  Terceros AS T ON R.IdTercero=T.IdTercero  INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN Companias AS CN ON R.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN  Terceros AS CL ON V.IdCliente=CL.IdTercero
 INNER JOIN Localidades AS L ON CL.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON CL.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON CL.IdSector=SE.IdSector
 LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia LEFT JOIN TiposCom AS TC ON R.TipCom=TC.IdCom
  WHERE R.TipDoc=@pmTipDoc AND R.nRadica BETWEEN @pmnRadicaIni  AND @pmnRadicaFin AND R.IdCia=@pmIdCia
 ORDER BY R.nRadica
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryRangosPeso @pmIdRango VARCHAR(4)
 AS 
 IF @pmIdRango IS NULL
 SELECT IdRango,DescripRango,ValorInicial,ValorFinal
 FROM RangosPeso
 WHERE  Inactivo=0
 ORDER BY IdRango
 ELSE
 SELECT IdRango,DescripRango,ValorInicial,ValorFinal,Inactivo
 FROM RangosPeso
 WHERE IdRango=@pmIdRango

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecAfavor @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend
 FROM Trn_RecAfavor
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecAfavorSal @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,VrAFavor,VrOtrosDb,VrAbono,IdCliente,IdAgencia,IdVend
 FROM Trn_RecAfavor
 WHERE  IdCliente=@pmIdCliente AND (VrAFavor-VrOtrosDb)>VrAbono AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 ORDER BY Fecha,IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecAfavorTNit @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)=Null
 AS 
 SELECT SUM(VrAFavor) AS STOTAL,SUM(VrOtrosDb) AS STOTODB,SUM(VrAbono) AS STOTABO
 FROM Trn_RecAfavor
 WHERE IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBuses @pmTipDoc VARCHAR(3), @pmRecibo INT, @pmIdCia CHAR(2)

AS
 SELECT  TipDoc, Recibo, IdCia, Fecha, IdConcepto, IdCliente, IdAgencia, Modalidad, TipCar, Cartulina, IdCiaCar,IdRuta,IdRelevador, IdVehiculo, IdConductor, VehPropio, CentInicial, 
                      CentFinal, NPasajeros, Recorridos,ValorRecibido, ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorOtrosDct, ValorPagMas, ValorNeto,
ValorAplicado, ValorAnticipo, ValorAbono, TotalProducido, TotalGastos, TotalAhorro, TotalOtros, TotalVales, TotalAbono, ValorPasaje, IdVendedor, Comision, 
                      IdTarCom, IdCCosto, IdCta, EnEfectivo, NumCheque, IdBanco, Referencia, BaseIva, BaseRet, BaseOtr, TarifaIva, TarifaRet, TarifaOtr, IdTarIva, 
                      IdTarRet, IdTarOtr,IdClase, TipCom, Comprobante, IdCiaCom, Anulado, NumDev, FecDev, Observacion, IdEstado, TimeSys, FecUpdate, IdCiaCrea,IdUsuario
 FROM  Trn_RecBuses
 WHERE TipDoc=@pmTipDoc AND  Recibo=@pmRecibo AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesAnt @pmIdVehiculo VARCHAR(10)
AS
 SELECT  TipDoc, Recibo, IdCia, Fecha, IdConcepto, IdCliente, IdAgencia, Modalidad,IdVehiculo, IdConductor, VehPropio,ValorTotal, ValorNeto,ValorAnticipo, ValorAbono
, ValorAnticipo-ValorAbono AS ValorSaldoAnt, IdVendedor, Comision , NumCheque, IdBanco, Referencia,IdClase
 FROM  Trn_RecBuses
 WHERE Anulado=0 AND ValorAnticipo>0 AND ValorAnticipo>ValorAbono AND IdVehiculo=@pmIdVehiculo
 ORDER BY IdCia,Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesAntVce @pmSaldo DECIMAL(14,2),@pmIdCia CHAR(2)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
  SELECT IdCliente,R.IdVehiculo AS PlacaVeh,R.IdClase AS CodClase,IdCia,R.IdConductor AS CedConductor,R.VehPropio AS VhPropio,SUM(ValorAnticipo-ValorAbono) AS SaldoAFavor
  FROM Trn_RecBuses AS R  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
  WHERE Anulado=0 AND ValorAnticipo>0 AND (ValorAnticipo-ValorAbono)>@pmSaldo 
  AND  Fecha BETWEEN ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME))
  AND IdCia like ISNULL(@pmIdCia,'%%')
  AND  R.IdVehiculo like ISNULL(@pmIdVehiculo,'%')  AND IdCliente like ISNULL(@pmIdCliente,'%') AND R.IdClase like ISNULL(@pmIdClase,'%') AND IdGrupo like ISNULL(@pmIdGrupo,'%')
  GROUP BY IdCliente,R.IdVehiculo,R.IdClase,IdCia,R.IdConductor,R.VehPropio

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesCli @pmIdCliente VARCHAR(16)
AS
 SELECT IdCliente,SUM(ValorAnticipo) AS StotAnt,SUM(ValorAbono) AS STotAbo
 FROM  Trn_RecBuses
 WHERE ValorAnticipo>0  AND  IdCliente like ISNULL(@pmIdCliente,'%')
 GROUP BY  IdCliente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryRecBusesIca @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
 SELECT IdCia,V.IdPropietario AS IdCliente,R.IdVehiculo AS PlacaVeh,NumVeh,V.IdConductor AS CedConductor,SUM(ValorNeto) AS Total_Neto
 FROM  Trn_RecBuses AS R INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  VehPropio=0 AND ValorNeto>0--TipoAfil<>'PROPIO' 
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 GROUP BY  IdCia,V.IdPropietario,R.IdVehiculo,NumVeh,V.IdConductor
 UNION 
 SELECT IdCia,V.IdPropietario AS IdCliente,D.IdVehiculo AS PlacaVeh,NumVeh,V.IdConductor AS CedConductor,SUM(ValorNeto*-1) AS Total_Neto
 FROM     Trn_DevRecBuses AS D  INNER JOIN Vehiculos AS V ON D.IdVehiculo=V.IdVehiculo
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  VehPropio=0 AND ValorNeto>0 --TipoAfil<>'PROPIO' 
 AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 GROUP BY  IdCia,V.IdPropietario,D.IdVehiculo,NumVeh,V.IdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesLiq  @pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmModalidad VARCHAR(10),@pmIdCliente VARCHAR(16)
,@pmIdVehiculo VARCHAR(10)
AS
 SELECT  TipDoc, Recibo, IdCia, Fecha,R.IdConcepto AS IdConcept, Concepto,IdCliente,T.RazonSocial AS Cliente, IdAgencia, Modalidad, TipCar, Cartulina, IdCiaCar,IdRuta,IdRelevador, IdVehiculo, IdConductor,O.RazonSocial AS Conductor,VehPropio, CentInicial, 
                      CentFinal, NPasajeros, Recorridos,ValorRecibido,ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorOtrosDct, ValorPagMas, ValorNeto,
                      ValorAplicado, ValorAnticipo, ValorAbono, TotalProducido, TotalGastos, TotalAhorro, TotalOtros, TotalVales, TotalAbono, ValorPasaje, IdVendedor,V.RazonSocial AS Vendedor, R.Comision AS Comison, 
                      IdTarCom, IdCCosto, IdCta, EnEfectivo, NumCheque,R.IdBanco AS IdBanc,Banco, Referencia, BaseIva, BaseRet, BaseOtr, TarifaIva, TarifaRet, TarifaOtr, IdTarIva, 
                      IdTarRet, IdTarOtr,R.IdClase AS IdClas, TipCom, Comprobante, IdCiaCom, Anulado, NumDev, FecDev, R.Observacion AS Observ,R.IdEstado AS IdEstad,Estado,TimeSys, FecUpdate, IdCiaCrea,R.IdUsuario AS IdUsuari
 FROM  Trn_RecBuses AS R INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
  INNER JOIN Terceros AS O ON R.IdConductor=O.IdTercero  INNER JOIN Terceros AS V ON R.IdVendedor=V.IdTercero
  INNER JOIN Bancos AS B ON R.IdBanco=B.IdBanco  INNER JOIN EstadoDoc AS E ON R.IdEstado=E.IdEstado
 WHERE Modalidad=@pmModalidad  AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 ORDER BY  TipDoc, Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesLta  @pmIdCia CHAR(2),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdVehiculo VARCHAR(10)
AS
 SELECT  TipDoc, Recibo, IdCia, Fecha,R.IdConcepto AS IdConcept, Concepto,IdCliente,T.RazonSocial AS Cliente, IdAgencia, Modalidad, IdVehiculo, IdConductor,O.RazonSocial AS Conductor,VehPropio, CentInicial AS CartInicial
,CentFinal AS CartFinal, ValorRecibido,ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorOtrosDct, ValorPagMas,TotalOtros,TotalAhorro, ValorNeto,ValorAplicado, ValorAnticipo, ValorAbono, IdVendedor,V.RazonSocial AS Vendedor, R.Comision AS Comison
, IdTarCom, IdCCosto, IdCta, EnEfectivo, NumCheque,R.IdBanco AS IdBanc,Banco, Referencia, BaseIva, BaseRet, BaseOtr, TarifaIva, TarifaRet, TarifaOtr, IdTarIva,IdTarRet, IdTarOtr,R.IdClase AS IdClas, TipCom, Comprobante, IdCiaCom, Anulado, NumDev, FecDev
, R.Observacion AS Observ,R.IdEstado AS IdEstad,Estado,TimeSys, FecUpdate, IdCiaCrea,R.IdUsuario AS IdUsuari,TotalVales
 FROM   Trn_RecBuses AS R INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Terceros AS O ON R.IdConductor=O.IdTercero  INNER  JOIN Terceros AS V ON R.IdVendedor=V.IdTercero
 INNER  JOIN Bancos AS B ON R.IdBanco=B.IdBanco  INNER JOIN EstadoDoc AS E ON R.IdEstado=E.IdEstado
 WHERE Modalidad IN ('VC','OTROS')  AND  IdCia LIKE ISNULL(@pmIdCia,'%%') AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 ORDER BY  TipDoc, Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesNMes @pmAnno INT,@pmMes INT=Null, @pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdClase VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT IdCliente,IdVehiculo,IdCia,IdClase,Month(Fecha) AS NMes,SUM(ValorTotal) AS SvalTot,SUM(ValorNeto) AS SValNet
 FROM  Trn_RecBuses
 WHERE Year(Fecha)=@pmAnno AND Month(Fecha)=ISNULL(@pmMes,Month(Fecha))  AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 GROUP BY  IdCliente,IdVehiculo,IdCia,IdClase,Month(Fecha)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryRecBusesNetIca @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
AS
 SELECT TipoDoc,R.IdCia AS CdCia,Compania,IdCliente,T.RazonSocial AS NomCliente,R.IdVehiculo AS PlacaVeh,NumVeh,R.IdConductor AS CedConductor,CD.RazonSocial AS NomConductor
,SUM(ValorNeto) AS Total_Neto
 FROM  Trn_RecBuses AS R INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN Terceros AS CD ON R.IdConductor=CD.IdTercero
 INNER JOIN Companias AS C ON R.IdCia=C.IdCia INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  VehPropio=0 AND ValorNeto>0--TipoAfil<>'PROPIO' 
 AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 GROUP BY  TipoDoc,R.IdCia,Compania,IdCliente,T.RazonSocial,R.IdVehiculo,NumVeh,R.IdConductor,CD.RazonSocial
 UNION 
 SELECT TipoDoc,R.IdCia AS CdCia,Compania,IdCliente,T.RazonSocial AS NomCliente,R.IdVehiculo AS PlacaVeh,NumVeh,R.IdConductor AS CedConductor,CD.RazonSocial AS NomConductor
,SUM(ValorNeto*-1) AS Total_Neto
 FROM  Trn_DevRecBuses AS R INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero INNER JOIN Terceros AS CD ON R.IdConductor=CD.IdTercero
 INNER JOIN Companias AS C ON R.IdCia=C.IdCia INNER JOIN Sys_TiposDoc AS TD ON R.TipDev=TD.IdDoc
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  VehPropio=0 AND ValorNeto>0--TipoAfil<>'PROPIO' 
 AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%') 
 GROUP BY  TipoDoc,R.IdCia,Compania,IdCliente,T.RazonSocial,R.IdVehiculo,NumVeh,R.IdConductor,CD.RazonSocial
 ORDER BY R.IdCia,R.IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesUlt  @pmTipDoc VARCHAR(3), @pmIdCia CHAR(2),@pmModalidad VARCHAR(10),@pmIdVehiculo VARCHAR(10)

AS
 SELECT  TipDoc, Recibo, IdCia, Fecha, IdConcepto, IdCliente, IdAgencia, Modalidad, TipCar, Cartulina, IdCiaCar, IdVehiculo, CentInicial, CentFinal,ValorNeto, Anulado, NumDev, FecDev, IdCiaCrea
 FROM  Trn_RecBuses
 WHERE  TipDoc=@pmTipDoc AND  IdCia=@pmIdCia AND Modalidad=@pmModalidad AND IdVehiculo=@pmIdVehiculo AND Anulado=0
 ORDER BY Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesVFec @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdClase VARCHAR(4)=Null
AS
 SELECT IdCliente,IdVehiculo,SUM(ValorTotal) AS SvalTot,SUM(ValorNeto) AS SValNet
 FROM  Trn_RecBuses
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdClase LIKE ISNULL(@pmIdClase,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY IdCliente,IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesVc @pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmAnulado BIT=null,@pmValorNeto MONEY=0
AS
 SELECT  TipDoc, Recibo, IdCia, Fecha, IdConcepto, IdCliente, IdAgencia, Modalidad, TipCar, Cartulina, IdCiaCar,IdRuta,IdRelevador, IdVehiculo, IdConductor, VehPropio
,ValorNeto,ValorAplicado,ValorAnticipo,IdVendedor, Comision,Referencia,IdClase,Anulado, Observacion
 FROM  Trn_RecBuses
 WHERE  Anulado=@pmAnulado AND ValorNeto>@pmValorNeto  AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY IdCia,Recibo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryRecBusesVeh @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
 IF @pmIdVehiculo IS NULL
  SELECT R.IdVehiculo AS IdVehic,R.IdClase AS IdClas, IdCia,SUM(ValorAnticipo) AS StotAnt,SUM(ValorAbono) AS STotAbo 
  FROM  Trn_RecBuses  AS R  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
  WHERE ValorAnticipo>0 AND  IdPropietario LIKE ISNULL(@pmIdPropietario,'%') 
  GROUP BY  R.IdVehiculo,R.IdClase, IdCia
 ELSE
  SELECT R.IdVehiculo AS IdVehic,R.IdClase AS IdClas, IdCia,SUM(ValorAnticipo) AS StotAnt,SUM(ValorAbono) AS STotAbo 
  FROM  Trn_RecBuses  AS R  --INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
  WHERE ValorAnticipo>0 AND  R.IdVehiculo=@pmIdVehiculo
 GROUP BY  R.IdVehiculo,R.IdClase, IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryRecBuses_Cr @pmTipDoc VARCHAR(3), @pmReciboIni INT,@pmReciboFin INT, @pmIdCia CHAR(2)
AS
 SELECT  R.TipDoc AS IdTipRec,TipoDoc, Recibo,R.IdCia AS CodCia,Compania, R.Fecha AS FechaRec, R.IdConcepto AS IdConcept,Concepto, R.IdCliente AS NitPropietario,NP.RazonSocial AS Propietario, R.IdAgencia, R.Modalidad AS RecModalidad, TipCar, Cartulina, IdCiaCar,R.IdRuta,Ruta,IdRelevador
, R.IdVehiculo AS PlacaVeh, R.IdConductor AS CedConductor,NC.RazonSocial AS Conductor, R.VehPropio AS VehcPropio, R.CentInicial AS CenInicial, R.CentFinal AS CenFinal, NPasajeros, Recorridos,ValorRecibido, ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros
, ValorOtrosDct, ValorPagMas, ValorNeto,ValorAplicado, ValorAnticipo, ValorAbono, TotalProducido, TotalGastos, TotalAhorro, TotalOtros, TotalVales, TotalAbono, ValorPasaje, IdVendedor,R.Comision,IdTarCom, R.IdCCosto AS IdCCost,CCosto,EnEfectivo,R.Referencia AS RefeRecibo, BaseIva
, BaseRet, BaseOtr, TarifaIva, TarifaRet, TarifaOtr, IdTarIva,IdTarRet, IdTarOtr,R.IdClase AS CodClase, ClaseCuenta,R.TipCom AS IdTipoCom,TM.TipoCom AS TipoComprobante, Comprobante, IdCiaCom, Anulado, NumDev, FecDev, R.Observacion AS Observ, R.IdEstado AS IdEstad,ED.Estado AS EstadDoc, R.TimeSys, FecUpdate, IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario,Leyenda
,Item,F.IdForma AS IdForm,FormaPago,VrPagado,VrCambio,EsCaja,FP.NumForma AS NumeroCheque, FP.IdBanco AS IdBanc,Banco,CtaForma,Beneficiario,NitCliente,FecForma,CdCta,NumeroCta,FP.CdLocal AS CodPlaza,PZA.Localidad AS Plaza
, FP.Referncia1 AS RefCheque,Referncia2,VrDenom,Cantidad,TipDenom,FP.Detalle AS DetallePago,TipRef,DocRef,IdCiaRef
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,V.CentInicial AS VehCentInicio,V.CentFinal AS CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  Trn_RecBuses AS R INNER JOIN Sys_TiposDoc  AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN Trn_Pagos AS FP ON R.TipDoc=FP.TipDoc AND R.Recibo=FP.Documento AND R.IdCia=FP.IdCia
 INNER JOIN Companias  AS CI ON R.IdCia=CI.IdCia  INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado  INNER JOIN adm_Usuarios AS U ON  R.IdUsuario=U.IdUsuario
 INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS NP ON R.IdCliente=NP.IdTercero
 INNER JOIN Terceros AS NC ON R.IdConductor=NC.IdTercero  INNER JOIN BusClaseCue AS CU ON R.IdClase=CU.IdClase
 INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto  INNER JOIN Bancos AS B ON FP.IdBanco=B.IdBanco
 INNER JOIN Formaspago AS F ON FP.IdForma=F.IdForma 
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON  R.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN CtasCorrientes AS CTA ON FP.CdCta=CTA.IdCta  LEFT JOIN Localidades AS PZA ON FP.CdLocal=PZA.IdLocal
 LEFT JOIN Rutas AS RT ON  R.IdRuta=RT.IdRuta LEFT JOIN TiposCom AS TM ON R.TipCom=TM.IdCom
 WHERE R.TipDoc=@pmTipDoc AND  Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND   R.IdCia=@pmIdCia
 ORDER BY R.IdCia,Recibo,item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryRecBuses_Crl @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME , @pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdRuta VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null, @pmEnEfectivo BIT=Null,@pmAnulado BIT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdUsuario VARCHAR(11)=Null,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT Recibo, R.IdCia AS CodCia, R.Fecha AS FecRecibo,TipCar, R.Cartulina AS NumCartulina, IdCiaCar,CL.Fecha AS FecCartulina,NumOrden,FechaRec,R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,R.IdRuta AS CodRuta,Ruta
,R.CentInicial AS CentenaIni,R.CentFinal AS CentenaFin,R.NPasajeros AS NPasajers,R.Recorridos AS Recorrdos,R.ValorPasaje AS ValorPasje,TotalProducido,TotalGastos ,TotalVales,TotalGastos+TotalVales AS TtGastos,TotalAhorro,TotalOtros,TotalAbono,ValorTotal,R.ValorOtros AS ValorOtrs
,ValorOtrosDct,ValorPagMas,ValorNeto,ValorAplicado,ValorAnticipo,ValorRecibido,EnEfectivo,NumCheque, R.IdBanco AS CodBanco,Banco,Referencia,TipCom,TipoCom, Comprobante, IdCiaCom, R.Anulado AS Anlado, R.NumDev As Ndevolucion, R.FecDev AS FechDev,R.Observacion AS Observ
,R.IdEstado AS IdEstad, ED.Estado AS EstadDoc,R.TimeSys AS Time_sys, R.FecUpdate AS FechUpdate, R.IdCiaCrea AS CodCiaCrea,R.IdUsuario,Usuario,Compania,R.IdRelevador AS IdRelevadr,NR.RazonSocial AS Relevador,R.IdConcepto AS IdConcep,Concepto,R.IdClase AS IdClaCue,ClaseCuenta,R.VehPropio AS VPropio
,IdCliente,NP.RazonSocial AS Propietario,NumAutoriza
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TA.TipoAdmon AS VehTipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,V.CentInicial AS VehCentInicio,V.CentFinal AS VehCentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
  FROM Trn_RecBuses AS R INNER JOIN Trn_Despachos AS CL ON R.TipCar=CL.TipDoc AND R.Cartulina=CL.Cartulina AND R.IdCiaCar=CL.IdCia
  INNER JOIN Companias  AS CI ON R.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON  R.IdUsuario=U.IdUsuario 
  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo   INNER JOIN Terceros AS NP ON R.IdCliente=NP.IdTercero
  INNER JOIN Terceros AS NC ON R.IdConductor=NC.IdTercero  INNER JOIN BusClaseCue AS CU ON R.IdClase=CU.IdClase
  INNER JOIN Bancos AS B ON R.IdBanco=B.IdBanco  INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
    INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa   LEFT JOIN TercCndtores AS CT ON R.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN Rutas AS RT ON  R.IdRuta=RT.IdRuta LEFT JOIN Terceros AS NR ON R.IdRelevador=NR.IdTercero
  LEFT JOIN TiposCom AS TM ON R.TipCom=TM.IdCom
 WHERE R.TipDoc=@pmTipDoc  AND  R.Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND  R.Modalidad like ISNULL(@pmModalidad,'%') 
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND R.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 AND  (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  (R.Anulado=ISNULL(@pmAnulado,0) or R.Anulado=ISNULL(@pmAnulado,1))
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND R.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND R.IdConductor LIKE ISNULL(@pmIdConductor,'%')  AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY R.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryRecBuses_Crr @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME , @pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdRuta VARCHAR(4)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null, @pmEnEfectivo BIT=Null,@pmAnulado BIT=Null
,@pmIdEstado VARCHAR(4)=Null,@pmIdUsuario VARCHAR(11)=Null,@pmIdAdmon VARCHAR(4)=Null
AS
 SELECT  Recibo, R.IdCia AS CodCia, Fecha, R.IdVehiculo AS PlacaVeh,R.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,IdCliente,NP.RazonSocial AS Propietario,ValorRecibido, ValorTotal, ValorDcto, ValorReteFte, ValorReteIva, ValorReteIca, ValorOtros, ValorOtrosDct
, ValorPagMas, TotalAhorro, TotalOtros, ValorNeto,ValorAplicado,ValorAnticipo,R.IdCta AS CodCta,NumeroCta,EnEfectivo, NumCheque, R.IdBanco AS IdBanc,Banco,Referencia,R.IdClase AS IdClasCue,ClaseCuenta,R.Modalidad AS RecModalidad,R.IdConcepto AS IdConcep,Concepto
,TipCom,TipoCom, Comprobante, IdCiaCom, Anulado, NumDev, FecDev, R.Observacion AS Observ, R.IdEstado AS IdEstad,ED.Estado AS EstadDoc, TimeSys, FecUpdate, IdCiaCrea,R.IdUsuario,Usuario,Compania,BaseIva, BaseRet, BaseOtr
,TarifaIva, TarifaRet, TarifaOtr, IdTarIva,IdTarRet, IdTarOtr,TipCar, Cartulina, IdCiaCar,R.IdRuta AS CodRuta,Ruta,IdRelevador,NR.RazonSocial AS Relevador,R.CentInicial AS CentenaIni, R.CentFinal AS CentenaFin, NPasajeros, Recorridos,ValorPasaje,TotalProducido
,TotalGastos ,TotalVales,TotalGastos+TotalVales AS TtGastos,TotalAbono,R.VehPropio AS VhPropio
--inf. vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,V.CentInicial AS VehCentInicio,V.CentFinal AS VehCentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
  FROM Trn_RecBuses AS R  INNER JOIN Companias  AS CI ON R.IdCia=CI.IdCia
  INNER JOIN Conceptos AS C ON R.IdConcepto=C.IdConcepto  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
  INNER JOIN adm_Usuarios AS U ON  R.IdUsuario=U.IdUsuario
  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo   INNER JOIN Terceros AS NP ON R.IdCliente=NP.IdTercero
  INNER JOIN Terceros AS NC ON R.IdConductor=NC.IdTercero  INNER JOIN BusClaseCue AS CU ON R.IdClase=CU.IdClase
  INNER JOIN Bancos AS B ON R.IdBanco=B.IdBanco   INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON R.IdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN Rutas AS RT ON  R.IdRuta=RT.IdRuta LEFT JOIN CtasCorrientes AS CTA ON R.IdCta=CTA.IdCta LEFT JOIN Terceros AS NR ON R.IdRelevador=NR.IdTercero
  LEFT JOIN TiposCom AS TM ON R.TipCom=TM.IdCom
  WHERE TipDoc=@pmTipDoc AND  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND R.Modalidad like ISNULL(@pmModalidad,'%') 
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND R.IdRuta LIKE ISNULL(@pmIdRuta,'%') AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 AND  (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND  (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND R.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND R.IdConductor LIKE ISNULL(@pmIdConductor,'%')  AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY R.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecCheques @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdCliente,IdAgencia,VrTotal,PlacaVehic,Cantidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_RecCheques
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecChequesBus @pmTipDoc VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,C.Recibo AS NumRecibo,C.IdCia AS CdCia,Compania,C.IdCliente AS NitPropietario,T.RazonSocial AS Propietario
,PlacaVehic,NumVeh,VrTotal,Cantidad,OrigenAdd,C.Anulado AS EstaAnulado,NumDev,C.FecDev AS FechDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,TimeSys,FecUpdate,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,Leyenda
--INFORMACION DEL CHEQUE
,TipForma,Numero,H.IdBanco AS CodBanco,B.Banco AS NomBanco,H.Fecha AS FechaCheq,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle
,NitCliente,HC.RazonSocial AS CheNombCliente,H.CdAgencia AS CheIdAgencia,CdVend,V.RazonSocial AS NomVendedor,H.IdLocal AS CodCiudad,LP.Localidad AS CiudadPlaza
,H.TipDoc AS TipDocAplica,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco
,H.Anulado AS CheAnulado,H.FecDev AS CheFecAnulado,H.IdEstado AS CheCodEstado,HE.Estado AS CheEstado
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
--datos del vehículo
,VH.IdTipoVeh AS CdTipoVeh,TipoVehiculo,VH.IdMarca AS CdMarca,Marca,Modelo,TipoAfil,FecIngreso,VH.Inactivo AS VehInactivo
 FROM Trn_RecCheques AS C  INNER JOIN Trn_Cheques AS H ON C.TipDoc=H.TipRec  AND C.Recibo=H.Recibo AND C.IdCia=H.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN Bancos AS B ON H.IdBanco=B.IdBanco INNER JOIN Localidades AS LP ON H.IdLocal=LP.IdLocal
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 LEFT JOIN  Vehiculos AS VH ON C.PlacaVehic=VH.IdVehiculo 
 LEFT JOIN TiposVeh AS TV ON VH.IdTipoVeh=TV.IdTipoVeh LEFT JOIN Marcas AS M ON VH.IdMarca=M.IdMarca
  LEFT JOIN Terceros AS HC ON H.NitCliente=HC.IdTercero  LEFT JOIN Terceros AS V ON H.CdVend=V.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON H.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON  CTA.IdBanco=BCT.IdBanco
 LEFT JOIN EstadoDoc AS HE ON H.IdEstado=HE.IdEstado
 WHERE C.TipDoc=@pmTipDoc AND C.Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND C.IdCia=@pmIdCia
 ORDER BY C.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecChequesLta  @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmReciboIni INT=Null,@pmReciboFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Recibo,IdCia,Fecha,IdCliente,RazonSocial,IdAgencia,PlacaVehic,VrTotal,Cantidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_RecCheques AS C INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario 
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') 
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecChequesPre @pmTipDoc VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,C.Recibo AS NumRecibo,C.IdCia AS CdCia,Compania,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS Cd_Agencia
,NomAgencia,VrTotal,Cantidad,OrigenAdd,PlacaVehic,C.Anulado AS EstaAnulado,NumDev,C.FecDev AS FechDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,TimeSys,FecUpdate,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,Leyenda
--INFORMACION DEL CHEQUE
,TipForma,Numero,H.IdBanco AS CodBanco,B.Banco AS NomBanco,H.Fecha AS FechaCheq,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle
,NitCliente,HC.RazonSocial AS CheNombCliente,H.CdAgencia AS CheIdAgencia,CdVend,V.RazonSocial AS NomVendedor,H.IdLocal AS CodCiudad,LP.Localidad AS CiudadPlaza
,H.TipDoc AS TipDocAplica,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig,IdCiaCon,FecConsig,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco
,BCT.Banco AS CtaBanco,H.Anulado AS CheAnulado,H.FecDev AS CheFecAnulado,H.IdEstado AS CheCodEstado,HE.Estado AS CheEstado
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,EsExento,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero
, DirAgencia,IdLocalAge,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto
 FROM Trn_RecCheques AS C  INNER JOIN Trn_Cheques AS H ON C.TipDoc=H.TipRec  AND C.Recibo=H.Recibo AND C.IdCia=H.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero 
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN Bancos AS B ON H.IdBanco=B.IdBanco INNER JOIN Localidades AS LP ON H.IdLocal=LP.IdLocal
 INNER JOIN  TercCliePrestamo AS CLI ON C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona  INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo  INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Localidades AS LA ON CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Terceros AS HC ON H.NitCliente=HC.IdTercero LEFT JOIN Terceros AS V ON H.CdVend=V.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON H.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON  CTA.IdBanco=BCT.IdBanco
 LEFT JOIN EstadoDoc AS HE ON H.IdEstado=HE.IdEstado LEFT JOIN TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 WHERE C.TipDoc=@pmTipDoc AND C.Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND C.IdCia=@pmIdCia
 ORDER BY C.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecChequesRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT C.TipDoc AS Tip_Doc,C.Recibo AS NumRecibo,C.IdCia AS CdCia,Compania,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS Cd_Agencia
,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,PlacaVehic,VrTotal,Cantidad,OrigenAdd,C.Anulado AS EstaAnulado,NumDev,C.FecDev AS FechDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,TimeSys,FecUpdate,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS IdUsuari,Usuario
--INFORMACION DEL CHEQUE
,TipForma,Numero,H.IdBanco AS CodBanco,B.Banco AS NomBanco,H.Fecha AS FechaCheq,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle
,NitCliente,HC.RazonSocial AS CheNombCliente,H.CdAgencia AS CheIdAgencia,HA.Agencia AS CheAgencia,HA.CodAgencia AS CheCodAgenc,HA.Referencia AS AgeReferencia
,CdVend,V.RazonSocial AS NomVendedor,H.IdLocal AS CodCiudad,LP.Localidad AS CiudadPlaza,H.TipDoc AS TipDocAplica,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig
,IdCiaCon,FecConsig,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco,H.Anulado AS CheAnulado,H.FecDev AS CheFecAnulado,H.IdEstado AS CheCodEstado,HE.Estado AS CheEstado
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_RecCheques AS C  INNER JOIN Trn_Cheques AS H ON C.TipDoc=H.TipRec  AND C.Recibo=H.Recibo AND C.IdCia=H.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero INNER JOIN Agencias  AS A ON C.IdAgencia=A.IdAgencia
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  
 INNER JOIN Bancos AS B ON H.IdBanco=B.IdBanco INNER JOIN Localidades AS LP ON H.IdLocal=LP.IdLocal
 INNER JOIN TercCliente AS CLI ON C.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS HC ON H.NitCliente=HC.IdTercero  LEFT JOIN Agencias  AS HA ON H.CdAgencia=HA.IdAgencia
 LEFT JOIN Terceros AS V ON H.CdVend=V.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON H.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON  CTA.IdBanco=BCT.IdBanco
 LEFT JOIN EstadoDoc AS HE ON H.IdEstado=HE.IdEstado
 WHERE C.TipDoc=@pmTipDoc  AND C.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND C.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND C.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')   AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (C.Anulado=ISNULL(@pmAnulado,0) or C.Anulado=ISNULL(@pmAnulado,1))
 ORDER BY C.IdCia,C.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecCheques_Cr @pmTipDoc VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT C.TipDoc AS Tip_Doc,TipoDoc,C.Recibo AS NumRecibo,C.IdCia AS CdCia,Compania,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS Cd_Agencia
,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrTotal,Cantidad,OrigenAdd,PlacaVehic,C.Anulado AS EstaAnulado,NumDev,C.FecDev AS FechDev
,C.Observacion AS Observ,C.IdEstado AS CdEstado,ED.Estado AS EstadoDoc,TimeSys,FecUpdate,C.IdCiaCrea AS CdCiaCrea,C.IdUsuario AS IdUsuari,Usuario,Leyenda
--INFORMACION DEL CHEQUE
,TipForma,Numero,H.IdBanco AS CodBanco,B.Banco AS NomBanco,H.Fecha AS FechaCheq,FecPago,VrForma,CtaForma,Beneficiario,Referncia1,Referncia2,Detalle
,NitCliente,HC.RazonSocial AS CheNombCliente,H.CdAgencia AS CheIdAgencia,HA.Agencia AS CheAgencia,HA.CodAgencia AS CheCodAgenc,HA.Referencia AS AgeReferencia
,CdVend,V.RazonSocial AS NomVendedor,H.IdLocal AS CodCiudad,LP.Localidad AS CiudadPlaza,H.TipDoc AS TipDocAplica,Documento,IdCiaDoc,FecDoc,TipCon,NumConsig
,IdCiaCon,FecConsig,CdCta,NumeroCta,CTA.IdBanco AS CtaIdBanco,BCT.Banco AS CtaBanco,H.Anulado AS CheAnulado,H.FecDev AS CheFecAnulado,H.IdEstado AS CheCodEstado,HE.Estado AS CheEstado
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_RecCheques AS C  INNER JOIN Trn_Cheques AS H ON C.TipDoc=H.TipRec  AND C.Recibo=H.Recibo AND C.IdCia=H.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero INNER JOIN Agencias  AS A ON C.IdAgencia=A.IdAgencia
 INNER JOIN EstadoDoc AS ED ON C.IdEstado=ED.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 INNER JOIN Bancos AS B ON H.IdBanco=B.IdBanco INNER JOIN Localidades AS LP ON H.IdLocal=LP.IdLocal
 INNER JOIN TercCliente AS CLI ON C.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS HC ON H.NitCliente=HC.IdTercero  LEFT JOIN Agencias  AS HA ON H.CdAgencia=HA.IdAgencia
 LEFT JOIN Terceros AS V ON H.CdVend=V.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON H.CdCta=CTA.IdCta LEFT JOIN Bancos AS BCT ON  CTA.IdBanco=BCT.IdBanco
 LEFT JOIN EstadoDoc AS HE ON H.IdEstado=HE.IdEstado
 WHERE C.TipDoc=@pmTipDoc AND C.Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND C.IdCia=@pmIdCia
 ORDER BY C.Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryRecFactura @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipRec,Recibo,IdCia,Item,TipDoc,Documento,IdCiaDoc,ItemDoc,Fecha,IdCargo,IdClase,ValorAbono,Devolucion,Descuento,IdCliente,IdAgencia,IdVehiculo,VehPropio,IdVendedor,Comision,Detalle,FecPago,AplAntcpo,TimeSys,IdUsuario
 FROM Trn_RecFactura
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaApl  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
AS
  SELECT  TipDoc, Documento, IdCiaDoc, ItemDoc, SUM(ValorAbono) AS STOTABO,SUM(Devolucion) AS STOTDEV
  FROM     Trn_RecFactura
  WHERE  TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia
 GROUP BY TipDoc, Documento, IdCiaDoc, ItemDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaCla @pmIdVehiculo VARCHAR(10),@pmIdCliente VARCHAR(16)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdCiaDoc CHAR(2)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdConcepto VARCHAR(4)=Null
AS
  SELECT SUM(R.ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
  FROM  Trn_RecFactura AS R INNER JOIN Trn_CausacVence AS VC ON R.TipDoc=VC.TipDoc AND R.Documento=VC.Documento AND R.IdCiaDoc=VC.IdCia AND R.ItemDoc=VC.Item
  WHERE R.IdVehiculo=@pmIdVehiculo AND  R.IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND R.TipDoc LIKE ISNULL(@pmTipDoc,'%')
  AND IdCiaDoc LIKE ISNULL(@pmIdCiaDoc,'%%') AND VC.IdClase LIKE ISNULL(@pmIdClase,'%') AND IdConcepto LIKE ISNULL(@pmIdConcepto,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaCli @pmIdCliente VARCHAR(16)=Null,@pmAplAntcpo BIT=Null
AS
 IF @pmAplAntcpo IS NULL
  SELECT IdCliente,SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
  FROM     Trn_RecFactura
  WHERE  IdCliente LIKE ISNULL(@pmIdCliente,'%')
  GROUP BY  IdCliente
ELSE
  SELECT IdCliente,SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
  FROM   Trn_RecFactura
  WHERE AplAntcpo=@pmAplAntcpo AND  IdCliente LIKE ISNULL(@pmIdCliente,'%')
  GROUP BY  IdCliente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaDoc  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmItemDoc INT
AS
 SELECT  TipRec, Recibo, IdCia, Item, TipDoc, Documento, IdCiaDoc, ItemDoc, Fecha, IdCargo, IdClase, ValorAbono, Devolucion, Descuento, IdCliente, IdAgencia, 
                       IdVehiculo, VehPropio, IdVendedor, Comision, Detalle,FecPago,AplAntcpo, TimeSys, IdUsuario
 FROM     Trn_RecFactura
  WHERE  TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
  AND (ItemDoc>=ISNULL(@pmItemDoc,0)  AND ItemDoc<=ISNULL(@pmItemDoc,2147483647))
 ORDER BY TipRec, Recibo, IdCia, Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaDocA  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmItemDoc INT=Null
AS
 SELECT  TipRec, Recibo, IdCia, Item, TipDoc, Documento, IdCiaDoc, ItemDoc, Fecha, IdCargo, IdClase, ValorAbono, Devolucion, Descuento
, IdCliente, IdAgencia,IdVehiculo, VehPropio, IdVendedor, Comision, Detalle,FecPago,AplAntcpo, TimeSys, IdUsuario
 FROM     Trn_RecFactura
 WHERE  TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc AND AplAntcpo=0 
 AND (ItemDoc>=ISNULL(@pmItemDoc,0)  AND ItemDoc<=ISNULL(@pmItemDoc,2147483647))
 ORDER BY TipRec, Recibo, IdCia, Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaLta  @pmTipRec VARCHAR(3),@pmIdCia CHAR(2),@pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdClase VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdVehiculo VARCHAR(10)

AS
  SELECT  TipRec, Recibo, IdCia, Item, Fecha, TipDoc, Documento, IdCiaDoc, ItemDoc, ValorAbono, Devolucion, Descuento, IdCliente,T.RazonSocial AS NomCliente, IdAgencia, 
                      IdVehiculo, VehPropio, IdVendedor,C.RazonSocial AS Conductor,Comision, Detalle,R.IdClase AS ClaCue,IdCargo,FecPago,AplAntcpo,TimeSys,R.IdUsuario AS IdUsuari
  FROM   Trn_RecFactura AS R INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero LEFT JOIN Terceros AS C ON R.IdVendedor=C.IdTercero
  WHERE  IdCia=@pmIdCia AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND TipRec like ISNULL(@pmTipRec,'%')  AND TipDoc like ISNULL(@pmTipDoc,'%')
 AND R.IdClase like ISNULL(@pmIdClase,'%') AND IdCliente like ISNULL(@pmIdCliente,'%') AND IdVehiculo like ISNULL(@pmIdVehiculo,'%')
 ORDER BY TipRec,Recibo,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaRec  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
AS
  SELECT  Item,Fecha,TipDoc, Documento, IdCiaDoc, ItemDoc,R.IdCargo AS IdCarg,Concepto,R.IdClase AS IdClas, ValorAbono, Devolucion, Descuento, IdCliente,T.RazonSocial AS Cliente,IdAgencia, 
                      IdVehiculo, VehPropio, IdVendedor,V.RazonSocial AS Vendedor, Comision, Detalle,FecPago,AplAntcpo, TimeSys, R.IdUsuario AS IdUsuari,TipRec, Recibo, IdCia
  FROM  Trn_RecFactura  AS R INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
    LEFT JOIN Terceros AS V ON R.IdVendedor=V.IdTercero
   LEFT JOIN Fijos AS F ON R.IdCargo=F.IdCargo
  WHERE  TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaTot  @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
AS
  SELECT  IdCliente,IdVehiculo, SUM(ValorAbono) AS STOTABO,SUM(Devolucion) AS STOTDEV
  FROM   Trn_RecFactura
  WHERE  TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia
  GROUP BY IdCliente,IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFacturaTpa @pmIdVehiculo VARCHAR(10),@pmAplAntcpo BIT=Null
AS
 IF @pmAplAntcpo IS NULL 
  SELECT  SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev, SUM(Descuento) AS STotDct
  FROM     Trn_RecFactura
  WHERE  IdVehiculo=@pmIdVehiculo
 ELSE
  SELECT  SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev, SUM(Descuento) AS STotDct
  FROM     Trn_RecFactura
  WHERE  IdVehiculo=@pmIdVehiculo  AND AplAntcpo=@pmAplAntcpo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecFacturaVeh @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmAplAntcpo BIT=Null
AS
 IF @pmAplAntcpo IS NULL
 BEGIN 
  IF @pmIdVehiculo IS NULL
    SELECT R.IdVehiculo AS IdVehic,R.IdClase AS IdClas,IdCia,SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
   FROM   Trn_RecFactura AS R  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
   WHERE  IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
  GROUP BY R.IdVehiculo,R.IdClase,IdCia
 ELSE
    SELECT R.IdVehiculo AS IdVehic,R.IdClase AS IdClas,IdCia,SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
   FROM   Trn_RecFactura AS R -- INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
   WHERE  R.IdVehiculo=@pmIdVehiculo
  GROUP BY R.IdVehiculo,R.IdClase,IdCia
 END
ELSE
 BEGIN
  IF @pmIdVehiculo IS NULL
    SELECT R.IdVehiculo AS IdVehic,R.IdClase AS IdClas,IdCia,SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
   FROM   Trn_RecFactura AS R  INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
   WHERE AplAntcpo=@pmAplAntcpo AND  IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
  GROUP BY R.IdVehiculo,R.IdClase,IdCia
 ELSE
    SELECT R.IdVehiculo AS IdVehic,R.IdClase AS IdClas,IdCia,SUM(ValorAbono) AS STotAbo,SUM(Devolucion) AS STotDev
   FROM   Trn_RecFactura AS R -- INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
   WHERE  R.IdVehiculo=@pmIdVehiculo AND AplAntcpo=@pmAplAntcpo
  GROUP BY R.IdVehiculo,R.IdClase,IdCia
 END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFactura_Ant  @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2)
AS
  SELECT  TipRec, RF.Recibo AS Nrecibo, RF.IdCia AS CodCia,RF.IdVehiculo AS Placaveh,ValorAnticipo,R.ValorAbono AS AbonoAnt,SUM(RF.ValorAbono) AS TotalAbono
  FROM  Trn_RecFactura AS RF INNER JOIN Trn_RecBuses AS R ON RF.TipRec=R.TipDoc AND RF.Recibo=R.Recibo AND RF.IdCia=R.IdCia
 WHERE RF.TipDoc=@pmTipDoc AND Documento=@pmDocumento  AND IdCiaDoc=@pmIdCiaDoc  AND AplAntcpo<>0
 GROUP BY  TipRec, RF.Recibo, RF.IdCia,RF.IdVehiculo,ValorAnticipo,R.ValorAbono

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecFactura_Cr  @pmTipRec VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
AS
  SELECT  TipRec,TR.TipoDoc AS TipoRecibo, Recibo, RF.IdCia AS CodCia,CR.Compania AS CompaniaRec, Item, TipDoc,TD.TipoDoc AS TipoDocumeto, Documento, IdCiaDoc,CD.Compania AS CompaniaDoc, ItemDoc, Fecha
, RF.IdCargo AS CodCargo,Concepto,RF.IdClase AS CodClase,ClaseCuenta,ValorAbono, Devolucion, Descuento, IdCliente,NP.RazonSocial AS Propietario, IdAgencia,RF.IdVehiculo AS PlacaVeh,NumVeh,RF.VehPropio, IdVendedor,NC.RazonSocial AS Conductor
, Comision, Detalle,FecPago,AplAntcpo,TimeSys, RF.IdUsuario AS Idusuari
  FROM  Trn_RecFactura AS RF INNER JOIN Sys_TiposDoc  AS TR ON RF.TipRec=TR.IdDoc  INNER JOIN Sys_TiposDoc  AS TD ON RF.TipDoc=TD.IdDoc
 INNER JOIN Companias  AS CR ON RF.IdCia=CR.IdCia INNER JOIN Companias  AS CD ON RF.IdCiaDoc=CD.IdCia
 INNER JOIN BusClaseCue AS CU ON RF.IdClase=CU.IdClase INNER JOIN Fijos AS F ON RF.IdCargo=F.IdCargo
 INNER JOIN Vehiculos AS V ON RF.IdVehiculo=V.IdVehiculo  INNER JOIN Terceros AS NP ON RF.IdCliente=NP.IdTercero 
 INNER JOIN Terceros AS NC ON RF.IdVendedor=NC.IdTercero
 WHERE  TipRec=@pmTipRec  AND Recibo between @pmReciboIni  AND @pmReciboFin AND  RF.IdCia=@pmIdCia
ORDER BY TipRec,RF.IdCia, Recibo,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecFactura_Cra @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmTipRec VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null,@pmTipDoc VARCHAR(3)=Null
,@pmIdCargo VARCHAR(4)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdAdmon VARCHAR(4)=Null,@pmIdVendedor VARCHAR(16)=Null,@pmIdUsuario VARCHAR(11)=Null,@pmRecibo INT=Null,@pmDocumento INT=Null
AS
 --cancelaciones vs facturas
  SELECT  RF.TipRec AS IdTipRec,TR.TipoDoc AS TipoRecibo, RF.Recibo AS NumRecibo, RF.IdCia AS CodCia,CR.Compania AS CompaniaRec, RF.Item AS ItemRec,Fecha,RF.TipDoc AS IdTipDoc,TD.TipoDoc AS TipoDocumeto
, RF.Documento AS NumFactura, IdCiaDoc,CD.Compania AS CompaniaDoc, ItemDoc,RF.ValorAbono AS ValrAbono, Devolucion,RF.IdCliente AS NitPropietario,NP.RazonSocial AS Propietario,RF.IdVehiculo AS PlacaVeh
,RF.VehPropio, RF.IdVendedor AS CedConductor,NC.RazonSocial AS Conductor,RF.Detalle AS DetalleRec,VC.IdCargo AS CodCargo,F.Concepto AS ConceptoCargo,VC.IdClase AS CodClase,ClaseCuenta
,AplAntcpo,FecEmision, FecVence, ValorFactura,Factura,Referencia,VC.Detalle AS DetalleDoc,VC.IdConcepto AS IdConcep,C.Concepto AS ConceptoDoc,FecUltPago,NotaInt,FecUltLiquida,RF.IdUsuario AS Idusuari,Usuario,FecPago,Descuento,TimeSys
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
 FROM  Trn_RecFactura AS RF INNER JOIN Trn_CausacVence AS VC ON RF.TipDoc=VC.TipDoc AND RF.Documento=VC.Documento AND RF.IdCiaDoc=VC.IdCia AND RF.ItemDoc=VC.Item
 INNER JOIN Sys_TiposDoc  AS TR ON RF.TipRec=TR.IdDoc  INNER JOIN Sys_TiposDoc  AS TD ON RF.TipDoc=TD.IdDoc
 INNER JOIN Companias  AS CR ON RF.IdCia=CR.IdCia  INNER JOIN Companias  AS CD ON RF.IdCiaDoc=CD.IdCia
 INNER JOIN Terceros AS NP ON RF.IdCliente=NP.IdTercero   INNER JOIN Terceros AS NC ON RF.IdVendedor=NC.IdTercero
 INNER JOIN adm_Usuarios AS U ON RF.IdUsuario=U.IdUsuario  INNER JOIN Vehiculos AS V ON RF.IdVehiculo=V.IdVehiculo
 INNER JOIN BusClaseCue  AS CU ON VC.IdClase=CU.IdClase  INNER JOIN Fijos AS F ON VC.IdCargo=F.IdCargo
 INNER JOIN Conceptos AS C ON VC.IdConcepto=C.IdConcepto   INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin  AND RF.TipRec LIKE ISNULL(@pmTipRec,'%') AND RF.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND RF.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 AND VC.IdCargo LIKE ISNULL(@pmIdCargo,'%') AND VC.IdClase LIKE ISNULL(@pmIdClase,'%') AND RF.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND RF.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND VC.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%') AND RF.IdVendedor LIKE ISNULL(@pmIdVendedor,'%')
 AND RF.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 AND ( RF.Recibo>=ISNULL(@pmRecibo,0)  AND  RF.Recibo<=ISNULL(@pmRecibo,2147483647))
 AND ( RF.Documento>=ISNULL(@pmDocumento,0)  AND  RF.Documento<=ISNULL(@pmDocumento,2147483647))
ORDER BY RF.TipRec,RF.IdCia, RF.Recibo,RF.Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRecTanqueos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Item,Fecha,Documento,IdTipoCom,TipoComb,Cantidad,ValorUnitario,R.IdEstacion AS CdEstacion,EstServicio
 FROM Trn_RecTanqueos AS R LEFT JOIN EdsServicio AS ED ON R.IdEstacion=ED.IdEstacion
 LEFT JOIN TiposFuel AS TF ON R.IdTipoCom=TF.IdCom
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 ORDER BY  Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryRecTanqueos_Crr @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdTipoCom VARCHAR(4)=Null
,@pmIdEstacion VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null,@pmAnulado BIT=Null
,@pmIdUsuario VARCHAR(11)=Null,@pmIdEstado VARCHAR(4)=Null
AS
 SELECT RT.Recibo AS NumRecibo, RT.IdCia AS CodCia, Item, RT.Fecha AS FechaRec, Documento, RT.IdTipoCom AS CodTipoCom,TipoComb, Cantidad, ValorUnitario,Cantidad*ValorUnitario AS ValorTotal
,RT.IdEstacion AS CodEstacion,EstServicio,Modalidad,TipCar,Cartulina,IdCiaCar,R.IdVehiculo AS PlacaVeh,NumVeh,IdCliente AS NitPropietario,T.RazonSocial AS Propietario,R.IdConductor AS CedConductor
,NC.RazonSocial AS Conductor,R.VehPropio AS VhPropio,R.CentInicial AS CentenaIni,R.CentFinal AS CentenaFin,Recorridos,TotalProducido,TotalGastos,TotalVales,TotalGastos*TotalVales AS SumaGastos
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdUsuario AS IdUsuari,Usuario,Compania,RT.TipDoc AS TipDocm,TimeSys
 FROM  Trn_RecTanqueos AS RT  INNER JOIN Trn_RecBuses AS R ON RT.TipDoc=R.TipDoc AND RT.Recibo=R.Recibo AND RT.IdCia=R.IdCia
 INNER JOIN  Companias AS CI ON RT.IdCia=CI.IdCia  INNER JOIN EdsServicio AS E ON RT.IdEstacion=E.IdEstacion
 INNER JOIN TiposFuel AS TC ON RT.IdTipoCom=TC.IdCom INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero 
 INNER JOIN Terceros AS NC ON R.IdConductor=NC.IdTercero INNER JOIN Vehiculos AS V ON R.IdVehiculo=V.IdVehiculo
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 WHERE RT.Fecha BETWEEN @pmFechaIni  AND @pmFechaFin AND RT.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND RT.IdTipoCom LIKE ISNULL(@pmIdTipoCom,'%') AND RT.IdEstacion LIKE ISNULL(@pmIdEstacion,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND R.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND R.IdUsuario LIKE ISNULL(@pmIdUsuario,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 ORDER BY RT.IdCia,RT.Recibo,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto
,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdCCosto
,IdSubCos,IdCajero,IdVend,TarifaCom,CodTarCom,IdLocal,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor
,TipDcm,Documento,IdCiaDcm,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Recibos
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecibosAcu @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrNeto) AS STOTNET
 FROM Trn_Recibos
 WHERE YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY  IdCliente,IdAgencia,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecibosDoc  @pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipDcm VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Recibo,IdCia,Fecha,IdConcepto,IdCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal
,IdEstado,TipDcm,Documento,IdCiaDcm,Anulado
 FROM Trn_Recibos
 WHERE Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm AND TipDcm LIKE ISNULL(@pmTipDcm,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecibosLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmReciboIni INT=Null,@pmReciboFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdLocal VARCHAR(8)=Null,@pmEnEfectivo BIT=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS 
 SELECT Recibo,IdCia,Fecha,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,FecPago,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto
,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv,CodTarRet,CodTarIca,CodTarRiv,IdCCosto,IdSubCos,IdCajero,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,R.IdLocal AS CdCiudad
,EnEfectivo,CdForma,NumForma,DetallePago,CdBanco,CdCta,Referencia,pVehiculo,CdConductor,TipDcm,Documento,IdCiaDcm,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom
,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Recibos AS R INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Recibo BETWEEN ISNULL(@pmReciboIni,0) AND ISNULL(@pmReciboFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')  AND R.IdLocal LIKE ISNULL(@pmIdLocal,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 ORDER BY IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecibosRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdCajero VARCHAR(11)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocal VARCHAR(8)=Null,@pmEnEfectivo BIT=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Recibo,R.IdCia AS CodCia,Compania,Fecha,FecPago,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,R.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA
,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv
,R.IdCCosto AS IdCenCost,CCosto,R.IdSubCos AS IdSubCent,SubCosto,IdCajero,UC.Usuario AS NomCajero
,R.IdVend AS NitVend,V.RazonSocial AS Vendedor,TarifaCom,R.IdLocal AS CdCiudad,LR.Localidad AS CiudadRecibo,EnEfectivo,CdForma,NumForma,DetallePago
,CdBanco,BR.Banco AS RecBanco,CdCta,R.Referencia AS Referncia,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,TipDcm,Documento,IdCiaDcm
,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Recibos AS R  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero  INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN Agencias  AS A ON R.IdAgencia=A.IdAgencia INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario  INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario 
 INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN TercCliente AS CLI ON R.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Localidades AS LR ON R.IdLocal=LR.IdLocal LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco LEFT JOIN Terceros AS TC ON R.CdConductor=TC.IdTercero
 WHERE  TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND R.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND R.IdVend LIKE ISNULL(@pmIdVend,'%') AND IdCajero LIKE ISNULL(@pmIdCajero,'%')
 AND R.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (EnEfectivo=ISNULL(@pmEnEfectivo,0) or EnEfectivo=ISNULL(@pmEnEfectivo,1))
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY R.IdCia,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRecibos_Cr @pmTipDoc VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS Tip_Doc,TipoDoc,Recibo,R.IdCia AS CodCia,Compania,R.Fecha AS FechaRec,FecPago,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente
,R.IdAgencia AS IdAgenc,A.Agencia AS NomAgencia,A.CodAgencia AS Cod_Agencia,VrRecibido,VrSubTotal,VrDescuento,VrRetencion,VrReteICA,VrReteIVA
,VrOtros,VrOtrDcto,VrAhorro,VrPagosMas,VrNeto,VrAplicado,VrAnticipo,VrEfectivo,BaseRet,BaseIca,BaseRiv,TarifaRet,TarifaIca,TarifaRiv
,R.IdCCosto AS IdCenCost,CC.CCosto AS CentCosto,R.IdSubCos AS IdSubCent,SC.SubCosto AS SubcCosto,IdCajero,UC.Usuario AS NomCajero
,R.IdVend AS NitVend,V.RazonSocial AS Vendedor,TarifaCom,R.IdLocal AS CdCiudad,LR.Localidad AS CiudadRecibo,EnEfectivo,CdForma,R.NumForma AS RecNumForma,DetallePago
,CdBanco,BR.Banco AS RecBanco,R.CdCta AS RecCodCta,R.Referencia AS RecReferencia,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,TipDcm,R.Documento AS NumDocmto,IdCiaDcm
,Modalidad,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
,TimeSys,R.FecUpdate AS Fec_Update,IdCiaCrea,R.IdUsuario AS IdUsuari,U.Usuario AS NombreUsuario,Leyenda
--detalle de pagos
,Item,P.IdForma AS CdForma,FormaPago,Detalle,VrPagado,VrCambio,EsCaja,P.IdBanco AS PagCdBanco,BF.Banco AS PagBanco,NitCliente,PC.RazonSocial AS NomCliente
,CdAgencia,PA.Agencia AS PagNomAgencia,PA.CodAgencia AS PagCodAgencia,PA.Referencia AS PagRefeAgencia,P.NumForma AS PagNumForma,FecForma,CtaForma,Beneficiario,Referncia1
,P.Referncia2 AS PagReferencia2,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,Cantidad,TipDenom,TipRef,DocRef,IdCiaRef
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CliCodBanco,BC.Banco AS CliBanco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Recibos AS R  INNER JOIN Trn_Pagos AS P ON R.TipDoc=P.TipDoc AND R.Recibo=P.Documento AND R.IdCia=P.IdCia
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero  INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN Agencias  AS A ON R.IdAgencia=A.IdAgencia
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia  INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN adm_Usuarios AS UC ON R.IdCajero=UC.IdUsuario 
 INNER JOIN CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto 
 INNER JOIN Formaspago AS FP ON P.IdForma=FP.IdForma INNER JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 INNER JOIN TercCliente AS CLI ON R.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Localidades AS LR ON R.IdLocal=LR.IdLocal LEFT JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposCom AS TCM ON R.TipCom=TCM.IdCom
 LEFT JOIN Bancos AS BR ON R.CdBanco=BR.IdBanco LEFT JOIN Terceros AS TC ON R.CdConductor=TC.IdTercero
 LEFT JOIN Terceros AS PC ON P.NitCliente=PC.IdTercero  LEFT JOIN Agencias  AS PA ON P.CdAgencia=PA.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 WHERE R.TipDoc=@pmTipDoc AND Recibo BETWEEN @pmReciboIni AND @pmReciboFin AND R.IdCia=@pmIdCia
 ORDER BY Recibo,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRegimenDian @pmIdRegimen VARCHAR(4)
 AS 
 IF @pmIdRegimen IS NULL
  SELECT IdRegimen,Regimen
  FROM RegimenDian
  WHERE Inactivo=0
 ORDER BY Regimen
 ELSE
  SELECT IdRegimen,Regimen,Inactivo
  FROM RegimenDian
  WHERE IdRegimen=@pmIdRegimen
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRegistro @pmIdCia CHAR(2)=Null
 AS 
 SELECT IdCia,IdEmpresa,NitEmpresa,DvNit,RazonSocial,Descripcion,Direccion,Telefono,Fax,R.IdLocal AS CodCiu
,R.IdPais AS IdPai,e_mail,ArchivoLogo,NumeroSerial,Regimen,Licencia,VigenciaLic,Personeria,Leyenda,FechaRegistro,FechaVence,CurrtyDate
,CodISO,NombrePais,P.IdMneda AS CodMnda,Mneda,Simbolo,MneLocal,VrCambio,Localidad,TipoId,NitRepLegal
,RepLegal,TipoPersna,CIIUDane,CurDate1,FechaPrest
 FROM adm_Registro AS R INNER JOIN adm_Paises AS P ON R.IdPais=P.IdPais 
 INNER JOIN adm_monedas AS M ON P.IdMneda=M.IdMneda
 INNER JOIN Localidades AS  L ON R.IdLocal=L.IdLocal
 WHERE IdCia  LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRelaciones @pmIdTabla VARCHAR(20)=Null,@pmTablaForanea VARCHAR(20)=Null,@pmCampoForaneo VARCHAR(20)=Null
 AS 
 SELECT IdTabla,TablaForanea,CampoForaneo,BasedeDatos,NoValidar
 FROM Sys_Relaciones
 WHERE IdTabla LIKE ISNULL(@pmIdTabla, '%') AND TablaForanea LIKE ISNULL(@pmTablaForanea,'%') 
 AND CampoForaneo LIKE ISNULL(@pmCampoForaneo,'%')
 ORDER BY IdTabla,TablaForanea,CampoForaneo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRelacionesEnt @pmIdTabla VARCHAR(20), @pmNoValidar BIT=Null
AS
 SELECT R.IdTabla AS TablaPmry,TablaForanea,CampoForaneo,BasedeDatos,NoValidar
,NivelRel,CamposPK,FngKey,Sp_Qry,Sp_Qpmt,Sp_Ins, Sp_Upd, Sp_Del
 FROM Sys_Relaciones AS R INNER JOIN Sys_Entidades AS E ON  R.IdTabla=E.IdTabla
 WHERE  R.IdTabla=@pmIdTabla  
 AND (NoValidar=ISNULL(@pmNoValidar,0) or NoValidar=ISNULL(@pmNoValidar,1))
 ORDER BY TablaForanea,CampoForaneo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRelacionesFor @pmIdTabla VARCHAR(20)
 AS 
 SELECT R.IdTabla AS NomTabla,TablaForanea,CampoForaneo,BasedeDatos,NoValidar,NivelRel
 FROM Sys_Relaciones AS R LEFT JOIN Sys_Entidades AS E ON R.TablaForanea=E.IdTabla
 WHERE R.IdTabla=@pmIdTabla
 ORDER BY  NivelRel
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemision @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Remision,IdCia,Fecha,FechaDesp,FechaVence,IdConcepto,IdCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto
,Cantidad,Unidades,IdVend,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,NitEmpTrans,EmpTrans
,pVehiculo,CdConductor,DetalleEnvio,IdForma,DetallePago,MulPlazos, IdPlazo, CdRuta, FechaPed,TipFac,Factura,IdCiaFac,FechaFact,TipPed,Pedido,IdCiaPed,NumAutoriza,Modalidad,OrigenAdd,Anulado
,FecDev,Observacion,IdEstado,NumAprob,FecAprob,CdUsuAprob,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Remision
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemisionDet  @pmRemision INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,K.IdProducto AS CdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega,Salidas,VrPrecio,Salidas*VrPrecio AS VrSubTotal
,TarifaDct,VrDctoSal,TarifaIva,VrIvaSal,((Salidas*VrPrecio)-VrDctoSal)+VrIvaSal AS VrTotal,VrBruto,K.ListaPrec AS LtaPrec,VrBase,K.Referencia AS Referncia,Referencia2,K.Unidades AS Undades
,CodTarDct,CodTarIva,K.IdVend AS NitVend,V.RazonSocial AS Vendedor,Comision,K.CodTarCom AS CdTarifCom,DescripProd,Servcios,Tanques,CdTanque,CdMoneda,VrTasaCamb
,R.Fecha AS FecRemision,FechaDesp,DiasEntraga,FechaVence,IdCliente,T.RazonSocial AS Cliente,IdAgencia,Modalidad,Cantidad,R.Unidades AS TotalUnd
,DirEnvio,IdLocEnv,Localidad,NitContac,NomContac,TelContac,emlContac,CargoContac,NitEmpTrans,EmpTrans
,R.pVehiculo AS PlacaVeh,DetalleEnvio,TipPed,Pedido,IdCiaPed,FechaPed
 FROM  Trn_Remision AS R INNER JOIN Trn_Kardex AS K ON R.TipDoc=K.TipDoc AND R.Remision=K.Documento AND R.IdCia=K.IdCia
 INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Terceros AS  V ON K.IdVend=V.IdTercero
 INNER JOIN Localidades AS L ON R.IdLocEnv=L.IdLocal
 WHERE R.Remision=@pmRemision AND R.IdCia=@pmIdCia
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemisionFac @pmFactura INT,@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null
,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
 AS
 SELECT Remision,IdCia,Fecha,FechaDesp,FechaVence,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,Cantidad,Unidades
,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago
,MulPlazos,IdPlazo,CdRuta,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,DetalleEnvio,TipPed,Pedido,IdCiaPed,FechaPed,Modalidad
,R.Observacion AS Observ,TimeSys,IdCiaCrea
 FROM Trn_Remision AS R INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0  AND Factura<=@pmFactura
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') 
 ORDER BY IdCia,Remision
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemisionFact @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmRemisionIni INT=Null,@pmRemisionFin INT=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null
,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdProducto VARCHAR(16)=Null,@pmFechaFacIni SMALLDATETIME=Null,@pmFechaFacFin SMALLDATETIME=Null
,@pmTipoVentaIni INT=Null,@pmTipoVentaFin INT=Null
 AS
 SELECT O.Remision AS NumRemision,O.IdCia AS CdCia,Compania,O.Fecha AS FechaRem,FechaDesp,FechaVence,K.IdProducto AS CodProducto,DescripProd
,SUM(Salidas) AS CantPedido,SUM(K.Unidades) AS Unidades
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,Unidad,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,Modalidad,O.IdConcepto AS CdConcepto,Concepto
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,O.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,NumAprob,TipPed,Pedido,IdCiaPed,FechaPed,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado
 FROM Trn_Remision AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado  INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia 
 INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Remision=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN (SELECT  Remision,IdCiaRem,K.TipDoc AS TipoFact,K.Documento AS NumFactura,K.IdCia AS CdCiaFac,KC.Compania AS NomCiaFac
  ,TipOrd,NumOrden,IdCiaOrd,K.Fecha AS FecFactura,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd
,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrPrecio) AS VrPrecioUnit,SUM(VrIvaEnt) AS IvaEntradas
,SUM(VrIvaSal) AS IvaSalidas,SUM(VrDctoEnt) AS DctoEntradas,SUM(VrDctoSal) AS DctoSalidas,SUM(K.Unidades) AS TotalUnidades
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 WHERE NoVentas BETWEEN ISNULL(@pmTipoVentaIni,1) AND ISNULL(@pmTipoVentaFin,4) AND Remision>0
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (Fecha>=ISNULL(@pmFechaFacIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFacFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY  Remision,IdCiaRem,K.TipDoc,K.Documento,K.IdCia,KC.Compania,TipOrd,NumOrden,IdCiaOrd,K.Fecha,K.IdProducto,PM.DescripProd) AS FA 
 ON  O.Remision=FA.Remision AND O.IdCia=FA.IdCiaRem AND K.IdProducto=FA.CdProducto
 WHERE O.Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Anulado=0
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')  AND  O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND O.Remision  BETWEEN ISNULL(@pmRemisionIni,0) AND ISNULL(@pmRemisionFin,2147483647)
 AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 GROUP BY O.Remision,O.IdCia,Compania,O.Fecha,FechaDesp,FechaVence,K.IdProducto,DescripProd
,TipoFact,NumFactura,CdCiaFac,NomCiaFac,FecFactura,CantEntradas,CantSalidas,VrPrecioUnit,IvaEntradas,IvaSalidas,DctoEntradas,DctoSalidas,TotalUnidades
,Unidad,IdCliente,T.RazonSocial,O.IdAgencia,Agencia,CodAgencia,Modalidad,O.IdConcepto,Concepto
,O.IdVend,VN.RazonSocial,O.IdLocEnv,LE.Localidad,LE.IdDep,DE.Departamento
,NumAprob,TipPed,Pedido,IdCiaPed,FechaPed,O.Observacion,O.IdEstado,Estado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemisionLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmRemisionIni INT=Null,@pmRemisionFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS
 SELECT Remision,IdCia,Fecha,FechaDesp,FechaVence,R.IdConcepto AS CdConcepto,Concepto,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto
,Cantidad,Unidades,IdVend,V.RazonSocial AS Vendedor,TarifaCom,CodTarCom,DirEnvio,IdLocEnv,DiasEntraga,NitContac,NomContac,TelContac,emlContac,CargoContac,IdForma,DetallePago,MulPlazos,IdPlazo,CdRuta,NitEmpTrans,EmpTrans
,pVehiculo,CdConductor,DetalleEnvio,TipFac,Factura,IdCiaFac,FechaFact,TipPed,Pedido,IdCiaPed,FechaPed,NumAutoriza,NumAprob,FecAprob,CdUsuAprob,Modalidad,OrigenAdd,Anulado,FecDev
,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Remision AS R INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON R.IdVend=V.IdTercero
 INNER JOIN  Conceptos AS CN ON R.IdConcepto=CN.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND Remision BETWEEN ISNULL(@pmRemisionIni,0) AND ISNULL(@pmRemisionFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') 
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Remision
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemisionPed @pmPedido INT,@pmIdCiaPed CHAR(2),@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT R.Remision AS NumRemision,R.IdCia AS CodCia,R.Fecha AS FechaRem,FechaVence,IdCliente,RazonSocial,IdAgencia,Item,IdProducto,Descripcion,K.IdBodega AS CdBodega,Bodega
,CdTanque,Salidas,VrPrecio,VrPrecio*Salidas AS SubTotal,TarifaIva,VrIvaSal,TarifaDct,VrDctoSal,((VrPrecio*Salidas)-TarifaDct)+TarifaIva AS SValTotal,VrUnitario
,VrCostoSal,TarifaRet,VrReteSal,TarifaIca,VrIcaSal,VrBruto,ListaPrec,VrBase,CdMoneda,VrTasaCamb,K.IdVend AS CedVend,Comision,Referencia,K.Unidades AS Unddes,R.Factura AS NumFactura,IdCiaFac,FechaFact
 FROM Trn_Remision AS R INNER JOIN Trn_Kardex AS  K ON R.Remision=K.Documento AND R.IdCia=K.IdCia
 AND R.TipDoc=K.TipDoc  INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 WHERE TipPed='PED' AND Pedido=@pmPedido AND IdCiaPed=@pmIdCiaPed  AND Anulado=0
AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
ORDER BY R.IdCia,R.Remision,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemisionRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdLocEnv VARCHAR(8)=Null
,@pmModalidad VARCHAR(10)=Null,@pmCdRuta VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
 AS
 SELECT TipDoc,Remision,O.IdCia AS CdCia,Compania,Fecha,FechaDesp,FechaVence,O.IdConcepto AS CdConcepto,Concepto
,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,Unidades
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,O.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,DiasEntraga,O.NitContac AS RemNitContac,O.NomContac AS RemNomContac,O.TelContac AS RemTelContac,O.emlContac AS RemEmailContac,CargoContac,O.IdForma AS CdForma,FormaPago
,DetallePago,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CdRuta,Ruta,NitEmpTrans,EmpTrans,pVehiculo,CdConductor,TC.RazonSocial AS Conductor,DetalleEnvio,TipFac,Factura,IdCiaFac,FechaFact
,TipPed,Pedido,IdCiaPed,FechaPed,NumAutoriza,NumAprob,FecAprob,CdUsuAprob,Modalidad,OrigenAdd,Anulado,FecDev
,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,TimeSys,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
 FROM Trn_Remision AS O  INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON O.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 WHERE O.TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND O.IdCia LIKE ISNULL(@pmIdCia,'%%') AND O.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND O.IdAgencia LIKE ISNULL(@pmIdAgencia ,'%')
 AND O.IdVend LIKE ISNULL(@pmIdVend,'%') AND O.IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND CdRuta LIKE ISNULL(@pmCdRuta,'%')
 AND  O.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY O.IdCia,Remision
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRemision_Cr @pmTipDoc VARCHAR(3),@pmRemisionIni INT,@pmRemisionFin INT,@pmIdCia CHAR(2)
 AS
 SELECT O.TipDoc AS Tip_Doc,TipoDoc,O.Remision AS NumRemision,O.IdCia AS CdCia,Compania,O.Fecha AS FechaDoc,FechaDesp,FechaVence,O.IdConcepto AS CdConcepto,Concepto
,IdCliente,T.RazonSocial AS NomCliente,O.IdAgencia AS IdAgenc,Agencia,CodAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrFletes,VrOtros,VrOtrDcto,VrNeto,Cantidad,O.Unidades AS NumUnidades
,O.IdVend AS NitVendedor,VN.RazonSocial AS Vendedor,TarifaCom,DirEnvio,O.IdLocEnv AS CodCiuEnvio,LE.Localidad AS CiudadEnvio,LE.IdDep AS CdDepEnvio,DE.Departamento AS DptoEnvio
,DiasEntraga,O.NitContac AS RemNitContac,O.NomContac AS RemNomContac,O.TelContac AS RemTelContac,O.emlContac AS RemEmailContac,CargoContac,O.IdForma AS CdForma,FormaPago
,DetallePago,MulPlazos,O.IdPlazo AS CdPlazo,Plazo,NVmto,DiasPago,CdRuta,Ruta,NitEmpTrans,EmpTrans,O.pVehiculo AS PlacaVeh,CdConductor,TC.RazonSocial AS Conductor,DetalleEnvio,TipFac,O.Factura AS NumFactura,IdCiaFac,FechaFact
,TipPed,Pedido,IdCiaPed,FechaPed,NumAutoriza,NumAprob,FecAprob,CdUsuAprob,Modalidad,OrigenAdd,Anulado,FecDev
,O.Observacion AS Observ,O.IdEstado AS CdEstado,Estado,O.TimeSys AS Fec_Add,O.FecUpdate AS Fec_Update,IdCiaCrea,O.IdUsuario AS IdUsuari,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,DirAgncia,TelAgncia,FaxAgncia,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia
--Detalles
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,CdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,TipOrd,NumOrden,IdCiaOrd,Cotizacion,IdCiaCot,K.Factura AS KarFactura,TipDocDev,NumDocDev
,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,K.Unidades AS KarUnidades,ItemCombo,Servcios,NoVentas,EsCombo,EsProdBase,K.ListaPrec AS KarLtaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM Trn_Remision AS O INNER JOIN Trn_Kardex AS K ON O.TipDoc=K.TipDoc AND O.Remision=K.Documento AND O.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON O.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON O.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON O.IdCliente=T.IdTercero INNER JOIN  Conceptos AS C ON O.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON O.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON O.IdUsuario=U.IdUsuario
 INNER JOIN  Plazos AS PZ ON O.IdPlazo=PZ.IdPlazo INNER JOIN  Terceros AS VN ON O.IdVend=VN.IdTercero
 INNER JOIN Agencias  AS A ON O.IdAgencia=A.IdAgencia  INNER JOIN Formaspago AS FP ON O.IdForma=FP.IdForma
 INNER JOIN TercCliente AS CLI ON O.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN  Localidades AS LE ON O.IdLocEnv=LE.IdLocal LEFT JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep 
 LEFT JOIN Terceros AS TC ON O.CdConductor=TC.IdTercero LEFT JOIN Rutas AS R ON O.CdRuta=R.IdRuta
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 WHERE O.TipDoc=@pmTipDoc AND O.Remision BETWEEN @pmRemisionIni AND @pmRemisionFin AND O.IdCia=@pmIdCia
 ORDER BY O.Remision,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReport @pmTipoRep VARCHAR(3),@pmIdRep CHAR(2)
 AS 
 SELECT TipoRep,NomRep,IdRep,Descripcion,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,sp_Nombre,Ind_Location
 FROM Sys_Report
 WHERE TipoRep like ISNULL(@pmTipoRep,'%')  AND IdRep like ISNULL(@pmIdRep,'%%')
 ORDER BY TipoRep, IdRep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportDso @pmTipoRep VARCHAR(3)
AS
 SELECT  TipoRep, IdRep,Descripcion,IdRep+ '  '+Descripcion AS DsRep,Formato
 FROM   Sys_Report
 WHERE TipoRep=@pmTipoRep
 ORDER BY IdRep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportFmto @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
 AS 
 SELECT TipoRep,Formato,TipoOrigen,CrVersion,sp_Nombre,Ind_Location
 FROM Sys_ReportFmto
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportFmtoLta @pmTipoRep VARCHAR(10)=Null
 AS 
 SELECT TipoRep,Formato,TipoOrigen,CrVersion,sp_Nombre,Ind_Location
 FROM Sys_ReportFmto
 WHERE TipoRep LIKE ISNULL(@pmTipoRep,'%')
 ORDER BY Formato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportGru
AS
 SELECT TipoRep,NomRep,count(IdRep) AS Cuenta
 FROM   Sys_Report
 GROUP BY  TipoRep,NomRep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportSub @pmTipoRep VARCHAR(10)
,@pmFormato VARCHAR(20),@pmNombreSub VARCHAR(20)
 AS 
 SELECT TipoRep,Formato,NombreSub,TipoOrigen,sp_Nombre,Ind_Location
 FROM Sys_ReportSub
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato AND NombreSub=@pmNombreSub
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportSubLta @pmTipoRep VARCHAR(10)=Null,@pmFormato VARCHAR(20)=Null
 AS 
 SELECT TipoRep,Formato,NombreSub,TipoOrigen,sp_Nombre,Ind_Location
 FROM Sys_ReportSub
 WHERE TipoRep like ISNULL(@pmTipoRep,'%') AND Formato LIKE ISNULL(@pmFormato,'%')
 ORDER BY Formato,NombreSub
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReportSubPmt @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmNombreSub VARCHAR(20),@pmIndParam INT
 AS 
 SELECT TipoRep,Formato,NombreSub,IndParam,NomParam,TipoDato,ValorPred
 FROM Sys_ReportSubPmt
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato  AND NombreSub LIKE ISNULL(@pmNombreSub,'%')
 AND (IndParam>=ISNULL(@pmIndParam,0)  AND IndParam<=ISNULL(@pmIndParam,2147483647))
 ORDER BY IndParam
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryReqDetalle @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,CdProducto,Descripcion,Cantidad,IdUnd,IdSubgrupo,VrUnitario
,TipOrd,NumOrden,IdCiaOrd,TipSal,NSalida,IdCiaSal,FechaSal,CdBodega
 FROM Trn_ReqDetalle
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicion @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Requisicion,IdCia,Fecha,FechaVence,IdConcepto,IdRespons,IdCCosto,IdSubCos,IdDep,VrSubTotal,Cantidad
,NContrato,IdCiaCont,NitCliente,CdAgencia,Modalidad,DirEntrega,IdLocEnt,TipSal,NumSalida,IdCiaSal,FechaSal
,NumAprob,FecAprob,CdUsuAprob,NomContacto,TelsContacto,EmailContacto,OrigenAdd,Anulado,FecDev,Observacion,IdEstado
,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Requisicion
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicionDet @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
 AS 
 SELECT Fecha,IdRespons,IdCCosto,IdSubCos,IdDep,NContrato,IdCiaCont,NitCliente,CdAgencia,Modalidad
,DirEntrega,IdLocEnt,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,Anulado,FecDev,Observacion,IdEstado
,Item,CdProducto,Descripcion,IdSubgrupo,CdBodega,D.Cantidad AS CantArt,IdUnd,VrUnitario
 FROM Trn_Requisicion AS R INNER JOIN  Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion
 AND R.IdCia=D.IdCia
 WHERE R.TipDoc=@pmTipDoc AND R.Requisicion=@pmRequisicion AND R.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicionLta @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmRequisicionIni INT=Null
,@pmRequisicionFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmAnulado BIT=Null
,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT Requisicion,IdCia,Fecha,FechaVence,R.IdConcepto AS CdConcepto,Concepto,IdRespons,T.RazonSocial AS NomResponsable
,R.IdCCosto AS CdCentCosto,CCosto,R.IdSubCos AS CdSubCent,R.IdDep AS CdDep,Dependencia,VrSubTotal,Cantidad,NContrato,IdCiaCont
,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,Modalidad,DirEntrega,IdLocEnt,TipSal,NumSalida,IdCiaSal,FechaSal,NumAprob,FecAprob,CdUsuAprob
,NomContacto,TelsContacto,EmailContacto,OrigenAdd,Anulado,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Requisicion AS R INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero
 INNER JOIN  CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto
 INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN Dependencias AS D ON R.IdDep=D.IdDep
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero
 WHERE Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND IdRespons LIKE ISNULL(@pmIdRespons,'%')
 AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 AND  Requisicion BETWEEN ISNULL(@pmRequisicionIni,0) AND ISNULL(@pmRequisicionFin,2147483647)
 ORDER BY IdCia,Requisicion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicionOrd @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdRespons VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null
 AS 
 SELECT R.Requisicion AS NumRequision,R.IdCia AS CdCia,Compania,Fecha,FechaVence,R.IdConcepto AS CdConcepto,Concepto,IdRespons,T.RazonSocial AS Responsable
,R.IdCCosto AS CdCentCosto,CCosto,R.IdSubCos AS CdSubCent,SubCosto,R.IdDep AS CdDep,Dependencia,R.NContrato AS NumContrato,IdCiaCont,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,Modalidad
,VrSubTotal,R.Cantidad AS CantTotal,DirEntrega,IdLocEnt,Localidad,NumAprob,FecAprob,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
--detalles
,Item,CdProducto,D.Descripcion AS DescProd,D.IdSubgrupo AS CdSubgrupo,Subgrupo,CdBodega,Bodega,D.Cantidad AS CantArtic,D.IdUnd AS CdUnidad,Unidad,VrUnitario,TipOrd,NumOrden,IdCiaOrd
,D.TipSal AS TipoSal,D.NSalida AS NumSalida,D.IdCiaSal AS CdCiaSal,D.FechaSal AS FecSalida
--datos del cliente
,CL.TipoId AS TercTipo,CL.Dv AS TercDv,CL.Codigo AS TercCodigo,CL.NomCial AS TercNomCial,CL.SiglaRaz AS TercSigal,CL.Direccion AS TercDireccion
 FROM Trn_Requisicion AS R  INNER JOIN  Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion AND R.IdCia=D.IdCia
 INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero  INNER JOIN  CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto
 INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep
 INNER JOIN  SubGrupos AS SG ON D.IdSubgrupo=SG.IdSubgrupo  INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd 
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero LEFT JOIN TercCliente AS CLI ON  R.NitCliente=CLI.IdClie
 LEFT  JOIN SubCentros AS SC ON R.IdSubCos=SC.IdSubCos LEFT JOIN Localidades AS LC ON R.IdLocEnt=LC.IdLocal
 LEFT JOIN Bodegas AS B ON D.CdBodega=B.IdBodega
 WHERE R.Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND Anulado=0 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  IdRespons LIKE ISNULL(@pmIdRespons,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%')
 ORDER BY  R.IdCia,R.Requisicion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicionPen @pmRequisicionIni INT=Null,@pmRequisicionFin INT=Null,@pmIdCia CHAR(2)=Null
,@pmIdRespons VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT  R.Requisicion AS NumRequis,R.IdCia AS CdCia,Fecha,FechaVence,IdRespons,T.RazonSocial AS NomResponsable
,Item,CdProducto,Descripcion,D.Cantidad AS CantArt,D.IdUnd AS CdUnid,Unidad,VrUnitario,D.Cantidad*VrUnitario AS ValorTotal
,R.IdConcepto AS CdConcepto,Concepto,R.IdCCosto AS CdCentCosto,CCosto,R.IdSubCos AS CdSubCent,R.IdDep AS CdDep,Dependencia
,NContrato,IdCiaCont,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,Modalidad,DirEntrega,NumAprob,FecAprob,CdUsuAprob
,R.Observacion AS Observ,TipOrd,NumOrden,IdCiaOrd,NomContacto,TelsContacto,EmailContacto
 FROM Trn_Requisicion AS R INNER JOIN Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion AND R.IdCia=D.IdCia
 INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero
 INNER JOIN  CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto
 INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero
 WHERE  Anulado=0 AND R.IdEstado='0001' AND NumAprob>0
 AND  R.Requisicion BETWEEN ISNULL(@pmRequisicionIni,0) AND ISNULL(@pmRequisicionFin,2147483647)
 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdRespons LIKE ISNULL(@pmIdRespons,'%')
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY R.IdCia,R.Requisicion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicionSal @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME
,@pmRequisicionIni INT=Null,@pmRequisicionFin INT=Null,@pmIdCia CHAR(2)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdRespons VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null,@pmNitCliente VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdDep VARCHAR(4)=Null,@pmFechaSalIni SMALLDATETIME=Null,@pmFechaSalFin SMALLDATETIME=Null
 AS 
 SELECT R.Requisicion AS NumRequision,R.IdCia AS CdCia,Compania,Fecha,FechaVence,D.IdSubgrupo AS CodSubgrupo,Subgrupo,SUM(D.Cantidad) AS CantReq
,D.IdUnd AS CdUnidad,Unidad
,TipoSalida,SD.NumSalida AS NroSalida,CdCiaSal,NomCiaSal,FecSalida,NumEntrada,CiaEntrada,SD.CdProducto AS CodProducto,NombreProd,CantEntradas,CantSalidas
,ValorUnit,CostoEntradas,CostoSalidas,TotalUnidades
,D.NumOrden AS NumOCompra,D.IdCiaOrd AS CdCiaOcompra,R.IdConcepto AS CdConcepto,Concepto,IdRespons,T.RazonSocial AS NomResponsable
,R.IdCCosto AS CdCentCosto,CCosto,R.IdSubCos AS CdSubCent,R.IdDep AS CdDep,Dependencia,NContrato,IdCiaCont
,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,Modalidad,NumAprob,FecAprob,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado
 FROM Trn_Requisicion AS R  INNER JOIN  Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion AND R.IdCia=D.IdCia
 INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero  INNER JOIN  CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto
 INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia INNER JOIN  SubGrupos AS SG ON D.IdSubgrupo=SG.IdSubgrupo
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd 
 INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep  LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero
 LEFT JOIN (SELECT TipOrd,NumOrden,IdCiaOrd,K.TipDoc AS TipoSalida,K.Documento AS NumSalida,K.IdCia AS CdCiaSal,KC.Compania AS NomCiaSal
,K.Fecha AS FecSalida,K.Remision AS NumEntrada,K.IdCiaRem AS CiaEntrada,K.IdProducto AS CdProducto,PM.DescripProd AS NombreProd
,PM.IdSubgrupo AS CdSubgrupo,SG.Subgrupo AS NomSubgrupo,SUM(Entradas) AS CantEntradas,SUM(Salidas) AS CantSalidas,MAX(VrUnitario) AS ValorUnit,SUM(VrCostoEnt) AS CostoEntradas,SUM(VrCostoSal) AS CostoSalidas
,SUM(K.Unidades) AS TotalUnidades
 FROM Trn_Kardex AS K INNER JOIN Companias AS KC ON K.IdCia=KC.IdCia  INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo  WHERE TipDoc IN ('SAI','DSI') AND TipOrd='REQ' AND NumOrden>0 
 AND (Fecha>=ISNULL(@pmFechaSalIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaSalFin,CAST('20781230' AS SMALLDATETIME)))
 GROUP BY TipOrd,NumOrden,IdCiaOrd,K.TipDoc,K.Documento,K.IdCia,KC.Compania,K.Fecha,K.Remision ,K.IdCiaRem,K.IdProducto,PM.DescripProd
,PM.IdSubgrupo,SG.Subgrupo) AS SD ON R.TipDoc=SD.TipOrd AND R.Requisicion=SD.NumOrden AND R.IdCia=SD.IdCiaOrd AND D.IdSubgrupo=SD.CdSubgrupo
 WHERE R.Fecha BETWEEN  @pmFechaIni AND @pmFechaFin AND Anulado=0 AND R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND R.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND IdRespons LIKE ISNULL(@pmIdRespons,'%')
 AND R.IdCCosto LIKE ISNULL(@pmIdCCosto,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND R.IdDep LIKE ISNULL(@pmIdDep,'%')
 AND  R.Requisicion BETWEEN ISNULL(@pmRequisicionIni,0) AND ISNULL(@pmRequisicionFin,2147483647)
 GROUP BY  R.Requisicion,R.IdCia,Compania,Fecha,FechaVence,D.IdSubgrupo,Subgrupo,D.IdUnd,Unidad
,TipoSalida,SD.NumSalida,CdCiaSal,NomCiaSal,FecSalida,NumEntrada,CiaEntrada,SD.CdProducto,NombreProd,CantEntradas,CantSalidas
,ValorUnit,CostoEntradas,CostoSalidas,TotalUnidades
,D.NumOrden,D.IdCiaOrd,R.IdConcepto,Concepto,IdRespons,T.RazonSocial,R.IdCCosto,CCosto,R.IdSubCos
,R.IdDep,Dependencia,NContrato,IdCiaCont,NitCliente,CL.RazonSocial,CdAgencia,Modalidad,NumAprob,FecAprob
,R.Observacion,R.IdEstado,Estado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRequisicion_Cr @pmTipDoc VARCHAR(3),@pmRequisicionIni INT,@pmRequisicionFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT R.TipDoc AS CdTipo,TipoDoc,R.Requisicion AS NumRequis,R.IdCia AS CdCia,Compania,Fecha,FechaVence,R.IdConcepto AS CdConcepto,Concepto,IdRespons,T.RazonSocial AS NomResponsable
,R.IdCCosto AS CdCentCosto,CCosto,R.IdSubCos AS CdSubCent,R.IdDep AS CdDep,Dependencia,VrSubTotal,R.Cantidad AS CantTotal,R.NContrato AS NumContrato,IdCiaCont
,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,Agencia,CodAgencia,Modalidad,DirEntrega,IdLocEnt,Localidad,Departamento,R.TipSal AS Tip_Sal,NumSalida,R.IdCiaSal AS CdCiaSal
,R.FechaSal AS Fec_Salida,NumAprob,FecAprob,CdUsuAprob,OrigenAdd,Anulado,NomContacto,TelsContacto,EmailContacto,FecDev,R.Observacion AS Observ,R.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,R.IdUsuario AS IdUsuari,Usuario
,Item,CdProducto,Descripcion,D.Cantidad AS CantArt,D.IdUnd AS CdUnid,Unidad,D.IdSubgrupo AS CdSubgrupo,Subgrupo,VrUnitario,TipOrd,NumOrden,IdCiaOrd,D.TipSal AS DetTipoSal,NSalida,D.IdCiaSal AS DetCiaSal
,D.FechaSal AS DetFecSalida,CdBodega,Bodega
 FROM Trn_Requisicion AS R INNER JOIN Trn_ReqDetalle AS D ON R.TipDoc=D.TipDoc AND R.Requisicion=D.Requisicion AND R.IdCia=D.IdCia
 INNER JOIN Terceros AS T ON R.IdRespons=T.IdTercero
 INNER JOIN  CentroCosto AS CC ON R.IdCCosto=CC.IdCCosto
 INNER JOIN  Conceptos AS C ON R.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON R.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario 
 INNER JOIN Sys_TiposDoc AS TD ON R.TipDoc=TD.IdDoc
 INNER JOIN Companias AS CI ON R.IdCia=CI.IdCia
 INNER JOIN Dependencias AS DP ON R.IdDep=DP.IdDep
 INNER JOIN UndMed AS UM ON D.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON D.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Localidades AS L ON R.IdLocEnt=L.IdLocal
 INNER JOIN Departamentos AS DT ON L.IdDep=DT.IdDep
 LEFT JOIN Terceros AS CL ON R.NitCliente =CL.IdTercero
 LEFT JOIN Bodegas AS BG ON D.CdBodega =BG.IdBodega
 LEFT JOIN Agencias AS A ON R.CdAgencia=A.IdAgencia
 WHERE R.TipDoc=@pmTipDoc AND  R.Requisicion BETWEEN  @pmRequisicionIni AND @pmRequisicionFin 
 AND R.IdCia=@pmIdCia 
 ORDER BY  R.Requisicion,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRoles @pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3)=Null
 AS 
 SELECT IdObj,IdRole,Funcion
 FROM Sys_Roles
 WHERE IdObj=@pmIdObj AND IdRole LIKE ISNULL(@pmIdRole,'%')
 ORDER BY IdRole
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutas @pmIdRuta VARCHAR(4)
 AS 
 SELECT IdRuta,Ruta,IdLocOri,IdLocDes,EsLocal,Distkmt,TmHoras,CodigoMt,FleteTon,Inactivo
 FROM Rutas
 WHERE IdRuta=@pmIdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryRutasBus @pmEsLocal BIT=Null
 AS 
 SELECT IdRuta,Ruta,IdRuta+' '+Ruta  AS DsRut
 FROM Rutas
 WHERE  Inactivo=0  AND (EsLocal=ISNULL(@pmEsLocal,0) or EsLocal=ISNULL(@pmEsLocal,1))
 ORDER BY IdRuta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutasDso  @pmIdLocOri VARCHAR(8)=Null,@pmIdLocDes VARCHAR(8)=Null,@pmEsLocal BIT=Null
 AS 
 SELECT IdRuta,Ruta,Ruta+' '+IdRuta+' ('+LO.Localidad+' - '+LD.Localidad+')' AS DsRut
 FROM Rutas AS R INNER JOIN Localidades AS LO ON R.IdLocOri=LO.IdLocal
 INNER JOIN Departamentos AS D ON LO.IdDep=D.IdDep
 INNER JOIN Localidades AS LD ON R.IdLocDes=LD.IdLocal
 INNER JOIN Departamentos AS DD ON LD.IdDep=DD.IdDep
 WHERE R.Inactivo=0 AND  IdLocOri like ISNULL(@pmIdLocOri,'%') AND IdLocDes LIKE ISNULL(@pmIdLocDes,'%')
 AND (EsLocal=ISNULL(@pmEsLocal,0) or EsLocal=ISNULL(@pmEsLocal,1))
 ORDER BY Ruta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutasLta  @pmIdLocOri VARCHAR(8)=Null,@pmIdLocDes VARCHAR(8)=Null,@pmEsLocal BIT=Null
 AS 
 SELECT IdRuta,Ruta,IdLocOri,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDptoOrig,D.Departamento AS DptoOrigen,IdLocDes
,LD.Localidad AS CiuDestino,LD.IdDep AS CdDptoDest,DD.Departamento AS DptoDestino,EsLocal,Distkmt,TmHoras,CodigoMt,FleteTon
 FROM Rutas AS R INNER JOIN Localidades AS LO ON R.IdLocOri=LO.IdLocal
 INNER JOIN Departamentos AS D ON LO.IdDep=D.IdDep
 INNER JOIN Localidades AS LD ON R.IdLocDes=LD.IdLocal
 INNER JOIN Departamentos AS DD ON LD.IdDep=DD.IdDep
 WHERE R.Inactivo=0 AND  IdLocOri like ISNULL(@pmIdLocOri,'%') AND IdLocDes LIKE ISNULL(@pmIdLocDes,'%')
 AND (EsLocal=ISNULL(@pmEsLocal,0) or EsLocal=ISNULL(@pmEsLocal,1))
 ORDER BY IdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutasPlan @pmIdRuta VARCHAR(4),@pmIdPunto INT
 AS 
 SELECT  IdRuta,IdPunto,Descripcion,Distancia,Tiempo,ClasePto,CdPto,IdLocal,PosLat,PosLon,Inactivo
 FROM RutasPlan
 WHERE IdRuta=@pmIdRuta 
 AND (IdPunto>=ISNULL(@pmIdPunto,0)  AND IdPunto<=ISNULL(@pmIdPunto,2147483647))
 ORDER BY IdPunto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutasPlanItm @pmIdRuta VARCHAR(4)
 AS 
 SELECT MAX(IdPunto) AS MXPUNTO
 FROM RutasPlan
 WHERE IdRuta=@pmIdRuta 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutasPlanLta @pmIdRuta VARCHAR(4)=Null,@pmIdLocOri VARCHAR(8)=Null,@pmIdLocDes VARCHAR(8)=Null
 AS 
 SELECT RP.IdRuta AS CdRuta,Ruta,IdLocOri,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDptoOrig,D.Departamento AS DptoOrigen,IdLocDes,LD.Localidad AS CiuDestino
,LD.IdDep AS CdDptoDest,DD.Departamento AS DptoDestino,IdPunto,Descripcion,Distancia,Tiempo,ClasePto,CdPto,PtoControl,Referencia
,Telefono,RP.IdLocal AS CdCiuPto,LP.Localidad AS CiudadPto,LP.IdDep AS CdDptoPto,DP.Departamento AS DptoPto,EsLocal,Distkmt,TmHoras,PosLat,PosLon
 FROM RutasPlan AS RP INNER JOIN Rutas AS R ON RP.IdRuta=R.IdRuta
 INNER JOIN Localidades AS LO ON R.IdLocOri=LO.IdLocal
 INNER JOIN Departamentos AS D ON LO.IdDep=D.IdDep
 INNER JOIN Localidades AS LD ON R.IdLocDes=LD.IdLocal
 INNER JOIN Departamentos AS DD ON LD.IdDep=DD.IdDep
 INNER JOIN Localidades AS LP ON RP.IdLocal=LP.IdLocal
 INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 LEFT JOIN Puestos AS P ON RP.CdPto=P.IdPto
 WHERE R.Inactivo=0 AND RP.Inactivo=0 AND RP.IdRuta LIKE ISNULL(@pmIdRuta,'%')
 AND IdLocOri like ISNULL(@pmIdLocOri,'%') AND IdLocDes LIKE ISNULL(@pmIdLocDes,'%') 
 ORDER BY Ruta,IdPunto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryRutasTramos @pmIdRuta VARCHAR(4),@pmNumTramo INT
 AS 
 SELECT IdRuta,NumTramo,IdLocal,kmstramo,kmstotal
 FROM RutasTramos
 WHERE IdRuta=@pmIdRuta AND NumTramo=@pmNumTramo


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryRutasTramosLta @pmIdRuta VARCHAR(4)=Null
 AS 
 SELECT  RT.IdRuta AS CdRuta,Ruta,NumTramo,RT.IdLocal AS CdLocal,LT.Localidad AS Ciudad,DT.Departamento AS Depto,kmstramo,kmstotal
,IdLocOri,LO.Localidad AS CiuOrigen,LO.IdDep AS CdDptoOrig,D.Departamento AS DptoOrigen,IdLocDes,LD.Localidad AS CiuDestino,CodigoMt
,LD.IdDep AS CdDptoDest,DD.Departamento AS DptoDestino
 FROM RutasTramos AS RT INNER JOIN Rutas AS R ON RT.IdRuta=R.IdRuta
 INNER JOIN Localidades AS LT ON RT.IdLocal=LT.IdLocal  INNER JOIN Departamentos AS DT ON LT.IdDep=DT.IdDep
 INNER JOIN Localidades AS LO ON R.IdLocOri=LO.IdLocal  INNER JOIN Departamentos AS D ON LO.IdDep=D.IdDep
 INNER JOIN Localidades AS LD ON R.IdLocDes=LD.IdLocal  INNER JOIN Departamentos AS DD ON LD.IdDep=DD.IdDep
 WHERE R.Inactivo=0 AND RT.IdRuta LIKE ISNULL(@pmIdRuta,'%')
 ORDER BY Ruta,NumTramo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySAutoriza @pmIdConcepto VARCHAR(4)
 AS 
 IF @pmIdConcepto IS NULL
  SELECT IdConcepto,ConceptoAut,Inactivo
  FROM Sys_Autoriza
  ORDER BY IdConcepto
 ELSE
  SELECT IdConcepto,ConceptoAut,Inactivo
  FROM Sys_Autoriza
  WHERE IdConcepto=@pmIdConcepto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySAutorizaDso
 AS 
  SELECT IdConcepto,ConceptoAut,ConceptoAut+' '+IdConcepto AS DsCon
  FROM Sys_Autoriza
  WHERE Inactivo=0
  ORDER BY ConceptoAut
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySaiArticulos @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Salida,IdCia,Item,TipEnt,Entrada,IdCiaEnt,ItemEnt,IdProducto,IdBodega,Cantidad,VrUnitario
 FROM Trn_SaiArticulos
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidas @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Salida,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp
,TarifaIva,CodTarIva,TipOrd,NumOrden,IdCiaOrd,IdCCosto,IdSubCos,pVehiculo,Modalidad
,CdConductor,CdDep,NContrato,IdCiaCont ,NitCliente,CdAgencia,ModRequis,DirEntrega,IdLocEnt,GuiaComb,CdCiaGuia,OrigenAdd
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Salidas
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidasAdi @pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmModalidad VARCHAR(10)
 AS 
 SELECT COUNT(Salida) AS SCANSAL
 FROM Trn_Salidas 
 WHERE  IdCia=@pmIdCia AND Fecha=@pmFecha AND Modalidad=@pmModalidad AND Anulado=0

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidasLta @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmSalidaIni INT=Null,@pmSalidaFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmAnulado BIT=Null
,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Salida,IdCia,Fecha,S.IdConcepto AS CdConcepto,Concepto,S.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,TarifaIva,CodTarIva
,TipOrd,NumOrden,IdCiaOrd,GuiaComb,CdCiaGuia,S.IdCCosto AS CdCcosto,CCosto,IdSubCos,pVehiculo,CdConductor,CdDep,NContrato,IdCiaCont ,NitCliente,CL.RazonSocial AS NomCliente,CdAgencia,ModRequis,DirEntrega,IdLocEnt
,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate
,IdCiaCrea,S.IdUsuario AS IdUsuari,Usuario,TipDoc
 FROM Trn_Salidas AS S INNER JOIN Terceros AS T ON S.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON S.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON S.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON  S.IdCCosto=CC.IdCCosto
 LEFT JOIN Terceros AS CL ON S.NitCliente=CL.IdTercero
 WHERE TipDoc=@pmTipDoc AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Salida BETWEEN ISNULL(@pmSalidaIni,0) AND ISNULL(@pmSalidaFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND S.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND S.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND S.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY IdCia,Salida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidasNet @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdLocEnt VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Salida,S.IdCia AS CdCia,Compania,Fecha,S.IdConcepto AS CdConcepto,Concepto,S.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,VrSubTotal
,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,TarifaIva,TipOrd,NumOrden,IdCiaOrd,S.IdCCosto AS CdCentCosto,CCosto,S.IdSubCos AS CdSubCenCost,SubCosto
,pVehiculo,CdConductor,CDT.RazonSocial AS NomConductor,S.NContrato AS NumContrato,IdCiaCont,NitCliente,CLI.RazonSocial AS NomCliente
,S.CdAgencia AS IdAgncia,Agencia,CdDep,ModRequis,DirEntrega,IdLocEnt,LE.Localidad AS CiuEntrega
,Modalidad,GuiaComb,CdCiaGuia,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado
,TimeSys,S.FecUpdate AS Fec_Update,IdCiaCrea,S.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_Salidas AS S INNER JOIN Companias AS CN ON S.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON S.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON S.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON S.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON S.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN SubCentros AS SC ON S.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS CLI ON S.NitCliente=CLI.IdTercero
 LEFT JOIN Terceros AS CDT ON S.CdConductor=CDT.IdTercero
 LEFT JOIN Agencias AS A ON S.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LE ON S.IdLocEnt=LE.IdLocal
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND S.IdCia LIKE ISNULL(@pmIdCia,'%%') AND S.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND S.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND S.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')  AND IdLocEnt LIKE ISNULL(@pmIdLocEnt,'%')
 UNION 
 SELECT TipDev,Devolucion,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDev,D.IdConcepto AS CdConcepto,Concepto,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,D.VrSubTotal AS SubTotal
,D.VrImpuesto AS Impuesto,D.VrCostos AS Costos,D.VrNeto AS TotalNeto,D.Cantidad AS CantDev,D.BaseImp AS VrBaseImp,TarifaIva,D.TipOrd AS TipoOrd,D.NumOrden AS NOrden,D.IdCiaOrd AS CodCiaOrden
,D.IdCCosto AS IdCenCosto,CCosto,D.IdSubCos AS IdSubCosto,SubCosto,D.pVehiculo AS PlacaVeh,CdConductor,CDT.RazonSocial AS NomConductor,D.NContrato AS NumContrato,D.IdCiaCont AS CodCiaContrato
,D.NitCliente AS Nit_Cliente,CLI.RazonSocial AS NomCliente,D.CdAgencia AS Id_Agencia,Agencia,CdDep,D.ModRequis AS ModdReq,DirEntrega,IdLocEnt,LE.Localidad AS CiuEntrega,D.Modalidad AS ModSalida
,GuiaComb,CdCiaGuia,D.TipCom AS TipoComp,D.Comprobante AS NumComp,D.IdCiaCom AS CodCiaComp,0,D.Salida AS NumSalida,FecDoc
,D.Observacion AS Observ,D.IdEstado AS CdEstado,Estado,D.TimeSys AS Fec_Add,D.FecUpdate AS Fec_Update,D.IdCiaCrea AS CdCiaCrea,D.IdUsuario AS IdUsuari,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_DevSal AS D  INNER JOIN Companias AS CN ON D.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON D.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON D.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON D.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON D.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON D.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Trn_Salidas AS S ON D.TipDoc=S.TipDoc AND D.Salida=S.Salida AND D.IdCiaDoc=S.IdCia
 LEFT JOIN Terceros AS CLI ON D.NitCliente=CLI.IdTercero
 LEFT JOIN Terceros AS CDT ON S.CdConductor=CDT.IdTercero
 LEFT JOIN Agencias AS A ON D.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LE ON S.IdLocEnt=LE.IdLocal
 LEFT JOIN SubCentros AS SC ON D.IdSubCos=SC.IdSubCos
 WHERE D.TipDoc=@pmTipDoc  AND D.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND D.IdCia LIKE ISNULL(@pmIdCia,'%%') AND S.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND D.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND D.Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND D.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')  AND IdLocEnt LIKE ISNULL(@pmIdLocEnt,'%')
 ORDER BY S.IdCia,Salida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidasOrd  @pmNumOrden INT,@pmTipOrd VARCHAR(3)
,@pmIdCiaOrd CHAR(2),@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Salida,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Modalidad
,TipOrd,NumOrden,IdCiaOrd
 FROM Trn_Salidas
 WHERE NumOrden=@pmNumOrden AND Anulado=0 AND TipOrd LIKE ISNULL(@pmTipOrd,'%')
 AND IdCiaOrd LIKE ISNULL(@pmIdCiaOrd,'%%') AND  Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,Salida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidasRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdLocEnt VARCHAR(8)=Null,@pmModalidad VARCHAR(10)=Null
,@pmAnulado BIT=Null
 AS 
 SELECT TipDoc,Salida,S.IdCia AS CdCia,Compania,Fecha,S.IdConcepto AS CdConcepto,Concepto,S.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,VrSubTotal
,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,TarifaIva,TipOrd,NumOrden,IdCiaOrd,GuiaComb,CdCiaGuia,S.IdCCosto AS CdCentCosto,CCosto,S.IdSubCos AS CdSubCenCost,SubCosto
,pVehiculo,CdConductor,CDT.RazonSocial AS NomConductor,S.NContrato AS NumContrato,IdCiaCont,NitCliente,CLI.RazonSocial AS NomCliente
,S.CdAgencia AS IdAgncia,Agencia,CdDep,ModRequis,DirEntrega,IdLocEnt,LE.Localidad AS CiuEntrega
,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado
,TimeSys,S.FecUpdate AS Fec_Update,IdCiaCrea,S.IdUsuario AS CdUsuario,Usuario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
 FROM Trn_Salidas AS S INNER JOIN Companias AS CN ON S.IdCia=CN.IdCia 
 INNER JOIN Terceros AS T ON S.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON S.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON S.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON S.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN SubCentros AS SC ON S.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS CLI ON S.NitCliente=CLI.IdTercero
 LEFT JOIN Terceros AS CDT ON S.CdConductor=CDT.IdTercero
 LEFT JOIN Agencias AS A ON S.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LE ON S.IdLocEnt=LE.IdLocal
 WHERE TipDoc=@pmTipDoc  AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND S.IdCia LIKE ISNULL(@pmIdCia,'%%') AND S.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND S.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND S.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')  AND IdLocEnt LIKE ISNULL(@pmIdLocEnt,'%')
 AND (Anulado=ISNULL(@pmAnulado,0) or Anulado=ISNULL(@pmAnulado,1))
 ORDER BY S.IdCia,Salida
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidasReq  @pmTipOrd  VARCHAR(3),@pmNumOrden  INT,@pmIdCiaOrd CHAR(2)
 AS 
 SELECT  Documento,K.IdCia AS CdCia,Compania,S.Fecha AS FechaDoc,Item,K.IdProducto AS CodProducto,DescripProd
,K.IdBodega AS CdBodega,Bodega,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,Salidas*VrUnitario AS CostoTotal
,K.CdAgencia AS KarIdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro
,CdLocal,KL.Localidad AS KarCiudad,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Remision,IdCiaRem,Unidades
,S.IdConcepto AS CdConcepto,Concepto,S.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,DirEntrega,IdLocEnt,LE.Localidad AS CiuEntrega
,S.Observacion AS Observ,TipCom,Comprobante,IdCiaCom
 FROM Trn_Salidas AS S INNER JOIN Trn_Kardex AS K ON S.TipDoc=K.TipDoc AND S.Salida=K.Documento AND S.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON K.IdCia=CN.IdCia
 INNER JOIN Terceros AS T ON S.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON S.IdConcepto=C.IdConcepto
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN Localidades AS KL ON K.CdLocal=KL.IdLocal
 LEFT JOIN Localidades AS LE ON S.IdLocEnt=LE.IdLocal
 WHERE S.TipDoc='SAI'  AND Anulado=0 AND K.TipOrd=@pmTipOrd  AND K.NumOrden=@pmNumOrden  AND K.IdCiaOrd=@pmIdCiaOrd
 ORDER BY Documento,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySalidas_Cr @pmTipDoc VARCHAR(3),@pmSalidaIni INT,@pmSalidaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT S.TipDoc AS Tipo_Doc,TipoDoc,Salida,S.IdCia AS CdCia,Compania,S.Fecha AS FechaDoc,S.IdConcepto AS CdConcepto
,Concepto,S.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,VrSubTotal,VrImpuesto,VrCostos,VrNeto,Cantidad,BaseImp,S.TarifaIva AS TarifIva
,S.TipOrd AS TipoOrden,S.NumOrden AS Num_Orden,S.IdCiaOrd AS CdCiaOrden,S.IdCCosto AS CdCentCosto,CC.CCosto AS CentCosto,S.IdSubCos AS CdSubCenCost,SC.SubCosto AS SubcCosto
,S.pVehiculo AS PlacaVeh,CdConductor,CDT.RazonSocial AS NomConductor,S.NContrato AS NumContrato,IdCiaCont
,NitCliente,CLI.RazonSocial AS NomCliente,S.CdAgencia AS IdAgncia,Agencia,CdDep,ModRequis,DirEntrega,IdLocEnt,LE.Localidad AS CiuEntrega,GuiaComb,CdCiaGuia
,Modalidad,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,S.Observacion AS Observ,S.IdEstado AS CdEstado,Estado
,S.TimeSys AS Fec_Add,S.FecUpdate AS Fec_Update,IdCiaCrea,S.IdUsuario AS CdUsuario,Usuario,Leyenda
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--Detalle
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario,VrPrecio,VrCostProm,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal
,VrCostoEnt,VrCostoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote,K.IdTercero AS KarNitTercero,KT.RazonSocial AS KarNomTercero,K.CdAgencia AS KarIdAgencia,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto
,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,CdLocal,KL.Localidad AS KarCiudad,K.pVehiculo AS KarPlacaVeh,K.Referencia AS KarReferencia,Descripcion,Comptmntos,CdProdEquiv,K.TipOrd AS KarTipOrden,K.NumOrden AS KarNumOrden,K.IdCiaOrd AS KarCiaOrden,Cotizacion,IdCiaCot
,Remision,IdCiaRem,K.Factura AS KarFactura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_Salidas AS S INNER JOIN Trn_Kardex AS K ON S.TipDoc=K.TipDoc AND S.Salida=K.Documento AND S.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON S.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON S.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON S.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON S.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON S.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON S.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd INNER JOIN Terceros AS KT ON K.IdTercero=KT.IdTercero
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN Localidades AS KL ON K.CdLocal=KL.IdLocal
 LEFT JOIN SubCentros AS SC ON S.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposCom AS TC ON S.TipCom=TC.IdCom
 LEFT JOIN Terceros AS CLI ON S.NitCliente=CLI.IdTercero
 LEFT JOIN Terceros AS CDT ON S.CdConductor=CDT.IdTercero
 LEFT JOIN Agencias AS A ON S.CdAgencia=A.IdAgencia
 LEFT JOIN Localidades AS LE ON S.IdLocEnt=LE.IdLocal
 WHERE S.TipDoc=@pmTipDoc AND Salida BETWEEN @pmSalidaIni AND  @pmSalidaFin AND S.IdCia=@pmIdCia
 AND EsProdBase=0
 ORDER BY Salida,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySectoresEco @pmIdSector VARCHAR(8)
 AS 
 IF @pmIdSector IS NULL
  SELECT IdSector,SectorEco
  FROM SectoresEco
  WHERE Inactivo=0
 ORDER BY SectorEco
 ELSE
  SELECT IdSector,SectorEco,Inactivo
  FROM SectoresEco
  WHERE IdSector=@pmIdSector
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySecurity @pmIdLla INT
 AS 
 SELECT IdLla,Descripcion,Pwd
 FROM Sys_Security
 WHERE IdLla=ISNULL(@pmIdLla,IdLla)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMov @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Documento,IdCia,Item,IdProducto,NumSerial,IdBodega,CdUbic,Linea,Movimiento,Fecha,Observacion,Referencia
 FROM Trn_SerMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMovDel @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,NumSerial,IdCia,Movimiento,COUNT(Documento) AS SCAN
 FROM Trn_SerMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 GROUP BY IdProducto,NumSerial,IdCia,Movimiento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMovDoc @pmTipDoc VARCHAR(3),@pmDocumentoIni INT
,@pmDocumentoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT Documento,IdCia,IdProducto,NumSerial,IdBodega
 FROM Trn_SerMov
 WHERE TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin
 AND IdCia=@pmIdCia 
 ORDER BY Documento,IdProducto,NumSerial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMovLta @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmLinea INT=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmMovimiento VARCHAR(3)=Null
 AS 
 SELECT NumSerial,M.IdProducto AS CdProducto,DescripProd,M.IdBodega AS CdBodega,Bodega,CdUbic,Linea,Item
,Movimiento,Observacion,M.Referencia AS Referncia
 FROM Trn_SerMov AS M INNER JOIN ProdMcias AS P ON M.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON M.IdBodega=B.IdBodega
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND M.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND M.IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND Movimiento LIKE ISNULL(@pmMovimiento,'%')
 AND (Linea>=ISNULL(@pmLinea,0)  AND Linea<=ISNULL(@pmLinea,2147483647))
 ORDER BY Linea,NumSerial

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMovPro @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmLinea INT=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmNumSerial VARCHAR(30)=Null,@pmMovimiento VARCHAR(3)=Null
 AS 
 SELECT TipDoc,Documento,IdCia,Item,IdProducto,NumSerial,IdBodega,CdUbic,Linea,Movimiento,Fecha,Observacion,Referencia
 FROM Trn_SerMov
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia 
 AND IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
 AND NumSerial LIKE ISNULL(@pmNumSerial,'%')  AND Movimiento LIKE ISNULL(@pmMovimiento,'%')
 AND (Linea>=ISNULL(@pmLinea,0)  AND Linea<=ISNULL(@pmLinea,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMovRel @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmIdProducto VARCHAR(16)=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null
,@pmInactivo BIT=Null,@pmProdInac BIT=Null,@pmNumSerial VARCHAR(30)=Null
 AS 
 SELECT  M.IdProducto AS CdProducto,DescripProd,M.NumSerial AS Num_Serial,Fecha,TipDoc,Documento,M.IdCia AS CdCia,Compania,Item
,Movimiento,TipoDoc,M.IdBodega AS CdBodega,B.Bodega AS NomBodega,M.CdUbic AS MovCdUbic,M.Observacion AS MovObserv,M.Referencia AS MovReferencia
,M.Linea AS NroLinea
--datos del serial
,S.IdBodega AS SerIdBodega,BS.Bodega AS SerNomBodega,FecEntrada,FecSalida,S.IdEstado AS CdEstado,Estado,S.Inactivo AS SerInactivo,S.Observacion AS SerObserv
,Referncia1,FecRef1,Referncia2,FecRef2,NitProv,RazonSocial,Ubicacion,S.CdUbic AS SerCdUbic,S.IdUsuario AS CdUsusari,Usuario
--Datos del producto
,P.Referencia AS Prod_Referencia,CodBarras,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,LN.Linea AS NomLinea,P.IdMarca AS CdMarca,Marca
,Tamano,P.UndMed AS CdUndMed,SU.Unidad AS ProdUnidad,P.IdUnd AS CdUndPre,UM.Unidad AS UnidadPre,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd
,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Lotes,Combo,Tanques,DescripLong,DescripAbrv
 FROM Trn_SerMov AS M INNER JOIN Trn_Seriales AS S ON M.NumSerial=S.NumSerial
 AND M.IdProducto=S.IdProducto AND M.IdCia=S.IdCia
 INNER JOIN Sys_TiposDoc AS TD ON M.TipDoc=TD.IdDoc INNER JOIN ProdMcias AS P  ON M.IdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON M.IdBodega=B.IdBodega INNER JOIN Bodegas AS BS ON S.IdBodega=BS.IdBodega
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CN ON M.IdCia=CN.IdCia  INNER JOIN EstadoSer AS E ON S.IdEstado=E.IdEstado
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd 
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS MC ON P.IdMarca=MC.IdMarca
 INNER JOIN Sys_Um AS SU ON P.UndMed=SU.UndMed 
 LEFT JOIN Terceros AS T ON S.NitProv=T.IdTercero
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND M.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND M.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND  M.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND S.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND M.NumSerial LIKE ISNULL(@pmNumSerial,'%')
 AND (S.Inactivo=ISNULL(@pmInactivo,0) or S.Inactivo=ISNULL(@pmInactivo,1))
 AND (P.Inactivo=ISNULL(@pmProdInac,0) or P.Inactivo=ISNULL(@pmProdInac,1))
 ORDER BY DescripProd,M.NumSerial,Fecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerMov_Cr @pmTipDoc VARCHAR(3),@pmDocumentoIni INT,@pmDocumentoFin INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT TipDoc,Documento,M.IdCia AS CdCia,Item,M.IdProducto AS CdProducto,DescripProd,M.NumSerial AS Num_Serial,M.IdBodega AS CdBodega,Bodega
,M.CdUbic AS CodUbic,Linea,Movimiento,Fecha,M.Observacion AS Observ,M.Referencia AS MovReferencia
--campos del serial
,FecEntrada,FecSalida,NitProv,RazonSocial,S.IdEstado AS CdEstado,Estado,S.Inactivo AS Inactiv
,S.Observacion AS SerObserv,Referncia1,Referncia2,FecRef1,FecRef2,Ubicacion
 FROM Trn_SerMov AS M INNER JOIN Trn_Seriales AS S ON M.NumSerial=S.NumSerial
 AND M.IdCia=S.IdCia AND M.IdProducto=S.IdProducto
 INNER JOIN ProdMcias  AS P ON M.IdProducto=P.IdProducto INNER JOIN Bodegas AS B ON M.IdBodega=B.IdBodega
 INNER JOIN EstadoSer AS E ON S.IdEstado=E.IdEstado
 LEFT JOIN Terceros AS T ON S.NitProv=T.IdTercero
 WHERE TipDoc=@pmTipDoc AND Documento BETWEEN @pmDocumentoIni AND @pmDocumentoFin AND M.IdCia=@pmIdCia 
 AND  M.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySeriales @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2)
 AS 
 SELECT IdProducto,NumSerial,IdCia,IdBodega,CdUbic,FecEntrada,FecSalida,NitProv,IdEstado,Inactivo,Observacion,Referncia1,Referncia2,FecRef1,FecRef2,Ubicacion,IdUsuario
 FROM Trn_Seriales
 WHERE IdProducto=@pmIdProducto AND NumSerial LIKE ISNULL(@pmNumSerial,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia,NumSerial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerialesLta @pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmFechaIni SMALLDATETIME=Null
,@pmFechaFin SMALLDATETIME=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmProdInac BIT=Null
 AS 
 SELECT S.IdProducto AS CdProd,DescripProd,NumSerial,IdCia,S.IdBodega AS CdBodega,Bodega,CdUbic,FecEntrada,FecSalida,NitProv,RazonSocial,S.IdEstado AS CdEstado
,Estado,S.Inactivo AS Inactiv,S.Observacion AS Observ,Referncia1,Referncia2,FecRef1,FecRef2,Ubicacion,S.IdUsuario AS IdUsuari,Usuario,NColor
 FROM Trn_Seriales AS S INNER JOIN ProdMcias AS P  ON S.IdProducto=P.IdProducto
 INNER JOIN EstadoSer AS E ON S.IdEstado=E.IdEstado
 INNER JOIN Bodegas AS B ON S.IdBodega=B.IdBodega
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS T ON S.NitProv=T.IdTercero
 WHERE S.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  S.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND S.IdEstado LIKE ISNULL(@pmIdEstado,'%')
AND (FecEntrada>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecEntrada<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (S.Inactivo=ISNULL(@pmInactivo,0) or S.Inactivo=ISNULL(@pmInactivo,1))
 AND (P.Inactivo=ISNULL(@pmProdInac,0) or P.Inactivo=ISNULL(@pmProdInac,1))
 ORDER BY DescripProd,IdCia,FecEntrada,NumSerial


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySerialesPro @pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdBodega VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null
 AS 
 SELECT S.IdProducto AS CdProd,DescripProd,NumSerial,IdCia,S.IdBodega AS CdBodega,CdUbic,FecEntrada,FecSalida,NitProv,S.IdEstado AS CdEstado
,S.Inactivo AS Inactiv,S.Observacion AS Observ,Referncia1,Referncia2,FecRef1,FecRef2,Ubicacion,S.IdUsuario AS IdUsuari
 FROM Trn_Seriales AS S INNER JOIN ProdMcias AS P  ON S.IdProducto=P.IdProducto
 WHERE S.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND S.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND S.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (S.Inactivo=ISNULL(@pmInactivo,0) or S.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY DescripProd,IdCia,FecEntrada,NumSerial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySeriales_Cr @pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmFechaIni SMALLDATETIME=Null
,@pmFechaFin SMALLDATETIME=Null,@pmIdBodega VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmProdInac BIT=Null
 AS 
 SELECT S.IdProducto AS CdProd,DescripProd,NumSerial,S.IdCia AS CdCia,Compania,S.IdBodega AS CdBodega,Bodega,Descrip,CdUbic,FecEntrada,FecSalida
,NitProv,RazonSocial,S.IdEstado AS CdEstado,Estado,S.Inactivo AS Inactiv,S.Observacion AS Observ
,Referncia1,Referncia2,FecRef1,FecRef2,Ubicacion,S.IdUsuario AS IdUsuari,Usuario
--información del producto
,P.Referencia AS Prod_Referencia,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca,Tamano,P.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,P.IdUnd AS CdUndPre,UM.Unidad AS UnidadPre,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong,NColor
 FROM Trn_Seriales AS S INNER JOIN ProdMcias AS P  ON S.IdProducto=P.IdProducto
 INNER JOIN EstadoSer AS E ON S.IdEstado=E.IdEstado
 INNER JOIN Bodegas AS B ON S.IdBodega=B.IdBodega
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN Companias AS CN ON S.IdCia=CN.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd 
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON P.UndMed=SU.UndMed 
 LEFT JOIN Terceros AS T ON S.NitProv=T.IdTercero
 WHERE S.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND S.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND  S.IdBodega LIKE ISNULL(@pmIdBodega,'%') AND S.IdEstado LIKE ISNULL(@pmIdEstado,'%')
AND (FecEntrada>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND FecEntrada<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND (S.Inactivo=ISNULL(@pmInactivo,0) or S.Inactivo=ISNULL(@pmInactivo,1))
 AND (P.Inactivo=ISNULL(@pmProdInac,0) or P.Inactivo=ISNULL(@pmProdInac,1))
 ORDER BY DescripProd,S.IdCia,FecEntrada,NumSerial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobDetalle @pmIdDec VARCHAR(4),@pmNumDeclara INT,@pmItem INT
 AS 
 SELECT IdDec,NumDeclara,Item,Concepto,Cantidad,VrUnitario,VrBase,Tarifa,VrLiquida,VrPrecio,ClaseProd,TarifGalon,PorcBase
 FROM Trn_SobDetalle
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY  Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobEntidades @pmIdEnterr VARCHAR(8)
 AS 
 SELECT IdEnterr,TipoEnterr,NitEntidad,TipoId,Dv,DirOficina,Telefono,Fax,SitioWeb,e_mail,CedRepLegal,RepLegal
,CargoRep,FormaPago,IdClase,NumCuenta,NomTitular,IdBanco,CdLocCue,NomEntidad,Inactivo,CodCtaPag
 FROM SobEntidades
 WHERE IdEnterr=@pmIdEnterr
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobEntidadesDep @pmTipoEnterr VARCHAR(10) 
 AS 
 SELECT IdEnterr,NomEntidad,Departamento+' '+IdEnterr AS DsEnt
 FROM SobEntidades AS S  INNER JOIN Departamentos AS D ON  S.IdEnterr=D.IdDep
 WHERE TipoEnterr=@pmTipoEnterr AND S.Inactivo=0
 ORDER BY  Departamento
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobEntidadesLta @pmTipoEnterr VARCHAR(10)=Null,@pmIdBanco VARCHAR(4)=Null
,@pmInactivo BIT=Null
 AS 
 SELECT IdEnterr,NitEntidad,RazonSocial,E.TipoId AS Tipo_Id,E.Dv AS DvNit,TipoEnterr,NomEntidad,LE.Localidad AS NomMunicipio
,LE.IdDep AS CdDpto,Departamento,DirOficina,E.Telefono AS NumTelefono,E.Fax AS NumFax,E.SitioWeb AS Sitio_Web,E.e_mail AS Email,CedRepLegal,RepLegal
,CargoRep,FormaPago,E.IdClase AS CdClase,ClaseCuenta,NumCuenta,E.IdBanco AS CdBanco,Banco,CodEntidad,NomTitular
,CdLocCue,L.Localidad AS LugarCuenta,CodCtaPag,E.Inactivo AS Inactiv
 FROM SobEntidades AS E INNER JOIN ClaseCta AS C ON E.IdClase=C.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS T ON E.NitEntidad=T.IdTercero
 LEFT JOIN  Localidades AS LE ON E.IdEnterr=LE.IdLocal LEFT JOIN Departamentos AS DP ON LE.IdDep=DP.IdDep
 LEFT JOIN  Localidades AS L ON E.CdLocCue=L.IdLocal
 WHERE TipoEnterr LIKE ISNULL(@pmTipoEnterr,'%')
 AND E.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND  (E.Inactivo=ISNULL(@pmInactivo,0) or E.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdEnterr
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobEntidadesMun @pmTipoEnterr VARCHAR(10) 
 AS 
 SELECT IdEnterr,NomEntidad,Localidad+' ('+Departamento+') '+IdEnterr AS DsEnt
 FROM SobEntidades AS S INNER JOIN Localidades AS L ON S.IdEnterr=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 WHERE TipoEnterr LIKE ISNULL(@pmTipoEnterr,'%') AND S.Inactivo=0
 ORDER BY  Localidad
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobProductos @pmIdClase INT
 AS 
 SELECT IdClase,IdProducto,ClaseProd
 FROM SobProductos
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobProductosExi @pmIdProducto VARCHAR(16),@pmIdClase INT
 AS 
 SELECT IdClase,IdProducto,ClaseProd
 FROM SobProductos
 WHERE IdProducto=@pmIdProducto AND IdClase<>@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobProductosId
 AS 
 SELECT MAX(IdClase) AS MXITM
 FROM SobProductos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobProductosLta 
 AS 
 SELECT IdClase,S.IdProducto AS CdProducto,DescripProd,ClaseProd
 FROM SobProductos AS S INNER JOIN ProdMcias AS P ON S.IdProducto=P.IdProducto
 ORDER BY  IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobTiposDec @pmIdDec VARCHAR(4)
 AS 
 SELECT IdDec,Declaracion,TipoDec,NitDec,TipoIdDec,NombreDec,DvDec,Direccion,IdLocal,Telefono
,CedRepLegal,RepLegal,TipoFirma,NitRevisor,NomRevisor,TpRevisor,TarifSubsidio,Consecutivo
 FROM SobTiposDec
 WHERE IdDec=@pmIdDec
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobTiposDecDso 
 AS 
 SELECT IdDec,Declaracion,Declaracion+' '+IdDec AS DsDec
 FROM SobTiposDec
 ORDER BY Declaracion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobTiposDecGas 
 AS 
 SELECT IdDec,Declaracion,Declaracion+' '+IdDec AS DsDec
 FROM SobTiposDec
 WHERE IdDec<>'0010'
 ORDER BY Declaracion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobTiposDecLta 
 AS 
 SELECT IdDec,Declaracion,TipoDec,TipoIdDec,NitDec,NombreDec,DvDec,Direccion,T.IdLocal AS CdLocal,Localidad
,L.IdDep AS CdDepto,Departamento,Telefono,CedRepLegal,RepLegal,TipoFirma,NitRevisor,NomRevisor,TpRevisor,TarifSubsidio,Consecutivo
 FROM SobTiposDec AS T INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 ORDER BY IdDec
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasas @pmIdDec VARCHAR(4),@pmNumDeclara INT
 AS 
 SELECT IdDec,NumDeclara,Fecha,nAnno,nMes,IdEnterr,TipoEntidad,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar
,VrFondoSubs,VrRecaudoDep,NitDeclara,DvDeclara,CedDeclara,Declarante,DirEntidad,EmailEnt,NomRepres,CargoRepres
,TipoFirma,NitRevisor,NomRevisor,TpRevisor,NumCheque,CodBanco,IdClase,NumCuenta,IdBanco,CdLocCue,TitularCuenta
,VrEfectivo,VrOtros,VrTransf,VrPagado,Estado,Adhesivo,FechaPago,EsCorr,NumCorr,FecCorr,Observacion,TimeSys,FecUpdate,IdUsuario,CodCta
,TipEgreso,NumEgreso,CiaEgreso
 FROM Trn_Sobretasas
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasasDet @pmnAnno INT,@pmnMesIni INT=Null,@pmnMesFin INT=Null
,@pmIdDec VARCHAR(4)=Null,@pmIdEnterr VARCHAR(8)=Null,@pmTipoEntidad VARCHAR(10)=Null,@pmIdBanco VARCHAR(4)=Null
,@pmNumDeclaraIni INT=Null,@pmNumDeclaraFin INT=Null
 AS 
 SELECT S.IdDec AS CdDec,S.NumDeclara AS NroDeclara,nAnno,nMes,S.IdEnterr AS CdEnterr,FormaPago,NomEntidad,Localidad,IdDep
,TipoEntidad,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar
,VrEfectivo,VrOtros,VrTransf,VrPagado,Estado,CodBanco,CodCta
,Item,Concepto,Cantidad,VrUnitario,VrBase,Tarifa,VrLiquida,VrPrecio,ClaseProd,TarifGalon,PorcBase
 FROM  Trn_Sobretasas AS S INNER JOIN  Trn_SobDetalle AS D ON S.IdDec=D.IdDec AND S.NumDeclara=D.NumDeclara
 INNER JOIN SobEntidades AS E ON S.IdEnterr=E.IdEnterr
 LEFT JOIN Localidades AS L ON E.IdEnterr=L.IdLocal
 WHERE nAnno=@pmnAnno AND Estado<=1 --2 Y 3 no se incluyen
 AND (nMes>=ISNULL(@pmnMesIni,0)  AND nMes<=ISNULL(@pmnMesFin,2147483647))
 AND S.IdDec LIKE ISNULL(@pmIdDec,'%') AND S.IdEnterr LIKE ISNULL(@pmIdEnterr,'%')
 AND TipoEntidad LIKE ISNULL(@pmTipoEntidad,'%') AND S.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND (S.NumDeclara>=ISNULL(@pmNumDeclaraIni,0)  AND S.NumDeclara<=ISNULL(@pmNumDeclaraFin,2147483647))
 ORDER BY S.IdDec,S.NumDeclara,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasasEgr  @pmnAnno INT,@pmnMes INT,@pmIdBanco VARCHAR(4)=Null
,@pmIdDec VARCHAR(4)=Null,@pmFormaPago VARCHAR(10)=Null
 AS 
 SELECT IdDec,NumDeclara,Fecha,nAnno,nMes,NitDeclara,DvDeclara,CedDeclara,Declarante
,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar,VrEfectivo,VrOtros,VrTransf,VrPagado
,TipoEntidad,S.IdEnterr AS CdEnterr,E.TipoId AS EntTipoID,NitEntidad,E.Dv AS EntDv,RazonSocial
,NomRepres,FormaPago,S.IdClase AS CdClase,S.NumCuenta AS NroCuenta,S.IdBanco AS IdBanc,Banco,CodEntidad
,S.CdLocCue AS IdLocCue,TitularCuenta,NumCheque,CodBanco,Estado,EsCorr,CodCta
 FROM Trn_Sobretasas AS S INNER JOIN SobEntidades AS E ON S.IdEnterr=E.IdEnterr
 INNER JOIN Bancos AS B ON S.IdBanco=B.IdBanco  LEFT JOIN Terceros AS T ON E.NitEntidad=T.IdTercero
 WHERE  nAnno=@pmnAnno AND nMes=@pmnMes AND Estado<=1 AND VrTotalPagar>0
 AND  IdDec LIKE ISNULL(@pmIdDec ,'%') AND S.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND FormaPago LIKE ISNULL(@pmFormaPago,'%')
 ORDER BY IdDec,NumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasasEnt @pmIdDec VARCHAR(4),@pmnAnno INT,@pmnMes INT
,@pmIdEnterr VARCHAR(8),@pmTipoEntidad VARCHAR(10)
 AS 
 SELECT IdDec,NumDeclara,Fecha,nAnno,nMes,IdEnterr,TipoEntidad,Estado
 FROM Trn_Sobretasas
 WHERE IdDec=@pmIdDec AND  nAnno=@pmnAnno AND nMes=@pmnMes
 AND IdEnterr=@pmIdEnterr AND TipoEntidad=@pmTipoEntidad
 ORDER BY NumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasasLta @pmIdDec VARCHAR(4),@pmnAnno INT,@pmnMesIni INT=Null,@pmnMesFin INT=Null
,@pmNumDeclaraIni INT=Null,@pmNumDeclaraFin INT=Null,@pmIdEnterr VARCHAR(8)=Null,@pmTipoEntidad VARCHAR(10)=Null
,@pmIdBanco VARCHAR(4)=Null,@pmEstado INT=Null
 AS 
 SELECT IdDec,NumDeclara,Fecha,nAnno,nMes,TipoEntidad,S.IdEnterr AS CdEnterr,TipoId,NitEntidad,Dv,DirEntidad,SitioWeb,EmailEnt
,NomRepres,CargoRepres,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar
,VrFondoSubs,VrRecaudoDep,NitDeclara,DvDeclara,CedDeclara,Declarante,TipoFirma,NitRevisor,NomRevisor
,TpRevisor,FormaPago,NumCheque,CodBanco,CodCta,NumeroCta,S.IdClase AS CdClase,ClaseCuenta,S.NumCuenta AS NroCuenta,S.IdBanco AS IdBanc,Banco
,S.CdLocCue AS IdLocCue,Localidad,TitularCuenta,VrEfectivo,VrOtros,VrTransf,VrPagado,TipEgreso,NumEgreso,CiaEgreso,Estado,Adhesivo,FechaPago,EsCorr
,NumCorr,FecCorr,Observacion,TimeSys,FecUpdate,S.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Sobretasas AS S INNER JOIN SobEntidades AS E ON S.IdEnterr=E.IdEnterr
 INNER JOIN Bancos AS B ON S.IdBanco=B.IdBanco
 INNER JOIN  ClaseCta AS CT ON S.IdClase=CT.IdClase
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 LEFT JOIN Localidades AS L ON S.CdLocCue=L.IdLocal
 LEFT JOIN CtasCorrientes AS CTA ON S.CodCta=CTA.IdCta
 WHERE IdDec=@pmIdDec AND nAnno=@pmnAnno AND nMes BETWEEN ISNULL(@pmnMesIni,0)  AND ISNULL(@pmnMesFin,20)
 AND NumDeclara BETWEEN  ISNULL(@pmNumDeclaraIni,0) AND ISNULL(@pmNumDeclaraFin,2147483647)
 AND S.IdEnterr LIKE ISNULL(@pmIdEnterr,'%') AND TipoEntidad  LIKE ISNULL(@pmTipoEntidad ,'%')
 AND S.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND (Estado>=ISNULL(@pmEstado,-1)  AND Estado<=ISNULL(@pmEstado,2147483647)) 
 ORDER BY NumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasasMes @pmIdDec VARCHAR(4),@pmnAnno INT,@pmnMes INT
,@pmIdEnterr VARCHAR(8),@pmTipoEntidad VARCHAR(10)
 AS 
 SELECT IdDec,NumDeclara,Fecha,nAnno,nMes,IdEnterr,TipoEntidad,Adhesivo,FechaPago,EsCorr,NumCorr,FecCorr,Estado
 FROM Trn_Sobretasas
 WHERE IdDec=@pmIdDec AND  nAnno=@pmnAnno AND nMes=@pmnMes
 AND IdEnterr=@pmIdEnterr AND TipoEntidad=@pmTipoEntidad AND Estado<2 --2anulado,3-Corregido
 ORDER BY NumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasas_Cr  @pmIdDec VARCHAR(4),@pmNumDeclaraIni INT,@pmNumDeclaraFin INT
,@pmnAnnoIni INT=Null,@pmnAnnoFin INT=Null,@pmnMesIni INT=Null,@pmnMesFin INT=Null
,@pmIdEnterr VARCHAR(8)=Null,@pmTipoEntidad VARCHAR(10)=Null,@pmIdBanco VARCHAR(4)=Null,@pmEstado INT=Null
 AS 
 SELECT S.IdDec AS CdDec,Declaracion,S.NumDeclara AS NumeroDec,Fecha,nAnno,nMes,NitDeclara,DvDeclara,CedDeclara,Declarante
,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar,VrFondoSubs,VrRecaudoDep,VrEfectivo,VrOtros,VrTransf,VrPagado,TipEgreso,NumEgreso,CiaEgreso
--detalles
,Item,Concepto,Cantidad,VrUnitario,VrBase,Tarifa,VrLiquida,VrPrecio,ClaseProd,TarifGalon,PorcBase
--entidad
,TipoEntidad,S.IdEnterr AS CdEnterr,NomEntidad,E.TipoId AS EntTipoID,NitEntidad,E.Dv AS EntDv,RazonSocial,DirEntidad,E.Telefono AS EntTelefono,E.Fax AS EntFax
,E.SitioWeb AS EntSitioWeb,EmailEnt,E.CedRepLegal AS CedRepres,NomRepres,CargoRepres,FormaPago
,S.IdClase AS CdClase,ClaseCuenta,S.NumCuenta AS NroCuenta,S.IdBanco AS IdBanc,Banco,CodEntidad
,S.CdLocCue AS IdLocCue,L.Localidad AS LugarCuenta,L.IdDep AS CdDepLugar,DP.Departamento AS DptoLugarCuenta,TitularCuenta,NumCheque,CodBanco,CodCta,NumeroCta
,S.TipoFirma AS TipFirma,S.NitRevisor AS IdRevisor,S.NomRevisor AS NombRevisor,S.TpRevisor AS TarjProf,Estado,Adhesivo,FechaPago
,EsCorr,NumCorr,FecCorr,S.Observacion AS Observ,S.IdUsuario AS IdUsuari,Usuario,TimeSys
--información del tipo de declaracion
,TipoDec,TipoIdDec,TD.Direccion AS DirDeclarante,TD.IdLocal AS CdCiuDec,LT.Localidad AS CiuDeclarante
,LT.IdDep AS CdDepDec,DT.Departamento AS DptoDeclarante,TD.Telefono AS TelefDeclara,TD.CedRepLegal AS CedRepDeclara,TD.RepLegal AS RepLegalDeclara
 FROM Trn_Sobretasas AS S INNER JOIN Trn_SobDetalle AS D ON S.IdDec=D.IdDec AND S.NumDeclara=D.NumDeclara
 INNER JOIN SobEntidades AS E ON S.IdEnterr=E.IdEnterr INNER JOIN SobTiposDec AS TD ON S.IdDec=TD.IdDec
 INNER JOIN Bancos AS B ON S.IdBanco=B.IdBanco
 INNER JOIN  ClaseCta AS CT ON S.IdClase=CT.IdClase
 INNER JOIN adm_Usuarios AS U ON S.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LT ON TD.IdLocal=LT.IdLocal INNER JOIN Departamentos AS DT ON LT.IdDep=DT.IdDep
 LEFT JOIN Localidades AS L ON S.CdLocCue=L.IdLocal LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN Terceros AS T ON E.NitEntidad=T.IdTercero LEFT JOIN CtasCorrientes AS CTA ON S.CodCta=CTA.IdCta
 WHERE S.IdDec LIKE ISNULL(@pmIdDec ,'%')
 AND S.NumDeclara BETWEEN  ISNULL(@pmNumDeclaraIni,0) AND ISNULL(@pmNumDeclaraFin,2147483647)
 AND nMes BETWEEN ISNULL(@pmnMesIni,0)  AND ISNULL(@pmnMesFin,20)
 AND nAnno BETWEEN ISNULL(@pmnAnnoIni,0) AND ISNULL(@pmnAnnoFin,2147483647)
 AND S.IdEnterr LIKE ISNULL(@pmIdEnterr,'%') AND TipoEntidad  LIKE ISNULL(@pmTipoEntidad ,'%')
 AND S.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND (Estado>=ISNULL(@pmEstado,-1)  AND Estado<=ISNULL(@pmEstado,2147483647)) 
 ORDER BY S.NumDeclara,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySobretasas_Crr  @pmnAnno INT,@pmnMesIni INT=Null,@pmnMesFin INT=Null,@pmIdDec VARCHAR(4)=Null
,@pmNumDeclaraIni INT=Null,@pmNumDeclaraFin INT=Null,@pmIdEnterr VARCHAR(8)=Null,@pmTipoEntidad VARCHAR(10)=Null
,@pmIdBanco VARCHAR(4)=Null
 AS 
 SELECT  IdDec,NumDeclara,nAnno,nMes,TipoEntidad,S.IdEnterr AS CdEnterr,E.TipoId AS EntTipoID
,NitEntidad,E.Dv AS EntDv,NomEntidad,S.IdClase AS CdClase,ClaseCuenta,S.NumCuenta AS NroCuenta,S.IdBanco AS IdBanc,Banco,CodEntidad,TitularCuenta
--valores
,tmAcpmCant,tmAcpmBase,tmAcpmValor,tmAcpmTarif,tmAcpmPrecio,tmAcpiCant,tmAcpiBase,tmAcpiValor,tmAcpiTarif,tmAcpiPrecio,tmGcorCant,tmGcorBase
,tmGcorValor,tmGcorTarif,tmGcorPrecio,tmGextCant,tmGextBase,tmGextValor,tmGextTarif,tmGextPrecio,tmGimpCant,tmGimpBase,tmGimpValor,tmGimpTarif
,tmGimpPrecio,tmGcoxCant,tmGcoxBase,tmGcoxValor,tmGcoxTarif,tmGcoxPrecio,tmGeoxCant,tmGeoxBase,tmGeoxValor,tmGeoxTarif,tmGeoxPrecio
,tmAczfCant,tmAczfBase,tmAczfValor,tmAczfTarif,tmAczfPrecio,tmGnzfCant,tmGnzfBase,tmGnzfValor,tmGnzfTarif,tmGnzfPrecio
,VrSobretasa,VrSanciones,VrCompensa,VrIntMora,VrTotalPagar,VrFondoSubs,VrRecaudoDep,VrEfectivo,VrOtros,VrTransf,VrPagado,TipEgreso,NumEgreso,CiaEgreso
,FormaPago,S.CdLocCue AS IdLocCue,L.Localidad AS LugarCuenta,L.IdDep AS CdDepLugar,DP.Departamento AS DptoLugarCuenta,NumCheque,CodBanco
,CodCta,NumeroCta,DirEntidad,E.Telefono AS EntTelefono,E.Fax AS EntFax,E.SitioWeb AS EntSitioWeb,EmailEnt,E.CedRepLegal AS CedRepres,NomRepres,CargoRepres
,RazonSocial,LT.Localidad AS NomCiudad,LT.IdDep AS CdDep,DT.Departamento AS NomDpto
,Estado,Fecha,NitDeclara,DvDeclara,CedDeclara,Declarante
 FROM Trn_Sobretasas AS S  INNER JOIN tm_Sobtasas AS T ON S.IdDec=T.tmIdDec AND S.NumDeclara=T.tmNumDeclara
 INNER JOIN SobEntidades AS E ON S.IdEnterr=E.IdEnterr
 INNER JOIN Bancos AS B ON S.IdBanco=B.IdBanco
 INNER JOIN ClaseCta AS CT ON S.IdClase=CT.IdClase
 LEFT JOIN Localidades AS L ON S.CdLocCue=L.IdLocal LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN Localidades AS LT ON S.IdEnterr=LT.IdLocal LEFT JOIN Departamentos AS DT ON LT.IdDep=DT.IdDep
 LEFT JOIN Terceros AS TE ON E.NitEntidad=TE.IdTercero
 LEFT JOIN CtasCorrientes AS CTA ON S.CodCta=CTA.IdCta
 WHERE nAnno=@pmnAnno AND Estado<=1 AND IdDec LIKE ISNULL(@pmIdDec ,'%')
 AND nMes BETWEEN ISNULL(@pmnMesIni,0)  AND ISNULL(@pmnMesFin,20)
 AND NumDeclara BETWEEN  ISNULL(@pmNumDeclaraIni,0) AND ISNULL(@pmNumDeclaraFin,2147483647)
 AND S.IdEnterr LIKE ISNULL(@pmIdEnterr,'%') AND TipoEntidad  LIKE ISNULL(@pmTipoEntidad ,'%')
 AND S.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 ORDER BY IdDec,NomEntidad,NumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubCentros @pmIdSubCos VARCHAR(16)
 AS 
 SELECT IdSubCos,SubCosto,IdCCosto,FecAdd,FecUpdate,Inactivo
 FROM SubCentros
 WHERE IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubCentrosDso @pmIdCCosto VARCHAR(16)=Null,@pmInactivo BIT=Null
 AS 
 IF @pmIdCCosto IS NULL
  BEGIN
   SELECT IdSubCos,SubCosto,SubCosto+' '+IdSubCos AS DsSub,IdCCosto
   FROM SubCentros
   WHERE  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
   ORDER BY IdCCosto,SubCosto
  END
 ELSE
  BEGIN
   SELECT IdSubCos,SubCosto,SubCosto+' '+IdSubCos AS DsSub,IdCCosto
   FROM SubCentros
   WHERE (IdSubCos='0'  OR IdCCosto='0' OR   IdCCosto=@pmIdCCosto)
   AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
   ORDER BY IdCCosto,SubCosto
  END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubCentrosLta @pmIdCCosto VARCHAR(16)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdSubCos,SubCosto,S.IdCCosto AS CodCcosto,CCosto,FecAdd,FecUpdate,S.Inactivo AS Inactvo
 FROM SubCentros AS S INNER JOIN CentroCosto AS C ON S.IdCCosto=C.IdCCosto
 WHERE S.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND (S.Inactivo=ISNULL(@pmInactivo,0) or S.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY CCosto,SubCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubGrupos @pmIdSubgrupo VARCHAR(8)
 AS 
 SELECT IdSubgrupo,Subgrupo,IdGrupo,CdTarIva,CdTarAut,CdTarRet,CdTarIca
,TipoInvent,Inactivo
 FROM SubGrupos
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQrySubGruposAgenc @pmIdSubAgenc VARCHAR(4)
 AS 
 SELECT IdSubAgenc,SubGrupoAgenc,IdGrupo,Inactivo
 FROM SubGruposAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubGruposAgencLta @pmIdGrupo VARCHAR(4)
 AS 
 SELECT IdSubAgenc,SubGrupoAgenc,S.IdGrupo AS CdGrupo,GrupoAgenc
 FROM SubGruposAgenc AS S INNER JOIN GruposAgenc AS G  ON S.IdGrupo=G.IdGrupo
 WHERE S.Inactivo=0 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 ORDER BY SubGrupoAgenc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubGruposDso @pmIdGrupo VARCHAR(8)=Null,@pmTipoInvent VARCHAR(10)=Null
 AS 
 SELECT IdSubgrupo,Subgrupo,Subgrupo+' '+IdSubgrupo +' ('+Grupo+')' AS DsSub
 FROM SubGrupos AS S INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
 WHERE S.Inactivo=0 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND  S.TipoInvent LIKE ISNULL(@pmTipoInvent,'%')
 ORDER BY Grupo,Subgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubGruposLin @pmIdSubgrupo VARCHAR(8)=Null
 AS 
 SELECT IdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea
,S.TipoInvent AS TipoInv
 FROM SubGrupos AS S INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 WHERE  IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubGruposLta @pmIdGrupo VARCHAR(8)=Null,@pmIdLinea VARCHAR(8)=Null
,@pmTipoInvent VARCHAR(10)=Null
 AS 
 SELECT IdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,CdTarIva
,Tarifa,Simbolo,CdTarAut,CdTarRet,CdTarIca,S.TipoInvent AS TipoInventario
 FROM SubGrupos AS S INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea
 LEFT JOIN Tablapor AS TP ON S.CdTarIva=TP.IdTarifa
 WHERE S.Inactivo=0 AND S.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND G.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND S.TipoInvent LIKE ISNULL(@pmTipoInvent,'%')
 ORDER BY Linea,Grupo,Subgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubGruposTas @pmIdSubgrupo VARCHAR(8)
 AS 
 SELECT IdSubgrupo,Subgrupo,CdTarIva,TI.Tarifa AS TarfIva,TI.Simbolo AS SimbIva
,CdTarRet,TR.Tarifa AS TarfRet,TR.Simbolo AS SimbRet
,CdTarIca,TC.Tarifa AS TarfIca,TC.Simbolo AS SimbIca
,CdTarAut,TA.Tarifa AS TarfAut,TA.Simbolo AS SimbAut
 FROM SubGrupos AS S LEFT JOIN Tablapor AS TI ON S.CdTarIva=TI.IdTarifa
 LEFT JOIN Tablapor AS TR ON S.CdTarRet=TR.IdTarifa
 LEFT JOIN Tablapor AS TC ON S.CdTarIca=TC.IdTarifa
 LEFT JOIN Tablapor AS TA ON S.CdTarAut=TA.IdTarifa
 WHERE IdSubgrupo=@pmIdSubgrupo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubgruposCue @pmIdSubgrupo VARCHAR(8),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdSubgrupo,IdClase,IdCuenta,CdCueDev,CdCueCom,CdCueCdv,FecAdd,FecUpdate
 FROM SubgruposCue
 WHERE IdSubgrupo=@pmIdSubgrupo AND IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubgruposCueLta @pmIdSubgrupo VARCHAR(8)=Null
 AS 
 SELECT SC.IdSubgrupo AS CdSubg,Subgrupo,SC.IdClase AS CdClase,NomClase,SC.IdCuenta AS CdCueVen
,P.NomCuenta AS CueVentas,CdCueDev,CdCueCom,CP.NomCuenta AS CueCompras,CdCueCdv,FecAdd,FecUpdate
 FROM SubgruposCue AS SC INNER JOIN  SubGrupos AS S ON SC.IdSubgrupo=S.IdSubgrupo
 INNER JOIN ClaseSub AS C ON SC.IdClase=C.IdClase
 INNER JOIN Puc AS P ON SC.IdCuenta=P.IdCuenta
 LEFT JOIN Puc AS CP ON SC.CdCueCom=CP.IdCuenta
 WHERE S.Inactivo=0 AND SC.IdSubgrupo LIKE ISNULL(@pmIdSubgrupo,'%') 
 ORDER BY Subgrupo,SC.IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubgruposCuePuc @pmIdSubgrupo VARCHAR(8)
 AS 
 SELECT IdSubgrupo,IdClase,SC.IdCuenta AS CdCuenta,P.NomCuenta AS NomCue,P.Movimiento AS MovCue
,P.CentroCosto AS CCcue,P.SubCentro AS SubCCue,CdCueCom,PC.NomCuenta AS NomCueCom
,PC.Movimiento AS MovCueCom,PC.CentroCosto AS CCCueCom,PC.SubCentro AS SubCCueCom
,CdCueDev,CdCueCdv
 FROM SubgruposCue AS SC INNER  JOIN Puc AS P ON SC.IdCuenta=P.IdCuenta
 LEFT JOIN Puc AS PC ON SC.CdCueCom=PC.IdCuenta
 WHERE IdSubgrupo=@pmIdSubgrupo
 ORDER BY IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubzonas @pmIdSzona VARCHAR(4)
 AS 
 SELECT IdSzona,Subzona,IdZona,Inactivo
 FROM Subzonas
 WHERE IdSzona=@pmIdSzona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubzonasDso @pmIdZona VARCHAR(4)=Null
 AS 
  SELECT IdSzona,Subzona,Subzona+' '+IdSzona+'('+Zona+')' AS DsSub
  FROM Subzonas AS S INNER JOIN Zonas AS Z ON S.IdZona=Z.IdZona
  WHERE S.Inactivo=0 AND S.IdZona LIKE ISNULL(@pmIdZona,'%')
  ORDER BY Subzona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrySubzonasLta @pmIdZona VARCHAR(4)=Null
 AS 
  SELECT IdSzona,Subzona,S.IdZona AS CdZona,Zona
  FROM Subzonas AS S INNER JOIN Zonas AS Z ON S.IdZona=Z.IdZona
  WHERE S.Inactivo=0 AND S.IdZona LIKE ISNULL(@pmIdZona,'%')
  ORDER BY Zona,Subzona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTFmtos @pmnFormato VARCHAR(10)
 AS 
 IF @pmnFormato IS NULL
  SELECT nFormato,FmtoFactura,TipFac
  FROM Sys_TFmtos
 ORDER BY nFormato
 ELSE
  SELECT nFormato,FmtoFactura,TipFac
  FROM Sys_TFmtos
  WHERE nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTFmtosDso @pmTipFac VARCHAR(3)=Null
 AS 
  SELECT nFormato,FmtoFactura,nFormato+' '+FmtoFactura AS DsFmt
  FROM Sys_TFmtos
  WHERE TipFac LIKE ISNULL(@pmTipFac,'%')
  ORDER BY nFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTablapor @pmIdTarifa VARCHAR(4)
 AS 
 SELECT IdTarifa,Descripcion,IdClase,Tarifa,Simbolo,CdLocdad,Minimo,Maximo,Inactivo
 FROM Tablapor
 WHERE IdTarifa=@pmIdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTablaporCiu @pmIdClase  VARCHAR(4),@pmCdLocdad VARCHAR(8)
 AS 
 SELECT IdTarifa,Descripcion,IdClase,Tarifa,Simbolo,CdLocdad,Minimo,Maximo,Inactivo
 FROM Tablapor
 WHERE IdClase=@pmIdClase AND CdLocdad=@pmCdLocdad AND Inactivo=0
 ORDER BY IdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTablaporDso @pmIdClase VARCHAR(4)=Null,@pmPrecsn INT,@pmDecm INT
 AS 
 --ICA  @pmPrecsn=8,@pmDecm=4
--IVA  @pmPrecsn=5,@pmDecm=2
--OTROS  @pmPrecsn=6,@pmDecm=2
 SELECT IdTarifa,Tarifa,IdTarifa+' '+STR(Tarifa,@pmPrecsn,@pmDecm)+Simbolo+' '+ISNULL(Descripcion,'') AS DsTar
 FROM Tablapor
 WHERE Inactivo=0 AND  IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY IdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTablaporLta @pmIdClase VARCHAR(4)=Null,@pmInactivo BIT=Null
 AS 
 SELECT T.IdClase AS CodClase,NomClase,IdTarifa,Tarifa,Simbolo,Descripcion,Minimo,Maximo,CdLocdad,T.Inactivo AS Inactvo
 FROM Tablapor AS T INNER JOIN ClaseTar AS CT ON T.IdClase=CT.IdClase
 WHERE  T.IdClase LIKE ISNULL(@pmIdClase,'%')
 AND  (T.Inactivo=ISNULL(@pmInactivo,0) or T.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY T.IdClase,IdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTablaporTar @pmIdClase VARCHAR(4),@pmTarifa DECIMAL(16,6)
,@pmIdTarifa VARCHAR(4)
 AS 
 SELECT IdTarifa,Descripcion,IdClase,Tarifa
 FROM  Tablapor
 WHERE IdClase=@pmIdClase AND Tarifa=@pmTarifa  AND  IdTarifa<>@pmIdTarifa
 ORDER BY IdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqAforo @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 SELECT IdTanque,UndAforo,Cantidad
 FROM TanqAforo
 WHERE IdTanque=@pmIdTanque AND UndAforo=@pmUndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqAforoFrac @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 SELECT IdTanque,UndAforo,Fraccion,Milimetros
 FROM TanqAforoFrac
 WHERE IdTanque=@pmIdTanque AND UndAforo=@pmUndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqAforoFracLta @pmIdTanque VARCHAR(4)=Null
 AS 
 SELECT A.IdTanque AS NumTanque,UndAforo,Fraccion,Milimetros,T.IdProducto AS CdProd,DescripProd,CapTanq,NivAgua,CapNeta
,SaldoActual,Descripcion,TnqGas,TnqUnido,TnqVirtual
 FROM TanqAforoFrac AS A INNER JOIN Tanques AS T ON A.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE T.Inactivo=0 AND A.IdTanque LIKE ISNULL(@pmIdTanque,'%')
 ORDER BY A.IdTanque,UndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqAforoLmi @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 SELECT IdTanque,UndAforo,Cantidad
 FROM TanqAforo
 WHERE IdTanque=@pmIdTanque AND UndAforo<@pmUndAforo
 ORDER BY UndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqAforoLms @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4)
 AS 
 SELECT IdTanque,UndAforo,Cantidad
 FROM TanqAforo
 WHERE IdTanque=@pmIdTanque AND UndAforo>@pmUndAforo
 ORDER BY UndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqAforoLta @pmIdTanque VARCHAR(4)=Null
 AS 
 SELECT A.IdTanque AS NumTanque,UndAforo,Cantidad,T.IdProducto AS CdProd,DescripProd,CapTanq,NivAgua,CapNeta
,SaldoActual,Descripcion,TnqGas,TnqUnido,TnqVirtual
 FROM TanqAforo AS A INNER JOIN Tanques AS T ON A.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE T.Inactivo=0 AND A.IdTanque LIKE ISNULL(@pmIdTanque,'%')
 ORDER BY A.IdTanque,UndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqDispens @pmIdMngra VARCHAR(10)
 AS 
 SELECT IdMngra,NumDisp,NumPos,NumMngra,IdTanque,Descripcion,LecGalAnt,LecGalUlt,LecPesAnt,LecPesUlt
,LtaPrec,LecGalMax,FechaAdd,FechaUpdate,Inactivo
 FROM TanqDispens
 WHERE IdMngra=@pmIdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqDispensDso @pmIdTanque VARCHAR(4)=Null,@pmIdCiaDef CHAR(2)=Null
 AS 
 SELECT IdMngra,D.IdTanque AS CdTanq,D.Descripcion AS Descript
,IdMngra+' T'+D.IdTanque+' ('+DescripProd+' '+T.IdProducto+')' AS DsMan
 FROM TanqDispens AS D INNER JOIN Tanques AS T ON D.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE  D.Inactivo=0 AND T.Inactivo=0 AND TnqUnido=0 AND D.IdTanque LIKE ISNULL(@pmIdTanque,'%')
 AND  (IdCiaDef LIKE ISNULL(@pmIdCiaDef,'%%')  OR IdCiaDef='00')
 ORDER BY IdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqDispensLta @pmIdTanque  VARCHAR(4)=Null,@pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT IdMngra,NumDisp,NumPos,NumMngra,D.IdTanque AS NumTanq,D.Descripcion AS DescripDisp,LecGalAnt,LecGalUlt,LecPesAnt,LecPesUlt,LtaPrec
,T.IdProducto AS CodProd,DescripProd,T.Descripcion AS DescripTanq,CapTanq,NivAgua,CapNeta,SaldoActual,TnqGas,TnqUnido,TnqVirtual,NColor
,D.FechaAdd AS Fec_Add,D.FechaUpdate AS Fec_Upd,LecGalMax
 FROM TanqDispens AS D INNER JOIN Tanques AS T ON D.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE D.Inactivo=0 AND D.IdTanque LIKE ISNULL(@pmIdTanque,'%') 
AND T.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY D.IdTanque,IdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqDispensMan @pmNumDisp VARCHAR(4),@pmNumPos VARCHAR(4),@pmNumMngra VARCHAR(4)
 AS 
 SELECT IdMngra,NumDisp,NumPos,NumMngra,D.IdTanque AS CdTanque,D.Descripcion AS DescripDisp,LecGalAnt,LecGalUlt
,LecPesAnt,LecPesUlt,LtaPrec,D.Inactivo AS Disp_inactivo,LecGalMax
--tanques
,T.IdProducto AS CdProd,T.Descripcion AS Tan_Descrip,CapTanq,NivAgua,CapNeta,SaldoActual
 ,TnqGas,TnqUnido,TnqVirtual,T.Inactivo AS Tan_inactivo
--productos
,DescripProd,Referencia,TipoRef,IdBodega,IdUnd,Tanques,ExtciaAct,Precio1
,Precio2,Precio3,Precio4,Precio5,P.Inactivo AS Pro_Inactivo
 FROM TanqDispens  AS D INNER JOIN Tanques AS T ON D.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE NumDisp=@pmNumDisp AND NumPos=@pmNumPos AND NumMngra=@pmNumMngra
 ORDER BY IdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqDispensPro @pmIdMngra VARCHAR(10)
 AS 
 SELECT IdMngra,NumDisp,NumPos,NumMngra,D.IdTanque AS CdTanque,D.Descripcion AS DescripDisp,LecGalAnt,LecGalUlt
,LecPesAnt,LecPesUlt,LtaPrec,D.Inactivo AS Disp_inactivo,LecGalMax
--tanques
,T.IdProducto AS CdProd,T.Descripcion AS Tan_Descrip,CapTanq,NivAgua,CapNeta,SaldoActual
 ,TnqGas,TnqUnido,TnqVirtual,T.Inactivo AS Tan_inactivo
--productos
,DescripProd,Referencia,TipoRef,IdBodega,IdUnd,Tanques,ExtciaAct,Precio1
,Precio2,Precio3,Precio4,Precio5,P.Inactivo AS Pro_Inactivo
 FROM TanqDispens  AS D INNER JOIN Tanques AS T ON D.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE IdMngra=@pmIdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqSaldos @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 SELECT IdTanque,IdCia,SaldoActual
 FROM TanqSaldos
 WHERE IdTanque=@pmIdTanque AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqSaldosSal @pmIdCia CHAR(2)=Null
 AS 
 SELECT S.IdTanque AS CdTanque,IdCia,S.SaldoActual AS VrSaldo,T.IdProducto AS CodProducto
,DescripProd,TipoRef,IdBodega
 FROM TanqSaldos AS S INNER JOIN Tanques AS T ON S.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE S.SaldoActual<>0  AND Tanques<>0  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY S.IdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqSaldosTan @pmIdCia CHAR(2)=Null
 AS 
 SELECT IdTanque,SUM(SaldoActual)  AS STOTSAL
 FROM TanqSaldos
 WHERE  IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY IdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqSaldosTot @pmIdTanque VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT sum(SaldoActual) AS SSALDO
 FROM TanqSaldos
 WHERE IdTanque LIKE ISNULL(@pmIdTanque,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanqSaldos_Cr @pmIdProducto VARCHAR(16)=Null,@pmIdTanque VARCHAR(4)=Null
,@pmIdCia CHAR(2)=Null
 AS 
 SELECT T.IdProducto AS CdProducto,DescripProd,S.IdTanque AS CdTanque,Descripcion,S.IdCia AS CdCia,Compania,S.SaldoActual AS SaldoTanque
,CapTanq,NivAgua,CapNeta,T.SaldoActual AS SaldoTotal,TnqGas,TnqUnido,TnqVirtual,NColor,T.Inactivo AS TanInactivo
,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea
,P.IdMarca AS CdMarca,Marca,P.IdUnd AS CdUnid,Unidad,ExtciaMin,ExtciaMax,CostoUlt,CostoPmd,P.Inactivo AS ProInactivo
 FROM TanqSaldos AS S INNER JOIN Tanques AS T ON S.IdTanque=T.IdTanque
 INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto INNER JOIN Companias AS CI ON S.IdCia=CI.IdCia
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS SG ON P.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 LEFT JOIN ProdCostos AS PC ON T.IdProducto=PC.IdProducto AND S.IdCia=PC.IdCia
 WHERE Tanques<>0  AND  T.IdProducto LIKE ISNULL(@pmIdProducto,'%')  AND S.IdTanque LIKE ISNULL(@pmIdTanque,'%')
 AND S.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY DescripProd,S.IdTanque,S.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanques @pmIdTanque VARCHAR(4)
 AS 
 SELECT IdTanque,IdProducto,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual
 ,TnqGas,TnqUnido,TnqVirtual,CdNueTan,NColor,FechaAdd,FechaUpdate,Inactivo,IdCiaDef,CodigoApi
 FROM Tanques
 WHERE IdTanque=@pmIdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanquesDso @pmIdProducto VARCHAR(16)=Null,@pmTnqUnido BIT=Null
,@pmTnqGas BIT=Null,@pmIdCiaDef CHAR(2)=Null
 AS 
 SELECT IdTanque,Descripcion,T.IdProducto AS CdProd,DescripProd
,IdTanque+' '+Descripcion+' ('+DescripProd+' '+T.IdProducto+')' AS DsTan
,IdTanque AS IdBodega,IdTanque+' '+Descripcion+' '+T.IdProducto AS DsBod
 FROM Tanques AS T INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE T.Inactivo=0 AND  T.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND (TnqUnido=ISNULL(@pmTnqUnido,0) or TnqUnido=ISNULL(@pmTnqUnido,1))
 AND (TnqGas=ISNULL(@pmTnqGas,0) or TnqGas=ISNULL(@pmTnqGas,1))
 AND (IdCiaDef LIKE ISNULL(@pmIdCiaDef,'%%')  OR IdCiaDef='00')
 ORDER BY IdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTanquesLta @pmIdProducto VARCHAR(16)=Null
 AS 
 SELECT IdTanque,T.IdProducto AS CdProd,DescripProd,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual
,TnqGas,TnqUnido,TnqVirtual,CdNueTan,NColor,T.FechaAdd AS Fec_Add,T.FechaUpdate AS Fec_Upd,IdCiaDef
 FROM Tanques AS T INNER JOIN ProdMcias AS P ON T.IdProducto=P.IdProducto
 WHERE T.Inactivo=0 AND  T.IdProducto LIKE ISNULL(@pmIdProducto,'%')
 ORDER BY DescripProd,IdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTarifBuses @pmIdTarifa VARCHAR(4)
 AS 
 IF @pmIdTarifa IS NULL
 SELECT IdTarifa,Descripcion,VrNormal,VrFestivo
 FROM TarifBuses
 WHERE Inactivo=0
 ORDER BY Descripcion
 ELSE
 SELECT IdTarifa,Descripcion,VrNormal,VrFestivo,Inactivo
 FROM TarifBuses
 WHERE IdTarifa=@pmIdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTarifasFletes @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmItem INT
 AS 
 SELECT IdOrigen,IdDestino,Item,TipoTarifa,TarifClie,TarifPago,TarifTabla,IdMneda,UndMed
,NitCliente,CdNat,CdTipoVeh,CdRango,Inactivo,FechaCrea,IdUsuario
 FROM TarifasFletes
 WHERE IdOrigen=@pmIdOrigen AND IdDestino=@pmIdDestino AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTarifasFletesLta @pmTipoTarifa VARCHAR(10)=Null,@pmIdOrigen VARCHAR(8)=Null,@pmIdDestino VARCHAR(8)=Null
,@pmNitCliente VARCHAR(16)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdOrigen,O.Localidad AS CiuOrigen,IdDestino,D.Localidad AS CiuDestino,Item,TarifClie,TarifPago,TarifTabla,IdMneda,T.UndMed AS Und_Med,Unidad
,NitCliente,RazonSocial,CdNat,Natlzaprod,CdTipoVeh,TipoVehiculo,CdRango,DescripRango,T.Inactivo AS Inactvo,FechaCrea,T.IdUsuario AS CdUsuario,Usuario
,O.IdDep AS CdDep,DPO.Departamento AS DptoOrigen,D.IdDep AS CdDepDest,DPD.Departamento AS DptoDestino,TipoTarifa
 FROM TarifasFletes AS T INNER JOIN Localidades AS O ON T.IdOrigen=O.IdLocal INNER JOIN Localidades AS D ON T.IdDestino=D.IdLocal
 INNER JOIN Departamentos AS DPO ON O.IdDep=DPO.IdDep INNER JOIN Departamentos AS DPD ON D.IdDep=DPD.IdDep
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS TC ON T.NitCliente=TC.IdTercero LEFT JOIN Sys_Um AS UM ON T.UndMed=UM.UndMed 
 LEFT JOIN TiposNat AS N ON T.CdNat=N.IdNat LEFT JOIN TiposVeh AS TV ON T.CdTipoVeh=TV.IdTipoVeh
 LEFT JOIN RangosPeso AS R ON T.CdRango=R.IdRango
 WHERE TipoTarifa LIKE ISNULL(@pmTipoTarifa,'%') AND  IdOrigen LIKE ISNULL(@pmIdOrigen,'%')
 AND IdDestino LIKE ISNULL(@pmIdDestino,'%') AND NitCliente LIKE ISNULL(@pmNitCliente,'%')
 AND  (T.Inactivo=ISNULL(@pmInactivo,0) or T.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY O.Localidad,D.Localidad,Item
 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryTasasInteres @pmIdTasa VARCHAR(4)
 AS 
 SELECT IdTasa,DescTasa,TipoInteres,TipoPeriodo,CausaInteres,TipoDTF,DTF_EA,Spread_TA,TasaEfectiva,OpcCompra,Inactivo
 FROM TasasInteres
 WHERE IdTasa=@pmIdTasa


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTasasInteresDso 
 AS 
 SELECT IdTasa,DescTasa,IdTasa+' '+DescTasa+' ( '+TipoInteres+'  '+TipoPeriodo
 + ' '+CASE CausaInteres WHEN 1 THEN 'ANTICIPADA' ELSE 'VENCIDA' END +'  DTF '+TipoDTF+')' AS DsTasa 
 FROM TasasInteres
 WHERE Inactivo=0 
 ORDER BY IdTasa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryTasasIntmoraDso 
 AS 
 SELECT IdMora,DescMora,IdMora+' '+DescMora+' ( '+TipoInteres+'  '+STR(TasaEM,7,4)+ ')' AS DsTasa 
 FROM TasasIntmora
 WHERE Inactivo=0 
 ORDER BY IdMora


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTaxRutas @pmIdRuta VARCHAR(4)
 AS 
 SELECT IdRuta,NomRuta,TipoRuta,TarifPago,TarifCobro,Origen,Destino,CodRuta,Inactivo
 FROM TaxRutas
 WHERE IdRuta=@pmIdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTaxRutasDso 
 AS 
 SELECT   IdRuta,NomRuta,IdRuta+' '+NomRuta AS DSRUT
 FROM TaxRutas
 WHERE  Inactivo=0
 ORDER BY IdRuta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTaxTarifClie @pmTipoTarif VARCHAR(10),@pmItem INT
 AS 
 SELECT TipoTarif,Item,IdClie,TipoHora,IdRuta,NumInicial,NumFinal,TarifPago,TarifCobro
 FROM TaxTarifClie
 WHERE TipoTarif=@pmTipoTarif AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTaxTarifas @pmTipoTarif VARCHAR(10),@pmItem INT
 AS 
 SELECT TipoTarif,Item,TipoHora,NumInicial,NumFinal,TarifPago,TarifCobro
 FROM TaxTarifas
 WHERE TipoTarif=@pmTipoTarif AND Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieCia @pmIdClie VARCHAR(16),@pmIdCia CHAR(2)
 AS 
 SELECT IdClie,IdCia
 FROM TercClieCia
 WHERE IdClie=@pmIdClie AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTercClieLineas @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4)
 AS 
 SELECT IdClie,IdAgencia,IdLinea,IdTasa,nPlazo,TipoPlazo,CupoCredito,SaldoCapital,SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,LiqSeguro,FecPreAprob
,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia,ReqCheques,FecActCupo
,CdTasaConv,NitConvenio,CuotasConv,EstadoAct,FechaCrea,FechaAct,IdUsuario
 FROM TercClieLineas
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieLineasLta @pmIdClie VARCHAR(16)=Null,@pmIdLinea VARCHAR(4)=Null,@pmIdTipoTerc  VARCHAR(4)=Null
 AS 
 SELECT LC.IdClie AS NitCliente,RazonSocial,LC.IdAgencia AS CdAgencia,NomAgencia,LC.IdLinea AS CdLinea,LinCredito,LC.IdTasa AS CdTasa,DescTasa,TasaEfectiva,nPlazo,TipoPlazo,CupoCredito
,SaldoCapital,SaldoInteres,MaxCupoSmlv,TipoCausac,DiasVence,FecPreAprob,PlazoAprob,EtdoAprob,TipoAprob,DescAprob,Garantias,TipoGarantia,DescGarantia,VrGarantia
,ReqCheques,FecActCupo,EstadoAct,LiqSeguro,CdTasaConv,NitConvenio,CuotasConv,LC.FechaCrea AS FecCrea,LC.FechaAct AS FecActualiza,LC.IdUsuario AS CdUsuario,Usuario
--datos del cliente
,TipoId,Dv,C.IdTipoTerc AS CdTipoTerc,TipoTercero,C.IdEstado AS CdEstado,Estado,FecIngreso,VrSalBasico,MaxDcto,Comentarios
 FROM TercClieLineas AS LC  INNER JOIN LineasCred AS L ON LC.IdLinea=L.IdLinea
 INNER JOIN  TercCliePrestamo AS C ON LC.IdClie=C.IdClie AND LC.IdAgencia=C.IdAgencia  INNER JOIN Terceros AS T ON LC.IdClie=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON LC.IdUsuario=U.IdUsuario
 INNER JOIN TiposTerc AS TC ON C.IdTipoTerc=TC.IdTipoTerc  INNER JOIN EstadoTer AS EC ON C.IdEstado=EC.IdEstado
 LEFT JOIN TasasInteres AS TI ON LC.IdTasa=TI.IdTasa
 WHERE C.Inactivo=0 AND LC.IdClie LIKE ISNULL(@pmIdClie,'%') AND LC.IdLinea LIKE ISNULL(@pmIdLinea,'%')
 AND  C.IdTipoTerc  LIKE ISNULL(@pmIdTipoTerc ,'%')
 ORDER BY RazonSocial,LC.IdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16)
 AS 
 SELECT IdClie,IdAgencia,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,NomAgencia,DirAgencia,IdLocalAge,IdSzona,IdGrupo,IdTipoTerc,IdVend,IdClase,NumCuenta,IdBanco
,EsExento,CalcIntMora,DiasGracia,FecIngreso,FecVigencia,FecRetiro,VrSalBasico,MaxDcto,TipoSalario,MatMerc,FecMat,Comentarios,IdEstado,Inactivo,FechaCrea,FechaAct,IdUsuario
 FROM TercCliePrestamo
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCliePrestamoLta  @pmIdGrupo VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdSzona VARCHAR(4)=Null
,@pmIdTipoTerc  VARCHAR(4)=Null,@pmFecIngIni SMALLDATETIME=Null,@pmFecIngFin SMALLDATETIME=Null,@pmIdEstado VARCHAR(4)=Null
 AS 
 SELECT IdClie,T.RazonSocial AS Razon_Social,T.TipoId AS TipId,T.Dv AS DvNit,T.NomCial AS NombreCmcial,T.SiglaRaz AS SiglaRazSocial
,IdAgencia,NomAgencia,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,T.Direccion AS TercDireccion,T.IdLocal AS CdCiudad,L.Localidad AS Ciudad,L.IdDep AS CdDpto
,D.Departamento AS Dpto,T.Telefono AS Tlefono,T.Fax AS NumFax,T.TelMovil AS TelCeluar,T.SitioWeb AS WebSite,T.e_mail AS Email
,DirAgencia,IdLocalAge,LE.Localidad AS CiuAgencia,DE.Departamento AS DptoAgencia,C.IdTipoTerc AS CdTipoTerc,TipoTercero
,SZ.IdZona AS CdZona,Zona,C.IdSzona AS CdSubzona,Subzona,C.IdGrupo AS CdGrupo,GrupoClie,T.IdSector AS CdSector,SectorEco,T.IdProf AS CdProf,Profesion,T.IdRegimen AS CdRgmen,Regimen
,T.TipEnte AS TipoEnte,IdVend,TV.RazonSocial AS NomVendedor,C.IdClase AS CdClase,ClaseCuenta,NumCuenta,C.IdBanco AS CdBanco,Banco
,EsExento,CalcIntMora,DiasGracia,FecIngreso,FecVigencia,FecRetiro,VrSalBasico,TipoSalario,MaxDcto,MatMerc,FecMat,Comentarios,C.IdEstado AS CdEstado,EC.Estado AS EstadoClie,C.Inactivo AS ClieInactivo,FechaCrea,FechaAct,C.IdUsuario AS CdUsuario,Usuario
,T.IniStgNom AS PosNomb,T.IdLugarCed AS IdLugCed,LC.Localidad AS LugarCed,T.FecExpCed AS FecExpCedula,T.Inactivo AS TercInactivo,T.Codigo AS TercCodigo
 FROM TercCliePrestamo AS C  INNER JOIN Terceros AS T ON C.IdClie=T.IdTercero  INNER JOIN Terceros AS TV ON C.IdVend=TV.IdTercero
 INNER JOIN GruposCli AS G ON C.IdGrupo=G.IdGrupo INNER JOIN TiposTerc AS TC ON C.IdTipoTerc=TC.IdTipoTerc
 INNER JOIN Subzonas AS SZ ON C.IdSzona=SZ.IdSzona  INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 INNER JOIN EstadoTer AS EC ON C.IdEstado=EC.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LE ON C.IdLocalAge=LE.IdLocal INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN  SectoresEco AS S ON T.IdSector=S.IdSector  INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen
 LEFT JOIN ClaseCta AS CTA ON  C.IdClase=CTA.IdClase LEFT JOIN Bancos AS B ON C.IdBanco=B.IdBanco 
 LEFT JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal  LEFT JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 WHERE C.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND  C.IdSzona LIKE ISNULL(@pmIdSzona,'%')  AND  C.IdTipoTerc  LIKE ISNULL(@pmIdTipoTerc ,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (FecIngreso>=ISNULL(@pmFecIngIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieRef @pmIdClie VARCHAR(16),@pmItem INT
 AS 
 SELECT IdClie,Item,TipoRef,Referencia,NitRefncia,DirRefncia,TelRefncia,emlRefncia,CargRefncia,NumCuenta,Observcion
 FROM TercClieRef
 WHERE IdClie=@pmIdClie
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieRefItm @pmIdClie VARCHAR(16)
 AS 
 SELECT MAX(Item) AS MXNUM 
 FROM TercClieRef
 WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieRefLta @pmIdClie VARCHAR(16)=Null
 AS 
 SELECT IdClie,RazonSocial,Item,TipoRef,Referencia,NitRefncia,DirRefncia,TelRefncia,emlRefncia,CargRefncia,NumCuenta,Observcion
 FROM TercClieRef AS TR INNER JOIN Terceros AS T ON TR.IdClie=T.IdTercero
 WHERE IdClie LIKE ISNULL(@pmIdClie,'%')
 ORDER BY RazonSocial,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieRef_Cr @pmIdClie VARCHAR(16)=Null
 AS 
 SELECT IdClie,Item,TipoRef,Referencia,NitRefncia,DirRefncia,TelRefncia,emlRefncia,CargRefncia,NumCuenta,Observcion
 FROM TercClieRef
 WHERE IdClie LIKE ISNULL(@pmIdClie,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
,@pmIdCia CHAR(2)
 AS 
 SELECT IdClie,CdAgencia,IdCia,VrSaldo
 FROM TercClieSaldos
 WHERE IdClie=@pmIdClie AND CdAgencia=@pmCdAgencia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieSaldosSum @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT SUM(VrSaldo) AS STotSal
 FROM TercClieSaldos
 WHERE IdClie=@pmIdClie AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieSaldos_Cr @pmIdClie VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmCdAgencia VARCHAR(16)=Null
AS 
 SELECT S.IdClie AS NitCliente,RazonSocial,CdAgencia,Agencia,S.IdCia AS CdCia,Compania,S.VrSaldo AS SaldoCia
--datos del cliente
,TipoId,Dv,NomCial,SiglaRaz,VrCupo,C.VrSaldo AS  ClieSaldo
,C.IdGrupo AS CdGrupo,GrupoClie,SZ.IdZona AS CdZona,Z.Zona AS ClieZona,C.IdSzona AS CdSubzona,SZ.Subzona AS ClieSubZona
,C.IdEstado AS ClieIdEstado,Estado,C.Inactivo AS ClieInactivo
--datos de Agencia
,VrCupoCre,VrSaldoAct,CodAgencia,Referencia,A.IdEstado AS Age_Estado,A.Inactivo AS Age_inactivo
 FROM TercClieSaldos AS S INNER JOIN  Terceros AS T ON S.IdClie=T.IdTercero
 INNER JOIN TercCliente AS C ON S.IdClie=C.IdClie INNER JOIN Companias AS CI ON S.IdCia=CI.IdCia
 INNER JOIN GruposCli AS G ON C.IdGrupo=G.IdGrupo INNER JOIN Subzonas AS SZ ON C.IdSzona=SZ.IdSzona
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona   INNER JOIN EstadoTer AS E ON C.IdEstado=E.IdEstado
 LEFT JOIN Agencias AS A ON S.IdClie=A.IdClie AND S.CdAgencia=A.IdAgencia
 WHERE S.IdClie LIKE ISNULL(@pmIdClie,'%') AND CdAgencia LIKE ISNULL(@pmCdAgencia,'%')
 AND S.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY RazonSocial,S.IdCia,CdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieVeh @pmIdClie VARCHAR(16),@pmpVehiculo VARCHAR(10)
 AS 
 SELECT IdClie,pVehiculo,CdConductor,NumVehiculo,Descripcion
 FROM TercClieVeh
 WHERE IdClie=@pmIdClie AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
 ORDER BY pVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieVehLta @pmIdClie VARCHAR(16)=Null
 AS 
 SELECT IdClie,T.RazonSocial AS NomCliente,pVehiculo,NumVehiculo,Descripcion,CdConductor,C.RazonSocial AS Conductor
 FROM TercClieVeh AS V INNER JOIN Terceros AS T ON V.IdClie=T.IdTercero
 LEFT JOIN  Terceros AS C ON V.CdConductor=C.IdTercero
 WHERE T.Inactivo=0 AND  IdClie LIKE ISNULL(@pmIdClie,'%')
 ORDER BY T.RazonSocial,pVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClieVehPla @pmpVehiculo VARCHAR(10)
 AS 
 SELECT IdClie,pVehiculo,CdConductor,NumVehiculo,Descripcion
 FROM TercClieVeh
 WHERE pVehiculo=@pmpVehiculo
 ORDER BY IdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCliente @pmIdClie VARCHAR(16)
 AS 
 SELECT IdClie,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirEnv,IdLocEnv,DiasEntga
,IdSzona,IdGrupo,IdPlazo,CdPlazoComb,IdForma,IdEstrato,IdVend,NitFact,IdRuta,IdClase,NumCuenta,IdBanco,CdMney,CdDct,CdRet,CdRiv
,CdCms,PlazosImp,ExcIva,TrfIntMora,DiasGracia,LiqFletes,FactSold,Autoret,IncRet,IncRiv,IncIca,FactTipo,VrCupo,VrSaldo,UidClie,PwdClie
,FecIngreso,FecVigencia,FecRetiro,MatMerc,FecMat,PathFoto,PathFirma,Cmntario1,Cmntario2,CdBandera,CodClieSicom,Contrato,NContrato,CiaContMay,Cmntario3,PrendGarant,FecUpCupo
,TipoCliente,CupoGalones,Restric_Cia,FecPlazoDoc,EdoRadicaDoc,CdTipBloq,DescEdoDoc,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM TercCliente
 WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercClienteLta @pmIdLocal VARCHAR(8)=Null,@pmIdSector VARCHAR(8)=Null,@pmIdRegimen VARCHAR(4)=Null,@pmTipoId CHAR(1)=Null
,@pmIdProf VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLocEnv VARCHAR(8)=Null,@pmIdForma VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null,@pmIdZona VARCHAR(4)=Null
,@pmIdSzona VARCHAR(4)=Null,@pmIdPlazo VARCHAR(4)=Null,@pmIdEstrato VARCHAR(4)=Null,@pmIdVend VARCHAR(16)=Null,@pmIdRuta VARCHAR(4)=Null
,@pmFecIngIni SMALLDATETIME=Null,@pmFecIngFin SMALLDATETIME=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdEstadoTer VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmInactivoTer BIT=Null
 AS 
 SELECT IdClie,T.RazonSocial AS RaznSocial,T.TipoId AS TipId,T.Dv AS DvNit,T.NomCial AS NombreCmcial,T.SiglaRaz AS SiglaRazSocial
,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,T.Direccion AS Dirccion,T.IdLocal AS CdCiudad,L.Localidad AS Ciudad,L.IdDep AS CdDpto
,D.Departamento AS Dpto,T.Telefono AS Tlefono,T.Fax AS NumFax,T.TelMovil AS TelCeluar,T.SitioWeb AS WebSite,T.e_mail AS Email,DirEnv,IdLocEnv,LE.Localidad AS CiuEnvio
,DE.Departamento AS Dptoenvio,DiasEntga,C.IdGrupo AS CdGrupo,GrupoClie,C.IdEstrato AS CdEstrato,Estrato,SZ.IdZona AS CdZona,Zona,C.IdSzona AS CdSubzona,Subzona
,C.IdRuta AS CdRuta,Ruta,C.IdPlazo AS CdPlazo,Plazo,NVmto,PlazosImp,CdPlazoComb,C.IdForma AS CdFrma,FormaPago,T.IdSector AS CdSector,SectorEco,T.IdProf AS CdProf,Profesion,T.IdRegimen AS CdRgmen
,Regimen,T.TipEnte AS TipoEnte,IdVend,TV.RazonSocial AS NomVendedor,NitFact,C.IdClase AS CdClase,ClaseCuenta,NumCuenta,C.IdBanco AS CdBanco,Banco,B.Direccion AS Dirbanco
,CdMney,CdDct,CdRet,CdRiv,CdCms,ExcIva,TrfIntMora,DiasGracia,LiqFletes,FactSold,Autoret,IncRet,IncRiv,IncIca,FactTipo,VrCupo,VrSaldo,UidClie,Contrato,NContrato,CiaContMay,FecIngreso,FecVigencia
,FecRetiro,MatMerc,FecMat,PathFoto,PathFirma,T.Observacion AS Observ,Cmntario1,Cmntario2,Cmntario3,CdBandera,PrendGarant,FecUpCupo,TipoCliente,CodClieSicom,C.IdEstado AS CdEst_clie,EC.Estado AS EstadoClie,C.Inactivo AS Clie_inactivo
,T.IdEstado AS CdEst_terc,ET.Estado AS Estad_terc,T.Inactivo AS Terc_inactivo,T.Codigo AS CodTerc,T.IdLugarCed AS IdLugCed,LC.Localidad AS LugarCed,T.FecExpCed AS FecExpCedula
,T.EsVendedor AS EsVend,T.EsConductor AS EsCond,T.EsPropietario AS EsProp,T.EsProveedor AS EsProvee,T.EsEmpleado AS EsEmpldo,T.EsOperario AS EsOper,T.EsAccnista AS EsAcc
,T.EsCiaAseg AS EsAseg,CdTipBloq,C.FechaAdd AS FecAdd,C.FechaUpdate AS FecUpd,C.IdUsuario AS IdUsuari,Usuario,T.IniStgNom AS PosNomb,Restric_Cia,CupoGalones,FecPlazoDoc,EdoRadicaDoc,DescEdoDoc
 FROM TercCliente  AS C INNER JOIN Terceros AS T ON C.IdClie=T.IdTercero  INNER JOIN Terceros AS TV ON C.IdVend=TV.IdTercero
 INNER JOIN GruposCli AS G ON C.IdGrupo=G.IdGrupo INNER JOIN Estratos AS ETC ON C.IdEstrato=ETC.IdEstrato
 INNER JOIN Subzonas AS SZ ON C.IdSzona=SZ.IdSzona  INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona 
 INNER JOIN Plazos AS PZ ON C.IdPlazo=PZ.IdPlazo INNER JOIN Rutas AS R ON C.IdRuta=R.IdRuta
 INNER JOIN Formaspago AS F ON C.IdForma=F.IdForma
 INNER JOIN ClaseCta AS CTA ON  C.IdClase=CTA.IdClase
 INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco INNER JOIN EstadoTer AS EC ON C.IdEstado=EC.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario   INNER JOIN Localidades AS LE ON C.IdLocEnv=LE.IdLocal
 INNER JOIN Departamentos AS DE ON LE.IdDep=DE.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN  SectoresEco AS S ON T.IdSector=S.IdSector  INNER JOIN RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen
 INNER JOIN EstadoTer AS ET ON T.IdEstado=ET.IdEstado INNER JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal 
 INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 WHERE T.IdLocal LIKE ISNULL(@pmIdLocal,'%') AND T.IdSector LIKE ISNULL(@pmIdSector,'%')  AND T.IdRegimen LIKE ISNULL(@pmIdRegimen,'%')
 AND T.TipoId LIKE ISNULL(@pmTipoId,'%') AND T.IdProf LIKE ISNULL(@pmIdProf,'%') AND C.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND IdLocEnv LIKE ISNULL(@pmIdLocEnv,'%') AND C.IdForma LIKE ISNULL(@pmIdForma,'%') AND C.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND SZ.IdZona LIKE ISNULL(@pmIdZona,'%') AND C.IdSzona LIKE ISNULL(@pmIdSzona,'%')
 AND C.IdPlazo LIKE ISNULL(@pmIdPlazo,'%') AND C.IdEstrato LIKE ISNULL(@pmIdEstrato,'%')
 AND IdVend LIKE ISNULL(@pmIdVend,'%') AND C.IdRuta LIKE ISNULL(@pmIdRuta,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND T.IdEstado LIKE ISNULL(@pmIdEstadoTer,'%')
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (T.Inactivo=ISNULL(@pmInactivoTer,0) or T.Inactivo=ISNULL(@pmInactivoTer,1))
 AND (FecIngreso>=ISNULL(@pmFecIngIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryTercCndContrato @pmNumContrato INT
 AS 
 SELECT NumContrato,IdConductor,IdTipCon,IdModd,FecIngreso,FecRetiro,FecVigencia,Observaciones,FecUpdate,IdUsuario
 FROM TercCndContrato
 WHERE NumContrato=@pmNumContrato


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryTercCndContrato_Cr @pmIdConductor VARCHAR(16),@pmIdContrato VARCHAR(4),@pmIdModalidad VARCHAR(4),@pmNumContrato INT

AS
 SELECT  TC.NumContrato AS NContrato,TC.IdConductor AS IdConductr,RazonSocial,Direccion,Telefono,TelMovil,Fax,L.Localidad As NomCiudad,T.IdLocal AS CodCiu,IdLugarCed,LEC.Localidad AS LugarCedula,Licencia, LugarLic,CatLicencia,VigLicencia,Sexo,FecNacmto,TipoSangre,FactorRh
,LibMilitar,T.IdProf AS Idprofesion,Profesion,C.IdEstado AS IdEstad,Estado,pVehiculo,TC.IdTipCon AS IdContrat,TipoContrato, TC.IdModd AS IdModaldad,Modalidad, TC.FecIngreso AS FechaIngreso,TC.FecVigencia AS FechaVigencia, TC.FecRetiro AS FechaRetiro
, Observaciones, FecUpdate, TC.IdUsuario AS IdUsuari
 FROM  TercCndContrato AS TC  INNER JOIN Terceros AS T ON TC.IdConductor=T.IdTercero
  INNER JOIN TercCndtores AS C ON TC.IdConductor=C.IdConductor  INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
  INNER JOIN Profesiones AS P ON T.IdProf=P.IdProf    INNER JOIN EstadoTer AS E ON C.IdEstado=E.IdEstado
  INNER JOIN TiposCon AS TN ON TC.IdTipCon=TN.IdTipCon  INNER JOIN TiposMod AS TM ON TC.IdModd=TM.IdModd
 LEFT JOIN   Localidades AS LEC ON T.IdLugarCed=LEC.IdLocal LEFT JOIN ExpLicencias AS LLC ON C.IdLugar=LLC.IdLugar
 WHERE TC.IdConductor LIKE ISNULL(@pmIdConductor,'%') AND TC.IdTipCon LIKE ISNULL(@pmIdContrato,'%')
 AND TC.IdModd LIKE ISNULL(@pmIdModalidad,'%')  
 AND (TC.NumContrato>=ISNULL(@pmNumContrato,0)  AND TC.NumContrato<=ISNULL(@pmNumContrato,2147483647))
 ORDER BY TC.NumContrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTercCndReferencias @pmIdConductor VARCHAR(16),@pmItem INT
 AS 
 SELECT Item,TipoRef,EmpresaRef,NitRef,DireccionRef,TelefonoRef,ContactoRef,EmailRef,CargoRef
,CuentaBanco,FechaInicial,FechaFinal,Verificado,ObservRef,IdConductor
 FROM TercCndReferencias
 WHERE IdConductor=@pmIdConductor 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCndTarjetas @pmIdConductor VARCHAR(16),@pmItem INT
 AS 
 SELECT IdConductor,Item,NumeroTarj,Secuencia,Fecha,FecVence
 FROM TercCndTarjetas
 WHERE IdConductor=@pmIdConductor 
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCndTarjetasItm @pmIdConductor VARCHAR(16)
 AS 
 SELECT MAX(Item) AS MXITM
 FROM TercCndTarjetas
 WHERE IdConductor=@pmIdConductor 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCndTarjetasLta @pmIdConductor VARCHAR(16)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT IdConductor,RazonSocial,Item,NumeroTarj,Secuencia,Fecha,FecVence
 FROM TercCndTarjetas AS TJ INNER JOIN Terceros AS T ON TJ.IdConductor=T.IdTercero
 WHERE IdConductor LIKE ISNULL(@pmIdConductor ,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY RazonSocial,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCndtores @pmIdConductor VARCHAR(16)
 AS 
 SELECT IdConductor,IdGrupo,IdModd,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,IdEstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial
,FecJudicial,VigJudicial,Pasporte,VigPaspte,Licencia,IdLugar,CatLicencia,FecLicencia,VigLicencia,IdClase,NumCuenta,IdBanco,CdLocCue,CdFonPen,CdFonEps
,CdFonArp,pVehiculo,PathFoto,PathFirma,PathLic,Cmntarios,IdEstado,Inactivo,NumTarjeta,EdicTarjeta,FecTarjeta,VigTarjeta,PasSeguridad,FecPasSeg,VigPasSeg,NitEmpTrans
,FechaAdd,FechaUpdate,IdUsuario,Num_Contrato,Contrato_Activo,CdTipCon,Fec_Ingreso,Fec_Retiro,Fec_Vigencia,Fec_IngresoAnt,Fec_UltimoAcc,PasadoJudicial,Restriccion
,FecPagoPen,FecPagoEps,FecPagoArp
 FROM TercCndtores
 WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCndtoresLic @pmLicencia VARCHAR(25)
 AS 
 SELECT TercCndtores.*
 FROM TercCndtores
 WHERE Licencia=@pmLicencia
 ORDER BY IdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercCndtoresLta @pmIdLocal VARCHAR(8)=Null,@pmIdProf VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdBanco VARCHAR(4)=Null,@pmIdLugar VARCHAR(8)=Null,@pmCatLicencia INT=Null,@pmIdModd VARCHAR(4)=Null,@pmEsEmpleado BIT=Null
,@pmEsPropietario BIT=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdEstadoTer VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmInactivoTer BIT=Null
 AS 
 SELECT IdConductor,RazonSocial,T.Codigo AS CodCondtor,TipoId,Dv,FecExpCed,IdLugarCed,LC.Localidad AS LugarCedula,T.Direccion AS Dirccion,T.IdLocal AS CodCiudad
,L.Localidad AS Ciudad,L.IdDep AS CodDpto,Departamento,Telefono,Fax,TelMovil,e_mail,T.IdProf AS CdProf,Profesion,C.IdGrupo AS CodGrupo,GrupoClie,C.IdModd AS CdModd,Modalidad,TipoSangre,FactorRh
,FecNacmto,LugarNacmto,Sexo,IdEstCivil,EstCivil,NHijos,LibMilitar,ClaseLib,FechaLib,DistMil,CertJudicial,FecJudicial,VigJudicial,Licencia,C.IdLugar AS CodLugarLic,LugarLic,CatLicencia,FecLicencia,VigLicencia,C.IdClase AS CdClaseCta
,Pasporte,VigPaspte,ClaseCuenta,NumCuenta,C.IdBanco AS CodBanco,Banco,CdLocCue,LCT.Localidad AS LugarCuenta,CdFonPen,FP.Fondo AS FondoPen,CdFonEps,FS.Fondo AS Eps,CdFonArp
,FR.Fondo AS FondoRiesgo,pVehiculo,PathFoto,PathFirma,PathLic,Observacion,Cmntarios,EsCliente,EsVendedor,EsPropietario,EsEmpleado,EsOperario,IniStgNom
,C.IdEstado AS CdEstado,EC.Estado AS EstadoCond,C.Inactivo AS Inactiv,T.IdEstado AS IdEstdTerc,ET.Estado AS EstadoTerc,T.Inactivo AS Ter_Inactivo
,NumTarjeta,EdicTarjeta,FecTarjeta,VigTarjeta,PasSeguridad,FecPasSeg,VigPasSeg,NitEmpTrans,C.IdUsuario AS IdUsuari,Usuario,EC.NColor AS EstdNColor,C.FechaAdd AS Fec_Add,C.FechaUpdate AS Fec_Upd
,Num_Contrato,Contrato_Activo,CdTipCon,Fec_Ingreso,Fec_Retiro,Fec_Vigencia,Fec_IngresoAnt,Fec_UltimoAcc,PasadoJudicial,Restriccion,FecPagoPen,FecPagoEps,FecPagoArp
 FROM TercCndtores AS C INNER JOIN Terceros AS T ON C.IdConductor=T.IdTercero
 INNER JOIN GruposCli AS G ON C.IdGrupo=G.IdGrupo INNER JOIN ExpLicencias AS LL ON C.IdLugar=LL.IdLugar
 INNER JOIN ClaseCta AS CTA ON  C.IdClase=CTA.IdClase  INNER JOIN Bancos AS B ON C.IdBanco=B.IdBanco
 INNER JOIN EstadoTer AS EC ON C.IdEstado=EC.IdEstado INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 INNER JOIN TiposMod AS M ON C.IdModd=M.IdModd INNER JOIN EstadoCiv AS ECV ON C.IdEstCivil=ECV.IdEstado
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf  INNER JOIN EstadoTer AS ET ON T.IdEstado=ET.IdEstado
 INNER JOIN  Localidades AS LC ON T.IdLugarCed=LC.IdLocal 
 LEFT JOIN  Localidades AS LCT ON  C.CdLocCue=LCT.IdLocal 
 LEFT JOIN Fondos AS FP ON C.CdFonPen=FP.IdFondo LEFT JOIN Fondos AS FS ON C.CdFonEps=FS.IdFondo
 LEFT JOIN Fondos AS FR ON C.CdFonArp=FR.IdFondo
 WHERE T.IdLocal LIKE ISNULL(@pmIdLocal,'%')  AND T.IdProf LIKE ISNULL(@pmIdProf,'%')
 AND C.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')   AND C.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND C.IdLugar LIKE ISNULL(@pmIdLugar,'%') AND C.IdModd LIKE ISNULL(@pmIdModd,'%')
 AND C.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND T.IdEstado LIKE ISNULL(@pmIdEstadoTer,'%')
 AND (CatLicencia>=ISNULL(@pmCatLicencia,0)  AND CatLicencia<=ISNULL(@pmCatLicencia,2147483647))
 AND (C.Inactivo=ISNULL(@pmInactivo,0) or C.Inactivo=ISNULL(@pmInactivo,1))
 AND (T.Inactivo=ISNULL(@pmInactivoTer,0) or T.Inactivo=ISNULL(@pmInactivoTer,1))
 AND (EsEmpleado=ISNULL(@pmEsEmpleado,0) or EsEmpleado=ISNULL(@pmEsEmpleado,1))
 AND (EsPropietario=ISNULL(@pmEsPropietario,0) or EsPropietario=ISNULL(@pmEsPropietario,1))
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryTercPropietario @pmIdPropietario VARCHAR(16)
 AS 
 SELECT IdPropietario,IdGrupo,CupoCredito,SaldoActual,Avala,IdAdmon,IdTipCon,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,Observacion,IdEstado,FechaAdd,FechaUpdate,IdUsuario
 FROM TercPropietario
 WHERE IdPropietario=@pmIdPropietario


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryTercPropietarioLta 
 AS 
 SELECT IdPropietario,RazonSocial,P.IdGrupo AS CdGrupo,GrupoProp,CupoCredito,SaldoActual,Avala,P.IdAdmon AS CdAdmon,TipoAdmon,P.IdTipCon AS CdTipCon,TipoContrato
,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,P.Observacion AS Observ,P.IdEstado AS CdEstado,P.FechaAdd AS Fec_Add,P.FechaUpdate AS Fec_Update,P.IdUsuario  AS CdUsuario
 FROM TercPropietario AS P INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero
 INNER JOIN GruposPro AS G ON P.IdGrupo=G.IdGrupo INNER JOIN TiposAdm AS TA ON P.IdAdmon=TA.IdAdmon
 INNER JOIN TiposCon AS TC ON P.IdTipCon=TC.IdTipCon
 ORDER BY RazonSocial


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryTercPropietarioReg @pmIdPropietario VARCHAR(16)
 AS 
 SELECT IdPropietario,RazonSocial,P.IdGrupo AS CdGrupo,GrupoProp,CupoCredito,SaldoActual,Avala,P.IdAdmon AS CdAdmon,TipoAdmon,P.IdTipCon AS CdTipCon,TipoContrato
,ContratoActivo,FecIngreso,FecRetiro,FecVigencia,P.Observacion AS Observ,P.IdEstado AS CdEstado,P.FechaAdd AS Fec_Add,P.FechaUpdate AS Fec_Update,P.IdUsuario  AS CdUsuario
,Direccion,Localidad,Telefono,T.IdLocal AS CdLocal,Estado,NColor
 FROM TercPropietario AS P INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero
 INNER JOIN GruposPro AS G ON P.IdGrupo=G.IdGrupo INNER JOIN TiposAdm AS TA ON P.IdAdmon=TA.IdAdmon
 INNER JOIN TiposCon AS TC ON P.IdTipCon=TC.IdTipCon
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN EstadoTer AS ET  ON T.IdEstado=ET.IdEstado
 WHERE IdPropietario=@pmIdPropietario

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryTercPropietario_Cr  @pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null,@pmIdContrato VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdProf VARCHAR(4)=Null
,@pmContratoActivo BIT=Null,@pmFecIngreso SMALLDATETIME=Null,@pmIdSector VARCHAR(4)=Null,@pmEsConductor BIT=Null,@pmEsProveedor BIT=Null, @pmIdRegimen VARCHAR(4)=Null
AS
 SELECT  IdPropietario,RazonSocial,TipoId,Dv,Codigo,T.Direccion AS Direcion,Telefono,Fax,TelMovil,e_mail,Localidad,T.IdLocal AS CodCiu, P.IdGrupo AS IdGrup,GrupoProp, CupoCredito, SaldoActual, Avala, T.IdProf AS IdProfesion,Profesion
, P.IdAdmon AS IdAdmn,TipoAdmon ,P.IdTipCon AS CodContrato,TipoContrato, ContratoActivo, FecIngreso,FecVigencia, FecRetiro ,P.Observacion AS Observ,P.IdEstado AS IdEstad,Estado,P.FechaAdd AS FecAdd,P.FechaUpdate AS FecUpdate, P.IdUsuario AS IdUsuari
,Usuario,T.IdSector AS IdSectr,SectorEco,T.IdRegimen AS IdRegmn,Regimen,TipEnte,EsConductor,EsProveedor,SitioWeb
 FROM  TercPropietario AS P INNER JOIN Terceros AS T ON P.IdPropietario=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal   INNER JOIN GruposPro AS G ON P.IdGrupo=G.IdGrupo
 INNER JOIN Profesiones AS F ON T.IdProf=F.IdProf   INNER JOIN TiposAdm AS A ON P.IdAdmon=A.IdAdmon
 INNER JOIN TiposCon AS CO ON P.IdTipCon=CO.IdTipCon  INNER JOIN EstadoTer AS E ON P.IdEstado=E.IdEstado
 INNER JOIN SectoresEco AS S ON T.IdSector=S.IdSector  INNER JOIN RegimenDian AS R ON T.IdRegimen=R.IdRegimen
 INNER JOIN adm_Usuarios AS U ON P.IdUsuario=U.IdUsuario
 WHERE P.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND P.IdAdmon LIKE ISNULL(@pmIdAdmon,'%') AND T.IdProf LIKE ISNULL(@pmIdProf,'%')
 AND P.IdTipCon LIKE ISNULL(@pmIdContrato,'%') AND P.IdEstado LIKE ISNULL(@pmIdEstado,'%') 
 AND T.IdSector LIKE ISNULL(@pmIdSector,'%') 
 AND T.IdRegimen LIKE ISNULL(@pmIdRegimen,'%')
 AND (ContratoActivo=ISNULL(@pmContratoActivo,0) or ContratoActivo=ISNULL(@pmContratoActivo,1))
 AND (EsConductor=ISNULL(@pmEsConductor,0) or EsConductor=ISNULL(@pmEsConductor,1))
 AND (EsProveedor=ISNULL(@pmEsProveedor,0) or EsProveedor=ISNULL(@pmEsProveedor,1))
AND (FecIngreso>=ISNULL(@pmFecIngreso,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngreso,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY  RazonSocial

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercProvee @pmIdProv VARCHAR(16)
 AS 
 SELECT IdProv,IdGrupo,NitRepLeg,NomRepLeg,NitContac,NomContac,TelContac,emlContac,CargContac,DirOrdComp,IdLocOrd,DiasEntga,IdPlazo
,IdForma,CdMney,CdDct,IdClase,NumCuenta,IdBanco,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv,GartProd,FecIngreso,PathFoto,PathFirma
,Contrato,NContrato,FecVigencia,FecRetiro,ObsFinanc,ObsJuridica,Cmntarios,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM TercProvee
 WHERE IdProv=@pmIdProv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercProveeLta  @pmIdLocal VARCHAR(8)=Null,@pmIdSector VARCHAR(8)=Null,@pmIdRegimen VARCHAR(4)=Null,@pmTipoId CHAR(1)=Null
,@pmIdProf VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null,@pmIdLocOrd VARCHAR(8)=Null,@pmIdForma VARCHAR(4)=Null,@pmIdBanco VARCHAR(4)=Null
,@pmFecIngIni SMALLDATETIME=Null,@pmFecIngFin SMALLDATETIME=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdEstadoTer VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmInactivoTer BIT=Null
 AS 
 SELECT IdProv,Dv,TipoId,RazonSocial,T.Codigo AS Codtercero,NomCial,SiglaRaz,NitRepLeg,NomRepLeg,T.Direccion AS Dreccion,T.IdLocal AS CodCiud,L.Localidad AS Ciudad,D.Departamento AS Departmto,Telefono,Fax,TelMovil
,DirOrdComp,IdLocOrd,LP.Localidad AS CiudadOrd,DP.Departamento AS DeptoOrd,SitioWeb,e_mail,TipEnte,T.IdSector AS CodSector,SectorEco,T.IdProf AS CodProf,Profesion,T.IdRegimen AS IdRegmen,Regimen,PV.IdGrupo AS CdGrupo,GrupoClie
,NitContac,NomContac,TelContac,emlContac,CargContac,DiasEntga,PV.IdPlazo AS CdPlazo,Plazo,PV.IdForma AS CdForma,FormaPago,PV.IdClase AS CdClase,ClaseCuenta,NumCuenta,PV.IdBanco AS CdBanco,Banco
,B.Direccion AS DirBanco,Contacto,CdMney,CdDct,Tarifa,Simbolo,Autoret,IncRet,IncRiv,IncIca,LiqFletes,CdRet,CdRiv,GartProd,IdLugarCed,LC.Localidad AS LugarCedula,DC.Departamento AS DptoLugcedula,FecExpCed
,Observacion,Cmntarios,ObsFinanc,ObsJuridica,PathFoto,PathFirma,T.IdEstado AS IdEstTerc,ET.Estado AS EstadTerc,T.Inactivo AS Ter_Inactvo,PV.IdEstado AS IdEstProv,EP.Estado AS EstadProv,PV.Inactivo AS Prv_Inactvo
,Contrato,NContrato,FecIngreso,FecVigencia,FecRetiro,EsCliente,EsProveedor,EsPropietario,EsAccnista,EsCiaAseg,PV.FechaAdd AS Fec_Add,PV.FechaUpdate AS Fec_Upd,PV.IdUsuario AS IdUsario,Usuario,IniStgNom,EP.NColor AS ClrEstado
 FROM TercProvee AS PV INNER JOIN Terceros AS T ON PV.IdProv=T.IdTercero
 INNER JOIN GruposCli AS G ON PV.IdGrupo=G.IdGrupo INNER JOIN Plazos AS PZ ON PV.IdPlazo=PZ.IdPlazo
 INNER JOIN Formaspago AS F ON PV.IdForma=F.IdForma INNER JOIN ClaseCta AS CTA ON  PV.IdClase=CTA.IdClase
 INNER JOIN Bancos AS B ON PV.IdBanco=B.IdBanco INNER JOIN EstadoTer AS EP ON PV.IdEstado=EP.IdEstado
 INNER JOIN adm_Usuarios AS U ON PV.IdUsuario=U.IdUsuario   INNER JOIN Localidades AS LP ON PV.IdLocOrd=LP.IdLocal
 INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN  SectoresEco AS S ON T.IdSector=S.IdSector  INNER JOIN RegimenDian AS R ON T.IdRegimen=R.IdRegimen
 INNER JOIN EstadoTer AS ET ON T.IdEstado=ET.IdEstado INNER JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal 
 INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep  INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 LEFT JOIN Tablapor AS TD ON PV.CdDct=TD.IdTarifa
 WHERE T.IdLocal LIKE ISNULL(@pmIdLocal,'%') AND T.IdSector LIKE ISNULL(@pmIdSector,'%')  AND T.IdRegimen LIKE ISNULL(@pmIdRegimen,'%')
 AND TipoId LIKE ISNULL(@pmTipoId,'%') AND T.IdProf LIKE ISNULL(@pmIdProf,'%') AND PV.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND IdLocOrd LIKE ISNULL(@pmIdLocOrd,'%') AND PV.IdForma LIKE ISNULL(@pmIdForma,'%') AND PV.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND PV.IdEstado LIKE ISNULL(@pmIdEstado,'%') AND T.IdEstado LIKE ISNULL(@pmIdEstadoTer,'%')
 AND (PV.Inactivo=ISNULL(@pmInactivo,0) or PV.Inactivo=ISNULL(@pmInactivo,1))
 AND (T.Inactivo=ISNULL(@pmInactivoTer,0) or T.Inactivo=ISNULL(@pmInactivoTer,1))
 AND (FecIngreso>=ISNULL(@pmFecIngIni,CAST('19100101' AS SMALLDATETIME)) AND FecIngreso<=ISNULL(@pmFecIngFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercVendedor @pmIdVend VARCHAR(16)
 AS 
 SELECT IdVend,IdTarCms,IdTarCm2,CdJorn,IdSzona,IdGrupo,PathFoto,PathFirma,IdClase,NumCuenta,IdBanco
,Comntarios,Inactivo,FecAdd,FecUpdate,IdUsuario
 FROM TercVendedor
 WHERE IdVend=@pmIdVend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercVendedorLta @pmIdLocal VARCHAR(8)=Null,@pmIdProf VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdSzona VARCHAR(4)=Null,@pmIdTarCms VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmInactivoTer BIT=Null,@pmIdEstado VARCHAR(4)=Null
,@pmIdBanco VARCHAR(4)=Null
 AS 
 SELECT IdVend,Dv,TipoId,RazonSocial,T.Codigo AS CodVend,T.Direccion AS Dreccion,T.IdLocal AS CodCiud,L.Localidad AS Ciudad,D.Departamento AS Departmto,Telefono
,Fax,TelMovil,e_mail,T.IdProf AS CodProf,Profesion,V.IdGrupo AS CdGrupo,GrupoClie,CdJorn,Jornada,IdTarCms,CM.Tarifa AS TarfCms,IdTarCm2,TCM.Tarifa AS TarfCms2,V.IdSzona AS CdSubzona,Subzona
,IdLugarCed,LC.Localidad AS LugarCedula,DC.Departamento AS DptoLugcedula,FecExpCed,NomCial
,V.IdClase AS CdClase,ClaseCuenta,NumCuenta,V.IdBanco AS CdBanco,Banco,B.Direccion AS DirBanco
,EsCliente,EsProveedor,EsPropietario,EsVendedor,EsConductor,EsEmpleado,EsOperario
,Observacion,Comntarios,PathFoto,PathFirma,T.IdEstado AS IdEstTerc,Estado,V.Inactivo AS Vend_inactivo,T.Inactivo AS Ter_Inactvo
,FecAdd,FecUpdate,V.IdUsuario AS IdUsuari,Usuario,IniStgNom,NColor
 FROM TercVendedor AS V INNER JOIN Terceros AS T ON V.IdVend=T.IdTercero  INNER JOIN GruposCli AS G ON V.IdGrupo=G.IdGrupo
 INNER JOIN Tablapor AS CM ON V.IdTarCms=CM.IdTarifa INNER JOIN Tablapor AS TCM ON V.IdTarCm2=TCM.IdTarifa
 INNER JOIN Subzonas AS SZ ON V.IdSzona=SZ.IdSzona  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 INNER JOIN ClaseCta AS CTA ON  V.IdClase=CTA.IdClase  INNER JOIN Bancos AS B ON V.IdBanco=B.IdBanco
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN EstadoTer AS ET ON T.IdEstado=ET.IdEstado INNER JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal
 INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep  INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 LEFT JOIN Jornadas AS J ON V.CdJorn=J.IdJornada 
 WHERE T.IdLocal LIKE ISNULL(@pmIdLocal,'%') AND  T.IdProf LIKE ISNULL(@pmIdProf,'%')
 AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')  AND V.IdSzona LIKE ISNULL(@pmIdSzona,'%')
 AND IdTarCms LIKE ISNULL(@pmIdTarCms,'%') AND T.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND V.IdBanco LIKE ISNULL(@pmIdBanco,'%')
 AND (V.Inactivo=ISNULL(@pmInactivo,0) or V.Inactivo=ISNULL(@pmInactivo,1))
 AND (T.Inactivo=ISNULL(@pmInactivoTer,0) or T.Inactivo=ISNULL(@pmInactivoTer,1))
ORDER BY RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercVendedorReg @pmIdVend VARCHAR(16)
 AS 
 SELECT IdVend,TipoId,RazonSocial,Dv,IdTarCms,Tarifa,Simbolo,IdTarCm2,CdJorn,V.IdSzona AS CdSubzona,Subzona
,SZ.IdZona AS CdZona,Zona,V.IdGrupo AS CdGrupo,GrupoClie,V.IdClase AS CdClase,ClaseCuenta,NumCuenta,V.IdBanco AS CdBco,Banco
,Comntarios,V.Inactivo AS Inactvo
 FROM TercVendedor AS V INNER JOIN Terceros AS T ON V.IdVend=T.IdTercero  INNER JOIN GruposCli AS G ON V.IdGrupo=G.IdGrupo
 INNER JOIN Tablapor AS CM ON V.IdTarCms=CM.IdTarifa INNER JOIN Subzonas AS SZ ON V.IdSzona=SZ.IdSzona 
 INNER JOIN Zonas AS Z ON SZ.IdZona=Z.IdZona
 INNER JOIN ClaseCta AS CTA ON  V.IdClase=CTA.IdClase  INNER JOIN Bancos AS B ON V.IdBanco=B.IdBanco
 WHERE IdVend=@pmIdVend

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTerceros @pmIdTercero VARCHAR(16)
 AS 
 SELECT IdTercero,RazonSocial,Codigo,TipoId,Dv,NomCial,SiglaRaz,Direccion,IdLocal,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor
,EsPropietario,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,IdSector,IdProf,IdRegimen,TipEnte,IdLugarCed,FecExpCed
,Observacion,IniStgNom,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Terceros
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercerosBus @pmCodigo VARCHAR(16)=Null,@pmTelefono VARCHAR(20)=Null
 AS 
--consultar por codigo o por telefono
 SELECT IdTercero,RazonSocial,Codigo,TipoId,Dv,NomCial,SiglaRaz,Direccion,IdLocal,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor
,EsPropietario,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,IdProf,IdSector,IdRegimen,IdLugarCed,TipEnte,FecExpCed
,Observacion,IniStgNom,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Terceros
 WHERE Codigo  LIKE ISNULL(@pmCodigo,'%') AND Telefono LIKE ISNULL(@pmTelefono,'%')
 ORDER BY RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercerosCod @pmIdTercero VARCHAR(16),@pmCodigo VARCHAR(16)
 AS 
 SELECT IdTercero,RazonSocial,Codigo,TipoId,Dv,NomCial,SiglaRaz,Direccion,IdLocal,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor
,EsPropietario,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,IdSector,IdRegimen,IdLugarCed,FecExpCed,TipEnte,IdProf
,Observacion,IniStgNom,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM Terceros
 WHERE IdTercero=@pmIdTercero OR Codigo=@pmCodigo
 ORDER BY  IdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercerosDso @pmIdLocal VARCHAR(8)=Null,@pmIdSector VARCHAR(8)=Null,@pmIdEstado VARCHAR(4)=Null,@pmIdRegimen VARCHAR(4)=Null
,@pmInactivo BIT=Null,@pmEsCliente BIT=Null,@pmEsVendedor BIT=Null,@pmEsProveedor BIT=Null,@pmEsConductor BIT=Null,@pmEsPropietario BIT=Null,@pmEsAccnista BIT=Null
,@pmEsCiaAseg BIT=Null,@pmEsEmpleado BIT=Null,@pmEsOperario BIT=Null,@pmTipoId CHAR(1)=Null,@pmIdProf VARCHAR(4)=Null
 AS 
 SELECT IdTercero,Dv,TipoId,RazonSocial,T.Codigo AS Codtercero,NomCial,SiglaRaz,T.Direccion AS Dreccion,T.IdLocal AS CodCiud,L.Localidad AS Ciudad,D.Departamento AS Departmto,Telefono,Fax,TelMovil,SitioWeb,e_mail,EsCliente,EsVendedor,EsConductor,EsPropietario
,EsProveedor,EsEmpleado,EsOperario,EsAccnista,EsCiaAseg,EsCliePres,TipEnte,T.IdSector AS CodSector,SectorEco,T.IdProf AS CodProf,Profesion,T.IdRegimen AS IdRegmen,Regimen,IdLugarCed,LC.Localidad AS LugarCedula,DC.Departamento AS DptoLugcedula,FecExpCed,Observacion,T.IdEstado AS IdEstdo,Estado
,T.Inactivo AS Inactvo,FechaAdd,FechaUpdate,T.IdUsuario AS IdUsario,Usuario,IniStgNom,NColor
 FROM Terceros AS T INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN  SectoresEco AS S ON T.IdSector=S.IdSector 
 INNER JOIN RegimenDian AS R ON T.IdRegimen=R.IdRegimen
 INNER JOIN EstadoTer AS ET ON T.IdEstado=ET.IdEstado
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LC ON T.IdLugarCed=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 INNER JOIN Profesiones AS PF ON T.IdProf=PF.IdProf 
 WHERE T.IdLocal LIKE ISNULL(@pmIdLocal,'%') AND T.IdSector LIKE ISNULL(@pmIdSector,'%') AND T.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND T.IdRegimen LIKE ISNULL(@pmIdRegimen,'%') AND TipoId LIKE ISNULL(@pmTipoId,'%')
 AND T.IdProf LIKE ISNULL(@pmIdProf,'%')
AND (T.Inactivo=ISNULL(@pmInactivo,0) or T.Inactivo=ISNULL(@pmInactivo,1))
AND (EsCliente=ISNULL(@pmEsCliente,0) or EsCliente=ISNULL(@pmEsCliente,1))
AND (EsVendedor=ISNULL(@pmEsVendedor,0) or EsVendedor=ISNULL(@pmEsVendedor,1))
AND (EsProveedor=ISNULL(@pmEsProveedor,0) or EsProveedor=ISNULL(@pmEsProveedor,1))
AND (EsConductor=ISNULL(@pmEsConductor,0) or EsConductor=ISNULL(@pmEsConductor,1))
AND (EsPropietario=ISNULL(@pmEsPropietario,0) or EsPropietario=ISNULL(@pmEsPropietario,1))
AND (EsAccnista=ISNULL(@pmEsAccnista,0) or EsAccnista=ISNULL(@pmEsAccnista,1))
AND (EsCiaAseg=ISNULL(@pmEsCiaAseg,0) or EsCiaAseg=ISNULL(@pmEsCiaAseg,1))
AND (EsEmpleado=ISNULL(@pmEsEmpleado,0) or EsEmpleado=ISNULL(@pmEsEmpleado,1))
AND (EsOperario=ISNULL(@pmEsOperario,0) or EsOperario=ISNULL(@pmEsOperario,1))
ORDER BY RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTercerosReg @pmIdTercero VARCHAR(16)
 AS 
 SELECT IdTercero,RazonSocial,Codigo,TipoId,Dv,Direccion,IdLocal,Telefono,IdEstado,Inactivo
 FROM Terceros
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposAdm @pmIdAdmon VARCHAR(4)
 AS 
 IF @pmIdAdmon IS NULL
 SELECT IdAdmon,TipoAdmon
 FROM TiposAdm
 WHERE Inactivo=0
 ORDER BY TipoAdmon
ELSE
 SELECT IdAdmon,TipoAdmon,Inactivo
 FROM TiposAdm
 WHERE IdAdmon=@pmIdAdmon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposAgenc @pmIdTipAgenc VARCHAR(4)
 AS 
 IF @pmIdTipAgenc IS NULL
 SELECT IdTipAgenc,TipoAgencia
 FROM TiposAgenc
 WHERE Inactivo=0
 ORDER BY IdTipAgenc
 ELSE
 SELECT IdTipAgenc,TipoAgencia,Inactivo
 FROM TiposAgenc
 WHERE IdTipAgenc=@pmIdTipAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposBan @pmIdBandera VARCHAR(4)
 AS 
 IF @pmIdBandera IS NULL 
  SELECT IdBandera,TipoBandera
  FROM TiposBan
  WHERE Inactivo=0
  ORDER BY IdBandera
ELSE
  SELECT IdBandera,TipoBandera,Inactivo
  FROM TiposBan
  WHERE IdBandera=@pmIdBandera
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposBloq @pmIdTipBloq VARCHAR(4)
 AS 
 IF @pmIdTipBloq IS NULL
  SELECT IdTipBloq,TipoBloqueo
  FROM TiposBloq
  WHERE Inactivo=0
 ORDER BY IdTipBloq
 ELSE
  SELECT IdTipBloq,TipoBloqueo,Inactivo
  FROM TiposBloq
  WHERE IdTipBloq=@pmIdTipBloq

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposCar @pmIdCrceria VARCHAR(4)
 AS 
 IF @pmIdCrceria IS NULL
 SELECT IdCrceria,TipoCar
 FROM TiposCar
 WHERE Inactivo=0 
 ORDER BY TipoCar
 ELSE
 SELECT IdCrceria,TipoCar,Inactivo
 FROM TiposCar
 WHERE IdCrceria=@pmIdCrceria
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposCarga @pmIdTipCarga VARCHAR(4)
 AS 
 IF @pmIdTipCarga IS NULL
  SELECT IdTipCarga,TipoCarga
  FROM TiposCarga
  WHERE Inactivo=0
 ORDER BY IdTipCarga
 ELSE
  SELECT IdTipCarga,TipoCarga,Inactivo
  FROM TiposCarga
  WHERE IdTipCarga=@pmIdTipCarga
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposCnd @pmIdTipoCon VARCHAR(4)
 AS 
 BEGIN
 IF @pmIdTipoCon IS NULL
  SELECT IdTipoCon,TipoContndor
  FROM TiposCnd
  WHERE Inactivo=0
  ORDER BY IdTipoCon
 ELSE
  SELECT IdTipoCon,TipoContndor,Inactivo
  FROM TiposCnd
  WHERE IdTipoCon=@pmIdTipoCon
 END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposCol @pmIdColor VARCHAR(4)
 AS 
 IF @pmIdColor IS NULL
 SELECT IdColor,NomColor,NColor
 FROM TiposCol
 WHERE Inactivo=0
 ORDER BY NomColor
 ELSE
 SELECT IdColor,NomColor,NColor,Inactivo
 FROM TiposCol
 WHERE IdColor=@pmIdColor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposCom @pmIdCom VARCHAR(3)
 AS 
 SELECT IdCom,TipoCom,IdDiario,EsEgreso,ConsUnico,Inactivo,FechaAdd,FechaUpdate
 FROM TiposCom
 WHERE IdCom=@pmIdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposComAgenc @pmIdSubAgenc VARCHAR(4)
 AS 
 SELECT IdSubAgenc,SubTipoAgenc,IdTipAgenc,Inactivo
 FROM TiposComAgenc
 WHERE IdSubAgenc=@pmIdSubAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposComAgencLta @pmIdTipAgenc VARCHAR(4)
 AS 
 SELECT IdSubAgenc,SubTipoAgenc,S.IdTipAgenc AS CdTipoAgenc,TipoAgencia
 FROM TiposComAgenc AS S INNER JOIN TiposAgenc AS A ON S.IdTipAgenc=A.IdTipAgenc
 WHERE S.Inactivo=0 AND S.IdTipAgenc LIKE ISNULL(@pmIdTipAgenc,'%')
 ORDER BY SubTipoAgenc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposComCons @pmIdCom VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT IdCom,IdCia,LDesde,LHasta,Numero,NumManual,Formato,ConfigFecha,TipoPapel
,Orientacion,VistaPrevia,VerSetup,NumCopias,FechaAdd,FechaUpdate
 FROM TiposComCons
 WHERE IdCom=@pmIdCom AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposComConsLta @pmIdCom VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT N.IdCom AS CdTipo,TipoCom,N.IdCia AS CdCia,Compania,LDesde,LHasta,Numero,NumManual,Formato
,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,ConfigFecha,EsEgreso,ConsUnico
,T.IdDiario AS CdDiario,Diario,N.FechaAdd AS Fec_Add,N.FechaUpdate AS Fec_Update
 FROM TiposComCons AS N INNER JOIN TiposCom AS T ON N.IdCom=T.IdCom
 INNER JOIN Diarios AS D ON T.IdDiario=D.IdDiario INNER JOIN Companias AS C ON N.IdCia=C.IdCia
 WHERE T.Inactivo=0  AND N.IdCom LIKE ISNULL(@pmIdCom,'%') AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY N.IdCom,N.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposComDsa @pmEsEgreso BIT
 AS
 SELECT   IdCom,TipoCom +'  ' + IdCom  AS DsTip  FROM  TiposCom 
 WHERE  Inactivo=0 AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 UNION
 SELECT   IdDoc AS IdCom,TipoDoc+ ' '+ IdDoc AS DsTip   FROM   Sys_TiposDoc
 WHERE Inactivo=0  AND IdDoc IN ('DVE','DEI','DOP','DEC','DVO','DRV')
 ORDER BY IdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposComDso @pmEsEgreso BIT

AS
 SELECT   IdCom, TipoCom,IdCom +'  ' + TipoCom  AS DsTip  FROM  TiposCom 
 WHERE  Inactivo=0 AND (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 ORDER BY  IdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposComLta @pmEsEgreso BIT=Null,@pmInactivo BIT=Null
 AS 
 SELECT TC.IdCom AS CdCom,TipoCom,TC.IdDiario AS CodDiario,Diario,EsEgreso,ConsUnico
,N.IdCia AS CdCia,Compania,LDesde,LHasta,Numero,NumManual,Formato
,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,ConfigFecha
,TC.Inactivo AS Inactvo,TC.FechaAdd AS Fec_Add,TC.FechaUpdate AS Fec_Update
 FROM TiposCom AS TC INNER JOIN Diarios AS  D ON TC.IdDiario=D.IdDiario
 LEFT JOIN  TiposComCons  AS N ON TC.IdCom=N.IdCom
 LEFT JOIN Companias AS C ON N.IdCia=C.IdCia 
 WHERE (EsEgreso=ISNULL(@pmEsEgreso,0) or EsEgreso=ISNULL(@pmEsEgreso,1))
 AND  (TC.Inactivo=ISNULL(@pmInactivo,0) or TC.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY TipoCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposCon @pmIdTipCon VARCHAR(4)
 AS 
 IF @pmIdTipCon IS NULL
  SELECT IdTipCon,TipoContrato,Duracion
  FROM TiposCon
  WHERE Inactivo=0
  ORDER BY IdTipCon
 ELSE
  SELECT IdTipCon,TipoContrato,Duracion,Inactivo
  FROM TiposCon
  WHERE IdTipCon=@pmIdTipCon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposCot @pmIdCot VARCHAR(4)
 AS 
 IF @pmIdCot IS NULL
  SELECT IdCot,Cotizante 
  FROM TiposCot
  WHERE Inactivo=0
  ORDER BY IdCot
 ELSE
 SELECT IdCot,Cotizante,Inactivo
 FROM TiposCot
 WHERE IdCot=@pmIdCot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposCotDso
 AS 
  SELECT IdCot,Cotizante,IdCot+ ' '+Cotizante AS DsCot
  FROM TiposCot
  WHERE Inactivo=0
  ORDER BY IdCot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDoc @pmIdDoc VARCHAR(3)
 AS 
 IF @pmIdDoc IS NULL
  SELECT IdDoc,TipoDoc,ConsUnico,Leyenda,IntegraCias
  FROM Sys_TiposDoc
  WHERE Inactivo=0 
  ORDER BY IdDoc
 ELSE
  SELECT IdDoc,TipoDoc,ConsUnico,Leyenda,IntegraCias,Inactivo
  FROM Sys_TiposDoc
  WHERE IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocAbo
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('DFC','DF1','DF2', 'DF3', 'DF4', 'DF5','NCR','REC','ODB' )  AND Inactivo=0
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocAut
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('FCR','DFO','ODB','OD2') --todas las fac.credito con este codigo
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocCli
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('FCR','FC1','FC2', 'FC3', 'FC4', 'FC5','NDB','CHE','ODB' )  AND Inactivo=0
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryTiposDocCmp 
 AS
 SELECT   IdDoc AS IdTipo, TipoDoc AS NomTipo,TipoDoc+ ' '+ IdDoc AS DsTip 
 FROM    Sys_TiposDoc
 WHERE Inactivo=0 
 UNION
 SELECT  IdCom AS IdTipo, TipoCom AS NomTipo,TipoCom+ ' '+IdCom AS DsTip
 FROM     TiposCom
 WHERE Inactivo=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocCons @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2)
 AS 
 SELECT IdDoc,IdCia,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero
,NumManual,IntLotes,ConfigFecha,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias,FechaAdd,FechaUpdate
 FROM TiposDocCons
 WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocConsLta @pmIdDoc VARCHAR(3)=Null,@pmIdCia CHAR(2)=Null
 AS 
 SELECT N.IdDoc AS CdDoc,TipoDoc,N.IdCia AS CdCia,Compania,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero
,NumManual,IntLotes,ConfigFecha,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias
,ConsUnico,Leyenda,N.FechaAdd AS Fec_Add,N.FechaUpdate AS Fec_Update
 FROM TiposDocCons AS N INNER JOIN Sys_TiposDoc AS T ON N.IdDoc=T.IdDoc
 INNER JOIN Companias AS C ON N.IdCia=C.IdCia
 WHERE T.Inactivo=0 AND N.IdDoc LIKE ISNULL(@pmIdDoc,'%')  AND N.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY N.IdDoc,N.IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocCue @pmIdDoc VARCHAR(3),@pmIdClase INT
 AS 
 SELECT IdDoc,IdClase,ClaseCuenta,TipoMov
 FROM Sys_TiposDocCue
 WHERE IdDoc=@pmIdDoc AND (IdClase>=ISNULL(@pmIdClase,0)  AND IdClase<=ISNULL(@pmIdClase,2147483647))
 ORDER BY IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocCueDso @pmIdDoc VARCHAR(3)=Null
 AS 
 SELECT TC.IdDoc AS CdTipo,TipoDoc,IdClase,ClaseCuenta,TipoMov
 FROM Sys_TiposDocCue AS TC  INNER JOIN Sys_TiposDoc AS T ON TC.IdDoc=T.IdDoc
 WHERE TC.IdDoc LIKE ISNULL(@pmIdDoc,'%')  OR IdClase=98 OR IdClase=99
 ORDER BY TipoDoc,IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocDso @pmInactivo BIT=Null
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip   FROM  Sys_TiposDoc 
 WHERE (Inactivo=ISNULL(@pmInactivo,0) or  Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocEnt
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('ENT','COM','SAL','SAI') 
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocFac 
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('FCR','FC1','FC2', 'FC3', 'FC4', 'FC5', 'FCO', 'FO1', 'FO2', 'FO3', 'FO4', 'FO5')
 AND Inactivo=0
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocFor
--tipos de documento de formas de pago
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('REC','FCO','FO1','FO2','FO3','FO4','FO5','PLA','RCO' ,'RC1','RC2')
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocInv
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc  NOT IN ('ODC','PED','REM','COT','GUI','ODS','OCC','FIS') AND Inactivo=0
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocKar @pmInactivo BIT=Null
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip 
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('AJF','AJS','COM','COT','DEI','DF1','DF2','DF3','DF4','DF5','DFC','DFO','DFV','DNP','DO1','DO2','DO3','DO4','DO5','DSI','DVE','DVN','DVP','DVS','ENT'
,'FC1','FC2','FC3','FC4','FC5','FCO','FCR','FIS','FO1','FO2','FO3','FO4','FO5','FVC','GUI','NPR','NTS','OCC','ODC','PAS','PED','PLA','REM','REQ','SAI','SAL')
 AND (Inactivo=ISNULL(@pmInactivo,0) or  Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdDoc


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocLta @pmInactivo BIT=Null
 AS 
 SELECT T.IdDoc AS CdDoc,TipoDoc,ConsUnico,Leyenda,N.IdCia AS CdCia,Compania
,LDesde,LHasta,Resolucion,RangoNum,FechaRes,Prefijo,Numero
,NumManual,IntLotes,ConfigFecha,Formato,TipoPapel,Orientacion,VistaPrevia,VerSetup,NumCopias
,N.FechaAdd AS Fec_Add,N.FechaUpdate AS Fec_Update,T.Inactivo AS Inactiv
 FROM Sys_TiposDoc AS T LEFT JOIN TiposDocCons AS N ON T.IdDoc=N.IdDoc
 LEFT JOIN Companias AS C ON N.IdCia=C.IdCia 
 WHERE  (T.Inactivo=ISNULL(@pmInactivo,0) or T.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY T.IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposDocPag
 AS 
 SELECT  IdDoc,TipoDoc,IdDoc+'  '+ TipoDoc AS DsTip
 FROM  Sys_TiposDoc 
 WHERE IdDoc IN ('REC','FCO','FO1','FO2','FO3','FO4','FO5','PLA','RCO'
,'PAS','EGO','CNS','DNP','DVP','DVC','ODB','DEC','DRC','DFO','DO1','DO2','DO3','DO4','DO5','OD1','RC1','RC2','OD2')
 ORDER BY IdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposEsc @pmIdTipoEsc VARCHAR(4)
 AS 
 IF @pmIdTipoEsc IS NULL
  SELECT IdTipoEsc,TipoEscolta
  FROM TiposEsc
  WHERE Inactivo=0
  ORDER BY TipoEscolta
 ELSE
  SELECT IdTipoEsc,TipoEscolta,Inactivo
  FROM TiposEsc
  WHERE IdTipoEsc=@pmIdTipoEsc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposFil @pmIdFiltro VARCHAR(4)
 AS 
 IF @pmIdFiltro IS NULL 
  SELECT IdFiltro,TipoFiltro
  FROM TiposFil
  WHERE Inactivo=0
  ORDER BY  IdFiltro
 ELSE
  SELECT IdFiltro,TipoFiltro,Inactivo
  FROM TiposFil
  WHERE IdFiltro=@pmIdFiltro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposFuel @pmIdCom VARCHAR(4)
 AS 
 IF @pmIdCom IS NULL
 SELECT IdCom,TipoComb
 FROM TiposFuel
 WHERE Inactivo=0
 ORDER BY TipoComb
 ELSE
 SELECT IdCom,TipoComb,Inactivo
 FROM TiposFuel
 WHERE IdCom=@pmIdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposLla @pmIdTlla VARCHAR(4)
 AS 
 IF @pmIdTlla IS NULL
 SELECT IdTlla,TipoLlanta
 FROM TiposLla
 WHERE Inactivo=0
 ORDER BY TipoLlanta
 ELSE
 SELECT IdTlla,TipoLlanta,Inactivo
 FROM TiposLla
 WHERE IdTlla=@pmIdTlla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposLub @pmIdLub VARCHAR(4)
 AS 
 IF @pmIdLub IS NULL
 SELECT IdLub,TipoLub
 FROM TiposLub
 WHERE Inactivo=0
 ORDER BY TipoLub
 ELSE
 SELECT IdLub,TipoLub,Inactivo
 FROM TiposLub
 WHERE IdLub=@pmIdLub
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposMat @pmIdMat VARCHAR(4)
 AS 
 IF @pmIdMat IS NULL
 SELECT IdMat,Material
 FROM TiposMat
 WHERE Inactivo=0
 ORDER BY IdMat
 ELSE
 SELECT IdMat,Material,Inactivo
 FROM TiposMat
 WHERE IdMat=@pmIdMat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposMcia @pmIdTmcia VARCHAR(4)
 AS 
 IF @pmIdTmcia IS NULL
  SELECT IdTmcia,TipoMcia 
  FROM TiposMcia
  WHERE  Inactivo=0
  ORDER BY IdTmcia
 ELSE
  SELECT IdTmcia,TipoMcia,Inactivo 
  FROM TiposMcia
  WHERE IdTmcia=@pmIdTmcia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposMnjo @pmIdMnjo VARCHAR(4)
 AS 
 IF @pmIdMnjo IS NULL
  SELECT IdMnjo,ManejoMcia
  FROM TiposMnjo
  WHERE  Inactivo=0
  ORDER BY IdMnjo
 ELSE
  SELECT IdMnjo,ManejoMcia,Inactivo
  FROM TiposMnjo
  WHERE IdMnjo=@pmIdMnjo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposMod @pmIdModd VARCHAR(4)
 AS 
 IF @pmIdModd IS NULL
 SELECT IdModd,Modalidad
 FROM TiposMod
 WHERE Inactivo=0
 ORDER BY IdModd
 ELSE
 SELECT IdModd,Modalidad,Inactivo
 FROM TiposMod
 WHERE IdModd=@pmIdModd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposMot @pmIdTipoMot VARCHAR(4)
 AS 
 IF @pmIdTipoMot IS NULL
 SELECT IdTipoMot,TipoMotor
 FROM TiposMot
 WHERE Inactivo=0
 ORDER BY TipoMotor
 ELSE
 SELECT IdTipoMot,TipoMotor,Inactivo
 FROM TiposMot
 WHERE IdTipoMot=@pmIdTipoMot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposNat @pmIdNat VARCHAR(4)
 AS 
 IF @pmIdNat IS NULL
  SELECT IdNat,Natlzaprod
  FROM TiposNat
  WHERE  Inactivo=0
  ORDER BY IdNat
 ELSE
  SELECT IdNat,Natlzaprod,Inactivo
  FROM TiposNat
  WHERE IdNat=@pmIdNat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposNivs @pmIdNiv VARCHAR(4)
 AS 
 IF @pmIdNiv IS NULL
 SELECT IdNiv,NivelServicio
 FROM TiposNivs
 WHERE Inactivo=0
 ORDER BY NivelServicio
 ELSE
 SELECT IdNiv,NivelServicio,Inactivo
 FROM TiposNivs
 WHERE IdNiv=@pmIdNiv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposNom @pmIdNom VARCHAR(4)
 AS 
 IF @pmIdNom IS NULL
  SELECT IdNom,TipoNomina,Asistencia,NoDevenga,TipoLiquida,FechaActual,CdCiaInt  FROM TiposNom
  WHERE Inactivo=0
  ORDER BY IdNom
ELSE
  SELECT IdNom,TipoNomina,Asistencia,NoDevenga,TipoLiquida,FechaActual,CdCiaInt,Inactivo
  FROM TiposNom
  WHERE IdNom=@pmIdNom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposNov @pmIdNov VARCHAR(4)
 AS 
 IF @pmIdNov IS NULL
  SELECT IdNov,Novedad,Admon
  FROM TiposNov
  WHERE Inactivo =0
  ORDER BY IdNov
 ELSE
  SELECT IdNov,Novedad,Admon,Inactivo 
  FROM TiposNov
  WHERE IdNov=@pmIdNov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposNovTra 
 AS 
  SELECT IdNov,Novedad,Admon  FROM TiposNov
  WHERE  IdNov IN ('TAE','TAP')
  ORDER BY IdNov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposPcnto @pmIdTipoPre VARCHAR(4)
 AS 
 IF @pmIdTipoPre IS NULL
 SELECT IdTipoPre,Precinto
 FROM TiposPcnto
 WHERE Inactivo=0
 ORDER BY Precinto
 ELSE
 SELECT IdTipoPre,Precinto,Inactivo
 FROM TiposPcnto
 WHERE IdTipoPre=@pmIdTipoPre
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposPpt @pmIdPpd VARCHAR(4)
 AS 
 IF @pmIdPpd IS NULL
  SELECT IdPpd,TipoProp,VehPropio
  FROM TiposPpt
 WHERE Inactivo=0
 ORDER BY  TipoProp
 ELSE
  SELECT IdPpd,TipoProp,VehPropio,Inactivo
  FROM TiposPpt
  WHERE IdPpd=@pmIdPpd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryTiposTerc @pmIdTipoTerc VARCHAR(4)
 AS 
 IF @pmIdTipoTerc IS NULL 
  SELECT IdTipoTerc,TipoTercero
  FROM TiposTerc
  WHERE Inactivo=0
 ORDER BY IdTipoTerc
 ELSE
  SELECT IdTipoTerc,TipoTercero,Inactivo
  FROM TiposTerc
  WHERE IdTipoTerc=@pmIdTipoTerc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposVeh @pmIdTipoVeh VARCHAR(4)
 AS 
 SELECT IdTipoVeh,TipoVehiculo,Configuracion,Ejes,Llantas,Inactivo
 FROM TiposVeh
 WHERE IdTipoVeh=@pmIdTipoVeh
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTiposVehDso 
 AS 
 SELECT IdTipoVeh,TipoVehiculo,Configuracion,Ejes,Llantas
 FROM TiposVeh
 WHERE Inactivo=0
 ORDER BY TipoVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTpapers
AS
 SELECT Idpapel,Nombre,STR(Idpapel,3,0)+'  '+Nombre AS DsPap FROM Sys_Tpapers ORDER BY IdPapel
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTraslados @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Traslado,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,Cantidad,NumDcmto,NumOrden,IdCCosto,IdSubCos,Modalidad
,OrigenAdd,TipCom,Comprobante,IdCiaCom,Observacion,IdEstado,TimeSys,FecUpdate,IdCiaCrea,IdUsuario
 FROM Trn_Traslados
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTrasladosLta  @pmFechaIni SMALLDATETIME, @pmFechaFin SMALLDATETIME,@pmTrasladoIni INT=Null,@pmTrasladoFin INT=Null
,@pmIdCia CHAR(2)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdConcepto VARCHAR(4)=Null,@pmIdEstado VARCHAR(4)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT Traslado,IdCia,Fecha,N.IdConcepto AS CdConcepto,Concepto,N.IdTercero AS NitTercero,RazonSocial,VrSubTotal,Cantidad,NumDcmto,NumOrden,N.IdCCosto AS CdCcosto,CCosto
,IdSubCos,Modalidad,OrigenAdd,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado,TimeSys,FecUpdate,IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario
 FROM Trn_Traslados AS N INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero
 INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON  N.IdCCosto=CC.IdCCosto
 WHERE  Fecha BETWEEN @pmFechaIni AND @pmFechaFin 
 AND Traslado BETWEEN ISNULL(@pmTrasladoIni,0) AND ISNULL(@pmTrasladoFin,2147483647)
 AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND N.IdTercero LIKE ISNULL(@pmIdTercero,'%')
 AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%') AND N.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 ORDER BY IdCia,Traslado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTrasladosOrd @pmNumOrden INT,@pmIdCia CHAR(2)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT TipDoc,Traslado,IdCia,Fecha,IdConcepto,IdTercero,VrSubTotal,Cantidad,NumDcmto,NumOrden,Modalidad
,TipCom,Comprobante,IdCiaCom
 FROM Trn_Traslados
 WHERE NumOrden=@pmNumOrden  AND  IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND Modalidad LIKE ISNULL(@pmModalidad,'%')
ORDER BY IdCia,Traslado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTrasladosRel @pmTipDoc VARCHAR(3),@pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmIdConcepto VARCHAR(4)=Null,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmModalidad VARCHAR(10)=Null
 AS 
 SELECT N.TipDoc AS Tip_Doc,Traslado,N.IdCia AS CdCia,Compania,N.Fecha AS FechaDoc,N.IdConcepto AS CdConcepto,Concepto,N.IdTercero AS NitTercero,RazonSocial
,VrSubTotal,Cantidad,NumDcmto,N.NumOrden AS Num_Orden,N.IdCCosto AS CdCcosto,CC.CCosto AS CentCosto,N.IdSubCos AS CdSubCent,SC.SubCosto AS SubcCosto,Modalidad
,OrigenAdd,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado
,N.TimeSys AS Fec_Add,N.FecUpdate AS Fec_Update,IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario
,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,VrCostoEnt,VrCostoSal,VrReteEnt,VrReteSal,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote
,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia
,Descripcion,Comptmntos,CdProdEquiv,TipOrd,K.NumOrden AS KarNumOrden,IdCiaOrd,Cotizacion,IdCiaCot
,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_Traslados AS N  INNER JOIN Trn_Kardex AS K ON N.TipDoc=K.TipDoc AND N.Traslado=K.Documento AND N.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia  
 INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd  
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 WHERE N.TipDoc=@pmTipDoc  AND N.Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND N.IdCia LIKE ISNULL(@pmIdCia,'%%') AND N.IdConcepto LIKE ISNULL(@pmIdConcepto,'%')
 AND N.IdTercero LIKE ISNULL(@pmIdTercero,'%') AND Modalidad LIKE ISNULL(@pmModalidad,'%')
 AND N.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')  
 ORDER BY N.IdCia,Traslado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryTraslados_Cr @pmTipDoc VARCHAR(3),@pmTrasladoIni INT,@pmTrasladoFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT N.TipDoc AS Tip_Doc,TipoDoc,Traslado,N.IdCia AS CdCia,Compania,N.Fecha AS FechaDoc,N.IdConcepto AS CdConcepto,Concepto,N.IdTercero AS NitTercero,RazonSocial
,VrSubTotal,Cantidad,NumDcmto,N.NumOrden AS Num_Orden,N.IdCCosto AS CdCcosto,CC.CCosto AS CentCosto,N.IdSubCos AS CdSubCent,SC.SubCosto AS SubcCosto,Modalidad
,OrigenAdd,TipCom,TipoCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS CdEstado,Estado
,N.TimeSys AS Fec_Add,N.FecUpdate AS Fec_Update,IdCiaCrea,N.IdUsuario AS IdUsuari,Usuario,Leyenda
,TipoId,Dv,T.Codigo AS CodTercero,NomCial,T.Direccion AS Terc_Direccion,T.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDep,Departamento,Telefono,Fax,e_mail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,TipEnte
,Item,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Descrip AS BodDescrip,CdTanque,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,VrUnitario
,VrPrecio,VrCostProm,TarifaIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,VrCostoEnt,VrCostoSal,VrReteEnt,VrReteSal,VrIcaEnt,VrIcaSal,VrBruto,CdUbic,NumLote,FechLote
,K.CdCCosto AS KarCodCenCosto,KC.CCosto AS Kar_Ccosto,K.CdSubCos AS KarCodSubcosto,KS.SubCosto AS Kar_Subcentro,pVehiculo,K.Referencia AS KarReferencia
,Descripcion,Comptmntos,CdProdEquiv,TipOrd,K.NumOrden AS KarNumOrden,IdCiaOrd,Cotizacion,IdCiaCot
,Remision,IdCiaRem,Factura,TipDocDev,NumDocDev,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal
,OtroImpto,Unidades,ItemCombo,Servcios,EsCombo,EsProdBase,ListaPrec,VrBase,CdMoneda,VrTasaCamb,VrDivisa1,VrDivisa2,VrDivisa3,Referencia2,FecOrden
--productos
,PM.Referencia AS Prod_Referencia,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,SG.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,ExtciaMin,ExtciaMax,ExtciaAct,VrCostAnt,VrCosto,VrCostPmd,Precio1,Precio2,Precio3,Precio4,Precio5,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,DescripLong
 FROM Trn_Traslados AS N  INNER JOIN Trn_Kardex AS K ON N.TipDoc=K.TipDoc AND N.Traslado=K.Documento AND N.IdCia=K.IdCia
 INNER JOIN Companias AS CN ON N.IdCia=CN.IdCia  INNER JOIN Sys_TiposDoc AS TD ON N.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON N.IdTercero=T.IdTercero INNER JOIN  Conceptos AS C ON N.IdConcepto=C.IdConcepto
 INNER JOIN EstadoDoc AS ED ON N.IdEstado=ED.IdEstado   INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario
 INNER JOIN CentroCosto AS CC ON N.IdCCosto=CC.IdCCosto
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd  
 INNER JOIN SubGrupos AS SG ON PM.IdSubgrupo=SG.IdSubgrupo INNER JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN CentroCosto AS KC ON K.CdCCosto=KC.IdCCosto LEFT JOIN SubCentros AS KS ON K.CdSubCos=KS.IdSubCos
 LEFT JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 LEFT JOIN TiposCom AS TC ON N.TipCom=TC.IdCom
 WHERE N.TipDoc=@pmTipDoc AND Traslado BETWEEN @pmTrasladoIni AND @pmTrasladoFin AND N.IdCia=@pmIdCia
 ORDER BY Traslado,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUOpciones @pmIdUsuario VARCHAR(11),@pmNomOpcion VARCHAR(20)=Null
 AS 
 SELECT IdUsuario,NomOpcion,Valor
 FROM adm_UOpciones
 WHERE IdUsuario=@pmIdUsuario AND NomOpcion LIKE ISNULL(@pmNomOpcion,'%')
 ORDER BY NomOpcion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUm @pmUndMed VARCHAR(10)
 AS 
 SELECT UndMed,Unidad,Carga
 FROM Sys_Um
 WHERE UndMed LIKE ISNULL(@pmUndMed,'%')
 ORDER BY UndMed
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUmDso @pmCarga BIT=Null
 AS 
 SELECT UndMed,Unidad,UndMed+'  '+Unidad AS DsUnd
 FROM Sys_Um
 WHERE (Carga=ISNULL(@pmCarga,0) or Carga=ISNULL(@pmCarga,1))
 ORDER BY UndMed
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUndMed @pmIdUnd VARCHAR(4)
 AS 
 SELECT IdUnd,Unidad,IdMat,IdEmp,FormRecp,ClaseEmp,DimAlto,DimAncho,DimLargo,DimVolm
,UndMed,PesoEpq,CapcMax,UndCapc,SimbUnd,Observ,Inactivo
 FROM UndMed
 WHERE IdUnd=@pmIdUnd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUndMedDso @pmClaseEmp VARCHAR(10)=Null
 AS 
 SELECT IdUnd,Unidad,Unidad+ ' ('+IdUnd+') '+ SimbUnd AS DsUnd
 FROM UndMed
 WHERE Inactivo=0 AND  ClaseEmp LIKE ISNULL(@pmClaseEmp,'%')
 ORDER BY Unidad

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUndMedLta @pmFormRecp VARCHAR(10)=Null,@pmClaseEmp VARCHAR(10)=Null
,@pmIdEmp VARCHAR(4)=Null,@pmInactivo BIT=Null
 AS 
 SELECT IdUnd,Unidad,U.IdMat AS CdMat,Material,U.IdEmp AS CdEmpq,Empaque,FormRecp,ClaseEmp,DimAlto,DimAncho,DimLargo,DimVolm
,UndMed,PesoEpq,CapcMax,UndCapc,SimbUnd,Observ,U.Inactivo AS Inactvo
 FROM UndMed AS U INNER JOIN TiposMat AS TM ON U.IdMat=TM.IdMat
 INNER JOIN Empaques AS E ON U.IdEmp=E.IdEmp
 WHERE FormRecp LIKE ISNULL(@pmFormRecp,'%') AND ClaseEmp LIKE ISNULL(@pmClaseEmp,'%')
 AND U.IdEmp LIKE ISNULL(@pmIdEmp,'%')
 AND (U.Inactivo=ISNULL(@pmInactivo,0) or U.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Unidad
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsuarios @pmIdUsuario VARCHAR(11)
 AS 
 SELECT IdUsuario,Usuario,PwdLog,IdGrupo,IdEstacion,IdCargo,Conectado,FechaAcc,FechaClave
,Caducidad,LoginSQL,PwdSQL,ModoSQL,Inactivo
 FROM adm_Usuarios
 WHERE IdUsuario LIKE ISNULL(@pmIdUsuario,'%')
 ORDER BY IdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsuariosCnn @pmIdUsuario VARCHAR(11)
AS
 SELECT IdUsuario,Usuario,Conectado,FechaAcc,Inactivo
 FROM adm_Usuarios
 WHERE Inactivo=0 AND Conectado<>0 AND IdGrupo<5 AND IdUsuario<>@pmIdUsuario
 ORDER BY IdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsuariosDso @pmIdGrupo INT
AS
 SELECT IdUsuario,Usuario,IdGrupo,Usuario+' '+IdUsuario AS DsUsu,Inactivo
 FROM adm_Usuarios
 WHERE Inactivo=0  AND  IdGrupo<=@pmIdGrupo
 ORDER BY Usuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsuariosEst @pmIdUsuario VARCHAR(11),@pmIdEstacion CHAR(2)
AS
 IF @pmIdUsuario IS NULL
  SELECT IdUsuario,Usuario,IdEstacion
  FROM adm_Usuarios WHERE  IdEstacion=@pmIdEstacion
 ELSE
  SELECT IdUsuario,Usuario,IdEstacion
  FROM adm_Usuarios WHERE  IdEstacion=@pmIdEstacion AND IdUsuario<>@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsuariosGru  @pmIdUsuario VARCHAR(11)
AS
 IF @pmIdUsuario IS NULL
  SELECT IdUsuario,Usuario,PwdLog,IdGrupo
  FROM adm_Usuarios
  WHERE  IdUsuario<>'ADMIN' AND IdUsuario<>'SUPERVISOR'
 ORDER BY  IdUsuario
 ELSE
  SELECT IdUsuario,Usuario,PwdLog,IdGrupo
  FROM adm_Usuarios
  WHERE IdUsuario<>'ADMIN' AND IdUsuario<>'SUPERVISOR' AND IdUsuario<>@pmIdUsuario
  ORDER BY  IdUsuario

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsuariosLta @pmIdGrupo INT,@pmInactivo BIT
AS
 SELECT IdUsuario,Usuario,IdGrupo,IdEstacion,Conectado,FechaAcc,Caducidad,FechaClave,LoginSQL,ModoSQL
,Inactivo,IdCargo,PwdLog,PwdSQL
 FROM adm_Usuarios 
 WHERE  IdGrupo<=@pmIdGrupo AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsucaja @pmIdUsuario VARCHAR(11),@pmnFormato VARCHAR(10)
 AS 
 SELECT IdUsuario,nFormato,CdCajero,IdConcepto,CdBodega,LtaPrec,IdVend,CdCms,CdCCosto,CdSubCos,AbreCajon,FechaAdd
 FROM adm_Usucaja
 WHERE IdUsuario=@pmIdUsuario AND nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryUsucajaLta @pmIdUsuario VARCHAR(11)=Null,@pmnFormato VARCHAR(10)=Null
 AS 
 SELECT UC.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario,nFormato,CdCajero,CJ.Usuario AS Cajero
,U.IdGrupo AS CdGrupo,U.IdEstacion AS CdEstacion,U.IdCargo AS CdCargo,Cargo,UC.IdConcepto AS CodConcepto,Concepto,IdDoc,CdBodega,Bodega
,LtaPrec,IdVend,RazonSocial,CdCms,CdCCosto,CCosto,CdSubCos,SubCosto,AbreCajon,UC.FechaAdd AS FecAdd
 FROM adm_Usucaja AS UC INNER JOIN adm_Usuarios AS U ON UC.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS T ON UC.IdVend=T.IdTercero
 INNER JOIN Conceptos AS C ON UC.IdConcepto=C.IdConcepto
 LEFT JOIN adm_Usuarios AS CJ ON UC.CdCajero=CJ.IdUsuario
 LEFT JOIN  Cargos AS CG ON U.IdCargo=CG.IdCargo
 LEFT JOIN Bodegas AS BG ON UC.CdBodega=BG.IdBodega
 LEFT JOIN CentroCosto AS CC ON UC.CdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON UC.CdSubCos=SC.IdSubCos
 WHERE UC.IdUsuario LIKE ISNULL(@pmIdUsuario,'%')  AND nFormato LIKE ISNULL(@pmnFormato,'%')
 ORDER BY U.Usuario,nFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,nRadica,IdCia,Item,TipoRango,IdRango,NumSerie,NumForma,Fecha,HoraServ,IdCliente
,IdAgencia,IdCuenta,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,PlacaVeh
,CdConductor,NomConductor,CantPasaj,Usuarios,TipFac,NumFactura,IdCiaFac,FechaFac,Anulado,NumDev,FecDev,Observacion,FechaUp,CdUsuario
 FROM Trn_Vales
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryValesRad @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,TipoRango,IdRango,NumSerie,NumForma,Fecha,HoraServ,IdCliente,RazonSocial,V.IdAgencia AS CdAgencia,Agencia
,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera,FactUnid,FactRutas,FactHoras,VrPago,VrCobro,IdCuenta
,CantPasaj,Usuarios,V.Observacion AS Observ,TipFac,NumFactura,IdCiaFac,FechaFac,Anulado,NumDev,FecDev
--,PlacaVeh,CdConductor,NomConductor
 FROM Trn_Vales AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia 
 ORDER BY  Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryValesRutas @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2) 
 AS 
 SELECT Item,V.IdRuta AS CdRuta,DescripRuta,NomRuta,TipoRuta,Origen,Destino,CodRuta
 FROM  Trn_ValesRutas AS V INNER JOIN TaxRutas AS R ON V.IdRuta=R.IdRuta
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryValesRutas_Cr  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmTipoRango VARCHAR(3)=Null,@pmNumFormaIni INT=Null,@pmNumFormaFin INT=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdRuta VARCHAR(4)=Null
 AS 
 SELECT  VR.IdRuta AS CdRuta,DescripRuta,VR.TarifPago AS VrPago,VR.TarifCobro AS VrCobro,TipoRango,IdRango,NumSerie,NumForma,V.Fecha AS FechaServ,HoraServ,V.nRadica AS NumRadica,V.IdCia AS CdCia,Compania,V.Item AS ValeItem
,IdCliente,CL.RazonSocial AS NomCliente,V.IdAgencia AS ValAgencia,Agencia,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera,FactUnid,FactRutas,FactHoras
,IdCuenta,CantPasaj,Usuarios,V.Observacion AS ValObserv,TipFac,NumFactura,IdCiaFac,FechaFac,V.Anulado AS ValAnulado,V.NumDev AS ValNumAnul,V.FecDev AS ValFechaDev
,FechaUp,CdUsuario,UV.Usuario AS ValUsuario--,VrPago,VrCobro
--datos de radicación
,R.Fecha AS FechaRad,FechaVence,R.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,R.Referencia AS DocReferncia,CxPagar,TipCom,Comprobante,IdCiaCom
,R.Anulado AS DocAnulado,R.NumDev AS DocNumDev,R.FecDev  AS DocFecDev,R.Observacion AS Observ,R.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--datos de ruta
,NomRuta,TipoRuta,Origen,Destino,CodRuta,RT.TarifPago AS RutTarifPago,RT.TarifCobro AS RutTarifCobro
,CL.TipoId AS CliTipoId,CL.Dv AS CliDv,CL.Codigo AS CliCodigo,CL.NomCial AS CliNomCial,CL.Direccion AS CliDireccion,CL.IdLocal AS CliCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento,CL.Telefono AS CliTelefono
 FROM Trn_Vales AS V INNER JOIN Trn_RadicaVales AS R ON V.TipDoc=R.TipDoc AND V.nRadica=R.nRadica AND V.IdCia=R.IdCia
 INNER JOIN  Terceros AS CL ON V.IdCliente=CL.IdTercero
 INNER JOIN Localidades AS L ON CL.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN Companias AS CN ON V.IdCia=CN.IdCia
 INNER JOIN  Terceros AS T ON R.IdTercero=T.IdTercero   INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 INNER JOIN Trn_ValesRutas AS VR  ON V.TipDoc=VR.TipDoc AND V.nRadica=VR.nRadica AND V.IdCia=VR.IdCia AND V.Item=VR.Item
 INNER JOIN TaxRutas AS RT ON VR.IdRuta=RT.IdRuta
 LEFT JOIN adm_Usuarios AS UV ON V.CdUsuario=UV.IdUsuario  LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia 
 WHERE  (R.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND  R.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND V.IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipoRango LIKE ISNULL(@pmTipoRango,'%')
 AND NumForma BETWEEN ISNULL(@pmNumFormaIni,0) AND ISNULL(@pmNumFormaFin,2147483647)
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND VR.IdRuta LIKE ISNULL(@pmIdRuta,'%')
 ORDER BY TipoRango,IdRango,NumForma


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVales_Cr  @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmIdCia CHAR(2)=Null
,@pmTipoRango VARCHAR(3)=Null,@pmNumFormaIni INT=Null,@pmNumFormaFin INT=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipoRango,IdRango,NumSerie,NumForma,V.Fecha AS FechaServ,HoraServ,V.nRadica AS NumRadica,V.IdCia AS CdCia,Compania,Item
,IdCliente,CL.RazonSocial AS NomCliente,V.IdAgencia AS ValAgencia,Agencia,NumMovil,CentCosto,Unidades,HoraRec,HoraEspera,FactUnid,FactRutas,FactHoras
,VrPago,VrCobro,IdCuenta,CantPasaj,Usuarios,V.Observacion AS ValObserv,TipFac,NumFactura,IdCiaFac,FechaFac,V.Anulado AS ValAnulado,V.NumDev AS ValNumAnul,V.FecDev AS ValFechaDev
,FechaUp,CdUsuario,UV.Usuario AS ValUsuario
--datos de radicación
,R.Fecha AS FechaRad,FechaVence,R.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,R.Referencia AS DocReferncia,CxPagar,TipCom,Comprobante,IdCiaCom
,R.Anulado AS DocAnulado,R.NumDev AS DocNumDev,R.FecDev  AS DocFecDev,R.Observacion AS Observ,R.IdUsuario AS IdUsuari,U.Usuario AS NomUsuario
--datos del cliente
,CL.TipoId AS CliTipoId,CL.Dv AS CliDv,CL.Codigo AS CliCodigo,CL.NomCial AS CliNomCial,CL.Direccion AS CliDireccion,CL.IdLocal AS CliCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,Departamento
,CL.Telefono AS CliTelefono,CL.Fax AS CliFax,CL.e_mail AS CliEmail,CL.IdSector AS CdSector,SectorEco,CL.IdRegimen AS CdRegimen,Regimen,CL.TipEnte AS CliTipoEnte
 FROM Trn_Vales AS V INNER JOIN Trn_RadicaVales AS R ON V.TipDoc=R.TipDoc AND V.nRadica=R.nRadica AND V.IdCia=R.IdCia
 INNER JOIN  Terceros AS CL ON V.IdCliente=CL.IdTercero
 INNER JOIN Localidades AS L ON CL.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON CL.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON CL.IdSector=SE.IdSector
 INNER JOIN Companias AS CN ON V.IdCia=CN.IdCia
 INNER JOIN  Terceros AS T ON R.IdTercero=T.IdTercero   INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 LEFT JOIN adm_Usuarios AS UV ON V.CdUsuario=UV.IdUsuario  LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia 
 WHERE  (R.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND  R.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 AND V.IdCia LIKE ISNULL(@pmIdCia,'%%') AND TipoRango LIKE ISNULL(@pmTipoRango,'%')
 AND NumForma BETWEEN ISNULL(@pmNumFormaIni,0) AND ISNULL(@pmNumFormaFin,2147483647)
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 ORDER BY TipoRango,IdRango,NumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehClientes @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT IdVehiculo,NumVeh,IdTipoVeh,IdMarca,LinMarca,ColorVeh,IdTipoMot,Modelo,ClaseMat,Cilind,IdCom,IdLub,IdMarLub,IdTlla,IdMarlla,IdFiltro,IdMarFil,NumSoat,FecSoat,VigSoat
,NitEmpSoat,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,KmInicial,KmActual,NitCliente,CondPago,NumCredito,Observacion,PathFoto,FecPriServ,FecUltServ
,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM VehClientes
 WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehClientesLta  @pmIdTipoVeh VARCHAR(4)=Null,@pmIdMarca VARCHAR(4)=Null,@pmIdTipoMot VARCHAR(4)=Null,@pmModelo VARCHAR(4)=Null
,@pmIdCom VARCHAR(4)=Null,@pmIdLub VARCHAR(4)=Null,@pmIdMarLub VARCHAR(4)=Null,@pmIdTlla VARCHAR(4)=Null,@pmIdMarlla VARCHAR(4)=Null
,@pmIdFiltro VARCHAR(4)=Null,@pmIdMarFil VARCHAR(4)=Null,@pmNitCliente VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmCondPago VARCHAR(10)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT IdVehiculo,NumVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,LinMarca,ColorVeh,V.IdTipoMot AS CdTipMotor,TipoMotor,Modelo,ClaseMat
,Cilind,V.IdCom AS CdTipComb,TipoComb,V.IdLub AS CdLub,TipoLub,IdMarLub,MU.Marca AS MarcaLub,V.IdTlla AS CdTipLlantas,TipoLlanta,IdMarlla,ML.Marca AS MarcaLlantas
,V.IdFiltro AS CdFiltro,TipoFiltro,IdMarFil,MF.Marca AS MarcaFiltro,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,KmInicial,KmActual,NitCliente,NC.RazonSocial AS Cliente,CondPago,NumCredito,V.Observacion AS Observ,PathFoto,FecPriServ,FecUltServ
,V.IdEstado AS CdEstado,Estado,V.Inactivo AS VehInactivo,V.FechaAdd AS FecAdd,V.FechaUpdate AS FecUpdate,V.IdUsuario AS CdUsuario,Usuario,EV.NColor AS NumColor,OutDemand
 FROM VehClientes  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca  INNER JOIN TiposMot AS TM ON V.IdTipoMot=TM.IdTipoMot
 INNER JOIN TiposFuel AS TF ON V.IdCom=TF.IdCom INNER JOIN TiposLub AS TL ON V.IdLub=TL.IdLub
 INNER JOIN TiposLla AS TLL ON V.IdTlla=TLL.IdTlla INNER JOIN Marcas AS ML ON V.IdMarlla=ML.IdMarca
 INNER JOIN Marcas AS MU ON V.IdMarLub=MU.IdMarca INNER JOIN TiposFil AS TFL ON  V.IdFiltro=TFL.IdFiltro
 INNER JOIN Marcas AS MF ON V.IdMarFil=MF.IdMarca
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS NC ON  V.NitCliente=NC.IdTercero   LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero 
 WHERE   V.IdTipoVeh LIKE ISNULL(@pmIdTipoVeh,'%')  AND V.IdMarca LIKE ISNULL(@pmIdMarca,'%') AND V.IdTipoMot LIKE ISNULL(@pmIdTipoMot,'%')
 AND Modelo LIKE ISNULL(@pmModelo,'%') AND V.IdCom LIKE ISNULL(@pmIdCom,'%')  AND V.IdLub LIKE ISNULL(@pmIdLub,'%')
 AND  IdMarLub LIKE ISNULL(@pmIdMarLub,'%') AND V.IdTlla LIKE ISNULL(@pmIdTlla,'%')  AND IdMarlla LIKE ISNULL(@pmIdMarlla,'%')
 AND V.IdFiltro LIKE ISNULL(@pmIdFiltro,'%') AND IdMarFil LIKE ISNULL(@pmIdMarFil ,'%')
 AND NitCliente LIKE ISNULL(@pmNitCliente,'%')  AND V.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND CondPago  LIKE ISNULL(@pmCondPago,'%') 
 AND (V.Inactivo=ISNULL(@pmInactivo,0) or V.Inactivo=ISNULL(@pmInactivo,1))
 AND (V.FechaAdd>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND V.FechaAdd<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehClientesNit @pmNitCliente VARCHAR(16),@pmCondPago VARCHAR(10)=Null
 AS 
 SELECT IdVehiculo,NumVeh,NitCliente,CondPago,NumCredito,KmActual,IdEstado,Inactivo
 FROM VehClientes
 WHERE NitCliente=@pmNitCliente  AND Inactivo=0 AND CondPago LIKE ISNULL(@pmCondPago,'%')
 ORDER BY  IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehClientesNum @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmInactivo BIT=Null
 AS 
 SELECT IdVehiculo,NumVeh,IdTipoVeh,IdMarca,LinMarca,ColorVeh,IdTipoMot,Modelo,ClaseMat,Cilind,IdCom,IdLub,IdMarLub,IdTlla,IdMarlla,IdFiltro,IdMarFil,NumSoat,FecSoat,VigSoat
,NitEmpSoat,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,KmInicial,KmActual,NitCliente,CondPago,NumCredito,Observacion,PathFoto,FecPriServ,FecUltServ
,IdEstado,Inactivo,FechaAdd,FechaUpdate,IdUsuario
 FROM VehClientes
 WHERE (IdVehiculo=@pmIdVehiculo OR NumVeh=@pmNumVeh)
 AND  (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY  IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehConductor @pmIdVehiculo VARCHAR(10),@pmNumero INT
 AS 
 SELECT IdVehiculo,Numero,Fecha,IdPropietario,IdConductor,IdCondNuevo,Observacion,TimeSys,IdUsuario
 FROM Trn_VehConductor
 WHERE IdVehiculo=@pmIdVehiculo
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehConductorDso @pmIdVehiculo VARCHAR(10)=Null,@pmIdConductor VARCHAR(16)=Null
,@pmIdCondNuevo VARCHAR(16)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT C.IdVehiculo AS PlacaVeh,Numero,Fecha,C.IdPropietario AS NitPropietario,NP.RazonSocial AS Propietario
,C.IdConductor AS CedConductor,NC.RazonSocial AS CondAnterior,IdCondNuevo,NN.RazonSocial AS NuevoConductor,C.Observacion AS Observ
,NumVeh,V.IdConductor AS VehCedCondtor,VC.RazonSocial AS VehConductor,TimeSys,C.IdUsuario AS IdUsuari,Usuario
 FROM Trn_VehConductor AS C INNER JOIN Vehiculos AS V ON C.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON C.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NC ON C.IdConductor=NC.IdTercero 
 INNER JOIN Terceros AS NN ON C.IdCondNuevo=NN.IdTercero 
 INNER JOIN Terceros AS VC ON V.IdConductor=VC.IdTercero 
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE V.Inactivo=0 AND  C.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND C.IdConductor LIKE ISNULL(@pmIdConductor,'%')
 AND IdCondNuevo LIKE ISNULL(@pmIdCondNuevo,'%') 
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY C.IdVehiculo,Numero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehConductorId @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT MAX(Numero) AS MxNum
 FROM Trn_VehConductor
 WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehConductoresLta @pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT  VP.IdVehiculo AS PlacaVeh,NumVeh,Item,CedConductor,T.RazonSocial AS Conductor,TipoCond,FechaIng,EstadoCond,IdPropietario,NP.RazonSocial AS VehPropietario
,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,NumMotor,SerieChasis,NumSerie
,NitEmpresa,NE.RazonSocial AS Empresa,IdPoseedor,NT.RazonSocial AS Poseedor,V.IdConductor AS VehCedCond,NC.RazonSocial AS VehConductor
,NContrato,FecIngreso, FecVigencia, FecRetiro,V.IdAdmon AS CdAdmon,TipoAdmon,V.IdEstado AS CdEstado,Estado,V.Inactivo AS Inactvo
--datos del conductor
,T.Codigo AS TercCodigo,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM VehConductores AS VP INNER JOIN  Vehiculos  AS V ON VP.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS T ON  VP.CedConductor=T.IdTercero
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh  INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor 
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero  INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  
 INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 WHERE VP.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 ORDER BY  VP.IdVehiculo,Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehLecturas @pmTipoLec VARCHAR(3),@pmNumero INT
 AS 
 SELECT TipoLec,Numero,Fecha,pVehiculo,KmtAnterior,KmtActual,Km2Anterior,Km2Actual,CdRemque,KmRemqAnt
,KmRemqActual,OrigenLect,TipDoc,Documento,CiaDoc,Observacion,TimeSys,IdUsuario
 FROM Trn_VehLecturas
 WHERE TipoLec=@pmTipoLec
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehLecturasDso @pmTipoLec VARCHAR(3)=Null,@pmpVehiculo VARCHAR(10)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT TipoLec,Numero,Fecha,pVehiculo,KmtAnterior,KmtActual,Km2Anterior,L.Km2Actual AS Km2_Actual,L.CdRemque AS Remolque,KmRemqAnt
,KmRemqActual,OrigenLect,TipDoc,Documento,CiaDoc,L.Observacion AS Observ,NumVeh,TimeSys,L.IdUsuario AS IdUsuari,Usuario
 FROM Trn_VehLecturas AS L INNER JOIN adm_Usuarios AS U ON L.IdUsuario=U.IdUsuario
 LEFT JOIN Vehiculos AS V ON L.pVehiculo=V.IdVehiculo
 WHERE pVehiculo LIKE ISNULL(@pmpVehiculo,'%') AND  TipoLec LIKE ISNULL(@pmTipoLec,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY TipoLec,Numero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehLecturasId @pmTipoLec VARCHAR(3)
 AS 
 SELECT MAX(Numero) AS MxNum
 FROM Trn_VehLecturas
 WHERE TipoLec=@pmTipoLec
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehLecturasVeh @pmTipoLec VARCHAR(3),@pmpVehiculo VARCHAR(10)=Null
 AS 
 SELECT TipoLec,Numero,Fecha,pVehiculo,KmtAnterior,KmtActual,Km2Anterior,Km2Actual,CdRemque,KmRemqAnt
,KmRemqActual,OrigenLect,TipDoc,Documento,CiaDoc,Observacion,TimeSys,IdUsuario
 FROM Trn_VehLecturas
 WHERE TipoLec=@pmTipoLec AND pVehiculo LIKE ISNULL(@pmpVehiculo,'%')
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehNumOrden @pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT  IdVehiculo,Numero,Fecha,NumOrden,NuevoNum,Observacion, TimeSys, C.IdUsuario AS CdUsuario,Usuario
 FROM  Trn_VehNumOrden AS C INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario
 WHERE IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehPolizas @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdVehiculo,IdClase,Numero,FecEmision,FecVigencia,NitCompania,Valor,Comntarios,DiasAviso
 FROM VehPolizas
 WHERE IdVehiculo=@pmIdVehiculo
  AND IdClase LIKE ISNULL(@pmIdClase,'%')
 ORDER BY IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehPolizasLta @pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT VP.IdVehiculo AS PlacaVeh,NumVeh,VP.IdClase AS CodClase,ClasePoliza,Numero,FecEmision
,VP.FecVigencia AS FecVence,NitCompania,RazonSocial,Valor,Comntarios,DiasAviso
 FROM VehPolizas AS VP INNER JOIN Vehiculos AS V ON VP.IdVehiculo=V.IdVehiculo
 INNER JOIN ClasePol AS CP ON VP.IdClase=CP.IdClase
 LEFT JOIN Terceros AS T ON VP.NitCompania=T.IdTercero
 WHERE V.Inactivo=0 AND VP.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY VP.IdVehiculo,VP.IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehPolizasPla @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT IdVehiculo,VP.IdClase AS CodClase,ClasePoliza,Numero,FecEmision
,FecVigencia,NitCompania,RazonSocial,Valor,Comntarios,DiasAviso
 FROM VehPolizas AS VP INNER JOIN ClasePol AS CP ON VP.IdClase=CP.IdClase
 LEFT JOIN Terceros AS T ON VP.NitCompania=T.IdTercero
 WHERE IdVehiculo=@pmIdVehiculo
 ORDER BY  VP.IdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehPropietariosLta @pmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT  VP.IdVehiculo AS PlacaVeh,NumVeh,Item,NitPropietario,T.RazonSocial AS Propietario,PorcPartic,FechaIng,EstadoProp,IdPropietario,NP.RazonSocial AS VehPropietario
,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,NumMotor,SerieChasis,NumSerie
,NitEmpresa,NE.RazonSocial AS Empresa,IdPoseedor,NT.RazonSocial AS Poseedor,IdConductor,NC.RazonSocial AS Conductor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,Adquisc,VrAvaludo,NContrato,FecIngreso, FecVigencia, FecRetiro,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdEstado AS CdEstado,Estado,V.Inactivo AS Inactvo
--datos del propietario
,T.Codigo AS TercCodigo,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
 FROM VehPropietarios AS VP INNER JOIN  Vehiculos  AS V ON VP.IdVehiculo=V.IdVehiculo INNER JOIN Terceros AS T ON  VP.NitPropietario=T.IdTercero
 INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh  INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor 
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero
 INNER JOIN TiposPpt AS TP ON V.IdPpd=TP.IdPpd  INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  
 INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon  LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero
 WHERE VP.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 ORDER BY  VP.IdVehiculo,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehRemolq @pmIdRemque VARCHAR(10)
 AS 
 SELECT IdRemque,IdMarca,IdColor,IdCrceria,Modelo,NEjes,nLlantas,Altura,Ancho,Largo,Vlumen,PesoKg,Capneta,UndCapc,NumComp,CapcComp
,NumSerial,IdPropietario,NitProv,FecCompra,VidaUtil,VrComercial,VrAvaludo,VrAsegurado,Kilmetraje,EquipoAdic,Descripcion,CdCenSer,CdLocal,Ubicacion
,Observacion,IdEstado,Inactivo,FecAdd,FecUpdate,IdUsuario,TarjetaProp,FecTarjProp,VigTarjProp,ConfEjes
 FROM VehRemolq
 WHERE IdRemque=@pmIdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehRemolqLta @pmIdMarca VARCHAR(4)=Null,@pmIdCrceria VARCHAR(4)=Null,@pmModelo VARCHAR(4)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmFecComIni SMALLDATETIME=Null,@pmFecComFin SMALLDATETIME=Null
 AS 
 SELECT IdRemque,R.IdMarca AS CdMarca,Marca,R.IdColor AS CodColor,NomColor,R.IdCrceria AS CodCarr,TipoCar,Modelo,ConfEjes,NEjes,nLlantas,Altura,Ancho,Largo,Vlumen
,PesoKg,Capneta,UndCapc,NumComp,CapcComp,NumSerial,IdPropietario,NP.RazonSocial AS Propietario,NitProv,NPV.RazonSocial AS Proveedor,FecCompra,VidaUtil
,VrComercial,VrAvaludo,VrAsegurado,Kilmetraje,EquipoAdic,Descripcion,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion
,LU.IdDep AS CodDpto,Departamento,Ubicacion,R.Observacion AS Observ,R.IdEstado AS CodEstado,Estado,R.Inactivo AS Inactiv,R.IdUsuario AS IdUsuari,Usuario
,FecAdd,FecUpdate,EV.NColor AS NumColor,OutDemand,TarjetaProp,FecTarjProp,VigTarjProp
 FROM VehRemolq AS R INNER JOIN Marcas AS M ON R.IdMarca=M.IdMarca  INNER JOIN TiposCol AS CL ON R.IdColor=CL.IdColor
 INNER JOIN TiposCar AS TC ON R.IdCrceria=TC.IdCrceria  INNER JOIN Terceros AS NP ON  R.IdPropietario=NP.IdTercero
 INNER JOIN EstadoVeh AS EV ON R.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON R.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS NPV ON R.NitProv=NPV.IdTercero
 LEFT JOIN CentrosServ AS CS ON R.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON R.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 WHERE R.IdMarca LIKE ISNULL(@pmIdMarca,'%')  AND R.IdCrceria LIKE ISNULL(@pmIdCrceria,'%')
 AND Modelo LIKE ISNULL(@pmModelo,'%')  AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND R.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (R.Inactivo=ISNULL(@pmInactivo,0) or R.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecCompra>=ISNULL(@pmFecComIni,CAST('19100101' AS SMALLDATETIME)) AND FecCompra<=ISNULL(@pmFecComFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  IdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehTraspasos @pmIdVehiculo VARCHAR(10),@pmNumero INT
 AS 
 SELECT IdVehiculo,Numero,Fecha,IdPpd,IdPropietario,IdPoseedor,IdPropNuevo,IdPoseeNuevo,IdNewPpd,IdGrupo,VrComercial
,VrCupo,VrAsegurado,Observacion,TipoAfil,TipoAfilNew,TimeSys,IdUsuario
 FROM Trn_VehTraspasos
 WHERE IdVehiculo=@pmIdVehiculo
 AND (Numero>=ISNULL(@pmNumero,0)  AND Numero<=ISNULL(@pmNumero,2147483647))
 ORDER BY Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehTraspasosDso @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null
,@pmIdPropNuevo VARCHAR(16)=Null,@pmIdPoseeNuevo VARCHAR(16)=Null,@pmIdPpd VARCHAR(4)=Null,@pmIdNewPpd VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT T.IdVehiculo AS PlacaVeh,Numero,Fecha,T.IdPropietario AS IdPropAnt,NP.RazonSocial AS PropAnterior,T.IdPoseedor AS IdPoseeAnt,NT.RazonSocial AS PoseeAnterior
,IdPropNuevo,NNP.RazonSocial AS NuevoPropietario,IdPoseeNuevo,NNT.RazonSocial AS NuevoPoseedor,T.IdPpd AS CdTipoProp,TP.TipoProp AS TipoPropAnt
,TP.VehPropio AS VehPropioAnt,IdNewPpd,TPN.TipoProp AS TipoPropNueva,TPN.VehPropio AS VehPropioNuevo,T.IdGrupo AS CdGrupo,GrupoProp,VrComercial,VrCupo,VrAsegurado,T.Observacion AS Observ
,NumVeh,Modelo,V.IdPropietario AS IdPropActual,NPA.RazonSocial AS PropActual,V.IdPoseedor AS IdPoseeActual,NTA.RazonSocial AS PoseeActual,IdConductor,NC.RazonSocial AS Conductor
,V.IdPpd AS CdTipPropActual,TPA.TipoProp AS TipoPropActual,FecCompra,T.TipoAfil AS TipoAfiAnt,TipoAfilNew,TimeSys,T.IdUsuario AS IdUsuari,Usuario
 FROM Trn_VehTraspasos  AS T INNER JOIN Vehiculos AS V ON T.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON T.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NT ON T.IdPoseedor=NT.IdTercero 
 INNER JOIN Terceros AS NNP ON T.IdPropNuevo=NNP.IdTercero 
 INNER JOIN Terceros AS NNT ON T.IdPoseeNuevo=NNT.IdTercero 
 INNER JOIN TiposPpt AS TP ON T.IdPpd=TP.IdPpd
 INNER JOIN TiposPpt AS TPN ON T.IdNewPpd=TPN.IdPpd
 INNER JOIN GruposPro AS G ON T.IdGrupo=G.IdGrupo
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS NC ON V.IdConductor=NC.IdTercero 
 INNER JOIN Terceros AS NPA ON V.IdPropietario=NPA.IdTercero 
 INNER JOIN Terceros AS NTA ON V.IdPoseedor=NTA.IdTercero 
 INNER JOIN TiposPpt AS TPA ON V.IdPpd=TPA.IdPpd
 WHERE  V.Inactivo=0  AND T.IdVehiculo like ISNULL(@pmIdVehiculo,'%')
 AND T.IdPropietario LIKE ISNULL(@pmIdPropietario,'%') AND T.IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')
 AND IdPropNuevo LIKE ISNULL(@pmIdPropNuevo,'%') AND IdPoseeNuevo LIKE ISNULL(@pmIdPoseeNuevo,'%')
 AND T.IdPpd LIKE ISNULL(@pmIdPpd,'%') AND IdNewPpd LIKE ISNULL(@pmIdNewPpd,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.IdVehiculo,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehTraspasosID @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT MAX(Numero) AS MxNum 
 FROM Trn_VehTraspasos
 WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryVehTraspasosVeh @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdPropNuevo VARCHAR(16)=Null
 AS 
 SELECT T.IdVehiculo AS PlacaVeh,Numero,Fecha,T.IdPropietario AS IdPropAnt,NP.RazonSocial AS PropAnterior,T.IdPoseedor AS IdPoseeAnt,NT.RazonSocial AS PoseeAnterior
,IdPropNuevo,NNP.RazonSocial AS NuevoPropietario,IdPoseeNuevo,NNT.RazonSocial AS NuevoPoseedor,T.IdPpd AS CdTipoProp,TP.TipoProp AS TipoPropAnt
,TP.VehPropio AS VehPropioAnt,IdNewPpd,TPN.TipoProp AS TipoPropNueva,TPN.VehPropio AS VehPropioNuevo,T.IdGrupo AS CdGrupo,GrupoProp,VrComercial,VrCupo,VrAsegurado,T.Observacion AS Observ
,NumVeh,Modelo,V.IdPropietario AS IdPropActual,NPA.RazonSocial AS PropActual,V.IdPoseedor AS IdPoseeActual,NTA.RazonSocial AS PoseeActual,IdConductor,NC.RazonSocial AS Conductor
,V.IdPpd AS CdTipPropActual,TPA.TipoProp AS TipoPropActual,FecCompra,T.TipoAfil AS TipoAfiAnt,TipoAfilNew,TimeSys,T.IdUsuario AS IdUsuari,Usuario
 FROM Trn_VehTraspasos  AS T INNER JOIN Vehiculos AS V ON T.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON T.IdPropietario=NP.IdTercero 
 INNER JOIN Terceros AS NT ON T.IdPoseedor=NT.IdTercero 
 INNER JOIN Terceros AS NNP ON T.IdPropNuevo=NNP.IdTercero 
 INNER JOIN Terceros AS NNT ON T.IdPoseeNuevo=NNT.IdTercero 
 INNER JOIN TiposPpt AS TP ON T.IdPpd=TP.IdPpd
 INNER JOIN TiposPpt AS TPN ON T.IdNewPpd=TPN.IdPpd
 INNER JOIN GruposPro AS G ON T.IdGrupo=G.IdGrupo
 INNER JOIN adm_Usuarios AS U ON T.IdUsuario=U.IdUsuario
 INNER JOIN Terceros AS NC ON V.IdConductor=NC.IdTercero 
 INNER JOIN Terceros AS NPA ON V.IdPropietario=NPA.IdTercero 
 INNER JOIN Terceros AS NTA ON V.IdPoseedor=NTA.IdTercero 
 INNER JOIN TiposPpt AS TPA ON V.IdPpd=TPA.IdPpd
 WHERE    T.IdVehiculo like ISNULL(@pmIdVehiculo,'%')  AND T.IdPropietario LIKE ISNULL(@pmIdPropietario,'%')  
 AND IdPropNuevo LIKE ISNULL(@pmIdPropNuevo,'%')
 ORDER BY T.IdVehiculo,Numero


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paQryVeh_Contrato @pmNumContrato INT
 AS 
 SELECT NumContrato,IdVehiculo,IdAdmon,IdTipCon,FecIngreso,FecRetiro,FecVigencia
,Observaciones,NumPagare,ArchPagare,FecUpdate,IdUsuario
 FROM Veh_Contrato
 WHERE NumContrato=@pmNumContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVeh_Contrato_Cr @pmNumContrato INT,@pmIdContrato VARCHAR(4),@pmIdAdmon VARCHAR(4)
AS
  SELECT  VC.NumContrato AS Ncontrato, VC.IdVehiculo AS PlacaVeh,NumVeh,VC.IdAdmon AS IdAdmn,TipoAdmon,VC.IdTipCon AS IdContrat,TipoContrato
,VC.FecIngreso AS FechaIngreso,VC.FecVigencia AS FechaVigencia, VC.FecRetiro AS FechaRetiro, Observaciones, FecUpdate, VC.IdUsuario AS IdUsuari
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,NumPagare,ArchPagare
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
  FROM  Veh_Contrato AS VC INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo
  INNER JOIN TiposAdm AS TA ON VC.IdAdmon=TA.IdAdmon
  INNER JOIN TiposCon AS TC ON VC.IdTipCon=TC.IdTipCon
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor   INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TCU ON  V.IdCom=TCU.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  WHERE  VC.IdTipCon LIKE ISNULL(@pmIdContrato,'%')  AND VC.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
  AND ( VC.NumContrato>=ISNULL(@pmNumContrato,0)  AND  VC.NumContrato<=ISNULL(@pmNumContrato,2147483647))
 ORDER BY VC.NumContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryVeh_Cupos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS 
 SELECT IdVehiculo,IdClase,ValorCupo,ValorSaldo,FecUpdate,IdUsuario
 FROM Veh_Cupos
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase=@pmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryVeh_CuposCte @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)=Null
AS
 IF @pmIdClase IS NULL
  SELECT  IdVehiculo,V.IdClase AS CodClase,ClaseCuenta, ValorCupo,ValorSaldo, FecUpdate, IdUsuario
  FROM  Veh_Cupos AS V INNER JOIN BusClaseCue ON V.IdClase=BusClaseCue.IdClase
  WHERE  IdVehiculo=@pmIdVehiculo  AND V.IdClase IN ('ADM','OTR')
  ORDER BY V.IdClase
 ELSE
  SELECT  IdVehiculo,V.IdClase AS CodClase,ClaseCuenta, ValorCupo,ValorSaldo, FecUpdate, IdUsuario
  FROM  Veh_Cupos AS V INNER JOIN BusClaseCue ON V.IdClase=BusClaseCue.IdClase
  WHERE  IdVehiculo=@pmIdVehiculo AND V.IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryVeh_Cupos_Cr @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4)
 AS
  SELECT  VC.IdVehiculo AS PlacaVeh,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor
,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,IdPropietario,VNP.RazonSocial AS Propietario,IdConductor,VNC.RazonSocial AS Conductor
,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro
,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
 ,VC.IdClase AS CodClase,ClaseCuenta, VC.ValorCupo AS VrCupo,ValorSaldo, FecUpdate, VC.IdUsuario AS IdUsuari
  FROM   Veh_Cupos AS VC INNER JOIN BusClaseCue AS C ON VC.IdClase=C.IdClase
  INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo 
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  WHERE  VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND VC.IdClase LIKE ISNULL(@pmIdClase,'%')
  ORDER BY  VC.IdVehiculo,ClaseCuenta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQryVeh_Saldos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmIdCia CHAR(2)
 AS 
 SELECT IdVehiculo,IdClase,IdCia,ValorSaldo
 FROM Veh_Saldos
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase LIKE ISNULL(@pmIdClase,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryVeh_SaldosCla @pmIdVehiculo VARCHAR(10)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
 SELECT S.IdVehiculo AS IdVehic, S.IdClase AS IdClas,SUM(ValorSaldo) AS SValSal 
 FROM Veh_Saldos AS S INNER JOIN Vehiculos AS V ON S.IdVehiculo=V.IdVehiculo
 WHERE S.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
 AND  S.IdClase LIKE ISNULL(@pmIdClase,'%')  AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 GROUP BY S.IdVehiculo, S.IdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryVeh_SaldosLta @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdCia CHAR(2)=Null
 AS
  SELECT VC.IdVehiculo AS PlacaVeh,NumVeh,VC.IdClase AS CodClase,ClaseCuenta,VC.IdCia AS CodCia, Compania,VC.ValorSaldo AS TotalSaldoCia,VU.ValorCupo AS ValorCupoClase,VU.ValorSaldo AS TotalSaldoClase
,IdPropietario,RazonSocial
 FROM  Veh_Saldos AS VC  INNER JOIN BusClaseCue AS C ON VC.IdClase=C.IdClase
 INNER JOIN Companias AS CI ON VC.IdCia=CI.IdCia  INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NP ON V.IdPropietario=NP.IdTercero
 LEFT JOIN Veh_Cupos AS VU ON VC.IdVehiculo=VU.IdVehiculo AND VC.IdClase=VU.IdClase
 WHERE  VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND VC.IdClase LIKE ISNULL(@pmIdClase, '%') AND VC.IdCia LIKE ISNULL(@pmIdCia,'%%')
 ORDER BY  VC.IdVehiculo,ClaseCuenta,VC.IdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryVeh_SaldosSal @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmIdCia CHAR(2)
AS
   SELECT   IdVehiculo, IdClase, IdCia, ValorSaldo   
   FROM  Veh_Saldos
   WHERE IdVehiculo=@pmIdVehiculo AND  IdClase like ISNULL(@pmIdClase,'%') AND IdCia like ISNULL(@pmIdCia,'%%')
  ORDER BY IdClase, IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQryVeh_Saldos_Crr @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
,@pmIdAdmon VARCHAR(4)=Null,@pmTipoAfil VARCHAR(10)=Null,@pmIdCia CHAR(2)=Null
 AS
  SELECT  VC.IdVehiculo AS PlacaVeh,NumVeh,VC.IdClase AS CodClase,ClaseCuenta,VC.IdCia AS CodCia, Compania,VC.ValorSaldo AS TotalSaldoCia,VU.ValorCupo AS ValorCupoClase,VU.ValorSaldo AS TotalSaldoClase
,Marca,NomColor,ClaseVeh,TipoVehiculo,Modelo,IdPropietario,NP.RazonSocial AS Propietario,IdConductor,NC.RazonSocial AS Conductor
,V.IdEstado AS IdEstad,Estado,VrSaldoAct,VrLmtCred,CentFinal,V.IdGrupo AS IdGrup,GrupoProp,V.IdAdmon AS IdAdm,TipoAdmon,NContrato,FecIngreso
,CV.Descripcion AS ClaseTarif
  FROM  Veh_Saldos AS VC INNER JOIN BusClaseCue AS C ON VC.IdClase=C.IdClase
  INNER JOIN Companias AS CI ON VC.IdCia=CI.IdCia  INNER JOIN Vehiculos AS V ON VC.IdVehiculo=V.IdVehiculo
  INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca   INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor
  INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
  INNER JOIN Terceros AS NP ON V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NC ON V.IdConductor=NC.IdTercero
  INNER JOIN EstadoVeh AS E ON V.IdEstado=E.IdEstado INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo
  INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN Veh_Cupos AS VU ON VC.IdVehiculo=VU.IdVehiculo AND VC.IdClase=VU.IdClase
  LEFT  JOIN TarifBuses AS CV ON V.CdTarifa=CV.IdTarifa 
  WHERE  VC.IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
  AND VC.IdClase LIKE ISNULL(@pmIdClase,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
  AND VC.IdCia LIKE ISNULL(@pmIdCia,'%%') 
  AND TipoAfil LIKE ISNULL(@pmTipoAfil,'%')
 ORDER BY  VC.IdVehiculo,ClaseCuenta,VC.IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculos @pmIdVehiculo VARCHAR(10)
 AS 
SELECT IdVehiculo,NumVeh,ClaseVeh,IdTipoVeh,IdMarca,IdLinea,IdColor,IdTipoMot,IdCrceria,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans,IdCat,CdCatv,ClaseMat,Cilind,CapTanq,IdCom,IdLub,IdTlla
,IdMarlla,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie,PasjerosSen,NitEmpresa
,IdPropietario,IdPoseedor,IdConductor,IdPpd,Adquisc,NitProv,FecCompra,VrComcial,VrAseg,VrAvaludo,VidaUtil,FecSalida,NContrato,IdAdmon,IdNiv,IdGrupo,CdGrupR,CdTarifa,FecIngreso,FecVigencia
,FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,TarjProp,FecTProp,VigTProp,CdLugTp,Ulttramite,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual
,Regtradora,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,Descripcion,Observacion,CdCenSer,CdLocal,Ubicacion,PathFoto,FecPriServ,FecUltServ,FecUltAcc,TieneAcc,FecPagImp,IdEstado,Inactivo
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,FechaAdd,FechaUpdate,IdUsuario
,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil,CdRutaHab
 FROM Vehiculos
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosCau @pmIdVehiculo VARCHAR(10),@pmIdEstado VARCHAR(4)
AS
 SELECT   IdVehiculo, NumVeh, CdTarifa,IdTipoVeh,PasjerosPie,PasjerosSen,IdPropietario,IdConductor, IdPpd,IdGrupo,TipoAfil, IdAdmon,NContrato
,FecIngreso, FecRetiro,FecVigencia, DocCompleta, ObligaTProd,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,IdEstado
 FROM  Vehiculos
 WHERE IdEstado<>@pmIdEstado AND  IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 ORDER BY IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosCon @pmIdVehiculo VARCHAR(10)
AS
 SELECT    IdVehiculo, NumVeh,IdGrupo,TipoAfil, IdAdmon,NContrato,FecIngreso,FecVigencia, FecRetiro
,FecSalida, VidaUtil,FecPagImp, DocCompleta,Observacion, IdEstado
 FROM     Vehiculos 
 WHERE   IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosFicha @pmIdVehiculo VARCHAR(4)
 AS 
 SELECT IdVehiculo,NumVeh,ClaseVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,V.IdTipoMot AS CdTipMotor,TipoMotor,V.IdCrceria AS CdCarr,TipoCar,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans
,V.IdCat AS CodCatg,Catpeaje,CdCatv,ClaseMat,Cilind,CapTanq,V.IdCom AS CdTipComb,TipoComb,V.IdLub AS CdLub,TipoLub,V.IdTlla AS CdTipLlantas,TipoLlanta
,IdMarlla,ML.Marca AS MarcaLlantas,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque
,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie, PasjerosSen,NitEmpresa,NE.RazonSocial AS Empresa,IdPropietario,NP.RazonSocial AS Propietario
,IdPoseedor,NT.RazonSocial AS Poseedor,V.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,V.IdPpd AS CdTipProp,TipoProp,VehPropio,Adquisc,NitProv,NPV.RazonSocial AS Proveedor
,FecCompra,VrComcial,VrAseg,VrAvaludo,VidaUtil,FecSalida,NContrato, V.IdAdmon AS CdAdmon,TipoAdmon, V.IdNiv AS CdNivel,NivelServicio, V.IdGrupo AS CdGrupo,GrupoProp,CdGrupR,CdTarifa
,TB.Descripcion AS ClaseTarifa, FecIngreso, FecVigencia, FecRetiro
,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,TarjProp,FecTProp,VigTProp,CdLugTp,LT.Localidad AS LugarTarjProp,Ulttramite,RespCivil,FecRCivil,VigRCivil
,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,V.Descripcion AS VehDescripcion,V.Observacion AS Observ,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion,LU.IdDep AS CodDpto,Departamento
,Ubicacion,FecPriServ,FecUltServ,Regtradora,CentInicial, CentFinal, VrLmtCred, VrSaldoAct, FecUltAcc, TieneAcc, FecPagImp,V.IdEstado AS CdEstado,Estado
,V.Inactivo AS Inactvo,V.IdUsuario AS CdUsuario,Usuario,V.FechaAdd AS Fec_Add,V.FechaUpdate AS Fec_Upd,EV.NColor AS NumColor,OutDemand
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil
--datos de la licencia
,Licencia,LugarLic,CatLicencia,VigLicencia,CdRutaHab
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposMot AS TM ON V.IdTipoMot=TM.IdTipoMot
 INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria INNER JOIN PeajesCat AS CP ON V.IdCat=CP.IdCat
 INNER JOIN TiposFuel AS TF ON V.IdCom=TF.IdCom INNER JOIN TiposLub AS TL ON V.IdLub=TL.IdLub
 INNER JOIN TiposLla AS TLL ON V.IdTlla=TLL.IdTlla INNER JOIN Marcas AS ML ON V.IdMarlla=ML.IdMarca
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero INNER JOIN TiposPpt AS TP ON V.IdPpd=TP.IdPpd
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
 INNER JOIN TiposNivs AS NV ON V.IdNiv=NV.IdNiv
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NPV ON V.NitProv=NPV.IdTercero
 LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero LEFT JOIN Localidades AS LT ON V.CdLugTp=LT.IdLocal
 LEFT JOIN CentrosServ AS CS ON V.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON V.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 LEFT JOIN TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa LEFT JOIN  TercCndtores AS CT ON V.IdConductor=CT.IdConductor
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 WHERE  IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosLta @pmClaseVeh VARCHAR(10)=Null,@pmIdTipoVeh VARCHAR(4)=Null,@pmIdMarca VARCHAR(4)=Null
,@pmIdTipoMot VARCHAR(4)=Null,@pmIdCrceria VARCHAR(4)=Null,@pmModelo VARCHAR(4)=Null,@pmConfig VARCHAR(5)=Null,@pmIdCat VARCHAR(4)=Null
,@pmClaseMat VARCHAR(10)=Null,@pmIdCom VARCHAR(4)=Null,@pmIdLub VARCHAR(4)=Null,@pmIdTlla VARCHAR(4)=Null,@pmIdMarlla VARCHAR(4)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPpd VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmFecComIni SMALLDATETIME=Null,@pmFecComFin SMALLDATETIME=Null
,@pmIdAdmon VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS 
 SELECT IdVehiculo,NumVeh,ClaseVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,V.IdTipoMot AS CdTipMotor,TipoMotor,V.IdCrceria AS CdCarr,TipoCar,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans
,V.IdCat AS CodCatg,Catpeaje,CdCatv,ClaseMat,Cilind,CapTanq,V.IdCom AS CdTipComb,TipoComb,V.IdLub AS CdLub,TipoLub,V.IdTlla AS CdTipLlantas,TipoLlanta
,IdMarlla,ML.Marca AS MarcaLlantas,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque
,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie, PasjerosSen,NitEmpresa,NE.RazonSocial AS Empresa,IdPropietario,NP.RazonSocial AS Propietario
,IdPoseedor,NT.RazonSocial AS Poseedor,IdConductor,NC.RazonSocial AS Conductor,V.IdPpd AS CdTipProp,TipoProp,VehPropio,Adquisc,NitProv,NPV.RazonSocial AS Proveedor
,FecCompra,VrComcial,VrAseg,VrAvaludo,VidaUtil,FecSalida,NContrato, V.IdAdmon AS CdAdmon,TipoAdmon, V.IdNiv AS CdNivel,NivelServicio, V.IdGrupo AS CdGrupo,GrupoProp,CdGrupR,CdTarifa, FecIngreso, FecVigencia, FecRetiro
,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,TarjProp,FecTProp,VigTProp,CdLugTp,LT.Localidad AS LugarTarjProp,Ulttramite,RespCivil,FecRCivil,VigRCivil
,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,Descripcion,V.Observacion AS Observ,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion,LU.IdDep AS CodDpto,Departamento
,Ubicacion,PathFoto,FecPriServ,FecUltServ,Regtradora,CentInicial, CentFinal, VrLmtCred, VrSaldoAct, FecUltAcc, TieneAcc, FecPagImp,V.IdEstado AS CdEstado,Estado
,V.Inactivo AS Inactvo,V.IdUsuario AS CdUsuario,Usuario,V.FechaAdd AS Fec_Add,V.FechaUpdate AS Fec_Upd,EV.NColor AS NumColor,OutDemand
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,ValorCupo,ObligaTProd,GarantiaAcc,DocCompleta,CertMovilizacion,FecCertMovil,VigCertMovil,CdRutaHab
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposMot AS TM ON V.IdTipoMot=TM.IdTipoMot
 INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria INNER JOIN PeajesCat AS CP ON V.IdCat=CP.IdCat
 INNER JOIN TiposFuel AS TF ON V.IdCom=TF.IdCom INNER JOIN TiposLub AS TL ON V.IdLub=TL.IdLub
 INNER JOIN TiposLla AS TLL ON V.IdTlla=TLL.IdTlla INNER JOIN Marcas AS ML ON V.IdMarlla=ML.IdMarca
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero INNER JOIN TiposPpt AS TP ON V.IdPpd=TP.IdPpd
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
 INNER JOIN TiposNivs AS NV ON V.IdNiv=NV.IdNiv
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NPV ON V.NitProv=NPV.IdTercero
 LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero LEFT JOIN Localidades AS LT ON V.CdLugTp=LT.IdLocal
 LEFT JOIN CentrosServ AS CS ON V.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON V.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 WHERE ClaseVeh LIKE ISNULL(@pmClaseVeh,'%') AND V.IdTipoVeh LIKE ISNULL(@pmIdTipoVeh,'%')
 AND V.IdMarca LIKE ISNULL(@pmIdMarca,'%') AND V.IdTipoMot LIKE ISNULL(@pmIdTipoMot,'%')
 AND V.IdCrceria LIKE ISNULL(@pmIdCrceria,'%')  AND Modelo LIKE ISNULL(@pmModelo,'%')
 AND Config LIKE ISNULL(@pmConfig,'%') AND V.IdCat LIKE ISNULL(@pmIdCat ,'%')
 AND ClaseMat  LIKE ISNULL(@pmClaseMat,'%') AND V.IdCom LIKE ISNULL(@pmIdCom,'%')
 AND V.IdLub LIKE ISNULL(@pmIdLub,'%') AND V.IdTlla LIKE ISNULL(@pmIdTlla,'%')
 AND IdMarlla LIKE ISNULL(@pmIdMarlla,'%')  AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')  AND IdConductor LIKE ISNULL(@pmIdConductor,'%') 
 AND V.IdPpd LIKE ISNULL(@pmIdPpd,'%') AND V.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND (V.Inactivo=ISNULL(@pmInactivo,0) or V.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecCompra>=ISNULL(@pmFecComIni,CAST('19100101' AS SMALLDATETIME)) AND FecCompra<=ISNULL(@pmFecComFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosMay  @pmIdTipoVeh VARCHAR(4)=Null,@pmIdMarca VARCHAR(4)=Null,@pmModelo VARCHAR(4)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null
,@pmFecComIni SMALLDATETIME=Null,@pmFecComFin SMALLDATETIME=Null
 AS 
 SELECT IdVehiculo,NumVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,Modelo,VehArtic,NumMotor,SerieChasis,NumSerie,ClaseMat,CdRemque
,Comptmtos,CapComp,NitEmpresa,NE.RazonSocial AS Empresa,IdPropietario,NP.RazonSocial AS Propietario
,IdConductor,NC.RazonSocial AS Conductor,FecIngreso, FecVigencia, FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,TarjProp,FecTProp,VigTProp,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal
,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,Descripcion,V.Observacion AS Observ
,FecPriServ,FecUltServ,TipoAfil,V.IdEstado AS CdEstado,Estado,V.Inactivo AS Inactvo,V.IdUsuario AS CdUsuario,Usuario
,V.FechaAdd AS Fec_Add,V.FechaUpdate AS Fec_Upd,EV.NColor AS NumColor,OutDemand
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero 
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero 
 LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero 
 WHERE V.IdTipoVeh LIKE ISNULL(@pmIdTipoVeh,'%')  AND V.IdMarca LIKE ISNULL(@pmIdMarca,'%')
 AND Modelo LIKE ISNULL(@pmModelo,'%')   AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND  IdConductor LIKE ISNULL(@pmIdConductor,'%')  AND  V.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND (V.Inactivo=ISNULL(@pmInactivo,0) or V.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecCompra>=ISNULL(@pmFecComIni,CAST('19100101' AS SMALLDATETIME)) AND FecCompra<=ISNULL(@pmFecComFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosMayPol @pmFecha SMALLDATETIME,@pmIdPropietario VARCHAR(16)=Null,@pmInactivo BIT=Null
 AS 
 SELECT V.IdVehiculo AS PlacaVeh,NumVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,Modelo,VehArtic,NumMotor,SerieChasis,NumSerie,ClaseMat,CdRemque
,Comptmtos,CapComp,NitEmpresa,NE.RazonSocial AS Empresa,IdPropietario,NP.RazonSocial AS Propietario
,V.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,FecIngreso, V.FecVigencia AS FecVigContrato, FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora
,TarjProp,FecTProp,VigTProp,RespCivil,FecRCivil,VigRCivil,RegNalCarga,FecRegNal,VigRegNal
,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,Descripcion,V.Observacion AS Observ
,FecPriServ,FecUltServ,TipoAfil,V.IdEstado AS CdEstado,Estado,V.Inactivo AS Inactvo,V.IdUsuario AS CdUsuario,Usuario
,V.FechaAdd AS Fec_Add,EV.NColor AS NumColor,OutDemand
--documentos del conductor
,Licencia,LugarLic,CatLicencia,VigLicencia,CertJudicial,FecJudicial,VigJudicial
,NumTarjeta,EdicTarjeta,FecTarjeta,VigTarjeta,PasSeguridad,FecPasSeg,VigPasSeg
--otras polizas
,VP.IdClase AS CodClase,ClasePoliza,Numero,FecEmision,VP.FecVigencia AS FecVencePoliza,NitCompania
,TCP.RazonSocial AS NombreCia,Valor,Comntarios
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero 
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero  LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero 
 LEFT JOIN  TercCndtores AS CT ON V.IdConductor=CT.IdConductor   LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar 
 LEFT JOIN VehPolizas AS VP ON V.IdVehiculo=VP.IdVehiculo LEFT JOIN ClasePol AS CLP ON VP.IdClase=CLP.IdClase
 LEFT JOIN Terceros AS TCP ON VP.NitCompania=TCP.IdTercero
 WHERE V.IdVehiculo<>'0' AND  (VigSoat<=@pmFecha 
  OR ISNULL(VigTProp,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigRCivil,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigRegNal,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigTecMec,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigCertGas,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigTarjOper,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
--DEL CONDUCTOR
  OR ISNULL(VigLicencia,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigJudicial,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
  OR ISNULL(VigTarjeta,CAST('20781230' AS SMALLDATETIME))<=@pmFecha
--OTRAS POLIZAS
  OR ISNULL(VP.FecVigencia,CAST('20781230' AS SMALLDATETIME))<=@pmFecha  ) 
  AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND (V.Inactivo=ISNULL(@pmInactivo,0) or V.Inactivo=ISNULL(@pmInactivo,1))
ORDER BY  V.IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosMot  @pmNumMotor VARCHAR(30),@pmInactivo BIT=Null
 AS 
 SELECT IdVehiculo,NumVeh,ClaseVeh,NumMotor,Inactivo,IdEstado
 FROM Vehiculos
 WHERE NumMotor=@pmNumMotor
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosNom @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT IdVehiculo,NumVeh,ClaseVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,V.IdCrceria AS CdCarr,TipoCar,Modelo,FecRep,Config,ClaseMat,V.IdCom AS CdTipComb,TipoComb
,NumMotor,SerieChasis,NumSerie,CdRemque,CarrCapac,UndCapc,Comptmtos,CapComp
,IdPropietario,NP.RazonSocial AS Propietario,IdPoseedor,NT.RazonSocial AS Poseedor,IdConductor,NC.RazonSocial AS Conductor
,V.IdPpd AS CdTipProp,TipoProp,VehPropio,NContrato,Descripcion,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion
,LU.IdDep AS CodDpto,Departamento,Ubicacion,V.IdEstado AS CdEstado,Estado,V.Inactivo AS Inactvo,EV.NColor AS NumColor,OutDemand
, V.IdNiv AS CdNivel,NivelServicio,TipoAfil
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor  INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria 
 INNER JOIN TiposFuel AS TF ON V.IdCom=TF.IdCom 
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero INNER JOIN TiposPpt AS TP ON V.IdPpd=TP.IdPpd
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado 
 LEFT JOIN CentrosServ AS CS ON V.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON V.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 LEFT JOIN TiposNivs AS NS ON V.IdNiv=NS.IdNiv
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosNum @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmInactivo BIT=Null
 AS 
 SELECT Vehiculos.*
 FROM Vehiculos
 WHERE (IdVehiculo=@pmIdVehiculo OR NumVeh=@pmNumVeh)
 AND (Inactivo=ISNULL(@pmInactivo,0) or Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosPol @pmClaseVeh VARCHAR(10)=Null,@pmIdTipoVeh VARCHAR(4)=Null,@pmIdMarca VARCHAR(4)=Null
,@pmIdTipoMot VARCHAR(4)=Null,@pmIdCrceria VARCHAR(4)=Null,@pmModelo VARCHAR(4)=Null,@pmConfig VARCHAR(5)=Null,@pmIdCat VARCHAR(4)=Null
,@pmClaseMat VARCHAR(10)=Null,@pmIdCom VARCHAR(4)=Null,@pmIdLub VARCHAR(4)=Null,@pmIdTlla VARCHAR(4)=Null,@pmIdMarlla VARCHAR(4)=Null
,@pmIdPropietario VARCHAR(16)=Null,@pmIdPoseedor VARCHAR(16)=Null,@pmIdConductor VARCHAR(16)=Null,@pmIdPpd VARCHAR(4)=Null
,@pmIdEstado VARCHAR(4)=Null,@pmInactivo BIT=Null,@pmFecComIni SMALLDATETIME=Null,@pmFecComFin SMALLDATETIME=Null
,@pmIdAdmon VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=Null
 AS 
 SELECT V.IdVehiculo AS PlacaVeh,NumVeh,ClaseVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,V.IdTipoMot AS CdTipMotor,TipoMotor,V.IdCrceria AS CdCarr,TipoCar,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans
,V.IdCat AS CodCatg,Catpeaje,CdCatv,ClaseMat,Cilind,CapTanq,V.IdCom AS CdTipComb,TipoComb,V.IdLub AS CdLub,TipoLub,V.IdTlla AS CdTipLlantas,TipoLlanta
,IdMarlla,ML.Marca AS MarcaLlantas,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque
,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie, PasjerosSen,NitEmpresa,NE.RazonSocial AS Empresa,IdPropietario,NP.RazonSocial AS Propietario
,IdPoseedor,NT.RazonSocial AS Poseedor,IdConductor,NC.RazonSocial AS Conductor,V.IdPpd AS CdTipProp,TipoProp,VehPropio,Adquisc,NitProv,NPV.RazonSocial AS Proveedor
,FecCompra,VrComcial,VrAseg,VrAvaludo,VidaUtil,FecSalida,NContrato, V.IdAdmon AS CdAdmon,TipoAdmon, V.IdNiv AS CdNivel,NivelServicio, V.IdGrupo AS CdGrupo,GrupoProp,CdGrupR,CdTarifa, FecIngreso
,V.FecVigencia AS FechVigCont, FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,TarjProp,FecTProp,VigTProp,CdLugTp,LT.Localidad AS LugarTarjProp,Ulttramite,RespCivil,FecRCivil,VigRCivil
,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,Descripcion,V.Observacion AS Observ,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion,LU.IdDep AS CodDpto,Departamento
,Ubicacion,PathFoto,FecPriServ,FecUltServ,Regtradora,CentInicial, CentFinal, VrLmtCred, VrSaldoAct, FecUltAcc, TieneAcc, FecPagImp,V.IdEstado AS CdEstado,Estado
,V.Inactivo AS Inactvo,V.IdUsuario AS CdUsuario,Usuario,V.FechaAdd AS Fec_Add,V.FechaUpdate AS Fec_Upd,EV.NColor AS NumColor,OutDemand
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper,CdRutaHab
--información de polizas
,VP.IdClase AS CodClase,ClasePoliza,Numero,FecEmision,VP.FecVigencia AS FecVencePoliza,NitCompania,TCP.RazonSocial AS NombreCia,Valor,Comntarios,DiasAviso
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposMot AS TM ON V.IdTipoMot=TM.IdTipoMot
 INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria INNER JOIN PeajesCat AS CP ON V.IdCat=CP.IdCat
 INNER JOIN TiposFuel AS TF ON V.IdCom=TF.IdCom INNER JOIN TiposLub AS TL ON V.IdLub=TL.IdLub
 INNER JOIN TiposLla AS TLL ON V.IdTlla=TLL.IdTlla INNER JOIN Marcas AS ML ON V.IdMarlla=ML.IdMarca
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero INNER JOIN TiposPpt AS TP ON V.IdPpd=TP.IdPpd
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
 INNER JOIN TiposNivs AS NV ON V.IdNiv=NV.IdNiv
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NPV ON V.NitProv=NPV.IdTercero
 LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero LEFT JOIN Localidades AS LT ON V.CdLugTp=LT.IdLocal
 LEFT JOIN CentrosServ AS CS ON V.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON V.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 LEFT JOIN VehPolizas AS VP ON V.IdVehiculo=VP.IdVehiculo LEFT JOIN ClasePol AS CLP ON VP.IdClase=CLP.IdClase
 LEFT JOIN Terceros AS TCP ON VP.NitCompania=TCP.IdTercero
 WHERE ClaseVeh LIKE ISNULL(@pmClaseVeh,'%') AND V.IdTipoVeh LIKE ISNULL(@pmIdTipoVeh,'%')
 AND V.IdMarca LIKE ISNULL(@pmIdMarca,'%') AND V.IdTipoMot LIKE ISNULL(@pmIdTipoMot,'%')
 AND V.IdCrceria LIKE ISNULL(@pmIdCrceria,'%')  AND Modelo LIKE ISNULL(@pmModelo,'%')
 AND Config LIKE ISNULL(@pmConfig,'%') AND V.IdCat LIKE ISNULL(@pmIdCat ,'%')
 AND ClaseMat  LIKE ISNULL(@pmClaseMat,'%') AND V.IdCom LIKE ISNULL(@pmIdCom,'%')
 AND V.IdLub LIKE ISNULL(@pmIdLub,'%') AND V.IdTlla LIKE ISNULL(@pmIdTlla,'%')
 AND IdMarlla LIKE ISNULL(@pmIdMarlla,'%')  AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 AND IdPoseedor LIKE ISNULL(@pmIdPoseedor,'%')  AND IdConductor LIKE ISNULL(@pmIdConductor,'%') 
 AND V.IdPpd LIKE ISNULL(@pmIdPpd,'%') AND V.IdEstado LIKE ISNULL(@pmIdEstado,'%')
 AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%') AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND (V.Inactivo=ISNULL(@pmInactivo,0) or V.Inactivo=ISNULL(@pmInactivo,1))
 AND (FecCompra>=ISNULL(@pmFecComIni,CAST('19100101' AS SMALLDATETIME)) AND FecCompra<=ISNULL(@pmFecComFin,CAST('20781230' AS SMALLDATETIME)))
ORDER BY  V.IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosPos @pmIdPoseedor VARCHAR(16)
 AS 
SELECT IdVehiculo,NumVeh,ClaseVeh,IdPropietario,IdPoseedor,IdConductor,Descripcion
,IdEstado,Inactivo
 FROM Vehiculos
 WHERE Inactivo=0 AND  IdPoseedor=@pmIdPoseedor
 ORDER BY IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryVehiculosPpt @pmIdPropietario VARCHAR(16) 
AS
 SELECT  IdVehiculo, NumVeh,IdPropietario,IdConductor,IdPoseedor,IdConductor,IdGrupo
,TipoAfil,IdAdmon,VrLmtCred,VrSaldoAct, IdEstado,Inactivo
 FROM  Vehiculos 
 WHERE  IdPropietario=@pmIdPropietario AND Inactivo=0
 ORDER BY IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryVehiculosPpt1 @pmIdPropietario VARCHAR(16) =Null,@pmIdVehiculo VARCHAR(10)=Null
AS
 SELECT  IdVehiculo, NumVeh,IdPropietario,IdConductor,IdGrupo,TipoAfil,IdAdmon,VrLmtCred,VrSaldoAct, IdEstado,Inactivo
 FROM  Vehiculos 
 WHERE IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')  AND  IdPropietario LIKE ISNULL(@pmIdPropietario,'%')
 ORDER BY IdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVehiculosPza @pmIdVehiculo VARCHAR(10)
 AS 
 SELECT V.IdVehiculo AS PlacaVeh,NumVeh,ClaseVeh,V.IdTipoVeh AS CdTipo,TipoVehiculo,V.IdMarca AS CdMarca,M.Marca AS MarcaVeh,V.IdLinea AS CdLinea,LineaVeh
,V.IdColor AS CdColor,NomColor,V.IdTipoMot AS CdTipMotor,TipoMotor,V.IdCrceria AS CdCarr,TipoCar,Modelo,FecRep,Config,VehArtic,NumLlan,NumLlans
,V.IdCat AS CodCatg,Catpeaje,CdCatv,ClaseMat,Cilind,CapTanq,V.IdCom AS CdTipComb,TipoComb,V.IdLub AS CdLub,TipoLub,V.IdTlla AS CdTipLlantas,TipoLlanta
,IdMarlla,ML.Marca AS MarcaLlantas,PesoVacio,PesoMax,NumMotor,SerieChasis,NumSerie,CdRemque
,Longitud,CarrAlto,CarrAncho,CarrLargo,CarrCapac,UndCapc,Comptmtos,CapComp,PasjerosPie, PasjerosSen,NitEmpresa,NE.RazonSocial AS Empresa,IdPropietario,NP.RazonSocial AS Propietario
,IdPoseedor,NT.RazonSocial AS Poseedor,V.IdConductor AS CedConductor,NC.RazonSocial AS Conductor,V.IdPpd AS CdTipProp,TipoProp,VehPropio,Adquisc,NitProv,NPV.RazonSocial AS Proveedor
,FecCompra,VrComcial,VrAseg,VrAvaludo,VidaUtil,FecSalida,NContrato, V.IdAdmon AS CdAdmon,TipoAdmon, V.IdNiv AS CdNivel,NivelServicio, V.IdGrupo AS CdGrupo,GrupoProp,CdGrupR,CdTarifa, FecIngreso
,V.FecVigencia AS FechVigCont, FecRetiro,NumSoat,FecSoat,VigSoat,NitEmpSoat,NS.RazonSocial AS CiaAsegurdora,TarjProp,FecTProp,VigTProp,CdLugTp,LT.Localidad AS LugarTarjProp,Ulttramite,RespCivil,FecRCivil,VigRCivil
,RegNalCarga,FecRegNal,VigRegNal,KmInicial,KmActual,Km2Actual,Descripcion,V.Observacion AS Observ,CdCenSer,CentroServ,CdLocal,LU.Localidad AS CiuUbicacion,LU.IdDep AS CodDpto,Departamento
,Ubicacion,FecPriServ,FecUltServ,Regtradora,CentInicial, CentFinal, VrLmtCred, VrSaldoAct, FecUltAcc, TieneAcc, FecPagImp,V.IdEstado AS CdEstado,Estado
,V.Inactivo AS Inactvo,V.IdUsuario AS CdUsuario,Usuario,V.FechaAdd AS Fec_Add,V.FechaUpdate AS Fec_Upd,EV.NColor AS NumColor,OutDemand
,TipoAfil,RevTecMec,FecTecMec,VigTecMec,CertGases,FecCertGas,VigCertGas,TarjOper,FecTarjOper,VigTarjOper
,Licencia,LugarLic,CatLicencia,VigLicencia
--información de polizas
,VP.IdClase AS CodClase,ClasePoliza,Numero,FecEmision,VP.FecVigencia AS FecVencePoliza,NitCompania,TCP.RazonSocial AS NombreCia,Valor,Comntarios,DiasAviso
 FROM Vehiculos  AS V INNER JOIN TiposVeh AS TV ON V.IdTipoVeh=TV.IdTipoVeh
 INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca INNER JOIN MarcasLin AS L ON V.IdLinea=L.IdLinea
 INNER JOIN TiposCol AS CL ON V.IdColor=CL.IdColor INNER JOIN TiposMot AS TM ON V.IdTipoMot=TM.IdTipoMot
 INNER JOIN TiposCar AS TC ON V.IdCrceria=TC.IdCrceria INNER JOIN PeajesCat AS CP ON V.IdCat=CP.IdCat
 INNER JOIN TiposFuel AS TF ON V.IdCom=TF.IdCom INNER JOIN TiposLub AS TL ON V.IdLub=TL.IdLub
 INNER JOIN TiposLla AS TLL ON V.IdTlla=TLL.IdTlla INNER JOIN Marcas AS ML ON V.IdMarlla=ML.IdMarca
 INNER JOIN Terceros AS NP ON  V.IdPropietario=NP.IdTercero  INNER JOIN Terceros AS NT ON  V.IdPoseedor=NT.IdTercero
 INNER JOIN Terceros AS NC ON  V.IdConductor=NC.IdTercero INNER JOIN TiposPpt AS TP ON V.IdPpd=TP.IdPpd
 INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado  INNER JOIN adm_Usuarios AS U ON V.IdUsuario=U.IdUsuario
 INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
 INNER JOIN TiposNivs AS NV ON V.IdNiv=NV.IdNiv
 LEFT JOIN Terceros AS NE ON  V.NitEmpresa=NE.IdTercero LEFT JOIN Terceros AS NPV ON V.NitProv=NPV.IdTercero
 LEFT JOIN Terceros AS NS ON V.NitEmpSoat =NS.IdTercero LEFT JOIN Localidades AS LT ON V.CdLugTp=LT.IdLocal
 LEFT JOIN CentrosServ AS CS ON V.CdCenSer =CS.IdCenSer 
 LEFT JOIN Localidades AS LU ON V.CdLocal=LU.IdLocal LEFT JOIN Departamentos AS DU ON LU.IdDep=DU.IdDep
 LEFT JOIN VehPolizas AS VP ON V.IdVehiculo=VP.IdVehiculo LEFT JOIN ClasePol AS CLP ON VP.IdClase=CLP.IdClase
 LEFT JOIN Terceros AS TCP ON VP.NitCompania=TCP.IdTercero
 LEFT JOIN  TercCndtores AS CT ON V.IdConductor=CT.IdConductor   LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar 
 WHERE V.IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQryVehiculosSal @pmIdVehiculo VARCHAR(10)
AS
 SELECT     IdVehiculo, NumVeh,VrSaldoAct,VrLmtCred,IdPropietario 
 FROM  Vehiculos WHERE   IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryVehiculosSalCue @pmSaldoActual MONEY,@pmIdAdmon VARCHAR(4)
AS
 SELECT IdPropietario, SUM(VrSaldoAct) AS StotSal
 FROM  Vehiculos
 WHERE IdAdmon<>@pmIdAdmon AND TipoAfil<>'PROPIO'
 GROUP BY IdPropietario
 HAVING  SUM(VrSaldoAct)<=@pmSaldoActual

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQryVehiculosSalPro @pmIdPropietario VARCHAR(16),@pmIdAdmon VARCHAR(4)
AS
 SELECT IdPropietario,IdVehiculo,NumVeh,IdAdmon,TipoAfil,IdConductor,RazonSocial,IdGrupo,VrSaldoAct
 FROM  Vehiculos INNER JOIN Terceros ON Vehiculos.IdConductor=Terceros.IdTercero
 WHERE IdPropietario=@pmIdPropietario AND IdAdmon<>@pmIdAdmon AND TipoAfil<>'PROPIO'
 ORDER  BY IdPropietario,IdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonos @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipRec,Recibo,IdCia,Item,Fecha,TipDoc,Factura,IdCiaFac,ItemFac,VrAbono,VrDescto,VrBaseCms,IdCliente,IdAgencia
,IdVend,Comision,Detalle,FecPago,TipoAplica,TimeSys
 FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
  AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosAbo @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2)
,@pmItemFac INT,@pmTipoAplicaExc VARCHAR(10)=Null
AS
 SELECT SUM(VrAbono) AS StotAbo
 FROM Trn_VencAbonos 
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCiaFac=@pmIdCiaFac
 AND (ItemFac>=ISNULL(@pmItemFac,0)  AND ItemFac<=ISNULL(@pmItemFac,2147483647))
 AND TipoAplica<>@pmTipoAplicaExc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosDap @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,Factura,IdCiaFac,ItemFac,SUM(VrAbono) AS STotAbo
 FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 GROUP BY TipDoc,Factura,IdCiaFac,ItemFac

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosFec @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2)
,@pmItemFac INT
 AS 
 SELECT TipRec,Recibo,IdCia,Item,Fecha,ItemFac,VrAbono,VrDescto,IdCliente,IdAgencia,FecPago,TipoAplica
 FROM Trn_VencAbonos
 WHERE TipRec IN ('REC','NCR') AND TipDoc=@pmTipDoc  AND Factura=@pmFactura AND IdCiaFac=@pmIdCiaFac
 AND (ItemFac>=ISNULL(@pmItemFac,0)  AND ItemFac<=ISNULL(@pmItemFac,2147483647))
 ORDER BY FecPago
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosItm @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT MAX(Item) AS MXITM  
 FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosLrc @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME,@pmFecActual SMALLDATETIME,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null,@pmFechaFacIni SMALLDATETIME=Null,@pmFechaFacFin SMALLDATETIME=Null,@pmIdZona VARCHAR(4)=Null
,@pmIdSzona VARCHAR(4)=Null,@pmIdGrupo VARCHAR(4)=NULL
 AS 
 SELECT  VA.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,VA.IdAgencia AS CdAgencia,A.Agencia AS NomAgencia,IdCiaFac,CI.Compania AS FactCompania,VA.TipDoc AS TipFact,TD.TipoDoc AS TipoFactura,VA.Factura AS NumFactura,VA.ItemFac AS NumVence
,V.Fecha AS FechaFact,FechaVence,FecPlazoCal,DATEDIFF(day,ISNULL(FecPlazoCal,FechaVence),@pmFecActual) AS DiasMora,VrFactura,VrAbonado,VrFactura-VrAbonado AS SaldoFactura
--recibo
,TipRec,TR.TipoDoc AS TipoRecibo,VA.Recibo AS NumRecibo,VA.IdCia AS RecIdCia,CR.Compania AS RecCompania,VA.Item AS RecItem,VA.FecPago AS FechaPago
,DATEDIFF(day,ISNULL(FecPlazoCal,V.FechaVence),VA.FecPago) AS DiasPago,VrAbono
,CASE WHEN VrFactura<>0 AND VrAbono>0 THEN (DATEDIFF(day,V.Fecha,VA.FecPago) *VrAbono)/VrFactura ELSE 0 END  AS PromDias,VA.Detalle AS RecDetalle,TipoAplica
,VA.IdVend AS RecCdVend,VR.RazonSocial AS RecVendedor,VrBaseCms,VA.Comision AS TarifComsCobro,VrDescto,VA.Fecha AS RecFecha
,V.IdAgencia AS FactIdAgencia,AF.Agencia AS FactAgencia,V.IdVend AS FactIdVend,VN.RazonSocial AS FactVendedor,V.Comision AS FactComision,V.Referencia AS FactReferencia,V.Detalle AS FactDetalle,TipRef,DocRef,IdCiaRef
,V.IdConcepto AS CdConcepto,Concepto
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.Direccion AS TercDireccion,T.IdLocal AS TercCdCiudad
,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera
,A.CodAgencia AS Cod_agencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,A.CiaCont AS AgeCiaCont,A.CdBandera AS AgeIdBandera,TBA.TipoBandera AS AgeBandera
 FROM  Trn_VencAbonos AS VA INNER JOIN Trn_Vencimientos AS V  ON  VA.TipDoc=V.TipDoc AND VA.Factura=V.Factura AND VA.IdCiaFac=V.IdCia AND VA.ItemFac=V.Item
 INNER JOIN Companias AS CR ON VA.IdCia=CR.IdCia  INNER JOIN  Companias AS CI ON VA.IdCiaFac=CI.IdCia
 INNER JOIN Sys_TiposDoc AS TD ON VA.TipDoc=TD.IdDoc
 INNER JOIN Terceros AS T ON VA.IdCliente=T.IdTercero INNER JOIN Agencias AS A ON VA.IdAgencia=A.IdAgencia
 INNER JOIN Terceros AS VR ON VA.IdVend=VR.IdTercero INNER JOIN Agencias AS AF ON V.IdAgencia=AF.IdAgencia 
 INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliente AS CLI ON VA.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 LEFT JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto LEFT JOIN Sys_TiposDoc AS TR ON VA.TipRec=TR.IdDoc
 LEFT JOIN  TiposBan AS TB ON CLI.CdBandera=TB.IdBandera LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera
 WHERE  FecPago BETWEEN @pmFechaIni AND @pmFechaFin  AND  VA.IdCiaFac LIKE ISNULL(@pmIdCia,'%%')
 AND VA.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND V.IdVend LIKE ISNULL(@pmIdVend,'%')  AND CLI.IdSzona LIKE ISNULL(@pmIdSzona,'%')
 AND  SZ.IdZona LIKE ISNULL(@pmIdZona,'%') AND CLI.IdGrupo LIKE ISNULL(@pmIdGrupo,'%')
 AND (V.Fecha>=ISNULL(@pmFechaFacIni,CAST('19100101' AS SMALLDATETIME)) AND V.Fecha<=ISNULL(@pmFechaFacFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial,VA.FecPago,VA.Factura,VA.ItemFac
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosLta @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 SELECT Item,TipDoc,Factura,IdCiaFac,ItemFac,VrAbono,VrDescto,VrBaseCms,Detalle,Fecha
,IdCliente,T.RazonSocial AS Cliente,IdAgencia,IdVend,V.RazonSocial AS Vendedor,Comision,FecPago,TipoAplica,TimeSys
 FROM Trn_VencAbonos AS A INNER JOIN Terceros AS T ON A.IdCliente=T.IdTercero
 INNER JOIN  Terceros AS V ON A.IdVend=V.IdTercero
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosSaNit @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)=Null
 AS 
 SELECT SUM(VrAbono)  AS STOTABO
 FROM Trn_VencAbonos
 WHERE IdCliente=@pmIdCliente AND TipoAplica='ANTICIPO' AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosSaf @pmTipRec VARCHAR(3),@pmRecibo INT
,@pmIdCia CHAR(2),@pmTipoAplica VARCHAR(10)=Null
 AS 
 SELECT TipRec,Recibo,IdCia,Item,Fecha,TipDoc,Factura,IdCiaFac,ItemFac,VrAbono,VrDescto,VrBaseCms,IdCliente,IdAgencia
,IdVend,Comision,Detalle,FecPago,TipoAplica,TimeSys
 FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 AND TipoAplica LIKE ISNULL(@pmTipoAplica,'%')
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosTnit  @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)=Null
 AS 
 SELECT SUM(VrAbono) AS STOTABO,SUM(VrDescto) AS STOTDCT,SUM(VrBaseCms) AS STOTCMS
 FROM Trn_VencAbonos
 WHERE IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonosTsaf @pmTipRec VARCHAR(3),@pmRecibo INT
,@pmIdCia CHAR(2),@pmTipoAplica VARCHAR(10)=Null
 AS 
 SELECT SUM(VrAbono) AS STOTABO,SUM(VrDescto) AS STOTDCT,SUM(VrBaseCms) AS STOTBAS
 FROM Trn_VencAbonos
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia 
 AND TipoAplica LIKE ISNULL(@pmTipoAplica,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonos_Cr @pmTipRec VARCHAR(3),@pmReciboIni INT,@pmReciboFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipRec,Recibo,VA.IdCia AS CdCia,CN.Compania AS RecCompania,VA.Item AS ItemRec,VA.Fecha AS FechaRec
,VA.TipDoc AS TipoFac,VA.Factura AS NumFactura,IdCiaFac,CF.Compania AS FacCompania,ItemFac,VrAbono,VrDescto
,VrBaseCms,VA.IdCliente AS NitCliente,T.RazonSocial AS NombCliente,VA.IdAgencia AS IdAgncia,Agencia,CodAgencia
,VA.IdVend AS NitVendedor,VN.RazonSocial AS NomVendedor,VA.Comision AS ComisionCobro,VA.Detalle AS DetalleAbono
,FecPago,TipoAplica,TimeSys
--datos del vencimiento
,V.Fecha AS FechaFac,FechaVence,VrFactura,V.VrAbonado AS TotalAbonos,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,V.IdConcepto AS CdConcepto,Concepto
,V.Referencia AS Referncia,V.Detalle AS DetalleVecmto,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora
 FROM Trn_VencAbonos AS VA INNER JOIN Trn_Vencimientos AS V ON VA.TipDoc=V.TipDoc AND VA.Factura=V.Factura
 AND VA.IdCiaFac=V.IdCia AND VA.ItemFac=V.Item
 INNER JOIN Terceros AS T ON VA.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON VA.IdVend=VN.IdTercero
 INNER JOIN Companias AS CN ON VA.IdCia=CN.IdCia INNER JOIN Companias AS CF ON VA.IdCiaFac=CF.IdCia
 LEFT JOIN Agencias AS A ON VA.IdAgencia=A.IdAgencia  LEFT JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto
 WHERE TipRec=@pmTipRec AND Recibo BETWEEN @pmReciboIni AND @pmReciboFin  AND VA.IdCia=@pmIdCia
 ORDER BY Recibo,VA.Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencAbonos_Fac @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2)
,@pmItemFac INT
 AS 
 SELECT TipRec,Recibo,A.IdCia AS CdCia,Compania,Item,Fecha,VrAbono,VrDescto,Detalle,TipoDoc,FecPago
,VrBaseCms,IdCliente,T.RazonSocial AS NombCliente,A.IdAgencia AS CdAgencia,Agencia
,A.IdVend AS NitVendedor,VN.RazonSocial AS NomVendedor,Comision,TipoAplica,ItemFac,TimeSys
 FROM Trn_VencAbonos AS A  INNER JOIN Terceros AS T ON A.IdCliente=T.IdTercero
 INNER JOIN Terceros AS VN ON A.IdVend=VN.IdTercero INNER JOIN Companias AS CN ON A.IdCia=CN.IdCia
 LEFT JOIN Agencias AS AG ON A.IdAgencia=AG.IdAgencia LEFT JOIN Sys_TiposDoc AS TD ON A.TipRec=TD.IdDoc
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura  AND IdCiaFac=@pmIdCiaFac
 AND (ItemFac>=ISNULL(@pmItemFac,0)  AND ItemFac<=ISNULL(@pmItemFac,2147483647))
 ORDER BY TimeSys,Recibo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientos @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 SELECT TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,IdCliente,IdAgencia,IdVend,Comision,TarifDcto,TipoDcto
,TipoBase,FecLmtDcto,IdConcepto,Referencia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,FecPlazoCal
 FROM Trn_Vencimientos
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
 AND (Item>=ISNULL(@pmItem,0)  AND Item<=ISNULL(@pmItem,2147483647))
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosAbo @pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME,@pmTipDoc VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT V.IdCliente AS NitCliente,T.RazonSocial AS NomCliente,V.IdAgencia AS CdAgencia,Agencia,V.TipDoc AS IdTipFact,TD.TipoDoc AS TipoFact,V.Factura AS NumFactura,V.IdCia AS CdCia,CI.Compania AS NomCiaFact,V.Item AS nItemFact
,VrFactura,VrAbonado AS TotalAbonado,VrFactura-VrAbonado AS ValorSaldo,V.Fecha AS FechaEmision,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
--información de abonos
,TipRec,TR.TipoDoc AS TipoRecibo,VA.Recibo AS NumRecibo,VA.IdCia AS RecCodCia,CR.Compania AS RecCompania,VA.Item AS RecItem,VA.Fecha AS RecFecha,VA.FecPago AS FechaPago,VrAbono,VrDescto
,VA.Detalle AS RecDetalle,VA.IdVend AS RecCdVend,VR.RazonSocial AS RecVendedor,VrBaseCms,VA.Comision AS TarifComsCobro,TipoAplica,VA.TimeSys AS FecHoraCrea
--datos del recibo
,EnEfectivo,CdForma,NumForma,CdBanco,RC.Referencia AS RecReferencia
--mas inf. del vencimiento
,V.IdConcepto AS FacCodConcepto,Concepto,V.Referencia AS FacReferencia,V.Detalle AS FacDetalle,V.IdVend AS FacIdVend,VN.RazonSocial AS FacVendedor,V.Comision AS FacTarifComs
,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont
 FROM Trn_Vencimientos AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia  INNER JOIN Sys_TiposDoc AS TD ON V.TipDoc=TD.IdDoc
 INNER JOIN  Companias AS CI ON V.IdCia=CI.IdCia  INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero
 INNER JOIN TercCliente AS CLI ON V.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto
 LEFT JOIN  Trn_VencAbonos AS VA ON V.TipDoc=VA.TipDoc AND V.Factura=VA.Factura AND V.IdCia=VA.IdCiaFac AND V.Item=VA.ItemFac
 LEFT JOIN Sys_TiposDoc AS TR ON VA.TipRec=TR.IdDoc LEFT JOIN Companias AS CR ON VA.IdCia=CR.IdCia 
 LEFT JOIN Terceros AS VR ON VA.IdVend=VR.IdTercero
 LEFT JOIN Trn_Recibos AS RC ON VA.TipRec=RC.TipDoc AND VA.Recibo=RC.Recibo AND VA.IdCia=RC.IdCia
  WHERE  (VrFactura-VrAbonado)>@pmSaldo AND V.TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  V.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND V.IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND V.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND (V.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND V.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial,FechaVence,V.Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosAcu @pmTipDoc VARCHAR(3),@pmnAnno INT,@pmnMes INT
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null
 AS 
 SELECT TipDoc,IdCliente,IdAgencia,IdCia,SUM(VrFactura) AS STOTFAC
 FROM Trn_Vencimientos
 WHERE YEAR(Fecha)=@pmnAnno AND MONTH(Fecha)=@pmnMes  AND TipDoc LIKE ISNULL(@pmTipDoc,'%')  
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 GROUP BY TipDoc,IdCliente,IdAgencia,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosAut @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)=Null
,@pmTipDoc VARCHAR(3)=Null
 AS 
 SELECT TipDoc,Factura,IdCia,Item,VrFactura-VrAbonado AS ValorSaldo,VrFactura,VrAbonado,Fecha,FechaVence,Referencia,Detalle
,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,IdVend,VD.RazonSocial AS Vendedor,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto
,TipRef,DocRef,IdCiaRef,FecUltPago
 FROM Trn_Vencimientos AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Terceros AS VD ON V.IdVend=VD.IdTercero
 WHERE VrFactura>VrAbonado AND IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 ORDER BY  FechaVence,TipDoc,IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosDab @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
,@pmItem INT=Null,@pmTipoAplicaExc VARCHAR(10)=Null
 AS 
 SELECT V.Item AS NItem,VrFactura,VrAbonado,SUM(VrAbono) AS STOTABO
 FROM Trn_Vencimientos AS V  INNER JOIN Trn_VencAbonos AS A ON V.TipDoc=A.TipDoc AND V.Factura=A.Factura
 AND V.IdCia=A.IdCiaFac AND V.Item=A.ItemFac
 WHERE V.TipDoc=@pmTipDoc AND V.Factura=@pmFactura AND V.IdCia=@pmIdCia AND TipoAplica<>@pmTipoAplicaExc
 AND (V.Item>=ISNULL(@pmItem,0)  AND V.Item<=ISNULL(@pmItem,2147483647))
 GROUP BY V.Item,VrFactura,VrAbonado
 ORDER BY  V.Item

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosDoc @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 SELECT  Item,Fecha,FechaVence,VrFactura,VrAbonado,VrFactura-VrAbonado AS TotalSaldo,IdVend,Comision,TarifDcto,TipoDcto
,TipoBase,FecLmtDcto,IdConcepto,Referencia,Detalle,TipRef,DocRef,IdCiaRef,IdCliente,IdAgencia
,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,TipDoc,Factura,IdCia,FecPlazoCal
 FROM Trn_Vencimientos
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosLca @pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME,@pmTipDoc VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT IdCliente,T.RazonSocial AS NomCliente,V.IdAgencia AS CdAgencia,Agencia,TipDoc,TipoDoc,Factura,V.IdCia AS CdCia,Compania,Item
,VrFactura,VrAbonado,VrFactura-VrAbonado AS ValorSaldo,Fecha,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
,V.IdConcepto AS CdConcepto,Concepto,V.Referencia AS FacReferencia,Detalle,V.IdVend AS CdVend,VN.RazonSocial AS Vendedor,V.Comision AS TarifComs
,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont
 FROM Trn_Vencimientos AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia  INNER JOIN Sys_TiposDoc AS TD ON V.TipDoc=TD.IdDoc
 INNER JOIN  Companias AS CI ON V.IdCia=CI.IdCia  INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero
 INNER JOIN TercCliente AS CLI ON V.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto
 WHERE  (VrFactura-VrAbonado)>@pmSaldo AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  V.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND V.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 UNION
--saldos a favor
 SELECT IdCliente,T.RazonSocial AS NomCliente,V.IdAgencia AS CdAgencia,Agencia,'SAF','SALDO A FAVOR',0,V.IdCia AS CdCia,Compania,0
,SUM(VrAFavor-VrOtrosDb) AS VrFactura,SUM(VrAbono) AS VrAbonado,SUM(((VrAFavor-VrOtrosDb)-VrAbono)*-1) AS ValorSaldo,@pmFecActual,@pmFecActual,0
,'SAF','SALDO A FAVOR','','SALDO A FAVOR',V.IdVend AS CdVend,VN.RazonSocial AS Vendedor,0,0,'','',Null,'',0,'00',Null,0,'00',0,Null
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont
 FROM Trn_RecAfavor AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia INNER JOIN  Companias AS CI ON V.IdCia=CI.IdCia 
 INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero INNER JOIN TercCliente AS CLI ON V.IdCliente=CLI.IdClie
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 WHERE  (VrAFavor-VrOtrosDb)>VrAbono AND V.IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')
 AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND V.IdVend LIKE ISNULL(@pmIdVend,'%')
 GROUP BY IdCliente,T.RazonSocial,V.IdAgencia,Agencia,V.IdCia,Compania,V.IdVend,VN.RazonSocial
,T.TipoId,T.Dv,T.Codigo,T.NomCial,T.SiglaRaz,T.Direccion,T.IdLocal,L.Localidad,L.IdDep,D.Departamento,T.Telefono
,T.Fax,T.e_mail,T.SitioWeb,T.IdSector,SectorEco,T.IdRegimen,Regimen,T.TipEnte
,CLI.IdSzona,Subzona,SZ.IdZona,Zona,CLI.IdGrupo,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato,CodAgencia,A.DirAgncia
,A.TelAgncia,A.FaxAgncia,A.IdLocal,LA.Localidad,LA.IdDep,DA.Departamento,A.Referencia,A.NContrato,CiaCont
UNION
--CHEQUES POSTFECHADOS
SELECT IdCliente,T.RazonSocial AS NomCliente,R.IdAgencia AS CdAgencia,Agencia,R.TipDoc AS TipDocRec,'CHEQUE',R.Recibo AS NumRecibo,R.IdCia AS CdCia,Compania,0
,VrForma,0,VrForma AS ValorSaldo,CH.Fecha AS FecEmision,FecPago,0,'CHE','CHEQUE POSTFECHADO',Numero,Detalle,CdVend,VN.RazonSocial AS Vendedor,0,0,'','',Null,'',0,'00',Null,0,'00',0,Null
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont
 FROM Trn_RecCheques AS R INNER JOIN  Trn_Cheques AS CH ON R.TipDoc=CH.TipRec AND R.Recibo=CH.Recibo AND R.IdCia=CH.IdCia
 INNER JOIN Terceros AS T ON R.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON R.IdAgencia=A.IdAgencia INNER JOIN  Companias AS CI ON R.IdCia=CI.IdCia 
 INNER JOIN TercCliente AS CLI ON R.IdCliente=CLI.IdClie  INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Terceros AS VN ON CH.CdVend=VN.IdTercero
 WHERE  CH.Anulado=0 AND R.Anulado=0 AND Documento<=0 AND CH.IdEstado<>'9998'  AND  R.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND R.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND CH.CdVend LIKE ISNULL(@pmIdVend,'%')
 AND (CH.Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND CH.Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial,FechaVence,V.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosLta @pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME,@pmTipDoc VARCHAR(3)=Null
,@pmIdCia CHAR(2)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdAgencia VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 SELECT IdCliente,T.RazonSocial AS NomCliente,V.IdAgencia AS CdAgencia,Agencia,TipDoc,TipoDoc,Factura,V.IdCia AS CdCia,Compania,Item
,VrFactura,VrAbonado,VrFactura-VrAbonado AS ValorSaldo,Fecha,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
,V.IdConcepto AS CdConcepto,Concepto,V.Referencia AS FacReferencia,Detalle,V.IdVend AS CdVend,VN.RazonSocial AS Vendedor,V.Comision AS TarifComs
,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,NitRepLeg,NomRepLeg,CLI.NitContac AS ClieNitContac,CLI.NomContac AS ClieNomContacto,CLI.TelContac AS ClieTelContac,CLI.emlContac AS ClieEmailContac,CargContac,DiasEntga,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdClase AS CdClaseCta,NumCuenta,CLI.IdBanco AS CdBanco,Banco,ExcIva,LiqFletes,CLI.FactSold AS FactSoldicom,Autoret,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont
 FROM Trn_Vencimientos AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia  INNER JOIN Sys_TiposDoc AS TD ON V.TipDoc=TD.IdDoc
 INNER JOIN  Companias AS CI ON V.IdCia=CI.IdCia  INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero
 INNER JOIN TercCliente AS CLI ON V.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Bancos AS BC ON CLI.IdBanco=BC.IdBanco
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto
 WHERE  (VrFactura-VrAbonado)>@pmSaldo AND TipDoc LIKE ISNULL(@pmTipDoc,'%') AND  V.IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND V.IdAgencia LIKE ISNULL(@pmIdAgencia,'%')
 AND V.IdVend LIKE ISNULL(@pmIdVend,'%')
 AND (Fecha>=ISNULL(@pmFechaIni,CAST('19100101' AS SMALLDATETIME)) AND Fecha<=ISNULL(@pmFechaFin,CAST('20781230' AS SMALLDATETIME)))
 ORDER BY T.RazonSocial,FechaVence,V.IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosMor @pmIdCliente VARCHAR(16),@pmFechaAct SMALLDATETIME,@pmIdAgencia VARCHAR(16)=Null
,@pmTipDoc VARCHAR(3)=Null
AS
 SELECT TipDoc,Factura,IdCia,Item,Fecha,FechaVence,VrFactura,VrAbonado,IdCliente,IdAgencia
,IdVend,Comision,IdConcepto,Referencia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago
 FROM Trn_Vencimientos 
 WHERE IdCliente=@pmIdCliente AND VrFactura>VrAbonado AND FechaVence<@pmFechaAct
 AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%') AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 ORDER BY FechaVence,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosNit  @pmFechaAct SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdVend VARCHAR(16)=Null,@pmTipDoc VARCHAR(3)=Null
 AS 
 SELECT TipDoc,Factura,IdCia,Item,VrFactura-VrAbonado AS ValorSaldo,VrFactura,VrAbonado,Fecha,FechaVence,DATEDIFF(day,FechaVence,@pmFechaAct) AS DiasMora
,Referencia,Detalle,IdCliente,T.RazonSocial AS NomCliente,IdAgencia,IdVend,VD.RazonSocial AS Vendedor,Comision,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,IdConcepto
,TipRef,DocRef,IdCiaRef,FecUltPago
 FROM Trn_Vencimientos AS V INNER JOIN   Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Terceros AS VD ON V.IdVend=VD.IdTercero
 WHERE VrFactura>VrAbonado AND IdCliente=@pmIdCliente AND IdVend LIKE ISNULL(@pmIdVend,'%')
 AND TipDoc LIKE ISNULL(@pmTipDoc,'%')
 ORDER BY  FechaVence,TipDoc,IdCia,Factura
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosTdoc @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
 AS 
 SELECT VrSubTotal,VrNeto,SUM(VrFactura) AS STOTFAC,SUM(VrAbonado) AS STOTABO
 FROM Trn_Vencimientos AS V LEFT JOIN Trn_Facturas AS F ON V.TipDoc=F.TipDoc
 AND V.Factura=F.Factura AND V.IdCia=F.IdCia
 WHERE V.TipDoc=@pmTipDoc AND V.Factura=@pmFactura AND V.IdCia=@pmIdCia
 GROUP BY VrSubTotal,VrNeto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientosTnit @pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)=Null
 AS 
 SELECT COUNT(Factura) AS SCANT,SUM(VrFactura) AS STOTFAC,SUM(VrAbonado) AS STOTABO
 FROM Trn_Vencimientos
 WHERE IdCliente=@pmIdCliente AND IdAgencia LIKE ISNULL(@pmIdAgencia,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryVencimientos_Crd @pmTipDoc VARCHAR(3),@pmFacturaIni INT,@pmFacturaFin INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,TipoDoc,Factura,V.IdCia AS CdCia,Compania,Item,Fecha,FechaVence,VrFactura,VrAbonado
,IdCliente,T.RazonSocial AS NombCliente,V.IdAgencia AS IdAgenc,Agencia,CodAgencia,V.IdVend AS CdVend,VN.RazonSocial AS NomVendedor,Comision,TarifDcto,TipoDcto
,TipoBase,FecLmtDcto,V.IdConcepto AS CdConcepto,Concepto,V.Referencia AS Referncia,Detalle,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora,FecPlazoCal
 FROM Trn_Vencimientos AS V INNER JOIN Terceros AS T ON V.IdCliente=T.IdTercero
 INNER JOIN Companias AS CN ON V.IdCia=CN.IdCia INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero
 INNER JOIN Sys_TiposDoc AS TD ON V.TipDoc=TD.IdDoc INNER JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto
 LEFT JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia 
 WHERE TipDoc=@pmTipDoc AND Factura BETWEEN @pmFacturaIni AND @pmFacturaFin AND V.IdCia=@pmIdCia
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryZonas @pmIdZona VARCHAR(4)
 AS 
 SELECT IdZona,Zona,CdSubCos,Inactivo
 FROM Zonas
 WHERE IdZona=@pmIdZona

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQryZonasDso 
 AS 
 SELECT IdZona,Zona
 FROM Zonas
 WHERE Inactivo=0
 ORDER BY Zona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrymonedas @pmIdMneda VARCHAR(5)
 AS 
 SELECT IdMneda,Mneda,Simbolo,MneLocal,VrCambio,Inactivo
 FROM adm_monedas
 WHERE IdMneda=@pmIdMneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrymonedasDso
 AS 
 SELECT IdMneda,Mneda,Mneda+' ('+Simbolo +') '+ IdMneda AS DsMon
 FROM adm_monedas
 WHERE  Inactivo=0
 ORDER BY Mneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrymonedasLoc
 AS 
 SELECT IdMneda,Mneda,Simbolo,MneLocal,VrCambio,Inactivo
 FROM adm_monedas
 WHERE Inactivo=0 AND MneLocal<>0
 ORDER BY  IdMneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrymonedasLta @pmInactivo BIT=Null
 AS 
 SELECT IdMneda,Mneda,Simbolo,MneLocal,VrCambio,Inactivo
 FROM adm_monedas
 WHERE  (Inactivo=ISNULL(@pmInactivo,0) or  Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Mneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytasas @pmIdMneda VARCHAR(5),@pmIdMnedaD VARCHAR(5),@pmFecha SMALLDATETIME
 AS 
 SELECT IdMneda,IdMnedaD,Fecha,TasaDia
 FROM Trn_tasas
 WHERE IdMneda=@pmIdMneda AND IdMnedaD=@pmIdMnedaD AND Fecha=@pmFecha

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytasasDiv @pmIdMneda VARCHAR(5),@pmIdMnedaD VARCHAR(5),@pmFecha SMALLDATETIME
 AS 
 SELECT T.IdMneda AS IdMnyO,MO.Mneda AS MnyOrig,MO.Simbolo AS SimbOrig
,IdMnedaD,MD.Mneda AS MnyDtn,MD.Simbolo AS SimbDtn,Fecha,TasaDia
 FROM Trn_tasas AS T INNER JOIN adm_monedas AS MO ON T.IdMneda=MO.IdMneda
 INNER JOIN adm_monedas AS MD ON T.IdMnedaD=MD.IdMneda
 WHERE T.IdMneda=@pmIdMneda AND IdMnedaD=@pmIdMnedaD AND Fecha=@pmFecha

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytasasLta @pmFechaIni SMALLDATETIME,@pmFechaFin SMALLDATETIME
,@pmIdMneda VARCHAR(5)=Null,@pmIdMnedaD VARCHAR(5)=Null,@pmInactivo BIT=Null
 AS 
 SELECT Fecha,'1 '+MO.Mneda+' '+MO.Simbolo AS MnyOrigen,TasaDia,MD.Mneda+' '+MD.Simbolo AS MnyDestino
,T.IdMneda AS IdMonO,IdMnedaD
 FROM Trn_tasas AS T INNER JOIN adm_monedas AS MO ON T.IdMneda=MO.IdMneda
 INNER JOIN adm_monedas AS MD ON T.IdMnedaD=MD.IdMneda
 WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
 AND  T.IdMneda LIKE ISNULL(@pmIdMneda,'%')
 AND IdMnedaD LIKE ISNULL(@pmIdMnedaD,'%')
 AND (MO.Inactivo=ISNULL(@pmInactivo,0) or MO.Inactivo=ISNULL(@pmInactivo,1))
 ORDER BY Fecha,MO.Mneda,MD.Mneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfAmtLta @pmtmNumero VARCHAR(5)
 AS 
 SELECT  tmItem,tmIdDiferido,NomDiferido,tmCostoIni,tmAdiciones,tmDisminucion,tmAcuAjustes,(tmCostoIni+tmAdiciones-tmDisminucion+tmAcuAjustes) AS CostoAjustado
,tmTarifa,tmAjustesInf,tmAmortizaMes,tmAmortizaAcum,tmAjuAmtzaAcum,tmAmortizaAcum+tmAjuAmtzaAcum AS TotalAmortizaAcum,tmAjustesAmtza
,tmAmtzaHisAcum,tmAmortizaHis,tmCorrMonAcum,tmBaseCorrMon,tmCorrMonDif,tmEstadoReg,tmObservacion,tmFechaIni,tmNumPeriodos
,tmIdCia,Compania,tmCdCCosto,CCosto,tmCdSubCos,SubCosto,tmCdDep,Dependencia,tmNitRespons,RazonSocial,D.IdEstado AS CdEstado,Estado,FecRetiro
,Ajustable,Contable,G.IdLinea AS CdLinea,Linea,D.IdGrupo AS CdGrupo,Grupo,tmCuota,tmGalones,IdProv,tmInversion
 FROM tm_AfAmt AS T INNER JOIN Diferidos AS D ON T.tmIdDiferido=D.IdDiferido
 INNER JOIN  AfGrupos AS G ON D.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN AfEstados AS ED ON D.IdEstado=ED.IdEstado
 LEFT JOIN CentroCosto AS CC ON T.tmCdCCosto=CC.IdCCosto
 LEFT JOIN Dependencias AS DP ON T.tmCdDep=DP.IdDep LEFT JOIN Terceros AS TR ON T.tmNitRespons=TR.IdTercero
 LEFT JOIN Companias AS CI ON T.tmIdCia=CI.IdCia LEFT JOIN SubCentros AS SC ON  T.tmCdSubCos=SC.IdSubCos
 WHERE tmNumero=@pmtmNumero
 ORDER BY NomDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfDep @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdActivo,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,tmDepreciaHis,tmDepreciaMes,tmAjustesInf,tmAjustesDep
,tmDepreciaAcum,tmAjuDepAcum,tmTarifa,tmIdCia,tmCdCCosto,tmCdSubCos,tmCdInstala,tmCdDep,tmNitRespons,tmVidaUtil,tmMetodo,tmFechaIni,tmEstadoReg,tmObservacion
 FROM tm_AfDep
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfDepLta @pmtmNumero VARCHAR(5)
 AS 
 SELECT  tmItem,tmIdActivo,NomActivo,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes,(tmCostoIni+tmMejoras+tmAcuAjustes)-tmDisminucion AS TotalBase
,tmTarifa,tmAjustesInf,tmDepreciaHis,tmDepreciaMes,tmDepreciaAcum,tmAjuDepAcum,tmDepreciaAcum+tmAjuDepAcum AS BaseAjuDepAcum,tmAjustesDep
,tmEstadoReg,tmObservacion,tmFechaIni,tmVidaUtil,FecFinDep,tmMetodo,tmIdCia,Compania,tmCdCCosto,CCosto,tmCdSubCos,SubCosto
,tmCdInstala,Instlacion,tmCdDep,Dependencia,tmNitRespons,RazonSocial,A.IdEstado AS CdEstado,Estado,FecRetiro
,Ajustable,Contable,NumPlaca,NumSerie,G.IdLinea AS CdLinea,Linea,A.IdGrupo AS CdGrupo,Grupo
 FROM tm_AfDep AS T INNER JOIN Activos AS A ON T.tmIdActivo=A.IdActivo
 INNER JOIN  AfGrupos AS G ON A.IdGrupo=G.IdGrupo INNER JOIN AfLineas AS L ON G.IdLinea=L.IdLinea
 INNER JOIN AfEstados AS ED ON A.IdEstado=ED.IdEstado
 LEFT JOIN CentroCosto AS CC ON T.tmCdCCosto=CC.IdCCosto LEFT JOIN Instalaciones AS I ON T.tmCdInstala=I.IdInstala
 LEFT JOIN Dependencias AS DP ON T.tmCdDep=DP.IdDep LEFT JOIN Terceros AS TR ON T.tmNitRespons=TR.IdTercero
 LEFT JOIN Companias AS CI ON T.tmIdCia=CI.IdCia LEFT JOIN SubCentros AS SC ON  T.tmCdSubCos=SC.IdSubCos
 WHERE tmNumero=@pmtmNumero 
 ORDER BY  NomActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfIngreso @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdActivo,tmFecha,tmVrMejoras,tmVrDisminucion,tmVrAjuInfAcum,tmVrDepAcuHis
,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses,tmVidaUtil,tmBaseDep,tmMetodo,tmFecUltDep,tmConcepto
 FROM tm_AfIngreso
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfIngresoDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,NomActivo,tmFecha,tmBaseDep,tmVrMejoras,tmVrDisminucion,tmBaseDep+tmVrMejoras-tmVrDisminucion AS TotalBaseDep
,tmVrAjuInfAcum,tmVrDepAcuHis,tmVrDepAcuAju,tmVrAjuInfDep,tmNumMeses,tmFecUltDep
,tmMetodo,tmVidaUtil,tmConcepto,IdEstado,IdCCosto,IdSubCos,IdInstala,IdDep,IdRespons
 FROM tm_AfIngreso AS T INNER JOIN Activos AS A ON T.tmIdActivo=A.IdActivo
 WHERE tmNumero=@pmtmNumero
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfRetiro @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdActivo,tmIdCausal,tmNitTercero,tmNomTercero,tmCostoIni,tmMejoras
,tmDisminucion,tmAcuAjustes,tmDepAcum,tmAjuDepAcum,tmVrIngreso,tmFechaRet,tmIdCueDeb,tmIdCueIng,tmConcepto,tmIdCia,tmCdCCosto,tmCdSubCos,tmNitRespons
 FROM tm_AfRetiro
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfRetiroDif @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo AS IdDiferido,NomDiferido,tmFechaRet,tmCostoIni,tmMejoras AS VrAdiciones,tmDisminucion,tmAcuAjustes
,tmCostoIni+tmMejoras+tmAcuAjustes-tmDisminucion AS CostoAjustado,tmDepAcum AS AmortizaAcum,tmAjuDepAcum AS AjuAmtzaAcum
,(tmCostoIni+tmMejoras+tmAcuAjustes-tmDisminucion)-(tmDepAcum+tmAjuDepAcum) AS SaldoActual
,tmIdCausal,Causal,tmConcepto,tmIdCia,tmCdCCosto,tmCdSubCos
 FROM tm_AfRetiro  AS T INNER JOIN Diferidos AS D ON T.tmIdActivo=D.IdDiferido
 INNER JOIN AfCausales AS C ON T.tmIdCausal=C.IdCausal
 WHERE tmNumero=@pmtmNumero 
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfRetiroDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,NomActivo,tmFechaRet,tmCostoIni,tmMejoras,tmDisminucion,tmAcuAjustes
,tmCostoIni+tmMejoras+tmAcuAjustes-tmDisminucion AS CostoAjustado,tmDepAcum,tmAjuDepAcum
,(tmCostoIni+tmMejoras+tmAcuAjustes-tmDisminucion)-(tmDepAcum+tmAjuDepAcum) AS ValorReal
,tmVrIngreso,tmVrIngreso-((tmCostoIni+tmMejoras+tmAcuAjustes-tmDisminucion)-(tmDepAcum+tmAjuDepAcum)) AS Utilidad
,tmIdCausal,Causal,tmIdCueDeb,tmIdCueIng,tmNitTercero,tmNomTercero,tmConcepto,tmIdCia,tmCdCCosto,tmCdSubCos
 FROM tm_AfRetiro  AS T INNER JOIN Activos AS A ON T.tmIdActivo=A.IdActivo
 INNER JOIN AfCausales AS C ON T.tmIdCausal=C.IdCausal
 WHERE tmNumero=@pmtmNumero 
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfTraslado @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdActivo,tmIdCia,tmIdGrupo,tmIdCCosto,tmIdSubCos,tmIdInstala,tmIdDep,tmIdRespons
,tmCdCiaDest,tmCdGrupoDest,tmCdCCostoDest,tmCdSubCosDest,tmCdInstalaDest,tmCdDepDest,tmCdResponsDest
,tmConcepto,tmUbicacion,tmUbicacionDest
 FROM tm_AfTraslado
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfTrasladoDif @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo AS CodDiferido,NomDiferido,tmIdGrupo,G.Grupo AS GrupoOrig,tmCdGrupoDest,GD.Grupo AS GrupoDest,tmIdCCosto,CC.CCosto AS CentroOrigen
,tmCdCCostoDest,CCD.CCosto AS CentroDest,tmIdSubCos,SC.SubCosto AS SubcentroOrig,tmCdSubCosDest,SCD.SubCosto AS SubCentroDest
,tmIdCia,CI.Compania AS CiaOrigen,tmCdCiaDest,CID.Compania AS CiaDestino,tmIdDep,DP.Dependencia AS DependOrig,tmCdDepDest,DPD.Dependencia AS DependDest
,tmIdRespons,TR.RazonSocial AS Responsable,tmCdResponsDest,TRD.RazonSocial AS ResponsDest,tmUbicacion,tmUbicacionDest,tmConcepto
 FROM tm_AfTraslado AS T INNER JOIN Diferidos AS A ON T.tmIdActivo=A.IdDiferido
 INNER JOIN AfGrupos AS G ON T.tmIdGrupo=G.IdGrupo INNER JOIN CentroCosto AS CC ON T.tmIdCCosto=CC.IdCCosto 
 INNER JOIN Dependencias AS DP ON T.tmIdDep=DP.IdDep INNER JOIN Terceros AS TR ON T.tmIdRespons=TR.IdTercero
 INNER JOIN Companias AS CI ON T.tmIdCia=CI.IdCia  LEFT JOIN SubCentros AS SC ON T.tmIdSubCos=SC.IdSubCos
 LEFT JOIN AfGrupos AS GD ON T.tmCdGrupoDest=GD.IdGrupo LEFT JOIN CentroCosto AS CCD ON T.tmCdCCostoDest=CCD.IdCCosto 
 LEFT JOIN Dependencias AS DPD ON T.tmCdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON T.tmCdResponsDest=TRD.IdTercero
 LEFT JOIN Companias AS CID ON T.tmCdCiaDest=CID.IdCia  LEFT JOIN SubCentros AS SCD ON T.tmCdSubCosDest=SCD.IdSubCos
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AfTrasladoDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdActivo,NomActivo,tmIdGrupo,G.Grupo AS GrupoOrig,tmCdGrupoDest,GD.Grupo AS GrupoDest,tmIdCCosto,CC.CCosto AS CentroOrigen
,tmCdCCostoDest,CCD.CCosto AS CentroDest,tmIdSubCos,SC.SubCosto AS SubcentroOrig,tmCdSubCosDest,SCD.SubCosto AS SubCentroDest
,tmIdCia,CI.Compania AS CiaOrigen,tmCdCiaDest,CID.Compania AS CiaDestino,tmIdDep,DP.Dependencia AS DependOrig,tmCdDepDest,DPD.Dependencia AS DependDest
,tmIdRespons,TR.RazonSocial AS Responsable,tmCdResponsDest,TRD.RazonSocial AS ResponsDest,tmIdInstala,I.Instlacion AS UbicOrigen,tmUbicacion
,tmCdInstalaDest,UDE.Instlacion AS UbicDestino,tmUbicacionDest,tmConcepto
 FROM tm_AfTraslado AS T INNER JOIN Activos AS A ON T.tmIdActivo=A.IdActivo
 INNER JOIN AfGrupos AS G ON T.tmIdGrupo=G.IdGrupo
 INNER JOIN CentroCosto AS CC ON T.tmIdCCosto=CC.IdCCosto INNER JOIN Instalaciones AS I ON T.tmIdInstala=I.IdInstala
 INNER JOIN Dependencias AS DP ON T.tmIdDep=DP.IdDep INNER JOIN Terceros AS TR ON T.tmIdRespons=TR.IdTercero
 INNER JOIN Companias AS CI ON T.tmIdCia=CI.IdCia  LEFT JOIN SubCentros AS SC ON T.tmIdSubCos=SC.IdSubCos
 LEFT JOIN AfGrupos AS GD ON T.tmCdGrupoDest=GD.IdGrupo
 LEFT JOIN CentroCosto AS CCD ON T.tmCdCCostoDest=CCD.IdCCosto LEFT JOIN Instalaciones AS UDE ON T.tmCdInstalaDest=UDE.IdInstala
 LEFT JOIN Dependencias AS DPD ON T.tmCdDepDest=DPD.IdDep LEFT JOIN Terceros AS TRD ON T.tmCdResponsDest=TRD.IdTercero
 LEFT JOIN Companias AS CID ON T.tmCdCiaDest=CID.IdCia  LEFT JOIN SubCentros AS SCD ON T.tmCdSubCosDest=SCD.IdSubCos
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Aportes @pmtmEst VARCHAR(5),@pmtmIdInstala VARCHAR(4)=Null,@pmtmIdDep VARCHAR(4)=Null,@pmtmIdNom VARCHAR(4)=Null
,@pmIdArea VARCHAR(4)=Null
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContto,tmDiasPen,tmBasePen,tmTarfaPen,tmValorPen,tmTarfaFsp,tmValorFsp,tmTarfaFsu,tmValorFsu,tmDiasSal
,tmBaseSal,tmTarfaSal,tmValorSal,tmDiasRie,tmBaseRie,tmTarfaRie,tmValorRie,tmDiasCom,tmBaseCom,tmTarfaCom,tmValorCom,tmTarfaIcb,tmValorIcbf,tmTarfaSen,tmValorSena
,tmTarfaMin,tmValorMin,tmTarfaEsa,tmValorESAP,tmDiasNov,tmNovIng,tmNovRet,tmNovTda,tmNovTaa,tmNovTdp,tmNovTap,tmNovVsp,tmNovVte,tmNovVst,tmNovSln,tmNovIge,tmNovLma,tmNovVac,tmNovAvp,tmNovIrp,tmNovVct,tmNAutInc,tmVrIncap,tmNAutLic,tmVrLicencia,tmNAutIrp,tmVrVolAfi,tmVrVolApo,tmVrNoReten,tmVrUpc,tmVrIncIrp
,tmIdFonPen,FP.Fondo AS FondoPen,tmIdFonSal,FS.Fondo AS FondoEps,tmIdFonRie,FR.Fondo AS FondoArp,tmIdFonCaj,CC.Fondo AS CajaComp,tmIdInstala,Instlacion,tmIdDep,Dependencia,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdNom,TipoNomina,tmVehiculo,tmVrBasico
,tmCodNewEps,tmCodNewPen,tmCodNewRie,tmCodNewIns,tmCodNewTar,CRN.ClaseRiesgo AS NewClaseRie,CRN.Tarifa AS NewTarifa,tmCodNewGru,TarfaEmp,TarfaTrab,tmEst,tmItem,tmApellido1,tmApellido2,tmNombre1,tmNombre2,tmBaseRef
--Datos empleado
,Codigo,TipoId,Dv,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,EsCondtor,FecNacmto,LugarNacmto,TipoSangre,FactorRh,Sexo,Licencia,CatLicencia,VigLicencia
,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,IdEstCivil,EstCivil,NHijos,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdClase AS CodClaRie,CR.ClaseRiesgo AS ClasRiesgo,CR.Tarifa AS TarifaRie,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,C.TipoLiquida AS TipLiq,FecAfiSeg
,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado AS Con_IdEstdo,Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,TipoTrabj,tmCdFonAvp,CdFonAvp
 FROM tm_Aportes AS A INNER JOIN Empleados AS E ON A.tmIdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON A.tmNContto=C.NContrato
 INNER JOIN Dependencias AS DP ON A.tmIdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON A.tmIdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON A.tmIdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON A.tmIdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON A.tmIdNom=TN.IdNom INNER JOIN Fondos AS FP ON A.tmIdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON A.tmIdFonSal=FS.IdFondo  INNER JOIN Fondos AS FR ON A.tmIdFonRie=FR.IdFondo
 INNER JOIN Fondos AS CC ON A.tmIdFonCaj=CC.IdFondo INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco INNER JOIN EstadoCiv AS EC ON E.IdEstCivil=EC.IdEstado
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado  LEFT JOIN ClaseRie AS CRN ON A.tmCodNewTar=CRN.IdClase
 LEFT JOIN GruposAR AS GAR ON A.tmCodNewGru=GAR.IdGrupo
 WHERE tmEst=@pmtmEst AND tmIdInstala LIKE ISNULL(@pmtmIdInstala,'%') AND tmIdDep LIKE ISNULL(@pmtmIdDep,'%')
 AND tmIdNom LIKE ISNULL(@pmtmIdNom,'%') AND C.IdArea LIKE ISNULL(@pmIdArea,'%')
 ORDER BY Apellidos,Nombres,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesAdm @pmtmEst VARCHAR(5),@pmAdmon VARCHAR(3)
 AS 
 IF @pmAdmon='PEN'
  SELECT tmIdFonPen,COUNT(tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasPen>0 OR tmValorPen>0)
  GROUP BY  tmIdFonPen
 IF @pmAdmon='SAL'
  SELECT tmIdFonSal,COUNT(tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasSal>0 OR tmValorSal>0)
  GROUP BY tmIdFonSal
 IF @pmAdmon='RIE'
  SELECT tmIdFonRie,COUNT(tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst  AND (tmDiasRie>0 OR tmValorRie>0)
 GROUP BY  tmIdFonRie
 IF @pmAdmon='COM'
  SELECT tmIdFonCaj,COUNT(tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasCom>0 OR tmValorCom>0)
  GROUP BY tmIdFonCaj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesAfi @pmtmEst VARCHAR(5),@pmAdmon VARCHAR(3)
 AS 
 IF @pmAdmon='PEN'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasPen>0 OR tmValorPen>0)
 IF @pmAdmon='SAL'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasSal>0 OR tmValorSal>0)
 IF @pmAdmon='RIE'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasRie>0 OR tmValorRie>0)
 IF @pmAdmon='COM'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND (tmDiasCom>0 OR tmValorCom>0)
 IF @pmAdmon='SEN'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND tmValorSena>0
 IF @pmAdmon='ICB'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND tmValorIcbf>0
 IF @pmAdmon='ESA'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND tmValorESAP>0
 IF @pmAdmon='MIN'
  SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad   FROM tm_Aportes
  WHERE tmEst=@pmtmEst AND tmValorMin>0

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesAtz @pmtmEst VARCHAR(5),@pmtmIdFonSal VARCHAR(8)=Null,@pmtmIdFonRie VARCHAR(8)=Null
 AS 
 SELECT tmIdEmpleado,tmNAutInc,tmNAutLic,tmNAutIrp,tmVrIncIrp,tmVrIncap,tmVrLicencia
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst AND tmIdFonSal LIKE ISNULL(@pmtmIdFonSal,'%') AND tmIdFonRie LIKE ISNULL(@pmtmIdFonRie,'%')
 AND (tmVrIncIrp>0 OR tmVrIncap>0 OR tmVrLicencia>0)

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesCed @pmtmEst VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT
 AS 
 SELECT tmEst,tmItem,tmIdEmpleado,tmApellido1,tmApellido2,tmNombre1,tmNombre2
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst AND tmIdEmpleado=@pmtmIdEmpleado  AND tmItem<>@pmtmItem
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesCom @pmtmEst VARCHAR(5)
 AS 
 SELECT SUM(tmBaseCom) AS SValBase
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst  AND (tmValorCom>0 OR tmValorSena>0 OR tmValorIcbf>0)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesDet @pmtmEst VARCHAR(5),@pmtmItem INT=Null
 AS 
 SELECT tmEst,tmItem,tmIdEmpleado,tmApellido1,tmApellido2,tmNombre1,tmNombre2,tmNContto,tmDiasPen,tmDiasSal,tmDiasRie,tmDiasCom,tmBasePen,tmBaseSal,tmBaseRie,tmBaseCom,tmBaseRef,tmValorPen,tmValorSal,tmValorRie,tmValorCom,tmValorSena
,tmValorIcbf,tmValorFsp,tmValorFsu,tmValorESAP,tmValorMin,tmTarfaPen,tmTarfaSal,tmTarfaRie,tmTarfaFsp,tmTarfaFsu,tmTarfaEsa,tmTarfaMin,tmTarfaCom,tmTarfaIcb,tmTarfaSen,tmNovIng,tmNovRet,tmNovTda,tmNovTaa,tmNovTdp,tmNovTap,tmNovVsp,tmNovVte
,tmNovVst,tmNovSln,tmNovIge,tmNovLma,tmNovVac,tmNovAvp,tmNovIrp,tmNovVct,tmCodNewEps,tmCodNewPen,tmCodNewRie,tmCodNewIns,tmCodNewTar,tmCodNewGru,tmVrIncap,tmVrLicencia,tmVrVolAfi,tmVrVolApo,tmVrNoReten,tmVrUpc,tmVrIncIrp,tmNAutLic,tmNAutInc,tmNAutIrp
,tmIdFonPen,tmIdFonSal,tmIdFonRie,tmIdFonCaj,tmIdInstala,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmVehiculo,tmVrBasico,tmDiasNov,tmCdFonAvp
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesDsn @pmtmEst VARCHAR(5)
 AS 
 SELECT tmItem,tmIdEmpleado,tmApellido1,tmApellido2,tmNombre1,tmNombre2,tmNovIng,tmNovRet,tmNovTda,tmNovTaa,tmNovTdp,tmNovTap,tmNovVsp,tmNovVte
,tmNovVst,tmNovSln,tmNovIge,tmNovLma,tmNovVac,tmNovAvp,tmNovIrp,tmNovVct,tmCodNewEps,FS.Fondo AS NuevaEps,tmCodNewPen,FP.Fondo AS NuevoFdoPen,tmCodNewIns,Instlacion
,tmCodNewTar,tmCodNewGru,tmDiasNov
 FROM tm_Aportes AS A LEFT JOIN Instalaciones AS I ON A.tmCodNewIns=I.IdInstala
 LEFT JOIN Fondos AS FP ON A.tmCodNewPen=FP.IdFondo LEFT JOIN Fondos AS FS ON A.tmCodNewEps=FS.IdFondo
 WHERE tmEst=@pmtmEst AND tmDiasNov>0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesDso @pmtmEst VARCHAR(5)
 AS 
 SELECT tmItem,tmIdEmpleado,tmApellido1,tmApellido2,tmNombre1,tmNombre2,tmVrBasico,tmDiasPen,tmBasePen,tmTarfaPen,tmValorPen,tmVrVolAfi,tmVrVolApo,tmVrNoReten,tmValorFsp,tmValorFsu
,tmDiasSal,tmBaseSal,tmTarfaSal,tmValorSal,tmVrUpc,tmNAutInc,tmVrIncap,tmNAutLic,tmVrLicencia,tmDiasRie,tmBaseRie,tmTarfaRie,tmValorRie,tmVrIncIrp,tmNAutIrp
,tmDiasCom,tmBaseCom,tmTarfaCom,tmValorCom,tmTarfaIcb,tmValorIcbf,tmTarfaSen,tmValorSena,tmBaseRef,tmTarfaEsa,tmValorESAP,tmTarfaMin,tmValorMin
,tmTarfaFsp,tmTarfaFsu,tmIdFonPen,FP.Fondo AS FondoPen,tmIdFonSal,FS.Fondo AS FondoEps,tmIdFonRie,FR.Fondo AS FondoArp,tmIdFonCaj,CC.Fondo AS CajaComp,tmIdInstala,Instlacion,tmNContto,tmIdNom,tmCdFonAvp
 FROM tm_Aportes AS A INNER JOIN Instalaciones AS I ON A.tmIdInstala=I.IdInstala
 INNER JOIN Fondos AS FP ON A.tmIdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON A.tmIdFonSal=FS.IdFondo
 INNER JOIN Fondos AS FR ON A.tmIdFonRie=FR.IdFondo INNER JOIN Fondos AS CC ON A.tmIdFonCaj=CC.IdFondo
 WHERE tmEst=@pmtmEst
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesEmp @pmtmEst VARCHAR(5)
 AS 
 SELECT COUNT(DISTINCT tmIdEmpleado) AS Cantidad
 FROM tm_Aportes
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesItm @pmtmEst VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM,COUNT(tmItem) AS SCANITM
 FROM tm_Aportes 
 WHERE tmEst=@pmtmEst


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesTCom @pmtmEst VARCHAR(5)
 AS 
 SELECT tmIdFonCaj,COUNT(DISTINCT tmIdEmpleado) AS Cantidad,SUM(tmBaseCom) AS TotalIBC,SUM(tmValorCom) AS TotalCajas
,SUM(tmValorSena) AS TotalSena,SUM(tmValorIcbf) AS TotalIcb,SUM(tmValorESAP) AS TotalESAP,SUM(tmValorMin) AS TotalMinEdu
,SUM(tmVrBasico) AS TotalSalarios
 FROM tm_Aportes 
 WHERE tmEst=@pmtmEst AND (tmDiasCom>0 OR tmValorCom>0 OR tmValorSena>0 OR tmValorIcbf>0 OR tmValorESAP>0 OR tmValorMin>0)
 GROUP BY  tmIdFonCaj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesTSal @pmtmEst VARCHAR(5)
 AS 
 SELECT tmIdFonSal,COUNT(DISTINCT tmIdEmpleado) AS Cantidad,SUM(tmBaseSal) AS TotalIBC,SUM(tmValorSal) AS TotalSalud,SUM(tmVrIncap) AS TotalIncap
,SUM(tmVrLicencia) AS TotalLicencias,SUM(tmVrUpc) AS TotalUpc,SUM(tmVrBasico) AS TotalSalarios
 FROM tm_Aportes 
 WHERE tmEst=@pmtmEst AND (tmDiasSal>0 OR tmValorSal>0 OR tmVrIncap>0 OR tmVrLicencia>0 OR tmVrUpc>0)
 GROUP BY  tmIdFonSal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesTot @pmtmEst VARCHAR(5),@pmtmIdInstala VARCHAR(4)=Null,@pmtmIdDep VARCHAR(4)=Null,@pmtmIdNom VARCHAR(4)=Null
,@pmIdArea VARCHAR(4)=Null
 AS 
 SELECT COUNT(tmIdEmpleado) AS Cantidad,SUM(tmValorPen) AS TotalPension,SUM(tmValorFsp) AS TotalFsp,SUM(tmValorFsu) AS TotalFsu,SUM(tmValorSal) AS TotalSalud
 ,SUM(tmValorRie) AS TotalRiesgos,SUM(tmValorCom) AS TotalCajas,SUM(tmValorIcbf) AS TotalICBF,SUM(tmValorSena) AS TotalSena,SUM(tmVrIncap) AS TotalIncap,SUM(tmVrLicencia) AS TotalLicencias
,SUM(tmVrVolAfi) AS TotalAfiVol,SUM(tmVrVolApo) AS TotalApoVol,SUM(tmVrNoReten) AS TotalNoRetnido,SUM(tmVrUpc) AS TotalUpc,SUM(tmVrIncIrp) AS TotalIncIrp,SUM(tmVrBasico) AS TotalSalarios
,SUM(tmValorESAP) AS TotalESAP,SUM(tmValorMin) AS TotalMinEdu
 FROM tm_Aportes AS A INNER JOIN  Emp_Contrato AS C ON A.tmNContto=C.NContrato
 WHERE tmEst=@pmtmEst AND tmIdInstala LIKE ISNULL(@pmtmIdInstala,'%') AND tmIdDep LIKE ISNULL(@pmtmIdDep,'%')
 AND tmIdNom LIKE ISNULL(@pmtmIdNom,'%') AND IdArea LIKE ISNULL(@pmIdArea,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesTpen @pmtmEst VARCHAR(5)
 AS 
 SELECT tmIdFonPen,COUNT(DISTINCT tmIdEmpleado) AS Cantidad,SUM(tmBasePen) AS TotalIBC,SUM(tmValorPen) AS TotalPension,SUM(tmValorFsp) AS TotalFsp,SUM(tmValorFsu) AS TotalFsu
,SUM(tmVrVolAfi) AS TotalAfiVol,SUM(tmVrVolApo) AS TotalApoVol,SUM(tmVrNoReten) AS TotalNoRetnido,SUM(tmVrBasico) AS TotalSalarios
 FROM tm_Aportes 
 WHERE tmEst=@pmtmEst AND (tmDiasPen>0 OR tmValorPen>0 OR tmVrVolAfi>0 OR tmVrVolApo>0 OR tmValorFsp>0)
 GROUP BY  tmIdFonPen
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AportesTrie @pmtmEst VARCHAR(5)
 AS 
 SELECT tmIdFonRie,COUNT(DISTINCT tmIdEmpleado) AS Cantidad,SUM(tmBaseRie) AS TotalIBC, SUM(tmValorRie) AS TotalRiesgos,SUM(tmVrIncIrp) AS TotalIncIrp,SUM(tmVrBasico) AS TotalSalarios
 FROM tm_Aportes 
 WHERE tmEst=@pmtmEst  AND (tmDiasRie>0 OR tmValorRie>0 OR tmVrIncIrp>0)
 GROUP BY  tmIdFonRie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AutTot @pmtmEst VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmEst,tmItem,tmTipAdm,tmIdFondo,tmCodTipoApo,tmTotalCot,tmTotalFsp,tmTotalFsu,tmTotalAva,tmTotalAve,tmTotalUpc,tmTotalLic,tmTotalInc
,tmTotalOtr,tmDiasMora,tmTarifMora,tmIntMora,tmIMoraOtr,tmTotalSAF,tmSAFUpc,tmTotalPagar,tmTotalFon,tmTotalAfi,tmNumAutLic,tmNumAutInc,tmNumPlaSAF
 FROM tm_AutTot
 WHERE tmEst=@pmtmEst 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AutTotDso @pmtmEst VARCHAR(5)
 AS 
 SELECT tmItem,tmTipAdm,tmIdFondo,Fondo,tmCodTipoApo,tmTotalCot,tmTotalFsp,tmTotalFsu,tmTotalAva,tmTotalAve,tmTotalUpc,tmTotalLic,tmTotalInc
,tmTotalOtr,tmDiasMora,tmTarifMora,tmIntMora,tmIMoraOtr,tmTotalSAF,tmSAFUpc,tmTotalPagar,tmTotalFon,tmTotalAfi,tmNumAutLic,tmNumAutInc,tmNumPlaSAF
 FROM tm_AutTot AS A LEFT JOIN Fondos AS F ON A.tmIdFondo=F.IdFondo
 WHERE tmEst=@pmtmEst 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AutTotFon @pmtmEst VARCHAR(5),@pmtmTipAdm VARCHAR(3),@pmtmIdFondo VARCHAR(8)
,@pmtmItem INT
 AS 
 SELECT tmEst,tmItem,tmTipAdm,tmIdFondo,tmCodTipoApo
 FROM tm_AutTot
 WHERE tmEst=@pmtmEst AND tmTipAdm=@pmtmTipAdm AND tmIdFondo=@pmtmIdFondo
 AND tmItem<>@pmtmItem
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AutTotItm @pmtmEst VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM,COUNT(tmItem) AS ScanItm
 FROM tm_AutTot
 WHERE tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AutTotPag @pmtmEst VARCHAR(5)
 AS 
 SELECT tmTipAdm,COUNT(tmItem) AS Cantidad,SUM(tmTotalPagar)  AS StotPag,SUM(tmTotalAfi) AS TotalAfi
 FROM tm_AutTot
 WHERE tmEst=@pmtmEst 
 GROUP BY tmTipAdm
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AuxInv @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdBodega,Bodega,tmNumLote,tmSaldoAnt,tmCostoAnt
,tmEntradas,tmCostoEnt,tmSalidas,tmCostoSal
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmSaldoAnt+tmEntradas-tmSalidas ELSE tmSalidas-tmEntradas  END AS NuevoSaldo
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmCostoAnt+tmCostoEnt-tmCostoSal ELSE 0  END AS CostoTotal
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmCostoProm ELSE 0  END AS CostoPromActual
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmCostoProm*(tmSaldoAnt+tmEntradas-tmSalidas) ELSE 0  END AS  CostoTotalPromedio
,TipoRef,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,tmCdTanque,PM.IdUnd AS CdUnid,UM.Unidad AS UnidadPres,PM.UndMed AS CdUndMed,SU.Unidad AS UnidadMed
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,Tamano,Color,Referencia,VrCosto AS ProdUltCosto,VrCostPmd AS ProdCostoProm,IdTarIva,Tarifa,Simbolo,IvaInc
,Precio1,Precio2,Precio3,Precio4,Precio5,IdProv,PV.RazonSocial AS NomProveedor,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct
,Factor1,Factor2,Factor3,Factor4,Factor5,PM.Inactivo AS ProdInactivo
 FROM tm_AuxInv  AS A INNER JOIN ProdMcias  AS PM ON A.tmIdProducto=PM.IdProducto 
 INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed   INNER JOIN Terceros AS PV ON PM.IdProv=PV.IdTercero
 LEFT JOIN Companias AS CN ON A.tmIdCia=CN.IdCia  LEFT JOIN Bodegas AS B ON A.tmIdBodega=B.IdBodega
 LEFT JOIN Tablapor AS TI ON PM.IdTarIva=TI.IdTarifa
 WHERE tmEst=@pmtmEst 
 ORDER BY DescripProd,tmIdCia,tmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AuxInvCpm @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,tmIdCia,SUM(tmEntradas) AS STOTCAN
 FROM tm_AuxInv
 WHERE tmEst=@pmtmEst AND  tmCostoProm=0 AND (tmSaldoAnt<>0 OR tmEntradas<>0 OR tmSalidas<>0)
 GROUP BY tmIdProducto,tmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AuxInvPro @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4)=Null
,@pmtmIdCia CHAR(2)=Null,@pmtmNumLote VARCHAR(30)=Null
 AS 
 SELECT tmEst,tmIdProducto,tmIdBodega,tmIdCia,tmNumLote,tmSaldoAnt,tmEntradas,tmSalidas
,tmCostoAnt,tmCostoEnt,tmCostoSal,tmCostoProm,tmCdTanque
 FROM tm_AuxInv
 WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%') AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 AND tmNumLote LIKE ISNULL(@pmtmNumLote,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_AuxInvVen @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdBodega,Bodega,tmNumLote,tmSaldoAnt,tmCostoAnt
,tmEntradas,tmCostoEnt,tmSalidas,tmCostoSal
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmSaldoAnt+tmEntradas-tmSalidas ELSE tmSalidas-tmEntradas  END AS NuevoSaldo
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmCostoAnt+tmCostoEnt-tmCostoSal ELSE 0  END AS CostoTotal
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmCostoProm ELSE 0  END AS CostoPromActual
,CASE  WHEN TipoRef IN ('PRODUCTO','INSUMO') THEN tmCostoProm*(tmSaldoAnt+tmEntradas-tmSalidas) ELSE 0  END AS  CostoTotalPromedio
--información de ventas
,tmVtaCant,tmVtaCantDev,tmVtaCant-tmVtaCantDev AS VtaCantNeto,tmVtaTotal,tmVtaTotalDev,(tmVtaTotal-tmVtaTotalDev)+(tmVtaGlobal-tmVtaGlobalDev) AS VtaNeto
,tmVtaCosto,tmVtaCostoDev,tmVtaCosto-tmVtaCostoDev AS VtaCostoNeto,tmVtaDcto,tmVtaDctoDev,tmVtaDcto-tmVtaDctoDev AS VtaDctoNeto
,((tmVtaTotal-tmVtaTotalDev)+(tmVtaGlobal-tmVtaGlobalDev))-(tmVtaCosto-tmVtaCostoDev)  AS VtaUtilidad,tmVtaIva,tmVtaIvaDev,tmVtaRet,tmVtaRetDev
,tmVtaIca,tmVtaIcaDev,tmVtaTasa,tmVtaTasaDev,tmVtaGlobal,tmVtaGlobalDev,tmVtaSol,tmVtaSolDev
,TipoRef,FecUltcom,FecUltVta,Seriales,Lotes,Combo,Tanques,tmCdTanque,PM.IdUnd AS CdUnid,UM.Unidad AS UnidadPres,PM.UndMed AS CdUndMed,SU.Unidad AS UnidadMed
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,Tamano,Color,Referencia,VrCosto AS ProdUltCosto,VrCostPmd AS ProdCostoProm,IdTarIva,Tarifa,Simbolo,IvaInc
,Precio1,Precio2,Precio3,Precio4,Precio5,IdProv,PV.RazonSocial AS NomProveedor,DescripLong,DescripAbrv,ExtciaMin,ExtciaMax,ExtciaAct
,Factor1,Factor2,Factor3,Factor4,Factor5,PM.Inactivo AS ProdInactivo
 FROM tm_AuxInv  AS A INNER JOIN ProdMcias  AS PM ON A.tmIdProducto=PM.IdProducto 
 INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed   INNER JOIN Terceros AS PV ON PM.IdProv=PV.IdTercero
 LEFT JOIN Companias AS CN ON A.tmIdCia=CN.IdCia  LEFT JOIN Bodegas AS B ON A.tmIdBodega=B.IdBodega
 LEFT JOIN Tablapor AS TI ON PM.IdTarIva=TI.IdTarifa
 WHERE tmEst=@pmtmEst 
 ORDER BY DescripProd,tmIdCia,tmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalAcum @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmCodAgncia VARCHAR(16)=Null
 AS 
 SELECT BC.tmIdCuenta AS IdCuent,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo
,tmIdCia,Compania,tmIdTercero,RazonSocial,tmCodAgncia,A.Agencia AS NomAgencia,tmIdVehiculo,tmIdCCosto,CCosto,tmIdSubCos,SubCosto
--información del tercero
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDepto,Departamento,Telefono 
--información de agencia y vehículos
,CodAgencia,Referencia--,NumVeh
,tmItem,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_BalAcum  AS  BC INNER JOIN Puc AS P ON BC.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON BC.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON BC.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON BC.tmIdSubCos=SC.IdSubCos
 LEFT JOIN tm_NivCue AS NV ON BC.tmIdCuenta=NV.tmIdCuenta
 LEFT JOIN  Agencias AS A ON BC.tmCodAgncia=A.IdAgencia
 LEFT JOIN Companias AS CI ON BC.tmIdCia=CI.IdCia
-- LEFT JOIN Vehiculos AS V ON BC.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst AND BC.tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmCodAgncia LIKE ISNULL(@pmtmCodAgncia,'%')
 ORDER BY BC.tmIdCuenta,tmIdCia,RazonSocial,tmCodAgncia,tmIdCCosto,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalAcumGCue @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_BalAcum
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalCom @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmCodAgncia VARCHAR(16)=Null
 AS 
 SELECT BC.tmIdCuenta AS IdCuent,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo
,tmIdTercero,RazonSocial,tmCodAgncia,A.Agencia AS NomAgencia,tmIdVehiculo,tmIdCCosto,CCosto,tmIdSubCos,SubCosto
,tmVrPresupto
--información del tercero
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDepto,Departamento,Telefono,EsAccnista,EsCliente,EsPropietario 
--información de agencia y vehículos
,CodAgencia,Referencia--,NumVeh
,tmItem,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,Tercero,CentroCosto
 FROM tm_BalCom  AS  BC INNER JOIN Puc AS P ON BC.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON BC.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON BC.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON BC.tmIdSubCos=SC.IdSubCos
 LEFT JOIN tm_NivCue AS NV ON BC.tmIdCuenta=NV.tmIdCuenta
 LEFT JOIN  Agencias AS A ON BC.tmCodAgncia=A.IdAgencia
-- LEFT JOIN Vehiculos AS V ON BC.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst AND BC.tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmCodAgncia LIKE ISNULL(@pmtmCodAgncia,'%')
 ORDER BY BC.tmIdCuenta,RazonSocial,tmCodAgncia,tmIdCCosto,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalComAux @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmTipo VARCHAR(4),@pmtmIdTercero VARCHAR(16),@pmtmIdVehiculo VARCHAR(10)
,@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmCodAgncia VARCHAR(16)
 AS 
 SELECT tmEst,tmIdCuenta,tmItem,tmTipo,tmSaldoAnterior
 FROM tm_BalCom
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmTipo=@pmtmTipo
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
 AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%') AND tmIdSubCos LIKE ISNULL(@pmtmIdSubCos,'%')
 AND tmCodAgncia LIKE ISNULL(@pmtmCodAgncia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalComCue @pmtmEst CHAR(2)
 AS
 SELECT  tmIdCuenta,COUNT(tmItem) AS StotItm
 FROM tm_BalCom
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalComSan @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmTipo VARCHAR(4)
 AS 
 SELECT tmEst,tmIdCuenta,tmItem,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
 FROM tm_BalCom
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmTipo=@pmtmTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_BalComVeh @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null, @pmtmIdTercero VARCHAR(16)=Null, @pmtmIdVehiculo VARCHAR(10)=Null
,@pmIdGrupo VARCHAR(4)=Null,@pmIdAdmon VARCHAR(4)=Null
 AS
 SELECT tmIdCuenta,NomCuenta,tmIdVehiculo,tmSaldoAnterior,tmTotalDebitos, tmTotalCreditos,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo
,tmIdTercero,T.RazonSocial AS NombreTercero
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,Dv,TipoId,T.Direccion AS Dirccion,Telefono,Localidad
,tmItem,tmEst,tmTipo
 FROM tm_BalCom AS BC INNER JOIN Puc AS P ON BC.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON BC.tmIdTercero=T.IdTercero
 INNER JOIN Vehiculos AS V ON BC.tmIdVehiculo=V.IdVehiculo
   INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
   INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo   LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa  LEFT JOIN Localidades AS L ON T.IdLocal=L.IdLocal 
 WHERE tmEst=@pmtmEst  AND tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%') AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')  AND V.IdGrupo LIKE ISNULL(@pmIdGrupo,'%') AND V.IdAdmon LIKE ISNULL(@pmIdAdmon,'%')
 ORDER BY tmIdCuenta,T.RazonSocial,tmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalCompara @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT BC.tmIdCuenta AS CdCuenta,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmSalAnt3,tmDebitos3,tmCreditos3,tmSaldo3,tmIdCia,Compania
,tmIdTercero,RazonSocial,tmCodAgncia,A.Agencia AS NomAgencia,tmIdVehiculo,tmIdCCosto,CCosto,tmIdSubCos,SubCosto
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDepto,Departamento,Telefono 
--información de agencia y vehículos
,CodAgencia,Referencia--,NumVeh
,tmItem,tmEst,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_BalCompara AS BC INNER JOIN Puc AS P ON BC.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON BC.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON BC.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON BC.tmIdSubCos=SC.IdSubCos
 LEFT JOIN tm_NivCue AS NV ON BC.tmIdCuenta=NV.tmIdCuenta
 LEFT JOIN  Agencias AS A ON BC.tmCodAgncia=A.IdAgencia
 LEFT JOIN Companias AS CI ON BC.tmIdCia=CI.IdCia
-- LEFT JOIN Vehiculos AS V ON BC.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst AND BC.tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 ORDER BY BC.tmIdCuenta,tmIdCia,RazonSocial,tmCodAgncia,tmIdCCosto,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalComparaCue @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdCia CHAR(2)=Null,@pmtmTipo VARCHAR(4)=Null
 AS 
 SELECT tmEst,tmIdCuenta,tmItem,tmIdCia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmSalAnt3,tmDebitos3,tmCreditos3,tmSaldo3
 FROM tm_BalCompara
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 AND tmTipo LIKE ISNULL(@pmtmTipo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalComparaGCue @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_BalCompara
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalComparaTer @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmTipo VARCHAR(4)
,@pmtmIdCia CHAR(2)=Null,@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdVehiculo VARCHAR(10)=Null
,@pmtmIdCCosto VARCHAR(16)=Null,@pmtmIdSubCos VARCHAR(16)=Null,@pmtmCodAgncia VARCHAR(16)=Null
 AS 
 SELECT tmEst,tmIdCuenta,tmItem,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmSalAnt3,tmDebitos3,tmCreditos3,tmSaldo3
 FROM tm_BalCompara
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta  AND tmTipo=@pmtmTipo
 AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%') AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%')
 AND tmIdSubCos LIKE ISNULL(@pmtmIdSubCos,'%') AND tmCodAgncia LIKE ISNULL(@pmtmCodAgncia,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalGen @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT BG.tmIdCuenta AS CdCuenta,NomCuenta,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmIdCia,Compania,tmEst,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_BalGen AS  BG INNER JOIN Puc AS P ON BG.tmIdCuenta=P.IdCuenta
 LEFT JOIN Companias AS CI ON BG.tmIdCia=CI.IdCia
 LEFT JOIN tm_NivCue AS NV ON BG.tmIdCuenta=NV.tmIdCuenta
 WHERE tmEst=@pmtmEst AND BG.tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 ORDER BY  BG.tmIdCuenta,tmIdCia 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalGenCue @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2)=Null
 AS 
 SELECT tmEst,tmIdCuenta,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2
 FROM tm_BalGen
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalGenGcue @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,count(tmIdCuenta) AS SCANT
 FROM tm_BalGen
 WHERE tmEst=@pmtmEst
 GROUP BY tmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalPspto @pmtmEst CHAR(2)
 AS 
 SELECT T.IdCuenta AS CdCuenta,NomCuenta,T.IdCCosto AS CdCcosto,CCosto,T.IdSubCos AS CdSubCentro,SubCosto,T.IdCia AS CdCia,Compania,VrPstoEne,VrSanEne,VrDebEne,VrCreEne,VrPstoFeb,VrSanFeb,VrDebFeb,VrCreFeb,VrPstoMar,VrSanMar,VrDebMar,VrCreMar
,VrPstoAbr,VrSanAbr,VrDebAbr,VrCreAbr,VrPstoMay,VrSanMay,VrDebMay,VrCreMay,VrPstoJun,VrSanJun,VrDebJun,VrCreJun,VrPstoJul,VrSanJul,VrDebJul,VrCreJul,VrPstoAgo,VrSanAgo,VrDebAgo,VrCreAgo,VrPstoSep,VrSanSep,VrDebSep,VrCreSep
,VrPstoOct,VrSanOct,VrDebOct,VrCreOct,VrPstoNov,VrSanNov,VrDebNov,VrCreNov,VrPstoDic,VrSanDic,VrDebDic,VrCreDic
,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,Naturaleza,CentroCosto,SubCentro
 FROM tm_BalPspto AS T INNER JOIN Puc AS P ON T.IdCuenta=P.IdCuenta
 LEFT JOIN CentroCosto AS CC ON T.IdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON T.IdSubCos=SC.IdSubCos
 LEFT JOIN Companias AS C ON T.IdCia=C.IdCia
 LEFT JOIN tm_NivCue AS NV ON T.IdCuenta=NV.tmIdCuenta
 WHERE T.tmEst=@pmtmEst
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_BalPsptoCue @pmtmEst CHAR(2)
 AS
 SELECT IdCuenta AS  tmIdCuenta,COUNT(IdCia) AS StotItm
 FROM tm_BalPspto
 WHERE tmEst=@pmtmEst
 GROUP BY IdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CajaDoc @pmtmEst CHAR(2),@pmtmItem INT=Null,@pmTipDoc VARCHAR(3)=Null
 AS 
 SELECT TipDoc,TipoDoc,Numero,C.IdCia AS CdCia,Compania,Fecha,C.IdTercero AS NitTercero,T.RazonSocial AS NomCliente,C.IdAgencia AS Id_Agencia,Agencia,CodAgencia,nClieCon,Nombre
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros
,EnEfectivo,CdForma,NumForma,CdBanco,Banco,DetallePagos,CdCta,NumeroCta,FecCheque,C.Referencia AS Referencia1,Referencia2
,C.IdConcepto AS CdConcepto,Concepto,Modalidad,C.IdVend AS NitVendedor,V.RazonSocial AS Vendedor,TarifaCom,IdCajero,Usuario,UC.IdEstacion AS CdEstacion,Estacion
,C.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDpto,Departamento,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev
,pVehiculo,Cantidad,CantPuntos,PuntosAcum,C.Observacion AS Observ,TimeSys,tmItem
 FROM tm_Caja AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON  C.IdTercero=T.IdTercero
 INNER JOIN Terceros AS V ON  C.IdVend=V.IdTercero INNER JOIN  Agencias  AS A ON C.IdAgencia=A.IdAgencia
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto 
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario  INNER JOIN Sys_Estaciones AS ET ON UC.IdEstacion =ET.IdEstacion
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN ClieContado AS CC ON C.nClieCon=CC.IdCliente  LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTA ON C.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON C.CdBanco=B.IdBanco
 WHERE tmEst=@pmtmEst  AND TipDoc LIKE ISNULL(@pmTipDoc ,'%') 
  AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem,TipDoc,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CajaFor @pmtmEst CHAR(2),@pmtmItem INT=Null,@pmTipDoc VARCHAR(3)=Null
 AS 
 SELECT C.TipDoc AS IdTipDoc,TipoDoc,Numero,C.IdCia AS CdCia,Compania,C.Fecha AS FechaDoc,P.IdForma AS CdForma,Detalle
 ,CASE  WHEN C.TipDoc IN ('NDB','NCR','DEG') OR P.IdForma IS NULL THEN VrEfectivo ELSE VrPagado  END AS ValPagado,VrCambio
,CASE  WHEN C.TipDoc IN ('NDB','NCR','DEG') OR P.IdForma IS NULL THEN EnEfectivo ELSE  EsCaja END AS EsCaja,FormaPago
,P.NumForma AS NumeroForma,FecForma,P.IdBanco AS CodBanco,BF.Banco AS NomBanco,C.IdTercero AS NitTercero,T.RazonSocial AS NomCliente
,C.IdAgencia AS Id_Agencia,A.Agencia AS NomAgencia,A.CodAgencia AS CodAgenc,nClieCon,Nombre
,C.IdVend AS NitVendedor,V.RazonSocial AS Vendedor,TarifaCom,IdCajero,Usuario,UC.IdEstacion AS CdEstacion,Estacion
,CtaForma,Beneficiario,Referncia1,Referncia2,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,P.Cantidad AS CantDenom,TipDenom
,P.TipRef AS PagTipRef,P.DocRef AS PagDocRef,P.IdCiaRef AS PagIdCiaRef,P.NitCliente AS PagNitCliente,TP.RazonSocial AS PagNomCliente
,CdAgencia,AP.Agencia AS PagAgencia,AP.CodAgencia AS PagCodAgenc,EnEfectivo,CdForma,C.NumForma AS NumCheque,CdBanco,B.Banco AS DocBanco
,DetallePagos,C.CdCta AS DocCodCta,NumeroCta,FecCheque,C.Referencia AS Referencia1,Referencia2,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion
,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrNeto,VrEfectivo,VrPagosOtr,VrCreditos,VrGastComb,VrGastOtros
,C.IdConcepto AS CdConcepto,Concepto,Modalidad,C.IdLocal AS CdLocal,L.Localidad AS DocNomCiudad,L.IdDep AS DocCdDpto,Departamento
,C.TipRef AS DocTipRef,C.DocRef AS DocNumDocRef,C.IdCiaRef AS DocIdCiaRef
,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,pVehiculo,C.Cantidad AS DocCantidad,CantPuntos,PuntosAcum,C.Observacion AS Observ,TimeSys,tmItem
 FROM tm_Caja AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON  C.IdTercero=T.IdTercero
 INNER JOIN Terceros AS V ON  C.IdVend=V.IdTercero INNER JOIN  Agencias  AS A ON C.IdAgencia=A.IdAgencia
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto 
 INNER JOIN adm_Usuarios AS UC ON C.IdCajero=UC.IdUsuario  INNER JOIN Sys_Estaciones AS ET ON UC.IdEstacion =ET.IdEstacion
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 LEFT JOIN ClieContado AS CC ON C.nClieCon=CC.IdCliente  LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTA ON C.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON C.CdBanco=B.IdBanco
 LEFT JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND C.Numero=P.Documento AND C.IdCia=P.IdCia
 LEFT JOIN Formaspago AS FP ON P.IdForma=FP.IdForma  LEFT JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 LEFT JOIN Terceros AS TP ON P.NitCliente=TP.IdTercero  LEFT JOIN Agencias  AS AP ON P.CdAgencia=AP.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 WHERE tmEst=@pmtmEst  AND C.TipDoc LIKE ISNULL(@pmTipDoc ,'%') 
  AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem,C.TipDoc,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CajaTot @pmtmEst CHAR(2)
 AS 
 SELECT tmConcepto,tmValorTotal,tmValorOtros,tmTipo,tmItem
 FROM tm_CajaTot
 WHERE tmEst=@pmtmEst 
 ORDER BY tmTipo,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_Cargos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdCliente,tmIdCargo,tmConcepto,tmReferencia
,tmFactura,tmValor,tmDetalle,tmFecVence,tmTipDoc,tmDocumento,tmIdCiaDoc,tmConductor,tmAbonado,tmTipDocAnt,tmDocAnticipo
,tmCiaDocAnt,tmPrestamoID,tmIdCiaPre,tmNumCuota,tmFecCausa,tmValorCapital,tmValorInteres,tmIdCuenta,tmCombCant,tmCombValor
 FROM tm_Cargos
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CargosCmp @pmtmNumero VARCHAR(5)
 AS
   SELECT tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor, tmIdCuenta
,SUM(tmValor) AS SValFac,SUM(tmCombValor) AS TotalBase
  FROM    tm_Cargos  WHERE tmNumero=@pmtmNumero  
 GROUP BY tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdCuenta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CargosCmpp @pmtmNumero VARCHAR(5)
 AS
 SELECT tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdCuenta,SUM(tmValor) AS SValFac,SUM(tmValorCapital) AS SValCap
,SUM(tmValorInteres) AS SValInt,SUM(tmValorCapital+tmValorInteres) AS STotCuo
 FROM    tm_Cargos  WHERE tmNumero=@pmtmNumero  
 GROUP BY tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,tmIdCuenta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CargosGru @pmtmNumero VARCHAR(5)
 AS
   SELECT tmIdCliente,tmIdVehiculo,SUM( tmValor ) AS STotFac
   FROM    tm_Cargos  WHERE tmNumero=@pmtmNumero  
  GROUP BY tmIdCliente,tmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CargosPre @pmtmNumero VARCHAR(5)
AS
 SELECT  tmItem,tmValor,tmDetalle,tmFactura,tmReferencia, tmIdVehiculo,tmVehPropio,tmIdConductor, tmIdCliente,tmNumero
, tmFecVence,tmPrestamoID, tmIdCiaPre, tmNumCuota, tmFecCausa, tmValorCapital, tmValorInteres
 FROM tm_Cargos  WHERE tmNumero=@pmtmNumero 
 ORDER BY tmIdCiaPre,tmPrestamoID,tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CargosVce @pmtmNumero VARCHAR(5)
AS
   SELECT  tmItem,tmFecVence, tmValor,tmIdVehiculo,tmDetalle,tmIdCuenta,tmFactura,tmReferencia
,tmVehPropio,tmIdConductor,tmConductor,tmIdCliente
  FROM    tm_Cargos 
  WHERE tmNumero=@pmtmNumero
  ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CargosVeh @pmtmNumero VARCHAR(5),@pmtmIdVehiculo VARCHAR(10)
AS
  SELECT  tmItem, tmIdCargo, tmConcepto, tmValor,tmDetalle,tmFactura,tmReferencia, tmIdVehiculo,tmVehPropio,tmIdConductor, tmIdCliente,tmNumero
  ,  tmFecVence, tmTipDoc, tmDocumento, tmIdCiaDoc, tmConductor,tmAbonado,tmTipDocAnt,tmDocAnticipo,tmCiaDocAnt
,tmPrestamoID, tmIdCiaPre, tmNumCuota, tmFecCausa, tmValorCapital, tmValorInteres
   FROM  tm_Cargos  WHERE tmNumero=@pmtmNumero  AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
   ORDER BY tmIdVehiculo,tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_Cargos_Cr @pmtmNumero VARCHAR(5)
 AS
   SELECT  tmItem, tmIdCargo, tmConcepto, tmValor,tmDetalle,tmFactura,tmReferencia, tmIdVehiculo,tmVehPropio,tmIdConductor,C.RazonSocial AS Conductor
, tmIdCliente,T.RazonSocial AS Propietario,tmNumero, tmFecVence, tmTipDoc, tmDocumento, tmIdCiaDoc, tmConductor,tmAbonado,tmTipDocAnt,tmDocAnticipo,tmCiaDocAnt
 ,tmPrestamoID, tmIdCiaPre, tmNumCuota, tmFecCausa, tmValorCapital, tmValorInteres,tmIdCuenta,tmCombCant,tmCombValor
   FROM   tm_Cargos INNER JOIN Terceros AS T ON tm_Cargos.tmIdCliente=T.IdTercero
    INNER JOIN Terceros AS C ON tm_Cargos.tmIdConductor=C.IdTercero
  WHERE tmNumero=@pmtmNumero
  ORDER BY  tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Cartera @pmtmEst CHAR(2),@pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME
 AS 
 SELECT  IdCliente,T.RazonSocial AS NomCliente,V.IdAgencia AS CdAgencia,Agencia,TipDoc,TipoDoc,Factura,V.IdCia AS CdCia,Compania,Item
,VrFactura,VrAbonado,CASE TipDoc WHEN 'SAF' THEN V.VrSaldo*-1  ELSE  V.VrSaldo END AS ValorSaldo,Fecha,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
,V.IdConcepto AS CdConcepto,Concepto,V.Referencia AS FacReferencia,Detalle,V.IdVend AS CdVend,VN.RazonSocial AS Vendedor,V.Comision AS TarifComs
,TarifDcto,TipoDcto,TipoBase,FecLmtDcto,TipRef,DocRef,IdCiaRef,FecUltPago,NumNota,IdCiaNot,TarifInt,FecLiqMora
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,CLI.VrSaldo AS SaldoClie,Contrato,CLI.NContrato AS CliNumContrato
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont,CLI.IdVend AS VendedorPre,VNC.RazonSocial AS NomVendedorPre
,PZ.DiasPago AS DiasPlazoAge,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco
-- PZC.DiasPago AS DiasPlazoAgeCom
 FROM tm_Cartera AS V INNER JOIN  Companias AS CI ON V.IdCia=CI.IdCia
 INNER JOIN  Terceros AS T ON V.IdCliente=T.IdTercero INNER JOIN Terceros AS VN ON V.IdVend=VN.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN TercCliente AS CLI ON V.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Agencias AS A ON V.IdAgencia=A.IdAgencia  
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Plazos AS PZ ON A.IdPlazo=PZ.IdPlazo   INNER JOIN Terceros AS VNC ON CLI.IdVend=VNC.IdTercero
 LEFT JOIN Conceptos AS C ON V.IdConcepto=C.IdConcepto  LEFT JOIN Sys_TiposDoc AS TD ON V.TipDoc=TD.IdDoc
 -- LEFT JOIN Plazos AS PZC ON A.CdPlazoComb=PZC.IdPlazo
 WHERE tmEst=@pmtmEst AND V.VrSaldo>@pmSaldo
 ORDER BY T.RazonSocial,FechaVence
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Combo @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdProdBase,tmCantidad,tmVrUnitario,tmIdBodega,tmCdTanque
,tmIdUnd,tmFactor,tmUndFac,tmServcios,tmLinea
 FROM tm_Combo
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ComboItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT  MAX(tmItem) AS  MXITM
 FROM tm_Combo
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ComboLin @pmtmNumero VARCHAR(5),@pmtmLinea INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdProdBase,tmCantidad,tmVrUnitario,tmIdBodega,tmCdTanque
,tmIdUnd,tmFactor,tmUndFac,tmServcios,tmLinea
,DescripProd,TipoRef,IdSubgrupo,IdUnd,IdBodega,IdUbic,VrCostPmd,Seriales,Lotes,Combo,Tanques
,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM  tm_Combo AS C INNER JOIN ProdMcias AS P ON C.tmIdProdBase=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmLinea=@pmtmLinea
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ComboPla @pmtmNumero VARCHAR(5),@pmtmReferencia2 VARCHAR(50)
 AS 
 SELECT COUNT(C.tmItem) AS SCANT,SUM(C.tmCantidad) AS STOTCAN,SUM(C.tmVrUnitario*C.tmCantidad) AS STOTCOS
 FROM tm_Combo AS C  INNER JOIN tm_Kdex AS  K ON C.tmNumero=K.tmNumero AND C.tmLinea=K.tmItem
 WHERE C.tmNumero=@pmtmNumero AND tmEsCombo<>0 AND tmReferencia2=@pmtmReferencia2
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ComboTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(C.tmItem) AS SCANT,SUM(C.tmCantidad) AS STOTCAN,SUM(C.tmVrUnitario*C.tmCantidad) AS STOTCOS
 FROM tm_Combo AS C  INNER JOIN tm_Kdex AS  K ON C.tmNumero=K.tmNumero AND C.tmLinea=K.tmItem
 WHERE C.tmNumero=@pmtmNumero AND tmEsCombo<>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Comisiones @pmtmEst CHAR(2)
 AS 
 SELECT  TipDoc,TipoDoc,Numero,C.IdCia AS CdCia,Compania,Item,Fecha,FecVence,C.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,C.IdTercero AS NitCliente,T.RazonSocial AS NomCliente
,C.IdAgencia AS CdAgencia,Agencia,CodAgencia,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,C.IdConcepto AS CdConcepto,Concepto,Cantidad,nClieCon,Nombre
,IdCajero,Usuario,C.IdLocal AS CdLocalidad,Localidad,L.IdDep AS CodDep,Departamento
,CdSzona,Subzona,SZ.IdZona AS CdZona,Zona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,C.Observacion AS Observ,Detalles,C.Referencia AS Referncia
--datos del vendedor
,VN.Codigo AS VendCodigo,VN.TipoId AS VendTipoId,VN.Dv AS VendDv,VN.IdProf AS VendIdProf,Profesion,CdJorn,TV.IdGrupo AS VendIdGrupo,GV.GrupoClie AS GrupoVend
--datos del cliente
,T.Codigo AS CliCodigo,T.TipoId AS CliTipoId,T.Dv AS CliDV,T.IdSector AS CdSector,SectorEco,CLI.IdGrupo AS CdGrupoCli,GC.GrupoClie AS Grupo_Clie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
 FROM tm_Comisiones  AS C INNER JOIN  Companias AS CI ON  C.IdCia=CI.IdCia  INNER JOIN Terceros AS VN ON C.IdVend=VN.IdTercero 
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero  INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  adm_Usuarios AS U ON C.IdCajero=U.IdUsuario INNER JOIN Profesiones AS PF ON VN.IdProf=PF.IdProf
 LEFT JOIN  Agencias  AS A ON C.IdAgencia=A.IdAgencia
 LEFT JOIN TercVendedor AS TV ON C.IdVend=TV.IdVend LEFT JOIN GruposCli AS GV ON TV.IdGrupo=GV.IdGrupo
 LEFT JOIN Jornadas AS J ON TV.CdJorn=J.IdJornada
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc  LEFT JOIN  Conceptos AS CO ON C.IdConcepto=CO.IdConcepto
 LEFT JOIN TercCliente AS CLI ON C.IdTercero=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Subzonas AS SZ ON C.CdSzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Localidades AS L ON C.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera  LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera
 LEFT JOIN ClieContado AS CC ON C.nClieCon=CC.IdCliente 
 WHERE tmEst=@pmtmEst 
 ORDER BY VN.RazonSocial,Numero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ComisionesDet @pmtmEst CHAR(2)
 AS 
 SELECT  TipDoc,TipoDoc,Numero,C.IdCia AS CdCia,Compania,Item,Fecha,FecVence,C.IdVend AS NitVend,VN.RazonSocial AS Vendedor,TarifaCom,C.IdTercero AS NitCliente,T.RazonSocial AS NomCliente
,C.IdAgencia AS CdAgencia,Agencia,CodAgencia,CdProducto,DescripProd,Cantidad,CdOperario,VO.RazonSocial AS NomOperario,ComisnOper
,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrOtros,VCargos,VrOtrosDctos,C.IdConcepto AS CdConcepto,Concepto,nClieCon,Nombre
,IdCajero,Usuario,C.IdLocal AS CdLocalidad,Localidad,L.IdDep AS CodDep,Departamento
,CdSzona,Subzona,SZ.IdZona AS CdZona,Zona,Modalidad,TipRef,DocRef,IdCiaRef,Anulado,C.Observacion AS Observ,Detalles,C.Referencia AS Referncia
--datos del vendedor
,VN.Codigo AS VendCodigo,VN.TipoId AS VendTipoId,VN.Dv AS VendDv,VN.IdProf AS VendIdProf,Profesion,CdJorn,TV.IdGrupo AS VendIdGrupo,GV.GrupoClie AS GrupoVend
--datos del cliente
,T.Codigo AS CliCodigo,T.TipoId AS CliTipoId,T.Dv AS CliDV,T.IdSector AS CdSector,SectorEco,CLI.IdGrupo AS CdGrupoCli,GC.GrupoClie AS Grupo_Clie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
--datos del producto
,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca
,PM.Referencia AS Prod_Referencia,PM.IdUnd AS CdUnid,UM.Unidad AS UnidMed,Tanques,DescripLong,DescripAbrv,Precio1,Precio2,Precio3,Precio4,Precio5
 FROM tm_Comisiones  AS C INNER JOIN  Companias AS CI ON  C.IdCia=CI.IdCia  INNER JOIN Terceros AS VN ON C.IdVend=VN.IdTercero 
 INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero  INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN  adm_Usuarios AS U ON C.IdCajero=U.IdUsuario INNER JOIN Profesiones AS PF ON VN.IdProf=PF.IdProf
 LEFT JOIN  Agencias  AS A ON C.IdAgencia=A.IdAgencia
 LEFT JOIN TercVendedor AS TV ON C.IdVend=TV.IdVend LEFT JOIN GruposCli AS GV ON TV.IdGrupo=GV.IdGrupo
 LEFT JOIN Jornadas AS J ON TV.CdJorn=J.IdJornada
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc  LEFT JOIN  Conceptos AS CO ON C.IdConcepto=CO.IdConcepto
 LEFT JOIN TercCliente AS CLI ON C.IdTercero=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Subzonas AS SZ ON C.CdSzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Localidades AS L ON C.IdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera  LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera
 LEFT JOIN ClieContado AS CC ON C.nClieCon=CC.IdCliente 
 LEFT JOIN Terceros AS VO ON C.CdOperario=VO.IdTercero
 LEFT JOIN ProdMcias  AS PM ON C.CdProducto=PM.IdProducto LEFT JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 LEFT JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo  LEFT JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 LEFT JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea  LEFT JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 WHERE tmEst=@pmtmEst 
 ORDER BY VN.RazonSocial,TipDoc,Numero,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Comp @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura
,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmTipCom,tmComprobante
,tmIdCia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompCue @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdCuenta,SUM(tmVrDebito) AS STOTDEB,SUM(tmVrCredito) AS STOTCRE
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 GROUP BY tmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompDsc @pmtmNumero VARCHAR(5) 
 AS 
 SELECT tmItem,tmCodCargo,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmTercero,tmCodAgncia,tmVrBase,tmTarifaBase
,tmIdCCosto,tmCcosto,tmIdSubCos,tmSubcc,tmIdVehiculo,tmTipoAplica,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmCodCta,tmNumCheque,tmReferncia,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNombre,tmNitOtros
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompDso @pmtmNumero VARCHAR(5) 
 AS 
 SELECT tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmTercero,tmCodAgncia,tmVrBase,tmTarifaBase
,tmIdCCosto,tmCcosto,tmIdSubCos,tmSubcc,tmIdVehiculo,tmTipoAplica,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmNitDoc,tmCodCta,tmNumCheque,tmReferncia,tmTipDocRef,tmDocRef,tmIdCiaRef
,tmNombre,tmNitOtros,tmVehPropio,tmCodSubgpo,tmCiuOrigen,tmNumero
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompFac @pmtmNumero VARCHAR(5), @pmtmIdTercero VARCHAR(16),@pmtmTipFac VARCHAR(3)
,@pmtmFactura  VARCHAR(15),@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmItem INT
AS
 SELECT tmNumero,tmItem,tmIdCuenta,tmIdTercero,tmVrDebito,tmVrCredito,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,tmTipoAplica
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero AND tmTipoAplica IN ('C','A')  AND  tmIdTercero=@pmtmIdTercero AND tmTipFac=@pmtmTipFac 
 AND tmFactura=@pmtmFactura AND tmIdCiaFac=@pmtmIdCiaFac AND tmItemFac=@pmtmItemFac
 AND tmItem<>@pmtmItem
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompID @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITEM
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompItm @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT COUNT(tmItem) AS SCANT
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompTot @pmtmNumero VARCHAR(5)
 AS
    SELECT SUM(tmVrDebito) AS STotDeb, SUM(tmVrCredito) AS STotCre,SUM(tmVrBase) AS STotBas  
    FROM  tm_Comp 
    WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompVce @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdCuenta,tmIdTercero,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmTipoAplica
,SUM(tmVrDebito) AS STotDeb,SUM(tmVrCredito) AS STotCre 
 FROM tm_Comp
 WHERE tmNumero=@pmtmNumero  AND tmTipoAplica  IN ('C','A')
 GROUP BY  tmIdCuenta,tmIdTercero,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmTipoAplica
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CompVen @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura
,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmTipCom,tmComprobante
,tmIdCia,tmNombre,tmCcosto,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
 FROM  tm_Comp AS T INNER JOIN puc AS C ON T.tmIdCuenta=C.IdCuenta
 WHERE tmNumero=@pmtmNumero   AND tmTipoAplica IN ('A','C')  AND Vencimiento<>0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Compau @pmtmNumero VARCHAR(5),@pmtmTipCom VARCHAR(3),@pmtmComprobante INT,@pmtmIdCia CHAR(2)
 AS 
 SELECT tmNumero,tmTipCom,tmComprobante,tmIdCia,tmItem,tmIdCuenta,tmDetalle,tmVrDebito,tmVrCredito,tmIdTercero,tmIdVehiculo,tmIdCCosto,tmIdSubCos
,tmVrBase,tmTarifaBase,tmTipDoc,tmDocumento,tmIdCiaDoc,tmCodConce,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCodCta,tmNumCheque
,tmIntegrado,tmTipoAplica,tmConsolida,tmCodCargo,tmNitOtros,tmCodSubgpo,tmCiuOrigen,tmCodAgncia,tmVehPropio,tmReferncia,tmNombre,tmCcosto
,tmSubcc,tmTercero,tmTipDocRef,tmDocRef,tmIdCiaRef,tmNitDoc
 FROM tm_Compau
 WHERE tmNumero=@pmtmNumero AND tmTipCom=@pmtmTipCom AND tmComprobante=@pmtmComprobante AND tmIdCia=@pmtmIdCia
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Contratos @pmtmEst CHAR(2)
 AS 
 SELECT tmContrato,tmIdCia,Compania,IdCliente,T.RazonSocial AS NomCliente,C.IdAgencia AS CdAgencia,Agencia,tmPeriodo,tmTipoProd
,tmCdProducto,DescripProd,tmPresptoAnno,tmPresptoMes,tmCantNeto,tmValorNeto
,Fecha,C.IdTipo AS CdTipo,TipContrato,C.IdBonif AS CdBonif,TipoBonif,ConvBonif,ModPlazo,Plazo,FechaVence,VrCuota,C.IdBandera AS CdBandera,TipoBandera
,C.IdVend AS CdVend,V.RazonSocial AS Vendedor
--datos del cliente
,T.IdLocal AS TercCodCiudad,L.Localidad AS NomCiudad,L.IdDep AS CodDpto,D.Departamento AS TercDpto,T.IdSector AS CdSector,SectorEco
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
 FROM tm_Contratos AS TC  INNER JOIN Trn_MayContratos AS C ON TC.tmContrato=C.NContrato AND TC.tmIdCia=C.IdCia
 INNER JOIN Companias AS CN ON TC.tmIdCia=CN.IdCia
 INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero INNER JOIN MayTipos AS TCO ON C.IdTipo=TCO.IdTipo
 INNER JOIN MayTiposBonif AS B ON C.IdBonif=B.IdBonif INNER JOIN  TiposBan AS TB ON C.IdBandera=TB.IdBandera
 INNER JOIN Terceros AS V ON C.IdVend=V.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN TercCliente AS CLI ON C.IdCliente=CLI.IdClie INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona 
 INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Agencias   AS A ON C.IdAgencia=A.IdAgencia LEFT JOIN ProdMcias AS PM ON TC.tmCdProducto=PM.IdProducto
 WHERE tmEst=@pmtmEst
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_Cuotas @pmtmEst CHAR(2),@pmtmPrestamoID INT=Null,@pmtmIdCia CHAR(2)=Null,@pmtmNumCuota INT=Null
AS
 SELECT tmNumCuota,tmFecCausa,tmFecVence,tmSaldo,tmValorInteres,tmValorCapital,tmValorCuota,tmValorCausado,tmValorAbonado
,tmValorCausado-tmValorAbonado AS SaldoAPagar,tmFecPago,tmTipRec,tmRecibo, tmIdCiaRec,tmPrestamoID,tmIdCia
 FROM    tm_Cuotas
 WHERE tmEst=@pmtmEst
 AND (tmPrestamoID>=ISNULL(@pmtmPrestamoID,0)  AND tmPrestamoID<=ISNULL(@pmtmPrestamoID,2147483647))
 AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 AND (tmNumCuota>=ISNULL(@pmtmNumCuota,0)  AND tmNumCuota<=ISNULL(@pmtmNumCuota,2147483647))
 ORDER BY tmNumCuota

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CuotasCr @pmtmEst CHAR(2)
AS
 SELECT tmPrestamoID,tmIdCia,tmNumCuota,tmFecCausa,tmFecVence,tmValorCapital,tmValorInteres,tmValorCuota,tmSaldo
,tmValorCausado AS VrPrestamo,tmFecPago,tmRecibo AS NPlazos
 FROM    tm_Cuotas
 WHERE tmEst=@pmtmEst
 ORDER BY tmPrestamoID,tmNumCuota



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxCobrar @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmItem,tmFecVence,tmVrFactura,tmDetalle,tmReferencia,tmTarifDcto,tmTipoDcto
,tmTipoBase,tmFecLmtDcto,tmTipRef,tmDocRef,tmIdCiaRef,tmNumero,tmTotAbono,tmFecPlazo
 FROM tm_CxCobrar
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxCobrarId @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_CxCobrar
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxCobrarTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrFactura) AS SVALTOT,SUM(tmTotAbono) AS STotAbono
 FROM tm_CxCobrar
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxPagar @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProveedor,tmFecVence,tmIdCuenta,tmTipDoc,tmDocumento,tmIdCiaDoc,tmVrFactura,tmItemCom,tmReferencia
,tmDetalle,tmpVehiculo,tmVehPropio,tmTipRef,tmDocRef,tmIdCiaRef,tmProveedor
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxPagarDso @pmtmNumero VARCHAR(5),@pmtmIdProveedor VARCHAR(16)=Null
 AS 
 SELECT tmItem,tmFecVence,tmVrFactura,tmDetalle,tmReferencia,tmIdCuenta,tmTipDoc,tmDocumento,tmIdCiaDoc
,tmIdProveedor,tmProveedor,tmpVehiculo,tmTipRef,tmDocRef,tmIdCiaRef,tmItemCom,tmNumero
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero  AND tmIdProveedor LIKE ISNULL(@pmtmIdProveedor,'%')
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxPagarID @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem)  AS MXITM
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CxPagarNit @pmtmNumero VARCHAR(5),@pmtmIdProveedor VARCHAR(16)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdProveedor,tmFecVence,tmIdCuenta,tmTipDoc,tmDocumento,tmIdCiaDoc,tmVrFactura,tmItemCom,tmReferencia
,tmDetalle,tmpVehiculo,tmVehPropio,tmTipRef,tmDocRef,tmIdCiaRef,tmProveedor
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero AND tmIdProveedor LIKE ISNULL(@pmtmIdProveedor,'%')
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxPagarTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT Count(tmItem) AS SCANT,SUM(tmVrFactura) AS SVALTOT
 FROM tm_CxPagar
 WHERE tmNumero=@pmtmNumero 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmVrDescto,tmVrBaseCms,tmIdCliente
,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmTipoAplica,tmCodTarDct,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonosDoc @pmtmNumero VARCHAR(5),@pmtmTipDoc VARCHAR(3),@pmtmFactura INT
,@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT
 AS 
 SELECT tmNumero,tmItem,tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero AND tmTipDoc=@pmtmTipDoc  AND tmFactura=@pmtmFactura
 AND tmIdCiaFac=@pmtmIdCiaFac AND tmItemFac=@pmtmItemFac
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonosDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmVrDescto,tmDetalle
,tmIdVend,tmComision,tmTipoAplica,tmVrBaseCms,tmCodTarDct,tmCodTarCom
,tmIdCliente,tmIdAgencia,tmNumero
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonosG @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac,SUM(tmVrAbono) AS STotAbo,SUM(tmVrDescto) AS STotDct
,SUM(tmVrBaseCms) AS STotBase
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero 
 GROUP BY  tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonosItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonosTip @pmtmNumero VARCHAR(5),@pmtmTipoAplica VARCHAR(10)
 AS 
 SELECT tmNumero,tmItem,tmTipDoc,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmVrDescto,tmVrBaseCms,tmIdCliente
,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmTipoAplica,tmCodTarDct,tmCodTarCom
,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero  AND tmTipoAplica LIKE ISNULL(@pmtmTipoAplica,'%')
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxcAbonosTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCAN,SUM(tmVrAbono) AS STotAbo,SUM(tmVrDescto) AS STotDct,SUM(tmVrBaseCms) AS STotCom
 FROM tm_CxcAbonos
 WHERE tmNumero=@pmtmNumero 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxpAplica @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmItem,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmDetAbono,tmIdProveedor,tmProveedor,tmIdCuenta
,tmTipDev,tmNumDev,tmpVehiculo,tmVehPropio,tmNumero
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxpAplicaFac @pmtmNumero VARCHAR(5),@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15)
,@pmtmIdCiaFac CHAR(2),@pmtmItemFac INT,@pmtmIdProveedor VARCHAR(16)
 AS 
 SELECT tmNumero,tmItem,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmVrAbono,tmIdProveedor
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero AND tmTipFac=@pmtmTipFac  AND tmFactura=@pmtmFactura
 AND tmIdCiaFac=@pmtmIdCiaFac  AND tmItemFac=@pmtmItemFac  AND  tmIdProveedor=@pmtmIdProveedor
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxpAplicaG @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmIdProveedor,SUM(tmVrAbono) AS STOTABO
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero
 GROUP BY tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmIdProveedor

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxpAplicaID @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_CxpAplicaTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrAbono) AS STOTABO
 FROM tm_CxpAplica
 WHERE tmNumero=@pmtmNumero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_CxpagarGru @pmtmNumero VARCHAR(5),@pmGruVeh BIT
AS
 IF @pmGruVeh<>0  --AGRUPADO POR VEHICULO
  SELECT  tmNumero,tmIdProveedor, tmFecVence,tmpVehiculo,tmVehPropio,SUM(tmVrFactura) AS STotVal 
  FROM     tm_Cxpagar WHERE tmNumero=@pmtmNumero
 GROUP BY    tmNumero,tmIdProveedor, tmFecVence,tmpVehiculo,tmVehPropio 
ELSE
  SELECT  tmNumero,tmIdProveedor,tmFecVence,SUM(tmVrFactura) AS STotVal 
  FROM     tm_Cxpagar WHERE tmNumero=@pmtmNumero
 GROUP BY    tmNumero,tmIdProveedor, tmFecVence

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DetCue @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmConcepto VARCHAR(10)=Null
 AS 
 SELECT tmNumero,tmItem,tmConcepto,tmIdCuenta,tmValor,tmDetalle,tmIdTercero,tmCodAgncia,tmCdCCosto,tmCdSubCos
,tmpVehiculo,tmVrBase,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCheque,tmCiuOrigen,tmReferencia
 FROM tm_DetCue
 WHERE tmNumero=@pmtmNumero AND tmConcepto LIKE ISNULL(@pmtmConcepto,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DetCueDso @pmtmNumero VARCHAR(5),@pmtmConcepto VARCHAR(10)=Null
 AS 
 SELECT tmConcepto,tmItem,tmIdCuenta,tmValor,tmDetalle,tmVrBase,tmIdTercero,RazonSocial,tmCodAgncia
,tmCdCCosto,CCosto,tmCdSubCos,tmpVehiculo,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence,tmCheque
,tmReferencia,tmCiuOrigen,Localidad
 FROM tm_DetCue AS D LEFT JOIN Terceros AS T ON D.tmIdTercero=T.IdTercero
 LEFT JOIN CentroCosto AS CC ON D.tmCdCCosto=CC.IdCCosto
 LEFT JOIN Localidades AS L ON D.tmCiuOrigen=L.IdLocal
 WHERE tmNumero=@pmtmNumero AND tmConcepto LIKE ISNULL(@pmtmConcepto,'%')
 ORDER BY tmConcepto,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DetCueFac @pmtmNumero VARCHAR(5),@pmtmConcepto VARCHAR(10)
,@pmtmIdTercero VARCHAR(16),@pmtmTipFac VARCHAR(3),@pmtmFactura VARCHAR(15),@pmtmIdCiaFac CHAR(2)
,@pmtmItemFac INT,@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmConcepto,tmIdCuenta,tmValor,tmDetalle,tmIdTercero
,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
 FROM tm_DetCue AS D INNER JOIN Puc AS P ON D.tmIdCuenta=P.IdCuenta
 WHERE tmNumero=@pmtmNumero AND Vencimiento<>0 AND tmIdTercero=@pmtmIdTercero
 AND tmTipFac=@pmtmTipFac AND tmFactura=@pmtmFactura AND tmIdCiaFac=@pmtmIdCiaFac
 AND tmItemFac=@pmtmItemFac AND tmItem<>@pmtmItem
 AND  tmConcepto LIKE ISNULL(@pmtmConcepto,'%')
 ORDER BY  tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DetCueItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM FROM tm_DetCue
 WHERE tmNumero=@pmtmNumero 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DetCueTot @pmtmNumero VARCHAR(5),@pmtmConcepto VARCHAR(10)=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmValor) AS SVALTOT,SUM(tmVrBase) AS SBASE
 FROM tm_DetCue
 WHERE tmNumero=@pmtmNumero AND tmConcepto LIKE ISNULL(@pmtmConcepto,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DetCueVce @pmtmNumero VARCHAR(5),@pmtmConcepto VARCHAR(10)=Null
 AS 
 SELECT tmNumero,tmItem,tmConcepto,tmIdCuenta,tmValor,tmIdTercero,tmTipFac,tmFactura,tmIdCiaFac,tmItemFac,tmFecVence
,Naturaleza,Vencimiento
 FROM tm_DetCue AS D INNER JOIN Puc AS P ON D.tmIdCuenta=P.IdCuenta
 WHERE tmNumero=@pmtmNumero AND Vencimiento<>0 AND tmConcepto LIKE ISNULL(@pmtmConcepto,'%')
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Diario @pmtmEst CHAR(2)
 AS 
 SELECT TipDoc,TipoDoc,Numero,D.IdCia AS CdCia,Compania,Fecha,D.IdConcepto AS CdConcepto,Concepto,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,D.IdAgencia AS Id_Agencia,Agencia
,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion,VrReteIca,VrReteIva,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto
,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,DescripProd,Cantidad,EnEfectivo
,CdForma,FormaPago,NumForma,CdBanco,Banco,DetallePagos,CdCta,NumeroCta,FecCheque,D.IdVend AS CdVend,V.RazonSocial AS Vendedor
,TarifaCom,nClieCon,Nombre,IdCajero,Usuario,D.IdLocal AS CdLocal,Localidad,L.IdDep AS CdDpto,Departamento,D.Referencia AS DetReferencia,Referencia2,Beneficiario
,Modalidad,pVehiculo,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,D.Observacion AS Observ,TimeSys,tmId,tmEst
--datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen
,A.Referencia AS AgeReferencia
--datos del producto
,P.Referencia AS Prod_Referencia,TipoRef,P.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,P.IdMarca AS CdMarca,Marca
 FROM tm_Diario AS D   INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS T ON  D.IdTercero=T.IdTercero 
 INNER JOIN Terceros AS V ON  D.IdVend=V.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN Localidades AS L ON D.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN ClieContado AS CC ON D.nClieCon=CC.IdCliente  LEFT JOIN Sys_TiposDoc AS TD ON D.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTA ON D.CdCta=CTA.IdCta LEFT JOIN Bancos AS B ON D.CdBanco=B.IdBanco
 LEFT JOIN  Agencias  AS A ON D.IdAgencia=A.IdAgencia LEFT JOIN Formaspago AS FP ON D.CdForma=FP.IdForma
 LEFT JOIN ProdMcias AS P ON D.CdProducto=P.IdProducto 
 LEFT JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo LEFT JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 LEFT JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea LEFT JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 WHERE  tmEst=@pmtmEst
 ORDER BY TipDoc,tmId
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DiarioFor @pmtmEst CHAR(2)
 AS 
 SELECT D.TipDoc AS IdTipDoc,TipoDoc,Numero,D.IdCia AS CdCia,Compania,D.Fecha AS FechaDoc,P.IdForma AS CdForma,FormaPago,Detalle
,CASE P.IdForma WHEN NULL THEN VrEfectivo ELSE VrPagado  END AS ValPagado,VrCambio,EsCaja
,P.NumForma AS NumeroForma,P.IdBanco AS CodBanco,B.Banco AS NomBanco,D.IdTercero AS NitTercero,T.RazonSocial AS NomTercero,D.IdAgencia AS Id_Agencia,A.Agencia AS DocAgencia
,D.IdVend AS CdVend,V.RazonSocial AS Vendedor,TarifaCom,nClieCon,Nombre,IdCajero,Usuario,D.IdLocal AS CdLocal,L.Localidad AS NomCiudad,L.IdDep AS CdDpto,Departamento
,CtaForma,P.Beneficiario AS NomBeneficiario,Referncia1,Referncia2,CdLocal,LF.Localidad AS CiudadPlaza,NumAutoriza,VrDenom,P.Cantidad AS CantDenom,TipDenom
,P.TipRef AS PagTipRef,P.DocRef AS PagDocRef,P.IdCiaRef AS PagIdCiaRef,P.NitCliente AS PagNitCliente,TP.RazonSocial AS PagNomCliente
,CdAgencia,AP.Agencia AS PagAgencia,AP.CodAgencia AS PagCodAgenc,VrSubTotal,VrDescuento,VrImpuesto,VrSobretasa,VrRetencion
,VrReteIca,VrReteIva,VrOtros,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto
,VrEfectivo,VrPagosOtr,VrCreditos,VrValesGastos,CantPuntos,CdProducto,DescripProd,D.Cantidad AS CantArtic
,EnEfectivo,CdBanco,BD.Banco AS DocBanco,DetallePagos,D.CdCta AS DocIdCta,NumeroCta,FecCheque,D.Referencia AS DetReferencia,Referencia2,D.Beneficiario AS BenefCheque
,D.IdConcepto AS CdConcepto,Concepto,Modalidad,pVehiculo,D.TipRef AS DocTipRef,D.DocRef AS DocNumRef,D.IdCiaRef AS DocCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev
,FecDev,D.Observacion AS Observ,TimeSys,tmId,tmEst
--datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen
,A.Referencia AS AgeReferencia
 FROM tm_Diario AS D   INNER JOIN Companias AS CI ON D.IdCia=CI.IdCia INNER JOIN Terceros AS T ON  D.IdTercero=T.IdTercero 
 INNER JOIN Terceros AS V ON  D.IdVend=V.IdTercero INNER JOIN  Conceptos AS CN ON D.IdConcepto=CN.IdConcepto 
 INNER JOIN adm_Usuarios AS UC ON D.IdCajero=UC.IdUsuario 
 INNER JOIN Localidades AS L ON D.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN ClieContado AS CC ON D.nClieCon=CC.IdCliente  LEFT JOIN Sys_TiposDoc AS TD ON D.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTA ON D.CdCta=CTA.IdCta LEFT JOIN Bancos AS BD ON D.CdBanco=BD.IdBanco
 LEFT JOIN  Agencias  AS A ON D.IdAgencia=A.IdAgencia LEFT JOIN ProdMcias AS PM ON D.CdProducto=PM.IdProducto 
 LEFT JOIN Trn_Pagos AS P ON D.TipDoc=P.TipDoc AND D.Numero=P.Documento AND D.IdCia=P.IdCia
 LEFT JOIN Formaspago AS FP ON P.IdForma=FP.IdForma  LEFT JOIN Bancos AS B ON P.IdBanco=B.IdBanco
 LEFT JOIN Terceros AS TP ON P.NitCliente=TP.IdTercero  LEFT JOIN Agencias  AS AP ON P.CdAgencia=AP.IdAgencia
 LEFT JOIN Localidades AS LF ON P.CdLocal=LF.IdLocal 
 WHERE  tmEst=@pmtmEst
 ORDER BY D.TipDoc,tmId
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DifIngreso @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdDiferido,tmFecha,tmPeriodos,tmVrBase,tmMejoras,tmDisminucion,tmAjustes,tmAmortiza
,tmAmortizaAju,tmAjuInfAmtza,tmMesesAcum,tmCorrMonDif,tmMesesCorr,tmFecUltAmtza,tmConcepto
 FROM tm_DifIngreso
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_DifIngresoDso @pmtmNumero VARCHAR(5) 
 AS 
 SELECT tmItem,tmIdDiferido,NomDiferido,tmFecha,tmVrBase,tmMejoras,tmDisminucion,tmVrBase+tmMejoras-tmDisminucion AS TotalBase
,tmAjustes,tmAmortiza,tmAmortizaAju,tmAjuInfAmtza,tmPeriodos,tmCorrMonDif,tmMesesCorr,tmFecUltAmtza,tmMesesAcum,tmConcepto
,IdEstado,IdCCosto,IdSubCos,IdDep,IdRespons,IdCia
 FROM tm_DifIngreso AS T INNER JOIN Diferidos AS D ON T.tmIdDiferido=D.IdDiferido
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_EVcau @pmtmEst CHAR(2),@pmtmTipo VARCHAR(3)=Null,@pmtmIdCia CHAR(2)=Null,@pmtmIdCliente VARCHAR(16)=Null,@pmtmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT tmTipo,TipoDoc,tmDocmnto,tmIdCia,compania,tmItem,tmFecha,tmGrupo,tmIdCargo,F.Concepto AS CargoFijo,tmIdConcepto,COC.Concepto AS NomConcepto,tmValor,tmValorDev,tmIdCliente,T.RazonSocial AS NomPropietario,tmIdVehiculo
,tmVehPropio,tmIdConductor,NC.RazonSocial AS NomConductor,tmModalidad,tmValorTotal,tmGalones,tmVrComb,tmDetalle,tmNumCheque,tmReferencia,tmIdClase,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev
,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmVrGastos,tmIdUsuario,Usuario,tmTimeSys,tmEst
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM tm_EVcau AS EC INNER JOIN Terceros AS T ON EC.tmIdCliente=T.IdTercero INNER JOIN Vehiculos AS V ON EC.tmIdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NC ON EC.tmIdConductor=NC.IdTercero INNER JOIN Sys_TiposDoc  AS TD ON EC.tmTipo=TD.IdDoc
 INNER JOIN Fijos AS F ON EC.tmIdCargo=F.IdCargo INNER JOIN Conceptos AS COC ON EC.tmIdConcepto=COC.IdConcepto
 INNER JOIN adm_Usuarios AS U ON EC.tmIdUsuario=U.IdUsuario
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa     LEFT JOIN TercCndtores AS CT ON  EC.tmIdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
  LEFT JOIN Companias  AS CI ON EC.tmIdCia=CI.IdCia
 WHERE tmEst=@pmtmEst AND tmTipo LIKE ISNULL(@pmtmTipo,'%')  AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') 
 AND tmIdCliente LIKE ISNULL(@pmtmIdCliente,'%') AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
 ORDER BY tmIdCliente,tmIdVehiculo,tmTipo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_EVcauVeh @pmtmEst CHAR(2)
 AS 
 SELECT tmEst,tmIdCliente,tmIdVehiculo,tmIdConductor,tmVehPropio,SUM(tmValor) AS SValor,SUM(tmValorDev) AS SValDev
,SUM(tmGalones) AS Sgalones,SUM(tmVrComb) AS SVrComb,SUM(tmVrGastos) AS StGastos
 FROM tm_EVcau
 WHERE tmEst=@pmtmEst
 GROUP BY tmEst,tmIdCliente,tmIdVehiculo,tmIdConductor,tmVehPropio

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_EVrec @pmtmEst CHAR(2),@pmtmTipo VARCHAR(3)=Null,@pmtmIdCia CHAR(2)=Null,@pmtmIdCliente VARCHAR(16)=Null,@pmtmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT tmTipo,TipoDoc,tmDocmnto,tmIdCia,compania,tmItem,tmFecha,tmGrupo,tmValor,tmValorDev,tmIdCliente,T.RazonSocial AS NomPropietario,tmIdVehiculo,tmVehPropio,tmIdConductor,NC.RazonSocial AS NomConductor,tmModalidad,tmTipCar,tmCartulina,tmIdCiaCar,tmFechaRec
,tmCentInicial,tmCentFinal,tmNPasajeros,tmRecorridos,tmValorTotal,tmValorOtros,tmTotalProducido,tmTotalGastos,tmTotalAhorro,tmTotalOtros,tmTotalVales,tmTotalAbono,tmValorPasaje,tmDetalle,tmNumCheque,tmReferencia,tmIdConcepto,Concepto
,tmIdBanco,Banco,tmIdClase,tmIdRuta,Ruta,tmAnulado,tmNumDev,tmFecDev,tmTipDcm,tmDocDev,tmIdCiaDcm,tmFecDcm,tmIdEstado,tmIdUsuario,Usuario,tmTimeSys,tmEst
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM tm_EVrec  AS EC INNER JOIN Terceros AS T ON EC.tmIdCliente=T.IdTercero INNER JOIN Vehiculos AS V ON EC.tmIdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NC ON EC.tmIdConductor=NC.IdTercero INNER JOIN Sys_TiposDoc AS TD ON EC.tmTipo=TD.IdDoc
 INNER JOIN adm_Usuarios AS U ON EC.tmIdUsuario=U.IdUsuario INNER JOIN Bancos AS B ON EC.tmIdBanco=B.IdBanco
 INNER JOIN Rutas AS R ON EC.tmIdRuta=R.IdRuta INNER JOIN Conceptos AS COC ON EC.tmIdConcepto=COC.IdConcepto
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
  LEFT JOIN TercCndtores AS CT ON  EC.tmIdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar  LEFT JOIN Companias  AS CI ON EC.tmIdCia=CI.IdCia
 WHERE tmEst=@pmtmEst AND tmTipo like ISNULL(@pmtmTipo,'%')  AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') 
 AND tmIdCliente LIKE ISNULL(@pmtmIdCliente,'%') AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
 ORDER BY tmIdCliente,tmIdVehiculo,tmTipo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_EVrecEst @pmtmEst CHAR(2),@pmtmGrupo VARCHAR(3)=Null,@pmtmModalidad VARCHAR(10)=Null
 AS 
 SELECT tmIdCliente,tmIdVehiculo,tmTipo,COUNT(tmDocmnto) AS SCANT,SUM(tmValor) AS SValor,SUM(tmValorDev) AS SValDev,SUM(tmNPasajeros) AS SNpasj,SUM(tmRecorridos) AS SRecorr
,SUM(tmTotalProducido) AS STotProd,MAX(tmTotalProducido) AS MXTotProd,MIN(tmTotalProducido) AS MinTotProd
 FROM tm_EVrec
 WHERE tmEst=@pmtmEst AND tmTipo IN ('RC1','OD1') AND tmGrupo LIKE ISNULL(@pmtmGrupo,'%')
 AND tmModalidad LIKE ISNULL(@pmtmModalidad,'%')
 GROUP BY tmIdCliente,tmIdVehiculo,tmTipo
 ORDER BY tmIdCliente,tmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormas @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma
,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo
,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza
,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero 
  AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasChe @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma
,tmCtaForma,tmBeneficiario,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdVend,tmReferencia,tmReferncia2
,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero  AND tmCredito=0
 AND (tmIdForma IN ('CHE','BON','CON','TRA') OR tmIdForma LIKE 'CH%')
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasCmp @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdForma,FormaPago,tmObservacion,tmNForma,tmFecForma,tmCtaForma,tmReferencia,tmReferncia2
,tmCdCta,tmNumAutoriza,tmIdCliente,tmIdAgencia,tmpVehiculo,tmnVehiculo,tmCdCCosto,tmCdSubCos
,SUM(tmVrTotal) AS STOTPAG
 FROM tm_EdsFormas AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 WHERE tmNumero=@pmtmNumero AND tmCredito=0
 GROUP BY tmIdForma,FormaPago,tmObservacion,tmNForma,tmFecForma,tmCtaForma,tmReferencia,tmReferncia2
,tmCdCta,tmNumAutoriza,tmIdCliente,tmIdAgencia,tmpVehiculo,tmnVehiculo,tmCdCCosto,tmCdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasCpla @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrTotal) AS STOTAL,SUM(tmCantPuntos) AS STOTPUN
,SUM(tmCantidad) AS STOTCAN
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito<>0 AND tmEsCaja=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasCre @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipoRango,COUNT(tmItem) AS SCANT,SUM(tmVrTotal) AS STOTAL,SUM(tmCantPuntos) AS STOTPUN
,SUM(tmCantidad) AS STOTCAN,SUM(tmVrDenom*tmCantDenm) AS STOTDEN,SUM(tmCantDenm) AS SCANDEN
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito<>0
 GROUP BY tmTipoRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasCue @pmtmNumero VARCHAR(5),@pmIdConcepto VARCHAR(4)
 AS 
 SELECT  tmIdForma,IdConcepto,IdCuenta
 FROM tm_EdsFormas AS EF INNER JOIN FormasCue AS F ON EF.tmIdForma=F.IdForma
 WHERE tmNumero=@pmtmNumero  AND tmCredito=0  AND IdConcepto=@pmIdConcepto
 ORDER BY tmIdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasDsc @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmTipoRango,tmIdRango,tmNumSerie,tmNumForma,tmVrTotal,tmIdCuenta,tmIdCliente,T.RazonSocial AS NomCliente
,tmIdAgencia,tmpVehiculo,tmnVehiculo,tmCdProducto,DescripProd,tmCantidad,tmReferencia,tmObservacion,tmIdForma,FormaPago
,tmIdUsuario,tmIdVend,V.RazonSocial AS Vendedor,tmCantPuntos,tmCdCCosto,tmCdSubCos
 FROM tm_EdsFormas  AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 INNER JOIN Terceros AS T ON P.tmIdCliente=T.IdTercero
 LEFT JOIN Terceros AS V ON P.tmIdVend=V.IdTercero
 LEFT JOIN ProdMcias AS PD ON P.tmCdProducto=PD.IdProducto
 WHERE tmNumero=@pmtmNumero  AND tmCredito<>0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCredito,tmIdForma,FormaPago,tmTipoRango,tmIdRango,tmNumSerie,tmNumForma,tmNForma,tmVrTotal
,tmIdBanco,Banco,tmObservacion,tmIdCuenta,tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,tmpVehiculo,tmnVehiculo
,tmIdVend,V.RazonSocial AS Vendedor,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmFecForma,tmCtaForma,tmBeneficiario
,tmIdUsuario,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmEsCaja,tmCantPuntos
,tmTipAplica,tmNumero,tmCdCCosto,tmCdSubCos
 FROM tm_EdsFormas  AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 INNER JOIN Bancos AS B ON P.tmIdBanco=B.IdBanco
 INNER JOIN Terceros AS T ON P.tmIdCliente=T.IdTercero
 LEFT JOIN Terceros AS V ON P.tmIdVend=V.IdTercero
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasEfe @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrTotal) AS STOTAL,SUM(tmCantPuntos) AS STOTPUN
,SUM(tmCantidad) AS STOTCAN,SUM(tmVrDenom*tmCantDenm) AS STOTDEN,SUM(tmCantDenm) AS SCANDEN
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito=0 AND tmIdForma='EFE'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasFac @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmTipoRango,tmIdRango,tmNumSerie,tmNumForma,tmEsCaja,tmVrTotal,tmIdCliente
,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo,tmIdVend,tmCdProducto,tmCantidad,tmReferencia
,TipDcm,Documento,IdCiaDcm,FechaDcm,AgencDcm,Anulado,Planilla,IdCia,IdCliente,IdAgencia
,tmCdCCosto,tmCdSubCos
 FROM tm_EdsFormas AS F INNER JOIN Trn_EdsFormas AS EF ON F.tmTipoRango=EF.TipoRango
 AND F.tmIdRango=EF.IdRango AND F.tmNumSerie=EF.NumSerie AND F.tmNumForma=EF.NumForma
 WHERE tmNumero=@pmtmNumero AND tmCredito<>0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasFma @pmtmNumero VARCHAR(5),@pmtmIdForma VARCHAR(4)
,@pmtmIdBanco VARCHAR(4),@pmtmNForma VARCHAR(20),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdForma,tmIdBanco,tmNForma,tmFecForma,tmCtaForma
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero  AND tmCredito=0 AND tmIdForma=@pmtmIdForma
  AND tmIdBanco=@pmtmIdBanco  AND tmNForma=@pmtmNForma AND tmItem<>@pmtmItem
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasPro @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipoRango,tmIdRango,tmIdForma,tmNumSerie,tmNumForma,tmVrTotal
,tmObservacion,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo
,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos
--VARIABLES DE ITEM
,Item,IdProducto,IdBodega,Cantidad,VrPrecio,TarifaIva,VrIva,TarifaDct,VrDcto,Servicios,Descripcion,ListaPrec
 FROM tm_EdsFormas AS F INNER JOIN Trn_EdsForItems AS I ON F.tmTipoRango=I.TipoRango
 AND F.tmIdRango=I.IdRango AND F.tmNumSerie=I.NumSerie AND F.tmNumForma=I.NumForma
 WHERE tmNumero=@pmtmNumero AND tmCredito<>0 AND tmEsCaja<>0
 ORDER BY tmItem,Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasRan @pmtmNumero VARCHAR(5),@pmtmTipoRango VARCHAR(3),@pmtmIdRango INT
,@pmtmNumSerie VARCHAR(5),@pmtmNumForma INT,@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmVrTotal,tmNumSerie,tmNumForma,tmIdCliente,tmIdAgencia,tmIdCuenta
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito<>0  AND tmTipoRango=@pmtmTipoRango AND tmIdRango=@pmtmIdRango
 AND tmNumSerie=@pmtmNumSerie AND tmNumForma=@pmtmNumForma
 AND tmItem<>@pmtmItem
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasTip @pmtmNumero VARCHAR(5),@pmtmCredito BIT
 AS 
 SELECT tmNumero,tmItem,tmCredito,tmTipoRango,tmIdRango,tmIdForma,tmObservacion,tmEsCaja,tmIdBanco,tmNForma,tmFecForma
,tmCtaForma,tmBeneficiario,tmNumSerie,tmNumForma,tmVrTotal,tmIdCliente,tmIdAgencia,tmIdCuenta,tmpVehiculo,tmnVehiculo
,tmIdVend,tmCdProducto,tmCantidad,tmReferencia,tmReferncia2,tmCantPuntos,tmCdLocal,tmNumAutoriza
,tmVrDenom,tmCantDenm,tmTipDenom,tmCdCta,tmTipAplica,tmIdUsuario,tmCdCCosto,tmCdSubCos
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero AND tmCredito=@pmtmCredito
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EdsFormasTot @pmtmNumero VARCHAR(5),@pmtmCredito BIT=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrTotal) AS STOTAL,SUM(tmCantPuntos) AS STOTPUN
,SUM(tmCantidad) AS STOTCAN,SUM(tmVrDenom*tmCantDenm) AS STOTDEN,SUM(tmCantDenm) AS SCANDEN
 FROM tm_EdsFormas
 WHERE tmNumero=@pmtmNumero 
 AND (tmCredito=ISNULL(@pmtmCredito,0) or tmCredito=ISNULL(@pmtmCredito,1))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstClientes @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,Agencia,tmIdCia,Compania
 ,tmFecUltFcr,tmPPlazoFcr,tmMayorFcr,tmMenorFcr,tmFecUltFco,tmPPlazoFco,tmMayorFco,tmMenorFco,tmVentaBruto
,tmVrDevolucion,tmVrDescuentos,tmCostoBruto,tmCostoDev,tmVrIva,tmFecUltPago,tmPromPago,tmSaldoMora,tmFecIniMora,tmDiasEnMora
,tmChequesDev,tmSaldoActual,tmSaldoAnterior,tmVrFacturas,tmVrDevFacturas,tmVrRecibos,tmVrDevRecibos,tmVrNotasDeb,tmVrNotasCre,tmVrCheques
--Datos del cliente
,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigla,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,CLI.IdForma AS CdForma,ExcIva,TrfIntMora,CLI.DiasGracia AS CliDiasGracia,LiqFletes,Autoret
,CLI.Inactivo AS ClieInactivo,CLI.CdBandera AS CliCdbandera,TipoBandera,CLI.IdPlazo AS CdPlazo,Plazo,DiasPago,CLI.NContrato AS CliNumContrato
,CLI.IdVend AS CdVend,VN.RazonSocial AS Vendedor
 FROM tm_EstClientes AS TC INNER JOIN Terceros AS T ON TC.tmIdCliente=T.IdTercero  INNER JOIN TercCliente AS CLI ON TC.tmIdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON CLI.IdVend=VN.IdTercero   INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona   INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Companias AS CI ON TC.tmIdCia=CI.IdCia   LEFT JOIN Agencias AS A ON TC.tmIdAgencia=A.IdAgencia
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 WHERE tmEst=@pmtmEst
 ORDER BY T.RazonSocial

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstClientesMov @pmtmEst CHAR(2)
 AS 
 SELECT TC.tmIdCliente AS NitCliente,T.RazonSocial AS NomCliente,TC.tmIdAgencia AS CdAgencia,A.Agencia AS NomAgencia,TC.tmIdCia AS CdCia,CI.Compania AS NomCia
 ,tmFecUltFcr,tmPPlazoFcr,tmMayorFcr,tmMenorFcr,tmFecUltFco,tmPPlazoFco,tmMayorFco,tmMenorFco,tmVentaBruto
,tmVrDevolucion,tmVrDescuentos,tmCostoBruto,tmCostoDev,tmVrIva,tmFecUltPago,tmPromPago,tmSaldoMora,tmFecIniMora,tmDiasEnMora
,tmChequesDev,tmSaldoActual,tmSaldoAnterior,tmVrFacturas,tmVrDevFacturas,tmVrRecibos,tmVrDevRecibos,tmVrNotasDeb,tmVrNotasCre,tmVrCheques
--Datos del cliente
,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigla,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,CLI.IdForma AS CdForma,ExcIva,TrfIntMora,CLI.DiasGracia AS CliDiasGracia,LiqFletes,Autoret
,CLI.Inactivo AS ClieInactivo,CLI.CdBandera AS CliCdbandera,TipoBandera,CLI.IdPlazo AS CdPlazo,Plazo,DiasPago,CLI.NContrato AS CliNumContrato
,CLI.IdVend AS CdVend,VN.RazonSocial AS Vendedor
--datos de movimientos
,M.tmIdAgencia AS MovAgencia,AM.Agencia AS MovNomAgencia,M.tmIdCia AS MovCodCia,CM.Compania AS MovNomCia,tmTipDoc,tmNumero,M.tmItem AS MovItem,tmFecha,tmCargos,tmAbonos,tmDetalle
,tmReferencia,tmNumForma,tmModalidad,tmTipRef,tmDocRef,tmIdCiaRef,tmCdConcepto
,tmTotalAbono,tmVrAfavor,tmTimeSys,tmFecVence,tmCdForma,tmEnEfectivo,tmCdBanco,tmBeneficiario,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario
 FROM tm_EstClientes AS TC INNER JOIN Terceros AS T ON TC.tmIdCliente=T.IdTercero  INNER JOIN TercCliente AS CLI ON TC.tmIdCliente=CLI.IdClie
 INNER JOIN Terceros AS VN ON CLI.IdVend=VN.IdTercero   INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona   INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN  Companias AS CI ON TC.tmIdCia=CI.IdCia   LEFT JOIN Agencias AS A ON TC.tmIdAgencia=A.IdAgencia
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 LEFT JOIN tm_MovClientes AS M ON TC.tmEst=M.tmEst AND TC.tmIdCliente=M.tmIdCliente
 AND TC.tmIdAgencia=M.tmIdAgencia AND TC.tmIdCia=M.tmIdCiaCom
 LEFT JOIN Agencias AS AM ON M.tmIdAgencia=AM.IdAgencia   LEFT JOIN  Companias AS CM ON M.tmIdCia=CM.IdCia
 WHERE TC.tmEst=@pmtmEst
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstCueCte @pmtmEst CHAR(2)
AS
SELECT  tmIdCliente AS NitPropietario,T.RazonSocial AS Propietario,tmIdVehiculo,NumVeh,tmTipDoc,TipoDoc, tmDocumento, tmIdCia, tmItem, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmIdCargo,F.Concepto AS Cargo,tmIdClase,ClaseCuenta
,tmIdConcepto,CO.Concepto AS ConceptoDoc, tmFecUltPago, tmReferencia, tmDetalle, tmFactura, tmIdVendedor AS CedConductor,NC.RazonSocial AS Conductor,tmTipCar, tmCartulina, tmIdCiaCar, tmModalidad, tmCentInicial,tmCentFinal, tmNPasajeros, tmIdBanco,Banco
, tmNumCheque, tmAnulado, tmNumDev, tmFecDev, tmTipDcm, tmDocDev, tmIdCiaDcm, tmFecDcm, tmIdEstado,Estado,tmIdUsuario,Usuario,Compania,tmTimeSys
,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,Modelo,FecRep,V.IdAdmon AS CdAdmon,TipoAdmon,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa
 FROM   tm_EstCueCte AS C INNER JOIN Companias  AS CI ON C.tmIdCia=CI.IdCia
 INNER JOIN  Terceros AS T ON C.tmIdCliente=T.IdTercero  INNER JOIN  Vehiculos AS V ON C.tmIdVehiculo=V.IdVehiculo
 INNER JOIN  Fijos AS F ON C.tmIdCargo=F.IdCargo INNER JOIN  BusClaseCue  AS CU ON C.tmIdClase=CU.IdClase
 INNER JOIN  Bancos AS B ON C.tmIdBanco=B.IdBanco INNER JOIN  adm_Usuarios AS U ON C.tmIdUsuario=U.IdUsuario
 INNER JOIN  EstadoDoc AS ED ON C.tmIdEstado=ED.IdEstado  INNER JOIN  Terceros AS NC ON C.tmIdVendedor=NC.IdTercero
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo   LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
 LEFT JOIN  Conceptos AS CO ON C.tmIdConcepto=CO.IdConcepto  LEFT JOIN Sys_TiposDoc  AS TD ON C.tmTipDoc=TD.IdDoc
 WHERE tmEst=@pmtmEst 
 ORDER BY T.RazonSocial,tmIdVehiculo,tmFecha,tmIndTip,tmIdCia,tmDocumento,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQrytm_EstCueCtePro @pmtmEst CHAR(2),@pmtmIdCliente VARCHAR(16),@pmtmIdClase VARCHAR(4)=Null,@pmtmIdCia CHAR(2)=Null
,@pmtmIdVehiculo VARCHAR(10)=Null
AS
 SELECT tmEst,tmIdCia,tmIdCliente,tmIdVehiculo,tmIdClase,tmSalAntPro,tmSalAntProCla,tmSalAntProCia
 FROM  tm_EstCueCte
 WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente AND tmIdClase LIKE ISNULL(@pmtmIdClase,'%')
  AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
 ORDER BY tmIdCliente,tmIdVehiculo,tmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paQrytm_EstCueCteVeh @pmtmEst CHAR(2),@pmtmIdVehiculo VARCHAR(10),@pmtmIdClase VARCHAR(4)=Null,@pmtmIdCia CHAR(2)=Null
AS
 SELECT tmEst,tmIdCia,tmIdVehiculo,tmIdClase,tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia
 FROM  tm_EstCueCte
 WHERE tmEst=@pmtmEst AND tmIdVehiculo=@pmtmIdVehiculo AND tmIdClase LIKE ISNULL(@pmtmIdClase,'%')
  AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 ORDER BY tmIdVehiculo,tmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_EstCueCte_Cr @pmtmEst CHAR(2)
AS
SELECT  tmIdCliente AS NitPropietario,T.RazonSocial AS Propietario,tmIdVehiculo,tmTipDoc,TipoDoc, tmDocumento, tmIdCia, tmItem, tmFecha, tmFecVence, tmValorDebito, tmValorCredito, tmIdCargo,F.Concepto AS Cargo,tmIdClase,ClaseCuenta
,tmIdConcepto,COC.Concepto AS ConceptoDoc, tmFecUltPago, tmReferencia, tmDetalle, tmFactura, tmIdVendedor AS CedConductor,NC.RazonSocial AS Conductor,tmTipCar, tmCartulina, tmIdCiaCar, tmModalidad, tmCentInicial,tmCentFinal, tmNPasajeros, tmIdBanco,Banco
, tmNumCheque, tmAnulado, tmNumDev, tmFecDev, tmTipDcm, tmDocDev, tmIdCiaDcm, tmFecDcm, tmIdEstado,ED.Estado AS EstadoDoc,tmIdUsuario,Usuario,Compania,tmTimeSys
,tmIndTip,tmValorAbono,tmVehPropio, tmRecorridos, tmValorRecibido, tmTotalGastos,tmTotalVales, tmTotalAhorro, tmTotalOtros, tmTotalAbono,tmIntMora,tmCxPagar,tmSaldoAnt, tmSaldoAntCla, tmSaldoAntCia, tmSaldoAntDia,tmNuevoSaldo,tmSalAntPro,tmSalAntProCla,tmSalAntProCia
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
 FROM   tm_EstCueCte AS C INNER JOIN Companias  AS CI ON C.tmIdCia=CI.IdCia
 INNER JOIN Terceros AS T ON C.tmIdCliente=T.IdTercero  INNER JOIN Vehiculos AS V ON C.tmIdVehiculo=V.IdVehiculo
 INNER JOIN  Fijos AS F ON C.tmIdCargo=F.IdCargo INNER JOIN  BusClaseCue  AS CU ON C.tmIdClase=CU.IdClase
 INNER JOIN  Bancos AS B ON C.tmIdBanco=B.IdBanco INNER JOIN  adm_Usuarios AS U ON C.tmIdUsuario=U.IdUsuario
 INNER JOIN  EstadoDoc AS ED ON C.tmIdEstado=ED.IdEstado  INNER JOIN  Terceros AS NC ON C.tmIdVendedor=NC.IdTercero
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
 LEFT JOIN  Conceptos AS COC ON C.tmIdConcepto=COC.IdConcepto  LEFT JOIN Sys_TiposDoc  AS TD ON C.tmTipDoc=TD.IdDoc
 WHERE tmEst=@pmtmEst 
 ORDER BY T.RazonSocial,tmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstadoGyp @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
 AS 
 SELECT GP.tmIdCuenta AS CdCuenta,NomCuenta,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2,tmIdCia,Compania
,tmIdTercero,RazonSocial,tmCodAgncia,A.Agencia AS NomAgencia,tmIdVehiculo,tmIdCCosto,CCosto,tmIdSubCos,SubCosto
,TipoId,Dv,Direccion,T.IdLocal AS CdCiudad,Localidad,L.IdDep AS CdDepto,Departamento,Telefono 
--información de agencia y vehículos
,CodAgencia,Referencia--,NumVeh
,tmItem,tmEst,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_EstadoGyp AS GP INNER JOIN Puc AS P ON GP.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON GP.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep
 INNER JOIN CentroCosto AS CC ON GP.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON GP.tmIdSubCos=SC.IdSubCos
 LEFT JOIN tm_NivCue AS NV ON GP.tmIdCuenta=NV.tmIdCuenta
 LEFT JOIN  Agencias AS A ON GP.tmCodAgncia=A.IdAgencia
 LEFT JOIN Companias AS CI ON GP.tmIdCia=CI.IdCia
-- LEFT JOIN Vehiculos AS V ON GP.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst AND GP.tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 ORDER BY GP.tmIdCuenta,tmIdCia,RazonSocial,tmCodAgncia,tmIdCCosto,tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstadoGypCue @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdCia CHAR(2)=Null,@pmtmTipo VARCHAR(4)=Null
 AS 
 SELECT tmEst,tmIdCuenta,tmItem,tmIdCia,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2
 FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 AND tmTipo LIKE ISNULL(@pmtmTipo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstadoGypGcue @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst 
 GROUP BY tmIdCuenta 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_EstadoGypTer @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmTipo VARCHAR(4)
,@pmtmIdCia CHAR(2)=Null,@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdVehiculo VARCHAR(10)=Null
,@pmtmIdCCosto VARCHAR(16)=Null,@pmtmIdSubCos VARCHAR(16)=Null,@pmtmCodAgncia VARCHAR(16)=Null
 AS 
 SELECT tmEst,tmIdCuenta,tmItem,tmTipo,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos,tmNuevoSaldo
,tmSalAnt2,tmDebitos2,tmCreditos2,tmSaldo2
 FROM tm_EstadoGyp
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta  AND tmTipo=@pmtmTipo
 AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%') AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%')
 AND tmIdSubCos LIKE ISNULL(@pmtmIdSubCos,'%') AND tmCodAgncia LIKE ISNULL(@pmtmCodAgncia,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ExtVeh @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16),@pmtmIdVehiculo VARCHAR(10)
 AS 
 SELECT tmEst,tmIdPropietario,tmIdVehiculo,tmIdConductor,tmVehPropio,tmTotaDebito,tmTotaCredito,tmSaldoAnt,tmSaldoAntCla,tmSalAntPro
,tmSalAntProCla,tmTotalProducido,tmMaxProducido,tmMinProducido,tmTotalRecrridos,tmTotalPasaj,tmTotalDias,tmTotalCausac,tmGalones,tmValorComb
 FROM  tm_ExtVeh
 WHERE  tmEst=@pmtmEst AND tmIdPropietario=@pmtmIdPropietario AND tmIdVehiculo=@pmtmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ExtVehCr @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16)=Null,@pmtmIdVehiculo VARCHAR(10)=Null
 AS 
 SELECT tmIdPropietario,NP.RazonSocial AS Propietario,tmIdVehiculo,tmIdConductor,NC.RazonSocial AS Conductor,tmVehPropio,tmTotaDebito,tmTotaCredito,tmSaldoAnt,tmSaldoAntCla,tmSalAntPro
,tmSalAntProCla,EV.tmTotalProducido AS TotalProducido,EV.tmMaxProducido AS MaxProducido,EV.tmMinProducido AS MinProducido,EV.tmTotalRecrridos AS TotalRecrridos,EV.tmTotalPasaj AS TotalPasaj
,EV.tmTotalDias AS TotalDias,EV.tmTotalCausac AS TotalCausac,EV.tmGalones AS tmGalones,EV.tmValorComb AS ValorComb,EV.tmEst AS Id_Est
,ESV.tmTotalProducido AS EvTotalProducido,ESV.tmMaxProducido AS EvMaxProducido ,ESV.tmMinProducido AS EvMinProducido,ESV.tmTotalRecrridos AS EvTotalRecrridos,ESV.tmTotalPasaj AS EvTotalPasaj
,ESV.tmTotalDias AS EvTotalDias,ESV.tmTotalCausac AS EvTotalCausac,ESV.tmGalones AS EvGalones,ESV.tmValorComb AS EvValorComb,tmCantVeh
--Datos del vehículo
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdCom AS CdComb,TipoComb,NumMotor,SerieChasis,PasjerosPie,PasjerosSen
,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon
,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,VrNormal,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat,KmActual,CentInicial,CentFinal,VrLmtCred,VrSaldoAct,ObligaTProd,V.IdEstado AS VehCdEstado
,EVH.Estado AS VehEstado,V.Inactivo AS VehInactivo
--datos del propietario
,NP.Codigo AS PropCodigo,NP.TipoId AS PropTipoId,NP.Dv AS PropDv,NP.Direccion AS PropDireccion,NP.IdLocal AS PropCdCiudad,LP.Localidad AS PropCiudad,LP.IdDep AS PropCdDep
,DP.Departamento AS PropDpato,NP.Telefono AS PropTelefono,NP.Fax AS PropFax,NP.TelMovil AS PropTelMovil
--datos de conductor
,NC.Codigo AS CondCodigo,NC.Direccion AS CondDireccion,NC.IdLocal AS CondCdCiudad,LC.Localidad AS CondCiudad,LC.IdDep AS CondCdDpto,DC.Departamento AS CondDpto
,NC.Telefono AS CondTelefono,NC.Fax AS CondFax,NC.TelMovil AS CondTelMovil,NC.e_mail AS CondEmail,NC.EsPropietario AS CondEspropietario,NC.IdLugarCed AS CondCdLugarCed,LCE.Localidad AS CondLugarCed,NC.FecExpCed AS CondFechaExpCedula
,CT.IdModd AS CondCdMod,MCD.Modalidad AS ModConductor,TipoSangre,FactorRh,FecNacmto,LugarNacmto,Sexo,CertJudicial,VigJudicial,Licencia,CatLicencia,VigLicencia,CT.IdLugar AS CdLugarLic,LugarLic
 FROM  tm_ExtVeh AS EV INNER JOIN Terceros AS NP ON EV.tmIdPropietario=NP.IdTercero  INNER JOIN Vehiculos AS V ON EV.tmIdVehiculo=V.IdVehiculo
 INNER JOIN Terceros AS NC ON EV.tmIdConductor=NC.IdTercero
 INNER JOIN Localidades AS LP ON NP.IdLocal=LP.IdLocal INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 INNER JOIN Localidades AS LC ON NC.IdLocal=LC.IdLocal INNER JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
  INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
  INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
  INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EVH ON V.IdEstado=EVH.IdEstado
  INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
  LEFT JOIN TiposFuel AS TC ON  V.IdCom=TC.IdCom LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa    LEFT JOIN TercCndtores AS CT ON EV.tmIdConductor=CT.IdConductor
  LEFT JOIN  Localidades AS LCE ON NC.IdLugarCed=LCE.IdLocal LEFT JOIN TiposMod AS MCD ON CT.IdModd=MCD.IdModd
  LEFT JOIN ExpLicencias AS ELC ON CT.IdLugar=ELC.IdLugar
 LEFT JOIN  tm_EVEst AS ESV ON EV.tmEst=ESV.tmEst AND V.CdTarifa=ESV.tmIdTipo
 WHERE  EV.tmEst=@pmtmEst AND tmIdPropietario LIKE ISNULL(@pmtmIdPropietario,'%') AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
 ORDER BY NP.RazonSocial,tmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_ExtVehSan @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16)=Null
 AS 
 SELECT tmEst,tmIdPropietario,tmIdVehiculo,tmSaldoAnt,tmSaldoAntCla,tmSalAntPro,tmSalAntProCla
 FROM  tm_ExtVeh
 WHERE  tmEst=@pmtmEst AND tmIdPropietario LIKE ISNULL(@pmtmIdPropietario,'%')
 ORDER BY tmIdPropietario,tmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_ExtVehVeh  @pmtmEst CHAR(2),@pmtmIdVehiculo VARCHAR(10),@pmtmIdPropietario VARCHAR(16)
AS
 SELECT   tmEst,tmIdPropietario,tmIdVehiculo,tmIdConductor
 FROM tm_ExtVeh
 WHERE tmEst=@pmtmEst AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
  AND tmIdPropietario LIKE ISNULL(@pmtmIdPropietario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ForItems @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCantidad,tmVrPrecio,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto
,tmVrBruto,tmDescripcion,tmServcios,tmListaPrec
 FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647)) 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ForItemsDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,tmDescripcion,tmIdBodega,Bodega,tmCantidad,tmVrPrecio,tmListaPrec
,tmTarifaDct,tmVrDcto,tmTarifaIva,tmVrIva,((tmCantidad*tmVrPrecio)-tmVrDcto)+tmVrIva AS ValorTotal,tmServcios
 FROM tm_ForItems AS F INNER JOIN Bodegas AS B ON F.tmIdBodega=B.IdBodega
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ForItemsItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM 
 FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ForItemsTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCOUNT,SUM(tmCantidad) AS SCANT,SUM(tmCantidad*tmVrPrecio) AS SSUBTOT
,SUM(tmVrIva) AS SVALIVA,SUM(tmVrDcto) AS SVALDCT,SUM(tmVrBruto) AS SVALBRU
 FROM tm_ForItems
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_GypVentas @pmtmEst CHAR(2)
 AS 
SELECT   tmItem, tmIdCia,Compania, tmTipo, tmConcepto, tmEnero, tmFebrero, tmMarzo, tmAbril, tmMayo, tmJunio
, tmJulio, tmAgosto, tmSeptiembre, tmOctubre,tmNoviembre, tmDiciembre
 FROM  tm_GypVentas AS GT  LEFT  JOIN Companias AS CN ON  GT.tmIdCia=CN.IdCia
 WHERE tmEst=@pmtmEst 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_InfDiarioDet @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3)=Null
 AS 
 SELECT C.TipDoc AS CdTipDoc,TipoDoc,Numero,C.IdCia AS  CdCia,Compania,C.Fecha AS FechaDoc,C.IdTercero AS NitCliente,T.RazonSocial AS NomCliente
,C.IdAgencia AS Id_Agencia,Agencia,CodAgencia,nClieCon,Nombre,VrSubTotal,VrDescuento,VrImpuesto,VrRetencion,VrReteICA,VrReteIVA,VrFletes,VCargos,VrOtrosDctos,VrImpGlobal,VrSoldicom,VrCostos,VrNeto
,VrEfectivo,VrPagosOtr,VrCreditos,EnEfectivo,CdForma,NumForma,CdBanco,DetallePagos,C.IdConcepto AS CdConcepto,Concepto,Modalidad
,C.IdVend AS NitVend,V.RazonSocial AS Vendedor,TarifaCom,IdCajero,Usuario,U.IdEstacion AS CdEstacion,Estacion,C.IdLocal AS CdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CodDpto,DP.Departamento AS NomDpto
,C.Referencia AS Refrncia1,C.Referencia2 AS Refrncia2,TipRef,DocRef,IdCiaRef,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,C.Observacion AS Observ,C.TimeSys AS Fec_Add,tmItem
--DETALLES
,K.IdProducto AS CodProducto,DescripProd,K.IdBodega AS CdBodega,Bodega,Entradas,Salidas,K.IdUnd AS CdUnid,UM.Unidad AS UnidMedida,Unidades
,VrPrecio,K.TarifaIva AS KarTarifIva,VrIvaEnt,VrIvaSal,TarifaDct,VrDctoEnt,VrDctoSal,K.TarifaRet AS KarTarifRet,VrReteEnt,VrReteSal,K.TarifaIca AS KarTarifIca,VrIcaEnt,VrIcaSal
,Sobretasa,TasaNac,TasaDep,TasaMun,Soldicom,ImpGlobal,OtroImpto,VrUnitario,VrCostoEnt,VrCostoSal,VrCostProm,Descripcion,K.Referencia AS KarReferencia,Servcios,NoVentas,ListaPrec,VrBase,VrBruto,CdTanque
--productos
,TipoRef,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,PM.IdMarca AS CdMarca,Marca,Tamano,PM.UndMed AS CdUndMed
,SU.Unidad AS ProdUnidad,FecUltcom,FecUltVta,Tanques
--Datos del cliente de contado
,CC.TipoId AS CliTipoId,CC.Dv AS CliDv,CC.IdLocal AS CliIdCiudad,LC.Localidad AS CliCiudad,LC.IdDep AS CliCodDpto,DC.Departamento AS CliDpto,CC.Telefono AS Clitelefono,email
 FROM tm_InfDiario AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON  C.IdTercero=T.IdTercero
 INNER JOIN Terceros AS V ON  C.IdVend=V.IdTercero INNER JOIN  Agencias  AS A ON C.IdAgencia=A.IdAgencia
 INNER JOIN  Conceptos AS CN ON C.IdConcepto=CN.IdConcepto 
 INNER JOIN adm_Usuarios AS U ON C.IdCajero=U.IdUsuario  INNER JOIN Sys_Estaciones AS ET ON U.IdEstacion =ET.IdEstacion
 INNER JOIN Localidades AS L ON C.IdLocal=L.IdLocal INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep
 INNER JOIN Trn_Kardex AS K ON C.TipDoc=K.TipDoc AND C.Numero=K.Documento AND C.IdCia=K.IdCia
 INNER JOIN ProdMcias  AS PM ON K.IdProducto=PM.IdProducto INNER JOIN Bodegas AS B ON K.IdBodega=B.IdBodega
 INNER JOIN UndMed AS UM ON K.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Sys_Um AS SU ON PM.UndMed=SU.UndMed 
 LEFT JOIN ClieContado AS CC ON C.nClieCon=CC.IdCliente  LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN Localidades AS LC ON CC.IdLocal=LC.IdLocal LEFT JOIN Departamentos AS DC ON LC.IdDep=DC.IdDep
 WHERE tmEst=@pmtmEst  AND C.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 ORDER BY tmItem,C.TipDoc,Numero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_InfDiarioFor @pmtmEst CHAR(2),@pmTipDoc VARCHAR(3)=Null
 AS 
 SELECT C.TipDoc AS CdTipDoc,TipoDoc,Numero,C.IdCia AS  CdCia,Compania,C.Fecha AS FechaDoc
,C.IdTercero AS NitCliente,RazonSocial,IdCajero,Usuario,U.IdEstacion AS CdEstacion,Estacion
,P.IdForma AS CodForma,FormaPago,P.IdBanco AS CdBanco,Banco,SUM(VrPagado) AS ValPagado,SUM(VrCambio) AS ValCambio
 FROM tm_InfDiario AS C  INNER JOIN Companias AS CI ON C.IdCia=CI.IdCia INNER JOIN Terceros AS T ON  C.IdTercero=T.IdTercero
 INNER JOIN adm_Usuarios AS U ON C.IdCajero=U.IdUsuario  INNER JOIN Sys_Estaciones AS ET ON U.IdEstacion =ET.IdEstacion
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN Trn_Pagos AS P ON C.TipDoc=P.TipDoc AND C.Numero=P.Documento AND C.IdCia=P.IdCia
 LEFT JOIN Formaspago AS FP ON P.IdForma=FP.IdForma  LEFT JOIN Bancos AS BF ON P.IdBanco=BF.IdBanco
 WHERE tmEst=@pmtmEst  AND C.TipDoc LIKE ISNULL(@pmTipDoc,'%')
 GROUP BY  C.TipDoc,TipoDoc,Numero,C.IdCia,Compania,C.Fecha,C.IdTercero,RazonSocial,IdCajero,Usuario,U.IdEstacion,Estacion
,P.IdForma,FormaPago,P.IdBanco,Banco
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Ingresos @pmtmEst CHAR(2)
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmNMes,tmIdConcepto,tmDetalle,tmCantDevg,tmVrDevngado,tmCantDed,tmVrDeducido,tmVrBase,tmTarifa,tmBaseRet,tmClaseLiq
,tmOrigenLiq,tmFecInicial,tmFecFinal,tmDiasBase,tmDiasNoRem,tmIdNom,TipoNomina,tmIdDep,Dependencia,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdInstala,Instlacion,tmVrSalario,tmpVehiculo
,Concepto,ClaseCon,CN.Tarifa AS TrfaDft,tmCodFondo,tmEsRetiro,tmNPrestamo,tmItem
--datos del empleado
,Codigo,TipoId,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,VvdaPropia,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,IdFonCes,FC.Fondo AS FondoCes,Sexo,FecNacmto,IdEstCivil,EstCivil,NHijos,Conyugue,TipoSangre,FactorRh,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
,EsCondtor,pVehiculo,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,FecUltCes,FecUltPri,FecUltVac
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecVigencia,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.TipoLiquida AS TipLiq,C.IdJornada AS CodJnada,Jornada,Asistencia,NoDevenga,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie
,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,TipoTrabj,IdCot,ModPromdio,FPnomina,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,ModoRet,FecAfiSeg,DenyCes,DenyPri,DenyVac,BasePrest,C.IdEstado AS IdEstdo,Estado,C.Inactivo AS Con_inactivo,C.IdUsuario AS Con_IdUsuari,Usuario
 FROM tm_Ingresos  AS TI  INNER JOIN Empleados AS E ON TI.tmIdEmpleado=E.IdEmpleado  INNER JOIN NomConceptos AS CN ON TI.tmIdConcepto=CN.IdConcepto
 INNER JOIN  Emp_Contrato AS C ON TI.tmNContrato=C.NContrato
 INNER JOIN Dependencias AS DP ON TI.tmIdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON TI.tmIdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON TI.tmIdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON TI.tmIdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON TI.tmIdNom=TN.IdNom
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN Fondos AS FC ON E.IdFonCes=FC.IdFondo
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar  INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN  EstadoCiv AS EV ON E.IdEstCivil=EV.IdEstado  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN EstadoDoc AS EN ON C.IdEstado=EN.IdEstado INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase 
 WHERE tmEst=@pmtmEst 
 ORDER BY Apellidos,Nombres,tmNMes,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_IntAhorro @pmtmEst CHAR(2),@pmtmIdVehiculo VARCHAR(10)=Null,@pmtmIdTercero VARCHAR(16)=Null,@pmtmNmes INT=Null
 AS
 SELECT  tmEst, tmItem, tmIdVehiculo, tmIdTercero, tmSaldoBase, tmTasa, tmValor, tmNmes, tmVehPropio, tmIdConductor
, tmIdPropietario, tmConductor,tmTercero,tmRetencion,tmVrAbono
 FROM   tm_IntAhorro
 WHERE tmEst=@pmtmEst AND tmIdVehiculo LIKE ISNULL(@pmtmIdVehiculo,'%')
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND (tmNmes>=ISNULL(@pmtmNmes,0)  AND tmNmes<=ISNULL(@pmtmNmes,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_IntAhorroLta @pmtmEst CHAR(2)
 AS
 SELECT  tmItem, tmIdVehiculo,NumVeh,tmIdTercero,tmTercero,tmSaldoBase,tmVrAbono, tmTasa, tmValor
, tmNmes,tmVehPropio, tmIdConductor,tmConductor, tmIdPropietario
 FROM   tm_IntAhorro AS T INNER JOIN  Vehiculos AS V ON T.tmIdVehiculo=V.IdVehiculo
 WHERE tmEst=@pmtmEst 
 ORDER BY tmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_IntAhorroVeh @pmtmEst CHAR(2)
 AS
 SELECT tmIdVehiculo,tmNmes,tmSaldoBase,tmTasa, tmValor,tmIdTercero,tmTercero,tmIdPropietario
,tmIdConductor,tmConductor,tmVehPropio,tmItem,tmRetencion,tmVrAbono
 FROM   tm_IntAhorro
 WHERE tmEst=@pmtmEst
 ORDER BY tmIdVehiculo,tmNmes,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_IntNomina @pmtmEst CHAR(2),@pmtmIdVehiculo VARCHAR(10),@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmEst,tmItem,tmIdVehiculo,tmIdEmpleado,tmIdConductor,tmIdPropietario,tmVehPropio,tmConductor,tmTercero,tmCcosto,tmValorAbono
 FROM tm_IntNomina
 WHERE tmEst=@pmtmEst AND tmIdVehiculo=@pmtmIdVehiculo AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_IntNominaVeh @pmtmEst CHAR(2)
 AS 
 SELECT tmEst,tmItem,tmIdVehiculo,tmIdEmpleado,tmIdConductor,tmIdPropietario,tmVehPropio,tmConductor,tmTercero,tmCcosto,tmValorAbono
 FROM tm_IntNomina
 WHERE tmEst=@pmtmEst 
 ORDER BY tmIdVehiculo,tmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KarUbi @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmLinea INT=Null
,@pmtmIdProducto VARCHAR(16)=Null,@pmtmIdBodega VARCHAR(4)=Null,@pmtmIdUbic VARCHAR(10)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmIdUbic,tmEntradas,tmSalidas,tmLinea,tmCdNueBod,tmCdNueUbic
 FROM tm_KarUbi
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND tmIdUbic LIKE ISNULL(@pmtmIdUbic,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647)) 
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KarUbiID @pmtmNumero VARCHAR(5)
 AS 
 SELECT  MAX(tmItem) AS MXITM
 FROM tm_KarUbi
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KarUbiPro @pmtmNumero VARCHAR(5),@pmtmLinea INT=Null
,@pmtmIdProducto VARCHAR(16)=Null,@pmtmIdBodega VARCHAR(4)=Null,@pmtmIdUbic VARCHAR(10)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmIdUbic,tmEntradas,tmSalidas,tmLinea,tmCdNueBod,tmCdNueUbic
,DescripProd,Seriales,Lotes,Combo,Tanques,TipoRef
 FROM tm_KarUbi AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND TipoRef<>'SERVICIO' AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')  AND tmIdUbic LIKE ISNULL(@pmtmIdUbic,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
 ORDER BY tmIdProducto,tmIdBodega,tmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KarUbiSal @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdUbic,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_KarUbi AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND TipoRef<>'SERVICIO'  AND Tanques=0
 GROUP BY  tmIdProducto,DescripProd,tmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KarUbiTot @pmtmNumero VARCHAR(5),@pmtmLinea INT=Null
,@pmtmIdProducto VARCHAR(16)=Null,@pmtmIdBodega VARCHAR(4)=Null,@pmtmIdUbic VARCHAR(10)=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_KarUbi
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND tmIdUbic LIKE ISNULL(@pmtmIdUbic,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Kdex @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas
,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
,tmCdSubgrupo,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb
,tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
,tmRec_Costo
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexAju @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmEntradas+tmSalidas AS Cant,tmVrUnitario
,(tmEntradas+tmSalidas)*tmVrUnitario AS VrTotal,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion
,tmCdCCosto,tmCdSubCos,tmVrBruto,tmVrBase,tmNumLote,tmFecVceLote,Tanques
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexBga @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
,@pmtmIdBodega VARCHAR(4),@pmtmComptmntos VARCHAR(50),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmVrUnitario,tmVrPrecio
,tmItemCbo,tmComptmntos,tmCdProdEquiv
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmSalidas>0 AND tmIdProducto=@pmtmIdProducto
 AND tmIdBodega=@pmtmIdBodega AND tmComptmntos=@pmtmComptmntos
 AND tmItem<>@pmtmItem
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexCc @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCodTarIca
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexCom @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrBruto,tmReferencia,tmCdProdEquiv,tmUnidades,tmServcios,tmEsCombo,tmEsProdBase,tmItemCbo,tmCdSubgrupo
,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend
,tmComision,tmCdOperario,tmComisnOper,tmCodTarCom,tmCodTarCmc
--variables de ProMcias
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Combo<>0 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDcom @pmtmNumero VARCHAR(5),@pmtmRemision INT,@pmtmIdCiaRem CHAR(2)
,@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4)
 AS 
 SELECT  SUM(tmEntradas) AS SCANENT,SUM(tmSalidas) AS SCANSAL
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmRemision=@pmtmRemision AND tmIdCiaRem=@pmtmIdCiaRem
 AND tmIdProducto=@pmtmIdProducto AND tmIdBodega=@pmtmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDet @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas
,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
,tmCdSubgrupo,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia
,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad,tmRec_Costo
--variables de productos
,DescripProd,TipoRef,IdSubgrupo,Seriales,Lotes,Combo,Tanques
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDetC @pmtmNumero VARCHAR(5)
 AS 
--traslado de productos combos
 SELECT tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas
,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
,tmCdSubgrupo,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia
,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmRec_Costo
--variables de productos
,DescripProd,TipoRef,IdSubgrupo,Seriales,Lotes,Combo,Tanques
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 WHERE  tmNumero=@pmtmNumero AND tmEsCombo<>0 AND Combo<>0 AND TipoRef<>'SERVICIO'
 AND  tmEsProdBase=0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDfo @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmEntradas,tmVrPrecio,tmTarifaDct,tmVrDcto
,(tmVrPrecio*tmEntradas)-tmVrDcto AS SubTotal,tmTarifaIva,tmVrIva,((tmVrPrecio*tmEntradas)-tmVrDcto)+tmVrIva AS ValorTotal
,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrUnitario,tmVrUnitario*tmEntradas AS CostoTotal,tmVrBruto,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion
,tmIdVend,tmComision,tmCdCCosto,tmCdSubCos,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet
,tmCodTarCom,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmServcios,Tanques,tmCdTanque,tmEsCombo,tmEsProdBase,tmItemCbo
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDve @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,tmCdTanque,tmSalidas,tmVrPrecio,tmTarifaDct,tmVrDcto,tmVrUnitario
,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrTotal,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmImpGlobal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom
,tmUnidades,Bodega,tmReferencia,tmDescripcion,tmCdCCosto,tmCdSubCos,tmNumLote,tmFecVceLote,tmCodTarDct,tmCodTarRet,tmCodTarIca
,tmServcios,Tanques,tmTipDoc,tmDocumento,tmIdCia,tmNumInicial
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDvf @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmEntradas,tmVrPrecio,tmTarifaDct,tmVrDcto
,(tmVrPrecio*tmEntradas)-tmVrDcto AS SubTotal,tmTarifaIva,tmVrIva,((tmVrPrecio*tmEntradas)-tmVrDcto)+tmVrIva AS ValorTotal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca
,tmVrUnitario,tmVrUnitario*tmEntradas AS CostoTotal,tmVrBruto,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion
,tmIdVend,tmComision,tmCdCCosto,tmCdSubCos,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet
,tmCodTarCom,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb
,tmServcios,Tanques,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmTipDoc,tmDocumento,tmIdCia
,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmCdAgencia
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexDvs @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmEntradas,tmVrUnitario,tmEntradas*tmVrUnitario AS CostoTotal
,tmVrPrecio,tmEntradas*tmVrPrecio AS ValorTotal,tmUnidades,tmReferencia,tmDescripcion,tmCdCCosto,tmCdSubCos,tmpVehiculo
,tmNumLote,tmFecVceLote,tmEsCombo,tmServcios,Tanques,tmTipDoc,tmDocumento,tmIdCia
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero  AND  tmEsProdBase=0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexEnt @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmEntradas,tmVrPrecio,tmTarifaDct,tmVrDcto,tmVrUnitario
,(tmEntradas*tmVrPrecio)-tmVrDcto AS VrTotal,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmImpGlobal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmUnidades,tmReferencia,tmDescripcion,tmCdCCosto,tmCdSubCos,tmNumLote,tmFecVceLote,tmCodTarDct,tmCodTarRet,tmCodTarIca
,tmTipDoc,tmDocumento,tmIdCia,tmServcios,Tanques,tmNumInicial
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexFco @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmSalidas,tmVrPrecio,tmListaPrec,tmTarifaDct,tmVrDcto,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva AS ValorTotal
,tmIdBodega,Bodega,tmTarifaIva,tmVrIva,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion,tmVrBruto,tmVrUnitario,tmVrUnitario*tmSalidas AS CostoTotal
,tmIdVend,tmComision,tmCodTarDct,tmCodTarIva,tmCodTarCom,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmServcios,Tanques,tmCdTanque
,tmEsCombo,tmEsProdBase,tmItemCbo,tmTipDoc,tmDocumento,tmIdCia,tmCdOperario,tmComisnOper,tmCodTarCmc,tmpVehiculo
FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexFcr @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmSalidas,tmVrPrecio,tmTarifaDct,tmVrDcto
,(tmVrPrecio*tmSalidas)-tmVrDcto AS SubTotal,tmTarifaIva,tmVrIva,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva AS ValorTotal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca
,tmVrUnitario,tmVrUnitario*tmSalidas AS CostoTotal,tmVrBruto,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion
,tmIdVend,tmComision,tmCdOperario,tmComisnOper,tmNumLote,tmFechLote,tmFecVceLote,tmCdCCosto,tmCdSubCos,tmComptmntos
,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb
,tmServcios,Tanques,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo,tmTipDoc,tmDocumento,tmIdCia
,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden,tmCdAgencia,tmRec_Costo
FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexGBod @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdProducto,DescripProd,ExtciaAct,tmIdBodega,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND TipoRef<>'SERVICIO'
 GROUP BY  tmIdProducto,DescripProd,ExtciaAct,tmIdBodega 
 ORDER BY tmIdProducto,tmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexGord @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipDoc,tmDocumento,tmIdCia,tmIdProducto,DescripProd
,SUM(tmEntradas) AS STOTENT,SUM(tmSalidas) AS STOTSAL
 FROM tm_Kdex AS T INNER JOIN ProdMcias AS P ON  T.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND TipoRef<>'SERVICIO' AND tmDocumento>0
 GROUP BY  tmTipDoc,tmDocumento,tmIdCia,tmIdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexGpro @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdProducto,DescripProd,SUM(tmEntradas) AS STOTENT,SUM(tmSalidas) AS STOTSAL
 FROM tm_Kdex AS T INNER JOIN ProdMcias AS P ON  T.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND TipoRef<>'SERVICIO'
 GROUP BY  tmIdProducto,DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexGtan @pmtmNumero VARCHAR(5)
 AS 
 SELECT  tmCdTanque,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Tanques<>0 AND tmServcios=0 AND tmEsCombo=0
 GROUP BY tmCdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexGui @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,tmSalidas*tmVrPrecio AS VrSubTotal,tmComptmntos
,tmUnidades,tmReferencia,tmDescripcion,tmIdBodega,Bodega,tmVrBruto,tmVrUnitario,tmListaPrec
,tmServcios,Tanques,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo,tmEsCombo
,tmgalsbruto,tmgalsneto,tmTemperatura,tmUmTemp,tmDensidad
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexID @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexIen @pmtmNumero VARCHAR(5),@pmtmItemCbo INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas
,tmVrUnitario,tmVrPrecio,tmComptmntos,tmCdProdEquiv
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmEntradas>0 AND tmItemCbo=@pmtmItemCbo
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexIns @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmRemision,tmIdCiaRem,tmIdProducto,DescripProd,tmIdBodega
,SUM(tmSalidas) AS STOTSAL,MAX(tmVrUnitario) AS SVALUNIT
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmTipRem='COM' AND tmRemision>0
 GROUP BY tmRemision,tmIdCiaRem,tmIdProducto,DescripProd,tmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexItm @pmtmNumero VARCHAR(5),@pmtmItem INT
,@pmtmIdProducto VARCHAR(16)=Null,@pmtmEsProdBase INT=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 AND (tmEsProdBase>=ISNULL(@pmtmEsProdBase,0)  AND tmEsProdBase<=ISNULL(@pmtmEsProdBase,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexItn @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
--items notas de traslado
 AS 
 SELECT COUNT(tmItem) AS SCANT
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmSalidas>0
 AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexLot @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmDescripcion,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmUnidades,tmCdProdEquiv,DescripProd
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero  AND Lotes<>0
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexLta @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,Unidad
,tmVrUnitario,tmVrPrecio,tmVrCostProm,tmVrProm,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
,tmCdOperario,tmComisnOper,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal
,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades,tmServcios,tmNoVentas
,tmEsCombo,tmEsProdBase,tmItemCbo,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
,tmCdSubgrupo,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb
,tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden
,TipoRef,Seriales,Lotes,Combo,Tanques
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE tmNumero=@pmtmNumero 
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexMng @pmtmNumero VARCHAR(5),@pmtmCdMngra VARCHAR(10),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmCdMngra
,tmNumInicial,tmNumFinal,tmVrUnitario,tmVrPrecio
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmCdMngra=@pmtmCdMngra
 AND tmItem<>@pmtmItem
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexNpr @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmEntradas,tmSalidas,tmVrUnitario
,(tmEntradas+tmSalidas)*tmVrUnitario AS CostoTotal,tmVrPrecio,(tmEntradas+tmSalidas)*tmVrPrecio AS ValorTotal
,tmIdUnd,Unidad,tmUnidades,tmReferencia,tmDescripcion,tmCdCCosto,tmCdSubCos,tmNumLote,tmFecVceLote
,tmCdUbic,tmVrBruto,tmServcios,Tanques,tmCdTanque
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexNts @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmComptmntos,tmSalidas,tmVrUnitario
,tmSalidas*tmVrUnitario AS VrTotal,tmCdProdEquiv,tmUnidades,tmIdUnd,Unidad
,tmReferencia,tmDescripcion,tmCdCCosto,tmCdSubCos,tmNumLote,tmFecVceLote,Tanques
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmSalidas>0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexNtsal @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmIdUnd,tmVrUnitario,tmVrPrecio,tmVrCostProm
,tmVrProm,tmTarifaIva,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos
,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmCdOperario,tmReferencia,tmDescripcion,tmComptmntos,tmCdProdEquiv
,tmUnidades,tmServcios,tmNoVentas,tmEsProdBase,tmItemCbo,tmCdSubgrupo
,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmSalidas>0
 ORDER BY  tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexNum @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmCdTanque,tmCdMngra,tmNumInicial,tmNumFinal,tmSalidas,tmEntradas,tmServcios
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Tanques<>0
 ORDER BY  tmCdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexOcc @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmCdTanque,tmSalidas,tmVrPrecio,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrSubTotal
,tmTarifaIva,tmVrIva,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva AS VrTotal,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom
,tmTasaNac,tmTasaDep,tmTasaMun,tmUnidades,tmReferencia,tmDescripcion,tmIdBodega,Bodega,tmListaPrec,tmVrBruto,tmVrUnitario
,tmServcios,Tanques,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmCdLocal,tmIdVend,tmComision,tmCodTarCom,tmpVehiculo,tmEsCombo
,tmReferencia2,tmRec_Costo
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexOdc @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,tmCdTanque,tmEntradas,tmVrPrecio,tmTarifaDct,tmVrDcto,tmVrUnitario
,(tmEntradas*tmVrPrecio)-tmVrDcto AS VrTotal,tmTarifaIva,tmVrIva,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca,tmUnidades,Bodega,tmReferencia,tmDescripcion,tmImpGlobal,tmSobretasa
,tmCdCCosto,tmCdSubCos,tmCodTarDct,tmCodTarRet,tmCodTarIca,tmServcios,Tanques,tmTipDoc,tmDocumento,tmIdCia
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexOrd @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem,count(tmItem) AS CantItm
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND (tmDocumento>0 OR tmRemision>0)
 GROUP BY  tmTipDoc,tmDocumento,tmIdCia,tmTipRem,tmRemision,tmIdCiaRem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexParc @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmVrUnitario
,tmVrPrecio,tmVrCostProm,tmVrProm,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmDescripcion,tmReferencia
,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo
,tmCdSubgrupo,tmComptmntos
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY tmIdProducto,tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexPas @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCdMngra,tmCdTanque,tmIdProducto,DescripProd,tmEntradas,tmVrPrecio,tmVrPrecio*tmEntradas AS SubTotal
,tmSobretasa,tmEntradas*tmSobretasa AS TotSobTasa,tmSoldicom,tmEntradas*tmSoldicom AS TotSoldicom,tmImpGlobal,tmEntradas*tmImpGlobal AS TotIGlobal
,(tmVrPrecio*tmEntradas)+(tmEntradas*tmSobretasa)+(tmEntradas*tmSoldicom)+(tmEntradas*tmImpGlobal) AS ValorTotal,tmVrUnitario,tmVrUnitario*tmEntradas AS CostoTotal
,tmVrBruto,tmVrPrecio+tmSobretasa+tmSoldicom+tmImpGlobal+tmVrIva AS PrecioPub,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion,tmIdBodega,Bodega,tmTarifaDct,tmVrDcto,tmTarifaIva,tmVrIva
,tmTasaNac,tmTasaDep,tmTasaMun,tmIdVend,tmComision,tmCodTarDct,tmCodTarIva,tmCodTarCom,tmListaPrec,tmVrBase,tmServcios,Tanques,tmNumInicial,tmNumFinal
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexPed @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmSalidas,tmVrPrecio,tmTarifaDct,tmVrDcto,(tmSalidas*tmVrPrecio)-tmVrDcto AS VrSubTotal
,tmTarifaIva,tmVrIva,((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva AS VrTotal,tmComptmntos,tmSobretasa,tmImpGlobal,tmSoldicom,tmTasaNac,tmTasaDep,tmTasaMun
,tmVrBruto,tmListaPrec,tmUnidades,tmReferencia,tmDescripcion,tmCodTarDct
,tmIdVend,tmComision,tmCodTarCom,tmServcios,Tanques,tmVrUnitario,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia,tmRec_Costo
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 WHERE  tmNumero=@pmtmNumero 
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexPla @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCdMngra,tmCdTanque,tmIdProducto,DescripProd,tmNumInicial,tmNumFinal,tmSalidas,tmVrPrecio,(tmVrPrecio*tmSalidas)-tmVrDcto AS SubTotal
,tmSobretasa,tmSalidas*tmSobretasa AS TotSobTasa,tmSoldicom,tmSalidas*tmSoldicom AS TotSoldicom,tmImpGlobal,tmSalidas*tmImpGlobal AS TotIGlobal
,((tmVrPrecio*tmSalidas)-tmVrDcto)+tmVrIva+(tmSalidas*tmSobretasa)+(tmSalidas*tmSoldicom)+(tmSalidas*tmImpGlobal) AS ValorTotal,tmVrUnitario,tmVrUnitario*tmSalidas AS CostoTotal
,tmVrBruto,tmVrPrecio+tmSobretasa+tmSoldicom+tmImpGlobal+tmVrIva AS PrecioPub,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion,tmIdBodega,Bodega,tmTarifaDct,tmVrDcto,tmTarifaIva,tmVrIva
,tmTasaNac,tmTasaDep,tmTasaMun,tmIdVend,tmComision,tmCodTarDct,tmCodTarIva,tmCodTarCom,tmListaPrec,tmVrBase,tmServcios,Tanques
FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexPnp @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
,@pmtmItem INT,@pmTipo VARCHAR(3)
 AS 
 IF @pmTipo='ENT'
  SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
  FROM tm_Kdex 
  WHERE tmNumero=@pmtmNumero AND tmIdProducto=@pmtmIdProducto  AND tmEntradas>0
  AND tmItem<>@pmtmItem
 ELSE
  SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
  FROM tm_Kdex 
  WHERE tmNumero=@pmtmNumero AND tmIdProducto=@pmtmIdProducto  AND tmSalidas>0
  AND tmItem<>@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexPro @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmVrUnitario
,tmVrPrecio,tmVrCostProm,tmVrProm,tmVrBruto,tmCdUbic,tmNumLote,tmFechLote,tmFecVceLote,tmDescripcion,tmReferencia
,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo
,tmCdSubgrupo,tmComptmntos
--variables de ProMcias
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques
--,tmTarifaIva,tmVrIva,tmTarifaDct,tmVrDcto,tmTarifaRet,tmVrRete,tmTarifaIca,tmVrIca
--,tmIdTercero,tmCdAgencia,tmCdCCosto,tmCdSubCos,tmCdLocal,tmCdSzona,tmpVehiculo,tmIdVend,tmComision
--,tmCdOperario,tmComisnOper,
--,tmSobretasa,tmTasaNac,tmTasaDep,tmTasaMun,tmSoldicom,tmImpGlobal,tmOtroImpto,tmUnidades
--,tmCodTarDct,tmCodTarIva,tmCodTarIca,tmCodTarRet,tmCodTarCom,tmCodTarCmc
--,tmListaPrec,tmVrBase,tmCdMoneda,tmVrTasaCamb,tmTipDoc,tmDocumento,tmIdCia
--,tmTipRem,tmRemision,tmIdCiaRem,tmReferencia2,tmFecOrden
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 ORDER BY tmIdProducto,tmIdBodega,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexPrv @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdProducto,tmVrUnitario,tmVrPrecio,tmVrProm,tmTarifaDct,tmCodTarDct,tmReferencia
,SUM(tmEntradas) AS Sent
 FROM   tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmServcios=0
 GROUP BY  tmIdProducto,tmVrUnitario,tmVrPrecio,tmVrProm,tmTarifaDct,tmCodTarDct,tmReferencia
 ORDER BY  tmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexSai @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmSalidas,tmVrUnitario,tmSalidas*tmVrUnitario AS CostoTotal
,tmRemision,tmIdCiaRem,tmTipDoc,tmDocumento,tmIdCia,tmIdUnd,Unidad,tmUnidades,tmReferencia,tmDescripcion
,tmCdCCosto,tmCdSubCos,tmIdTercero,tmIdVend,tmpVehiculo,tmVrPrecio,tmSalidas*tmVrPrecio AS VrTotal,tmTarifaIva,tmVrIva
,tmVrBruto,tmNumLote,tmFecVceLote,Tanques,tmServcios,tmEsCombo,tmCdTanque
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexSal @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdTanque,tmSalidas,tmVrUnitario
,tmSalidas*tmVrUnitario AS CostoTotal,tmVrPrecio,tmSalidas*tmVrPrecio AS VrTotal,tmTarifaIva,tmVrIva
,tmUnidades,tmIdUnd,Unidad,tmReferencia,tmDescripcion,tmCdCCosto,tmCdSubCos,tmIdTercero,tmpVehiculo
,tmVrBruto,tmNumLote,tmFecVceLote,Tanques,tmServcios,tmEsCombo,tmTipDoc,tmDocumento,tmIdCia
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto 
 INNER JOIN Bodegas AS B ON K.tmIdBodega=B.IdBodega
 LEFT JOIN UndMed AS U ON K.tmIdUnd=U.IdUnd
 WHERE  tmNumero=@pmtmNumero AND tmEsProdBase=0
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexSer @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmCdUbic,tmNumLote,tmFechLote
,tmFecVceLote,tmDescripcion,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmUnidades,tmCdProdEquiv,DescripProd
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero  AND Seriales<>0
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexSub @pmtmNumero VARCHAR(5)
 AS 
 SELECT IdSubgrupo,tmIdProducto,DescripProd,TipoRef,Combo,tmEsProdBase,IdProv,COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
,SUM(tmEntradas*tmImpGlobal) AS SGLOENT,SUM(tmSalidas*tmImpGlobal) AS SGLOSAL
,SUM(tmSobretasa*tmEntradas) AS SSOBENT,SUM(tmSobretasa*tmSalidas) AS SSOBSAL
,SUM(tmTasaNac*tmEntradas) AS SNACENT,SUM(tmTasaNac*tmSalidas) AS SNACSAL
,SUM(tmTasaDep*tmEntradas) AS SDEPENT,SUM(tmTasaDep*tmSalidas) AS SDEPSAL
,SUM(tmTasaMun*tmEntradas) AS SMUNENT,SUM(tmTasaMun*tmSalidas) AS SMUNSAL
,SUM(tmSoldicom*tmEntradas) AS SSOLENT,SUM(tmSoldicom*tmSalidas) AS SSOLSAL
,SUM(tmOtroImpto*tmEntradas) AS SOTRENT,SUM(tmOtroImpto*tmSalidas) AS SOTRSAL
--Para los ajustes con cantidad en cero (0)
,SUM(tmVrUnitario) AS SCOSAJU,SUM(tmOtroImpto) AS SDVEAJU
,SUM(tmRec_Costo*tmEntradas) AS SRCOSENT,SUM(tmRec_Costo*tmSalidas) AS SRCOSSAL
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero
 GROUP BY IdSubgrupo,tmIdProducto,DescripProd,TipoRef,Combo,tmEsProdBase,IdProv
 ORDER BY IdSubgrupo,tmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexSubc @pmtmNumero VARCHAR(5)
 AS 
 SELECT IdSubgrupo,tmIdProducto,DescripProd
,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmEsCombo<>0 AND Combo<>0 AND TipoRef<>'SERVICIO'
 AND  tmEsProdBase=0
 GROUP BY IdSubgrupo,tmIdProducto,DescripProd
 ORDER BY IdSubgrupo,tmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTAju @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrUnitario) AS SCOSAJU
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmEntradas<=0 AND tmSalidas<=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTBas @pmtmNumero VARCHAR(5),@pmTipo VARCHAR(3)
 AS 
 IF @pmTipo='IVA'
 BEGIN
  SELECT SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
 ,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
 ,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT
 ,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
  FROM  tm_Kdex
  WHERE tmNumero=@pmtmNumero AND tmVrIva>0
 END
 ELSE
   BEGIN
   IF @pmTipo='RET'
    BEGIN
     SELECT SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
     ,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
    ,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
    ,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
    FROM  tm_Kdex
    WHERE tmNumero=@pmtmNumero AND tmVrRete>0
   END
  ELSE
   BEGIN --ICA
     SELECT SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
     ,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
    ,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
    ,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
    FROM  tm_Kdex
    WHERE tmNumero=@pmtmNumero AND  tmVrIca>0
   END
 END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTBod @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4)
,@pmtmItem INT
 AS 
 SELECT SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmIdProducto=@pmtmIdProducto
 AND tmItem<>@pmtmItem AND tmServcios=0
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%') 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTTan @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16),@pmtmCdTanque VARCHAR(4)
,@pmtmItem INT
 AS 
 SELECT SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmIdProducto=@pmtmIdProducto
 AND tmCdTanque=@pmtmCdTanque
 AND tmItem<>@pmtmItem AND tmServcios=0

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTUbi @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16),@pmtmCdUbic VARCHAR(10)
,@pmtmItem INT
 AS 
 SELECT SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero  AND tmIdProducto=@pmtmIdProducto
 AND tmItem<>@pmtmItem AND tmServcios=0
 AND tmCdUbic=@pmtmCdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTVcc @pmtmNumero VARCHAR(5)
 AS 
--total de vales crédito combustible
 SELECT  SUM(((tmSalidas*tmVrPrecio)-tmVrDcto)+tmVrIva) AS STOTBRU
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero  AND TipoRef='SERVICIO' AND ValesComb<>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTan @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmVrUnitario
,tmVrPrecio,tmVrCostProm,tmVrProm,tmVrBruto,tmDescripcion,tmReferencia
,tmCdProdEquiv,tmCdMngra,tmNumInicial,tmNumFinal,tmServcios,tmNoVentas,tmEsCombo,tmEsProdBase,tmItemCbo
,tmCdSubgrupo,tmComptmntos
--variables de ProMcias
,DescripProd,TipoRef,IdSubgrupo,IdBodega,IdUbic,VrCostAnt,VrCosto,VrCostPmd,ExtciaMin,ExtciaMax,ExtciaAct
,Seriales,Lotes,Combo,Tanques
 FROM tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Tanques<>0
 ORDER BY tmIdProducto,tmCdTanque,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTanq @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
,@pmtmCdTanque VARCHAR(4),@pmtmComptmntos VARCHAR(50),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmIdBodega,tmCdTanque,tmEntradas,tmSalidas,tmVrUnitario,tmVrPrecio
,tmItemCbo,tmComptmntos,tmCdProdEquiv
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmSalidas>0 AND tmIdProducto=@pmtmIdProducto
 AND tmCdTanque=@pmtmCdTanque AND tmComptmntos=@pmtmComptmntos
 AND tmItem<>@pmtmItem
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTar @pmtmNumero VARCHAR(5),@pmTipo VARCHAR(3)
 AS 
 IF @pmTipo='IVA' 
  SELECT tmNumero,tmItem,tmIdProducto,tmCodTarIva,tmTarifaIva,tmVrIva
  FROM tm_Kdex
  WHERE tmNumero=@pmtmNumero  AND tmVrIva>0
 ELSE
  BEGIN
   IF @pmTipo='ICA' 
    SELECT tmNumero,tmItem,tmIdProducto,tmCodTarIca,tmTarifaIca,tmVrIca
    FROM tm_Kdex
    WHERE tmNumero=@pmtmNumero  AND tmVrIca>0
   ELSE
    SELECT tmNumero,tmItem,tmIdProducto,tmCodTarRet,tmTarifaRet,tmVrRete
    FROM tm_Kdex
    WHERE tmNumero=@pmtmNumero  AND tmVrRete>0
  END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotFac @pmtmNumero VARCHAR(5)
 AS 
--totaliza el valor de ventas de la factura o salida, de los productos no combos
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotPla @pmtmNumero VARCHAR(5)
 AS 
--totaliza el valor de ventas de planilla solamente combustible
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
,SUM(tmEntradas*tmImpGlobal) AS SGLOENT,SUM(tmSalidas*tmImpGlobal) AS SGLOSAL
,SUM(tmSobretasa*tmEntradas) AS SSOBENT,SUM(tmSobretasa*tmSalidas) AS SSOBSAL
,SUM(tmTasaNac*tmEntradas) AS SNACENT,SUM(tmTasaNac*tmSalidas) AS SNACSAL
,SUM(tmTasaDep*tmEntradas) AS SDEPENT,SUM(tmTasaDep*tmSalidas) AS SDEPSAL
,SUM(tmTasaMun*tmEntradas) AS SMUNENT,SUM(tmTasaMun*tmSalidas) AS SMUNSAL
,SUM(tmSoldicom*tmEntradas) AS SSOLENT,SUM(tmSoldicom*tmSalidas) AS SSOLSAL
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero  AND Tanques<>0 AND tmEsProdBase=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotPlaC @pmtmNumero VARCHAR(5),@pmtmReferencia2 VARCHAR(50)
 AS 
--totaliza el valor de ventas de los productos COMBOS
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
 FROM  tm_Kdex  AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo<>0 AND Tanques=0
 AND tmReferencia2=@pmtmReferencia2
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotPlaV @pmtmNumero VARCHAR(5),@pmtmReferencia2 VARCHAR(50)
 AS 
--totaliza el valor de ventas de los productos no combos
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
 FROM  tm_Kdex  AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo=0 AND Tanques=0
 AND tmReferencia2=@pmtmReferencia2
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotPun @pmtmNumero VARCHAR(5)
 AS 
--totaliza el valor de ventas de la factura o salida, de los productos no combos
 SELECT tmIdProducto,tmCdSubgrupo,COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo=0
 GROUP BY tmIdProducto,tmCdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotSal @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
,SUM(tmEntradas*tmVrUnitario) AS SCOSENT,SUM(tmSalidas*tmVrUnitario) AS SCOSSAL
,SUM(tmVrPrecio*tmEntradas) AS SVALENT,SUM(tmVrPrecio*tmSalidas) AS SVALSAL
,SUM(tmVrIva) AS SIVA,SUM(tmVrDcto) AS SDCT,SUM(tmVrRete) AS SRET,SUM(tmVrIca) AS SICA
,SUM(tmVrBruto*tmEntradas) AS SBRUENT,SUM(tmVrBruto*tmSalidas) AS SBRUSAL
 FROM  tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmEsProdBase=0 AND tmEsCombo<>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTotSob @pmtmNumero VARCHAR(5)
 AS 
--totales de sobretasas e imp. global
 SELECT  SUM(tmEntradas*tmImpGlobal) AS SGLOENT,SUM(tmSalidas*tmImpGlobal) AS SGLOSAL
,SUM(tmSobretasa*tmEntradas) AS SSOBENT,SUM(tmSobretasa*tmSalidas) AS SSOBSAL
,SUM(tmTasaNac*tmEntradas) AS SNACENT,SUM(tmTasaNac*tmSalidas) AS SNACSAL
,SUM(tmTasaDep*tmEntradas) AS SDEPENT,SUM(tmTasaDep*tmSalidas) AS SDEPSAL
,SUM(tmTasaMun*tmEntradas) AS SMUNENT,SUM(tmTasaMun*tmSalidas) AS SMUNSAL
,SUM(tmSoldicom*tmEntradas) AS SSOLENT,SUM(tmSoldicom*tmSalidas) AS SSOLSAL
 FROM  tm_Kdex AS K INNER JOIN ProdMcias AS P ON K.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero  AND Tanques<>0 AND tmEsProdBase=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexTpro @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)
,@pmtmItem INT
 AS 
 SELECT COUNT(tmItem) AS SCANT
 FROM tm_Kdex 
 WHERE tmNumero=@pmtmNumero AND tmIdProducto=@pmtmIdProducto AND tmItem<>@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_KdexVcr @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmTipRem,tmRemision,tmReferencia2,tmUnidades,SUM(tmSalidas) AS STOTSAL
 FROM tm_Kdex
 WHERE tmNumero=@pmtmNumero AND tmIdCiaRem='VC'
 GROUP BY tmTipRem,tmRemision,tmReferencia2,tmUnidades
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LibOf @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT O.tmIdCuenta AS CdCuenta,NomCuenta,tmTipCom,TipoCom,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo,tmIdTercero,RazonSocial
,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmSanNit,tmSanNitcc,tmSanNitCcSub
,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12,tmItem,Compania,TC.IdDiario AS CdDiario,Diario
--información del tercero
,TipoId,Dv,NomCial,SiglaRaz,Direccion,T.IdLocal AS TercCdCiudad,Localidad,L.IdDep AS CdDep
,Departamento,Telefono,TipEnte
 FROM tm_LibOf AS O  INNER JOIN  TiposCom AS TC ON O.tmTipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN Puc AS P ON O.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON O.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN CentroCosto AS CC ON O.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON O.tmIdSubCos=SC.IdSubCos
 LEFT JOIN Companias AS CI ON O.tmIdCia=CI.IdCia
 LEFT JOIN tm_NivCue AS NV ON O.tmIdCuenta=NV.tmIdCuenta
 WHERE tmEst=@pmtmEst AND O.tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%') AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%')
 ORDER BY O.tmIdCuenta,RazonSocial,tmTipCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LibOfDia @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)=Null
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT tmIdCuenta,NomCuenta,tmTipCom,TipoCom,tmIdCia,tmSaldoAnterior,tmTotalDebitos,tmTotalCreditos
,tmSaldoAnterior+tmTotalDebitos-tmTotalCreditos AS NuevoSaldo,tmIdTercero,RazonSocial
,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmSanNit,tmSanNitcc,tmSanNitCcSub
,Compania,TC.IdDiario AS CdDiario,Diario,tmItem
--información del tercero
,TipoId,Dv,NomCial,SiglaRaz,Direccion,T.IdLocal AS TercCdCiudad,Localidad,L.IdDep AS CdDep
,Departamento,Telefono,TipEnte
 FROM tm_LibOf AS O  INNER JOIN  TiposCom AS TC ON O.tmTipCom=TC.IdCom
 INNER JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 INNER JOIN Puc AS P ON O.tmIdCuenta=P.IdCuenta
 INNER JOIN Terceros AS T ON O.tmIdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN CentroCosto AS CC ON O.tmIdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON O.tmIdSubCos=SC.IdSubCos
 LEFT JOIN Companias AS CI ON O.tmIdCia=CI.IdCia
 WHERE tmEst=@pmtmEst AND tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%') AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%')
 ORDER BY O.tmIdCuenta,tmTipCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LibOfGcue @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,COUNT(tmItem) AS SCANT
 FROM tm_LibOf
 WHERE tmEst=@pmtmEst 
 GROUP BY tmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LibOfSan @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdCCosto VARCHAR(16)=Null
,@pmtmIdSubCos VARCHAR(16)=Null,@pmtmIdCia CHAR(2)=Null
 AS 
 SELECT tmEst,tmTipCom,tmItem,tmTotalDebitos,tmTotalCreditos,tmSaldoAnterior
 FROM tm_LibOf
 WHERE tmEst=@pmtmEst AND  tmIdCuenta=@pmtmIdCuenta
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%') AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%')
 AND tmIdSubCos LIKE ISNULL(@pmtmIdSubCos,'%') AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LibOfcue @pmtmEst CHAR(2),@pmtmTipCom VARCHAR(3)
,@pmtmIdCuenta VARCHAR(16),@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdCCosto VARCHAR(16)=Null
,@pmtmIdSubCos VARCHAR(16)=Null,@pmtmIdCia CHAR(2)=Null
 AS 
 SELECT tmEst,tmTipCom,tmItem,tmTotalDebitos,tmTotalCreditos,tmSaldoAnterior
 FROM tm_LibOf
 WHERE tmEst=@pmtmEst AND tmTipCom=@pmtmTipCom AND tmIdCuenta=@pmtmIdCuenta
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%') AND tmIdCCosto LIKE ISNULL(@pmtmIdCCosto,'%')
 AND tmIdSubCos LIKE ISNULL(@pmtmIdSubCos,'%') AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Liquida @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmDetalle,tmFecUltLiq,tmFecInicial,tmFecFinal,tmVrBase,tmCantidad,tmVrLiquida,tmVrDeducido,tmFecIniPmd,tmFecFinPmd
,tmDiasBase,tmDiasNoRem,tmTarifa,tmNPrestamo,tmNCuota,tmLogPmdio,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmVrSalario,tmVrParcial
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaCon @pmtmNumero VARCHAR(5),@pmtmIdConcepto VARCHAR(4),@pmtmIdEmpleado VARCHAR(16)
,@pmtmItem INT
 AS 
 SELECT Count(tmItem) AS SITEM
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero  AND tmIdConcepto=@pmtmIdConcepto AND tmIdEmpleado=@pmtmIdEmpleado
 AND tmItem<>@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaDed @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,EsCondtor,pVehiculo
,tmIdConcepto,Concepto,SUM(tmVrBase) AS tVrBase,SUM(tmCantidad) AS tCant,SUM(tmVrDeducido) AS tVrDeducido
,SUM(tmVrLiquida) AS tVrLiquida,MAX(tmTarifa) AS tTarifa
 FROM tm_Liquida AS L INNER JOIN NomConceptos AS C ON L.tmIdConcepto=C.IdConcepto
 INNER JOIN Empleados AS E ON L.tmIdEmpleado=E.IdEmpleado
 WHERE tmNumero=@pmtmNumero AND tmIdConcepto NOT IN ('CES','PRI','INT') AND tmVrDeducido>0
 GROUP BY tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,EsCondtor,pVehiculo
,tmIdConcepto,Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaDso @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmItem,tmIdConcepto,tmDetalle,tmVrBase,tmCantidad,tmVrLiquida,tmVrDeducido
,tmFecUltLiq,tmFecInicial,tmFecFinal,tmFecIniPmd,tmFecFinPmd,tmDiasBase,tmDiasNoRem,tmNPrestamo,tmNCuota,tmTarifa
,tmLogPmdio,tmNContrato,tmVrSalario,tmVrParcial --tmNumero,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom
 FROM tm_Liquida AS L INNER JOIN Empleados AS E ON L.tmIdEmpleado=E.IdEmpleado
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY Apellidos,Nombres,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaEmp @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,SUM(tmVrLiquida) AS tVrLiquida,SUM(tmVrDeducido) AS tVrDeducido,sum(tmVrParcial) AS tVrParcial
 FROM tm_Liquida AS L INNER JOIN Empleados AS E ON L.tmIdEmpleado=E.IdEmpleado
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 GROUP BY tmIdEmpleado,Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaId @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITEM
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaItm @pmtmNumero VARCHAR(5),@pmtmItem INT, @pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT Count(tmItem) AS SITEM
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero  AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaLpre @pmtmNumero VARCHAR(5),@pmtmIdConcepto VARCHAR(4)=Null,@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmDetalle,tmCantidad,tmVrDeducido,tmNPrestamo,tmNCuota
,TotPrestamo,TotAbonado,VrPrestamo,NumCuotas
 FROM tm_Liquida AS  L  INNER JOIN Trn_NomPrest AS P ON L.tmNPrestamo=P.NPrestamo
 WHERE tmNumero=@pmtmNumero AND  tmNPrestamo>0 AND tmVrDeducido>0 AND  tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
  AND tmIdConcepto LIKE ISNULL(@pmtmIdConcepto,'%')
 ORDER BY tmNPrestamo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaLta @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdEmpleado,Apellidos,Nombres,tmIdConcepto,tmDetalle,tmFecUltLiq,tmFecInicial,tmFecFinal,tmVrBase,tmCantidad,tmVrLiquida,tmVrDeducido,tmFecIniPmd,tmFecFinPmd
,tmDiasBase,tmDiasNoRem,tmTarifa,tmNPrestamo,tmNCuota,tmLogPmdio,tmNContrato,tmpVehiculo,tmIdDep,tmIdCCosto,tmIdSubCos,tmIdNom,tmVrSalario,EsCondtor,pVehiculo,tmVrParcial
 FROM  tm_Liquida AS L INNER JOIN Empleados AS E ON L.tmIdEmpleado=E.IdEmpleado
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmIdEmpleado,tmIdConcepto,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaPar @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdEmpleado,tmIdConcepto,tmDetalle,tmCantidad,tmVrDeducido
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero AND tmVrDeducido>0  AND  tmIdConcepto IN ('CES','INT')
 AND  tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY tmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaPre @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmNPrestamo INT
 AS 
 SELECT COUNT(tmItem) AS SITEM
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero AND tmNPrestamo=@pmtmNPrestamo AND  tmItem<>@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LiquidaTot @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT COUNT(tmItem) AS CANITM,SUM(tmVrBase) AS SBASE,SUM(tmCantidad) AS SCANTIDAD,SUM(tmVrLiquida) AS SVALLIQ
,SUM(tmVrDeducido) AS SDEDUCIDO,SUM(tmDiasBase) AS SDIASBASE,SUM(tmDiasNoRem) AS DIASNOREM,SUM(tmVrParcial) AS STOTPAR
 FROM tm_Liquida
 WHERE tmNumero=@pmtmNumero  AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Liquida_Cr @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null,@pmtmIdDep VARCHAR(4)=Null,@pmtmIdNom VARCHAR(4)=Null
 AS 
 SELECT tmNumero,tmItem,tmIdEmpleado,Apellidos,Nombres,tmIdConcepto,Concepto,tmDetalle,ClaseCon,tmFecUltLiq,tmFecInicial,tmFecFinal,tmVrBase,tmCantidad,tmVrLiquida
,tmVrDeducido,tmFecIniPmd,tmFecFinPmd,tmDiasBase,tmDiasNoRem,tmTarifa,tmNPrestamo,tmNCuota,tmLogPmdio,tmNContrato,tmpVehiculo,tmIdDep,Dependencia,tmIdCCosto,CCosto
,tmIdSubCos,SubCosto,tmIdNom,TipoNomina,tmVrSalario
--datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdInstala AS CodInst,Instlacion,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga
,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes
,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado AS Con_IdEstdo,Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada
,NumVeh,TipoAdmon,VehPropio,tmVrParcial
 FROM tm_Liquida AS T INNER JOIN NomConceptos AS CN ON T.tmIdConcepto=CN.IdConcepto  INNER JOIN  Empleados AS E ON T.tmIdEmpleado=E.IdEmpleado
 INNER JOIN Dependencias AS DP ON T.tmIdDep=DP.IdDep INNER JOIN CentroCosto AS CO ON T.tmIdCCosto=CO.IdCCosto
 INNER JOIN SubCentros AS SC ON T.tmIdSubCos=SC.IdSubCos INNER JOIN TiposNom AS TN ON T.tmIdNom=TN.IdNom 
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal  INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf  
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN  Emp_Contrato AS C ON T.tmNContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  
 INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado 
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea  
 INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada 
 LEFT JOIN NomVehiculos AS VH ON  T.tmpVehiculo=VH.IdVehiculo
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 AND tmIdDep LIKE ISNULL(@pmtmIdDep,'%') AND tmIdNom LIKE ISNULL(@pmtmIdNom,'%')
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ListaPrec @pmtmEst CHAR(2)
 AS 
 SELECT LT.IdProducto AS CdProducto,DescripProd,TipoRef,CiaPrecio1 AS Precio1,CiaPrecio2 AS Precio2,CiaPrecio3 AS Precio3,CiaPrecio4 AS Precio4
,CiaPrecio5 AS Precio5,IdTarIva,Tarifa,Simbolo,CiaIvaInc AS IvaInc,Margen1,Margen2,Margen3,Margen4,Margen5,BaseMgn,TarifDcto1,TarifDcto2,TarifDcto3,TarifDcto4,TarifDcto5
,TrmDia1,TrmDia2,TrmDia3,TrmDia4,TrmDia5
,ExtciaMin,ExtciaMax,ExtciaAct,ExistenciaCia,ISNULL(FecUltcom,CAST('19900101' AS SMALLDATETIME)) AS FecUltCompra,ISNULL(FecUltVta,CAST('19900101' AS SMALLDATETIME)) AS FecUltVenta
,LT.IdCia AS CdCia,Compania,CostoAnt,CostoUlt,CostoPmd,VrCostAnt,VrCosto,VrCostPmd
,Factor1,Factor2,Factor3,Factor4,Factor5,Seriales,Lotes,Combo,NoAjustes,Tanques,ValesComb,P.IdBodega AS CdBodga,Bodega,Descrip
,CodBarras,Referencia,DescripAbrv,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,P.IdSubgrupo AS CdSubgpo,Subgrupo
,P.IdMarca AS CdMarc,Marca,Color,Tamano,MedAlto,MedAncho,MedLargo,MedVolm,P.UndMed AS UndVolm,P.IdUnd AS CdUnd,UM.Unidad AS UndadMed
,IdUndP,UP.Unidad AS EmpPrim,IdUbic,DesUbic,P.IdProv AS IdProvee,RazonSocial,GarProv,GarClie,LtPreDef
,CdMon1,CdMon2,CdMon3,CdMon4,CdMon5,CodMcia,DescripLong,P.Cmntarios AS Prod_Observ,P.IdEstado AS CdEstdo,Estado,NColor,P.Inactivo AS Inactvo
,P.FechaAdd AS FecAdd,P.FechaUpdate AS FecUpd,P.IdUsuario AS IdUsuari,KarTipDoc,KarEntradas,KarSalidas,KarCostoEnt,KarCostoSal
--información del proveedor
,TipoId,Dv,NomCial,Direccion,TP.IdLocal AS CdCiudad,Localidad,Departamento,Telefono,Fax,e_mail
,CiaTarifIva
 FROM tm_ListaPrec AS LT INNER JOIN ProdMcias AS P ON LT.IdProducto=P.IdProducto
 INNER JOIN SubGrupos AS S ON P.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS L ON G.IdLinea=L.IdLinea INNER JOIN Marcas AS M ON P.IdMarca=M.IdMarca
 INNER JOIN UndMed AS UM ON P.IdUnd=UM.IdUnd  INNER JOIN UndMed AS UP ON P.IdUndP=UP.IdUnd
 INNER JOIN Bodegas AS B ON P.IdBodega=B.IdBodega INNER JOIN Terceros AS TP ON P.IdProv=TP.IdTercero
 INNER JOIN Tablapor AS TI ON P.IdTarIva=TI.IdTarifa INNER JOIN EstadoPro AS EP ON P.IdEstado=EP.IdEstado
 INNER JOIN Localidades AS LP ON TP.IdLocal=LP.IdLocal  INNER JOIN Departamentos AS DP ON LP.IdDep=DP.IdDep
 LEFT JOIN Companias AS CI ON LT.IdCia=CI.IdCia
 WHERE tmEst=@pmtmEst  
 ORDER BY DescripProd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Lot @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 SELECT tmItem,tmNumLote,tmIdProducto,DescripProd,tmIdBodega,Bodega,tmCdUbic,tmEntradas,tmSalidas
,tmFecha,tmFecVence,tmLinea,tmNumero,Lotes,tmCdNueUbic
 FROM tm_Lot AS L INNER JOIN ProdMcias AS P ON L.tmIdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON L.tmIdBodega=B.IdBodega
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LotGru @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 SELECT tmNumLote,tmIdProducto,DescripProd,tmIdBodega,tmCdUbic,tmFecVence
,sum(tmEntradas) AS SEnt,SUM(tmSalidas) AS SSal
 FROM tm_Lot AS L INNER JOIN ProdMcias AS P ON L.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Lotes<>0  AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
 GROUP BY  tmNumLote,tmIdProducto,DescripProd,tmIdBodega,tmCdUbic,tmFecVence
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LotID @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_Lot 
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LotIng @pmtmNumero VARCHAR(5),@pmtmLinea INT,@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmNumLote VARCHAR(30)=Null
 AS 
 SELECT SUM(tmEntradas) AS SENT,SUM(tmSalidas) AS SSAL
 FROM tm_Lot
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%') AND tmNumLote LIKE ISNULL(@pmtmNumLote,'%')
 AND  tmLinea<>@pmtmLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LotLta @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 SELECT tmItem,tmNumLote,tmFecVence,tmEntradas+tmSalidas AS Cantidad,tmIdProducto,DescripProd
,tmIdBodega,Bodega,tmCdUbic,tmLinea,tmFecha
 FROM tm_Lot AS L INNER JOIN ProdMcias AS P ON L.tmIdProducto=P.IdProducto
 INNER JOIN Bodegas AS B ON L.tmIdBodega=B.IdBodega
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LotTot @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SEntradas,SUM(tmSalidas) AS SSalidas
 FROM tm_Lot 
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_LotTotU @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null,@pmtmCdUbic VARCHAR(10)=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmEntradas) AS SEntradas,SUM(tmSalidas) AS SSalidas
 FROM tm_Lot 
 WHERE tmNumero=@pmtmNumero AND tmCdUbic=@pmtmCdUbic 
 AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')  AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Lotr @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmNumLote,tmIdProducto,DescripProd,tmIdBodega,tmCdUbic,tmEntradas,tmSalidas
,tmFecha,tmFecVence,tmLinea,tmCdNueUbic
 FROM tm_Lot AS L INNER JOIN ProdMcias AS P ON L.tmIdProducto=P.IdProducto
 WHERE tmNumero=@pmtmNumero AND Lotes<>0 
 ORDER BY tmNumLote,tmIdProducto,tmIdBodega,tmCdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovClientes @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,Agencia,tmIdCia,Compania,tmTipDoc,TipoDoc,tmNumero,tmItem,tmFecha
,tmCargos,tmAbonos,tmDetalle,tmReferencia,tmNumForma,tmModalidad,tmTipRef,tmDocRef,tmIdCiaRef,tmCdConcepto,Concepto
,tmIdVend,VN.RazonSocial AS Vendedor,tmSanAgeCia,tmSanAge,tmSanClieCia,tmSanClie,tmTotalAbono,tmVrAfavor,tmTimeSys,tmFecVence
,tmCdForma,tmEnEfectivo,tmCdBanco,Banco,tmBeneficiario,tmCdLocal,LD.Localidad AS DocCiudad,tmTipCom,tmComprobante,tmIdCiaCom
,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,Usuario
--información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.SitioWeb AS TercSitioWeb,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
--información del cliente
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,VrCupo,VrSaldo,Contrato,CLI.NContrato AS CliNumContrato,CLI.IdPlazo AS CdPlazo,Plazo,NVmto,CLI.CdBandera AS CodBandera,TipoBandera
,CodAgencia,A.DirAgncia AS AgeDireccion,A.TelAgncia AS AgeTelefono,A.FaxAgncia AS AgeFax,A.IdLocal AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep
,DA.Departamento AS AgeDpto,A.Referencia AS AgeReferencia,A.NContrato AS AgeNumContrato,CiaCont
 FROM tm_MovClientes AS M  INNER JOIN Terceros AS T ON M.tmIdCliente=T.IdTercero
 INNER JOIN Agencias AS A ON M.tmIdAgencia=A.IdAgencia   INNER JOIN  Companias AS CI ON M.tmIdCia=CI.IdCia
 INNER JOIN Terceros AS VN ON M.tmIdVend=VN.IdTercero  INNER JOIN TercCliente AS CLI ON  M.tmIdCliente=CLI.IdClie
 INNER JOIN adm_Usuarios AS U ON M.tmIdUsuario=U.IdUsuario
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona  INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo INNER JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 INNER JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  INNER JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Conceptos AS C ON M.tmCdConcepto=C.IdConcepto  LEFT JOIN Sys_TiposDoc AS TD ON M.tmTipDoc=TD.IdDoc
 LEFT JOIN Bancos AS B ON M.tmCdBanco =B.IdBanco LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera
 LEFT JOIN Localidades AS LD ON M.tmCdLocal=LD.IdLocal
 WHERE tmEst=@pmtmEst 
 ORDER BY T.RazonSocial,tmIdAgencia,tmTimeSys
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovClientesCli @pmtmEst CHAR(2),@pmtmIdCliente VARCHAR(16)=Null,@pmtmIdAgencia VARCHAR(16)=Null
,@pmtmIdCia CHAR(2)=Null
 AS 
 SELECT tmEst,tmTipDoc,tmNumero,tmIdCia,tmItem,tmIdCliente,tmIdAgencia,tmSanClie,tmSanClieCia,tmSanAge,tmSanAgeCia
 FROM tm_MovClientes
 WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') AND tmIdAgencia LIKE ISNULL(@pmtmIdAgencia,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovCue @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT C.IdCuenta AS CdCuenta,NomCuenta,TipCom,TipoCom,Comprobante,C.IdCia AS CdCia,Compania,Item,Fecha,Detalle,VrDebito,VrCredito,C.IdTercero AS NitTercero,T.RazonSocial AS RaznSocial
,IdVehiculo,C.IdCCosto AS CdCenCosto,CCosto,C.IdSubCos AS CdSubcentro,SubCosto,VrBase,TarifaBase,TipDoc,TipoDoc,Documento,IdCiaDoc,CodConce,Concepto
,NitDoc,ND.RazonSocial AS NomNitDoc,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumeroCta,CTE.IdBanco AS CodBanco,Banco
,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,NIO.RazonSocial AS NomNitOtros,CodSubgpo,Subgrupo,CiuOrigen,LD.Localidad AS NomCiudad
,CodAgncia,A.Agencia AS NomAgencia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,VrSanCue,VrSanCueCia,VrSanNit,VrSanNitCia,VrSanVeh,VrSanVehCia
,VrSanCc,VrSanCcCia,VrSanCcSub,VrSanCcSubCia,VrSanNitCc,VrSanNitCcCia,VrSanNitCcSub,VrSanNitCcSubCia,VrSanNitAge,VrSanNitAgeCia
,VrSanNitAgeCc,VrSanNitAgeCcCia,VrSanNitAgeCcSub,VrSanNitAgeCcSubCia,VrSanNitVeh
,VrSanNitVehCia,VrSanNitVehCc,VrSanNitVehCcCia,VrSanVehCc,VrSanVehCcCia,VrSanVehCcSub,VrSanVehCcSubCia,tmEst
,TC.IdDiario AS CdDiario,Diario
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono,T.TipEnte AS TercTipEnte
,CodAgencia,DirAgncia,Referencia
 FROM tm_MovCue AS C  INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta
 INNER JOIN CentroCosto AS O ON C.IdCCosto=O.IdCCosto
 LEFT JOIN  TiposCom AS TC ON C.TipCom=TC.IdCom LEFT JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 LEFT JOIN Companias AS CI ON C.IdCia=CI.IdCia
 LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NIO ON C.NitOtros=NIO.IdTercero
 LEFT JOIN Terceros AS ND ON C.NitDoc=ND.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN Localidades AS LD ON C.CiuOrigen=LD.IdLocal
 LEFT JOIN SubGrupos AS SG ON  C.CodSubgpo=SG.IdSubgrupo
 LEFT JOIN Agencias AS A ON C.CodAgncia=A.IdAgencia
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTE ON C.CodCta=CTE.IdCta LEFT JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 WHERE tmEst=@pmtmEst AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND C.IdTercero LIKE ISNULL(@pmIdTercero ,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 ORDER BY C.IdCuenta,Fecha,C.IdCia,TipCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovCueCue @pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16),@pmIdCia CHAR(2)=Null
,@pmIdTercero VARCHAR(16)=Null,@pmIdCCosto VARCHAR(16)=Null,@pmIdSubCos VARCHAR(16)=Null
,@pmIdVehiculo VARCHAR(10)=Null,@pmCodAgncia VARCHAR(16)=Null
 AS 
 SELECT tmEst,IdCuenta,IdTercero,IdVehiculo,IdCCosto,IdSubCos,CodAgncia
 FROM tm_MovCue
 WHERE tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta  AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 AND IdTercero LIKE ISNULL(@pmIdTercero,'%') AND IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 AND IdSubCos LIKE ISNULL(@pmIdSubCos,'%') AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
 AND CodAgncia LIKE ISNULL(@pmCodAgncia,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovCueDia @pmtmEst CHAR(2),@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmIdTercero VARCHAR(16)=Null
,@pmIdCCosto VARCHAR(16)=Null
 AS 
 SELECT C.IdCuenta AS CdCuenta,NomCuenta,TipCom,TipoCom,Comprobante,C.IdCia AS CdCia,Compania,Item,Fecha,Detalle,VrDebito,VrCredito,C.IdTercero AS NitTercero,T.RazonSocial AS RaznSocial
,IdVehiculo,C.IdCCosto AS CdCenCosto,CCosto,C.IdSubCos AS CdSubcentro,SubCosto,VrBase,TarifaBase,TipDoc,TipoDoc,Documento,IdCiaDoc,CodConce,Concepto
,NitDoc,ND.RazonSocial AS NomNitDoc,TipFac,Factura,IdCiaFac,ItemFac,FecVence,CodCta,NumeroCta,CTE.IdBanco AS CodBanco,Banco
,NumCheque,Integrado,TipoAplica,Consolida,CodCargo,NitOtros,NIO.RazonSocial AS NomNitOtros,CodSubgpo,Subgrupo,CiuOrigen,LD.Localidad AS NomCiudad
,CodAgncia,A.Agencia AS NomAgencia,VehPropio,Referncia,TipDocRef,DocRef,IdCiaRef,VrSanCue,VrSanCueCia,VrSanNit,VrSanNitCia
,VrSanNitCc,VrSanNitCcCia,VrSanNitCcSub,VrSanNitCcSubCia
,TC.IdDiario AS CdDiario,Diario,tmEst,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
--no disponible
--,VrSanVeh,VrSanVehCia,VrSanCc,VrSanCcCia,VrSanCcSub,VrSanCcSubCia,,VrSanNitAge,VrSanNitAgeCia
--,VrSanNitAgeCc,VrSanNitAgeCcCia,VrSanNitAgeCcSub,VrSanNitAgeCcSubCia,VrSanNitVeh
--,VrSanNitVehCia,VrSanNitVehCc,VrSanNitVehCcCia,VrSanVehCc,VrSanVehCcCia,VrSanVehCcSub,VrSanVehCcSubCia,
--Información del tercero
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS TercCiudad,L.IdDep AS CdDep,Departamento,T.Telefono AS TercTelefono,T.TipEnte AS TercTipEnte
,CodAgencia,DirAgncia,Referencia
 FROM tm_MovCue AS C  INNER JOIN Terceros AS T ON C.IdTercero=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN Puc AS P ON C.IdCuenta=P.IdCuenta
 INNER JOIN CentroCosto AS O ON C.IdCCosto=O.IdCCosto
 LEFT JOIN  TiposCom AS TC ON C.TipCom=TC.IdCom LEFT JOIN Diarios AS DR ON TC.IdDiario=DR.IdDiario
 LEFT JOIN Companias AS CI ON C.IdCia=CI.IdCia
 LEFT JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 LEFT JOIN Terceros AS NIO ON C.NitOtros=NIO.IdTercero
 LEFT JOIN Terceros AS ND ON C.NitDoc=ND.IdTercero
 LEFT JOIN Conceptos AS CN ON C.CodConce=CN.IdConcepto
 LEFT JOIN Localidades AS LD ON C.CiuOrigen=LD.IdLocal
 LEFT JOIN SubGrupos AS SG ON  C.CodSubgpo=SG.IdSubgrupo
 LEFT JOIN Agencias AS A ON C.CodAgncia=A.IdAgencia
 LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN CtasCorrientes AS CTE ON C.CodCta=CTE.IdCta LEFT JOIN Bancos AS B ON CTE.IdBanco=B.IdBanco
 LEFT JOIN tm_NivCue AS NV ON C.IdCuenta=NV.tmIdCuenta
 WHERE tmEst=@pmtmEst AND C.IdCia LIKE ISNULL(@pmIdCia,'%%') AND C.IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
 AND C.IdTercero LIKE ISNULL(@pmIdTercero ,'%') AND C.IdCCosto LIKE ISNULL(@pmIdCCosto,'%')
 ORDER BY C.IdCuenta,Fecha,C.IdCia,TipCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovCueGru @pmtmEst CHAR(2)
 AS 
 SELECT IdCuenta AS tmIdCuenta,count(Item) AS SCANT
 FROM tm_MovCue 
 WHERE tmEst=@pmtmEst
 GROUP BY IdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovInvent @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdCliente,T.RazonSocial AS NomCliente,tmCdAgencia,Agencia,CodAgencia,tmIdVend,VN.RazonSocial AS Vendedor
,tmIdBodega,Bodega,tmTipDoc,TipoDoc,tmIdConcepto,Concepto,tmCdLocal,Localidad,L.IdDep AS CodDep,Departamento
,tmCdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,tmFecha
,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal,tmValorEnt,tmValorSal,tmDctosEnt,tmDctosSal,tmIvaEnt,tmIvaSal,tmOtrosImpEnt,tmOtrosImpSal,tmUnidVolumen
,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmOtrosImpEnt2,tmOtrosImpSal2,tmUnidVolumen2
,tmEst,tmItem
--Datos del producto
,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,TipoRef,PM.IdMarca AS CdMarca,Marca
,PM.IdUnd AS CdUnid,Unidad,FecUltcom,FecUltVta,Tanques
--datos del tercero
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
 FROM tm_MovInvent AS V  INNER JOIN ProdMcias  AS PM ON V.tmIdProducto=PM.IdProducto  INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 LEFT JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero LEFT JOIN Agencias  AS A ON V.tmCdAgencia=A.IdAgencia
 LEFT JOIN TercCliente AS CLI ON V.tmIdCliente=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Subzonas AS SZ ON V.tmCdSubzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Terceros AS VN ON V.tmIdVend=VN.IdTercero   LEFT JOIN Bodegas AS B ON V.tmIdBodega=B.IdBodega 
 LEFT  JOIN Companias AS CN ON  V.tmIdCia=CN.IdCia  LEFT JOIN Sys_TiposDoc AS TD ON V.tmTipDoc=TD.IdDoc
 LEFT JOIN  Conceptos AS C ON V.tmIdConcepto=C.IdConcepto
 LEFT JOIN Localidades AS L ON V.tmCdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera  LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera
 WHERE tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,tmFecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovTanquesDia @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16)=Null
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdTanque,tmFecha,tmSaldoAntDia,tmEntradas,tmDevEnt
,tmPlanillas,tmDevPlanillas,tmNotasCalib,tmFacturas,tmDevFact,tmSalidas,tmDevSalidas,tmAjusteSob,tmAjusteFal
,tmTraslados,tmOtros,tmNuevoSaldo,tmCantFisico,tmDiferencia,tmCantFisico-tmNuevoSaldo AS Sob_Fal,tmCantAgua
--datos del producto
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,DescripAbrv
,PM.IdUnd AS CdUnid,Unidad,Precio1,Precio2,Precio3,Precio4,Precio5,VrCosto,VrCostPmd,ExtciaAct,ExtciaMin,ExtciaMax,FecUltcom,FecUltVta
,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual,TnqGas,TnqUnido,TnqVirtual,NColor
--costos
,tmCostoAntDia,tmCostoEnt,tmCostoDve,tmCostoPla,tmCostoDvp,tmCostoNot,tmCostoFac,tmCostoDfc
,tmCostoSal,tmCostoDvs,tmCostoAjs,tmCostoAjf,tmCostoTra,tmCostoOtr,tmCostoNsal
,tmSaldoAnt,tmCostoAnt,tmSaldoAntCia,tmCostoSanCia,tmSaldoAntTan,tmCostoSanTan,tmSaldoAntPro,tmCostoSanPro
 FROM tm_MovTanques AS M INNER JOIN ProdMcias AS PM ON M.tmIdProducto=PM.IdProducto
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea   INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 LEFT JOIN Companias AS CN ON M.tmIdCia=CN.IdCia  LEFT JOIN Tanques AS TQ ON M.tmIdTanque=TQ.IdTanque
 WHERE tmEst=@pmtmEst AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 ORDER BY DescripProd,tmIdCia,tmIdTanque,tmFecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovTanquesFis @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16)=Null
 AS 
 SELECT tmEst,tmIdProducto,DescripProd,tmIdTanque,tmItem,tmIdCia,tmFecha,tmSaldoAnt,tmCostoAnt
,tmEntradas+tmDevSalidas+tmDevFact+tmAjusteSob+tmDevPlanillas+tmNotasCalib+tmTraslados AS TotalEntradas
,tmCostoEnt+tmCostoDvs+tmCostoDfc+tmCostoAjs+tmCostoDvp+tmCostoNot+tmCostoTra AS CostoEntradas
,tmDevEnt+tmSalidas+tmFacturas+tmAjusteFal+tmPlanillas+tmOtros AS TotalSalidas
,tmCostoDve+tmCostoSal+tmCostoFac+tmCostoAjf+tmCostoPla+tmCostoOtr AS CostoSalidas
,tmNuevoSaldo,tmCostoNsal,tmCantFisico,tmCantAgua,tmDiferencia
 FROM tm_MovTanques AS M INNER JOIN ProdMcias AS PM ON M.tmIdProducto=PM.IdProducto
 WHERE tmEst=@pmtmEst AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 ORDER BY tmIdProducto,tmIdCia,tmIdTanque,tmFecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovTanquesPro @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16)
,@pmtmIdTanque VARCHAR(4)=Null,@pmtmIdCia CHAR(2)=Null
 AS 
 SELECT tmEst,tmIdProducto,tmIdTanque,tmItem,tmIdCia,tmFecha,tmSaldoAnt,tmCostoAnt
 FROM tm_MovTanques
 WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto
  AND tmIdTanque LIKE ISNULL(@pmtmIdTanque,'%') AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovTanquesRes @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16)=Null
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdTanque,tmSaldoAnt,tmEntradas,tmSalidas,tmSaldoAnt+tmEntradas-tmSalidas AS NuevoSaldo
,tmCostoAnt,tmCostoEnt,tmCostoSal,tmCostoAnt+tmCostoEnt-tmCostoSal AS CostoTotal
,tmSaldoAntCia,tmCostoSanCia,tmSaldoAntTan,tmCostoSanTan,tmSaldoAntPro,tmCostoSanPro
--datos del producto
,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,S.IdGrupo AS CdGrupo,Grupo,G.IdLinea AS CdLinea,Linea,DescripAbrv
,PM.IdUnd AS CdUnid,Unidad,Precio1,Precio2,Precio3,Precio4,Precio5,VrCosto,VrCostPmd,ExtciaAct,ExtciaMin,ExtciaMax,FecUltcom,FecUltVta
,Descripcion,CapTanq,NivAgua,CapNeta,SaldoActual,TnqGas,TnqUnido,TnqVirtual,NColor
 FROM tm_MovTanques AS M INNER JOIN ProdMcias AS PM ON M.tmIdProducto=PM.IdProducto
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea   INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 LEFT JOIN Companias AS CN ON M.tmIdCia=CN.IdCia  LEFT JOIN Tanques AS TQ ON M.tmIdTanque=TQ.IdTanque
 WHERE tmEst=@pmtmEst AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 ORDER BY DescripProd,tmIdCia,tmIdTanque


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovVentas @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdCliente,T.RazonSocial AS NomCliente,tmCdAgencia,Agencia,CodAgencia,tmIdVend,VN.RazonSocial AS Vendedor
,tmIdBodega,Bodega,tmTipDoc,TipoDoc,tmIdConcepto,Concepto,tmCdLocal,Localidad,L.IdDep AS CodDep,Departamento
,tmCdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,tmFecha
,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal,tmValorEnt,tmValorSal,tmDctosEnt,tmDctosSal,tmIvaEnt,tmIvaSal,tmImpGlobalEnt,tmImpGlobalSal,tmOtrosEnt,tmOtrosSal
,tmUnidVolumen,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmImpGlobalEnt2,tmImpGlobalSal2,tmOtrosEnt2,tmOtrosSal2,tmUnidVolumen2
,tmItem
--Datos del producto
,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,TipoRef,PM.IdMarca AS CdMarca,Marca
,PM.IdUnd AS CdUnid,Unidad,FecUltcom,FecUltVta,Tanques
--datos del tercero
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
 FROM tm_MovVentas AS V LEFT JOIN ProdMcias  AS PM ON V.tmIdProducto=PM.IdProducto  LEFT JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 LEFT JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo LEFT JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 LEFT JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea LEFT JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 LEFT JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero LEFT JOIN Agencias  AS A ON V.tmCdAgencia=A.IdAgencia
 LEFT JOIN TercCliente AS CLI ON V.tmIdCliente=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Subzonas AS SZ ON V.tmCdSubzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Terceros AS VN ON V.tmIdVend=VN.IdTercero   LEFT JOIN Bodegas AS B ON V.tmIdBodega=B.IdBodega 
 LEFT  JOIN Companias AS CN ON  V.tmIdCia=CN.IdCia  LEFT JOIN Sys_TiposDoc AS TD ON V.tmTipDoc=TD.IdDoc
 LEFT JOIN  Conceptos AS C ON V.tmIdConcepto=C.IdConcepto
 LEFT JOIN Localidades AS L ON V.tmCdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera  LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera
 WHERE tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,tmFecha

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_MovVentasAes @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdCliente,T.RazonSocial AS NomCliente,tmCdAgencia,Agencia,CodAgencia,tmIdVend,VN.RazonSocial AS Vendedor
,tmCdLocal,Localidad,L.IdDep AS CodDep,Departamento,tmCdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,tmEntradas,tmSalidas,tmCostoEnt,tmCostoSal,tmValorEnt,tmValorSal,tmDctosEnt,tmDctosSal,tmIvaEnt,tmIvaSal,tmImpGlobalEnt,tmImpGlobalSal,tmOtrosEnt,tmOtrosSal
,tmUnidVolumen,tmEntradas2,tmSalidas2,tmCostoEnt2,tmCostoSal2,tmValorEnt2,tmValorSal2,tmDctosEnt2,tmDctosSal2,tmIvaEnt2,tmIvaSal2,tmImpGlobalEnt2,tmImpGlobalSal2,tmOtrosEnt2,tmOtrosSal2,tmUnidVolumen2
,tmItem
--datos del tercero
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
,CLI.IdVend AS IdVendDef,VD.RazonSocial AS VendedorDef,CLI.NContrato AS CliNumContrato,CiaContMay,A.NContrato AS AgeNContrato,CiaCont
 FROM tm_MovVentas AS V  INNER JOIN ProdMcias  AS PM ON V.tmIdProducto=PM.IdProducto 
 INNER JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Agencias  AS A ON V.tmCdAgencia=A.IdAgencia  LEFT JOIN TercCliente AS CLI ON V.tmIdCliente=CLI.IdClie 
 LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN Subzonas AS SZ ON V.tmCdSubzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Terceros AS VN ON V.tmIdVend=VN.IdTercero   LEFT  JOIN Companias AS CN ON  V.tmIdCia=CN.IdCia
 LEFT JOIN Localidades AS L ON V.tmCdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera  LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera
 LEFT JOIN Terceros AS VD ON CLI.IdVend=VD.IdTercero
 WHERE tmEst=@pmtmEst
 ORDER BY T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NomHoras 
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmHorDiurno,tmHorNocturno,tmHorExtraDia,tmHorExtraNoc,tmHorRecNoc,tmHorDomDia,tmHorDomNoc,tmHorExtDom,tmHorExtDomNoc
,tmVrHoras1,tmVrHoras2,tmVrHoras3,tmVrHoras4,tmVrSalario,tmVrATrans
,tmIdArea,Area,tmIdDep,Dependencia,tmIdInstala,Instlacion,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdNom,TipoNomina,tmTipoLiquida,tmpVehiculo
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,E.Inactivo AS Emp_Inactvo
 FROM tm_NomHoras AS N INNER JOIN Empleados AS E ON N.tmIdEmpleado=E.IdEmpleado
 INNER JOIN  Emp_Contrato AS C ON N.tmNContrato=C.NContrato
 INNER JOIN Dependencias AS DP ON N.tmIdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON N.tmIdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON N.tmIdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON N.tmIdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON N.tmIdNom=TN.IdNom  INNER JOIN Areas AS AR ON N.tmIdArea=AR.IdArea 
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 ORDER BY Apellidos,Nombres
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NomRes @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmItem,tmIdConcepto,tmDetalle,tmFecInicial,tmFecFinal,tmIdPeriodo
,tmCantDevg,tmVrTotDevg,tmCantDed,tmVrTotDed,tmUnidad,tmVrOrigen,Concepto,tmClaseLiq,ClaseCon,tmTarifa,tmDiasCalc,tmDiasNov
,tmCodFondo,FCO.Fondo AS NomFondo,tmNPrestamo,tmNCuota,tmVrBaseLiq,tmVrUnitario
,tmIdArea,Area,tmIdDep,Dependencia,tmIdInstala,Instlacion,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdNom,TipoNomina,tmTipoLiq,tmEsRetiro
,tmNLiqRet,tmVrSalario,tmVrATrans,tmpVehiculo,tmTipCom,tmComprobante,tmIdCiaCom,tmObservacion
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,FC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,E.Inactivo AS Emp_Inactvo
 FROM tm_NomRes AS N INNER JOIN Empleados AS E ON N.tmIdEmpleado=E.IdEmpleado
 INNER JOIN Areas AS AR ON N.tmIdArea=AR.IdArea INNER JOIN Dependencias AS DP ON N.tmIdDep=DP.IdDep
 INNER JOIN TiposNom AS TN ON N.tmIdNom=TN.IdNom 
 INNER JOIN CentroCosto AS CC ON N.tmIdCCosto=CC.IdCCosto INNER JOIN SubCentros AS SC ON N.tmIdSubCos=SC.IdSubCos
 INNER JOIN Instalaciones AS I ON N.tmIdInstala=I.IdInstala
 INNER JOIN NomConceptos AS NC ON N.tmIdConcepto=NC.IdConcepto
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco 
 INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo 
 INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo INNER JOIN Fondos AS FC ON E.IdCajaCom=FC.IdFondo 
 INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 LEFT JOIN Fondos AS FCO ON N.tmCodFondo=FCO.IdFondo
 WHERE tmNumero=@pmtmNumero
 ORDER BY Apellidos,Nombres,tmFecInicial,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NomResCon @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmIdConcepto,Concepto,ClaseCon,tmDetalle
,SUM(tmCantDevg) AS CantDev,SUM(tmCantDed) AS CantDed,SUM(tmVrTotDevg) AS TotalDeveng
,SUM(tmVrTotDed) AS TotalDed,SUM(tmVrOrigen) AS VrOrigen,SUM(tmDiasCalc) AS CantDias,SUM(tmDiasNov) AS DiasNov
,SUM(tmVrBaseLiq) AS TotalBaseLiq
,tmIdArea,Area,tmIdDep,Dependencia,tmIdInstala,Instlacion,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdNom,TipoNomina
,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco
 FROM tm_NomRes AS N INNER JOIN Empleados AS E ON N.tmIdEmpleado=E.IdEmpleado
 INNER JOIN Areas AS AR ON N.tmIdArea=AR.IdArea INNER JOIN Dependencias AS DP ON N.tmIdDep=DP.IdDep
 INNER JOIN TiposNom AS TN ON N.tmIdNom=TN.IdNom 
 INNER JOIN CentroCosto AS CC ON N.tmIdCCosto=CC.IdCCosto INNER JOIN SubCentros AS SC ON N.tmIdSubCos=SC.IdSubCos
 INNER JOIN Instalaciones AS I ON N.tmIdInstala=I.IdInstala
 INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco 
 INNER JOIN NomConceptos AS NC ON N.tmIdConcepto=NC.IdConcepto
 WHERE tmNumero=@pmtmNumero
 GROUP BY  tmIdEmpleado,Apellidos,Nombres,tmNContrato,tmIdConcepto,Concepto,ClaseCon,tmDetalle
 ,tmIdArea,Area,tmIdDep,Dependencia,tmIdInstala,Instlacion,tmIdCCosto,CCosto,tmIdSubCos,SubCosto,tmIdNom,TipoNomina
,E.IdClase,ClaseCuenta,NumCuenta,E.IdBanco,Banco
 ORDER BY Apellidos,Nombres,Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Nomina @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT
 AS 
 SELECT tmNumero,tmIdEmpleado,tmItem,tmIdConcepto,tmClaseCon,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad,tmClaseLiq
,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmOrigCargue,tmVrBaseLiq,tmNContto,tmFecIni,tmFecFin,tmIdPeriodo
,tmFecUltLiq,tmFecIniPmd,tmFecFinPmd,tmLogPmdio,tmVrPension,tmVrSalud
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
ORDER BY tmIdEmpleado,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaBas @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmTipBas VARCHAR(3)
,@pmtmClaseLiq VARCHAR(10)
 AS 
 IF @pmTipBas ='SD' --SALARIO BASICO+EXTRAS
  SELECT tmIdConcepto,SUM(tmCantDevg) AS SCANDEV,SUM(tmVrTotDevg) AS STOTDEV,SUM(tmCantDed) AS SCANDED
,SUM(tmVrTotDed) AS STOTDED,SUM(tmVrBaseLiq) AS SBASLIQ
  FROM tm_Nomina 
  WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')
  AND tmIdConcepto NOT IN ('BAS','LIC','IGE','IRP','HHD','HHN','DSR','VAC')
 GROUP BY tmIdConcepto
 ELSE --TOTAL DEVENGADO
  SELECT tmIdConcepto,SUM(tmCantDevg) AS SCANDEV ,SUM(tmVrTotDevg) AS STOTDEV,SUM(tmCantDed) AS SCANDED
,SUM(tmVrTotDed) AS STOTDED,SUM(tmVrBaseLiq) AS SBASLIQ
  FROM tm_Nomina 
  WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND tmClaseLiq IN ('NOMINA','NOVEDAD')
  GROUP BY tmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaCau @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmNContto,tmFecIni,tmFecFin,Count(tmItem) AS SCANT,SUM(tmCantDevg) AS SCANDEV,SUM(tmVrTotDevg) AS STOTDEV
,SUM(tmCantDed) AS SCANDED,SUM(tmVrTotDed) AS STOTDED,SUM(tmDiasCalc-tmDiasNov) AS SDIASNET,SUM(tmVrBaseLiq) AS SVALLIQ
 FROM tm_Nomina AS N INNER JOIN Empleados AS E ON N.tmIdEmpleado=E.IdEmpleado
 WHERE tmNumero=@pmtmNumero  AND tmItem>=@pmtmItem AND tmClaseLiq IN ('NOMINA','NOVEDAD')
 GROUP BY  tmIdEmpleado,Apellidos,Nombres,tmNContto,tmFecIni,tmFecFin
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaCip @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT tmItem,tmIdConcepto,tmDetalle,tmVrBaseLiq,tmIdEmpleado
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq IN ('NOMINA','NOVEDAD')
 AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
ORDER BY tmIdEmpleado,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaCon @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmIdConcepto VARCHAR(4),@pmtmItem INT,@pmtmClaseLiq VARCHAR(10)=Null
AS 
  SELECT Count(tmItem) AS SITEM
  FROM tm_Nomina
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND tmIdConcepto=@pmtmIdConcepto
 AND tmItem<>@pmtmItem  AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%') 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaCpre @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmClaseLiq VARCHAR(10)=Null
AS 
  SELECT Count(tmItem) AS SITEM
  FROM tm_Nomina
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND tmNPrestamo>0
 AND  tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%') 


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaDia @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmTipo CHAR(1),@pmtmIdConcepto VARCHAR(4),@pmtmItem INT
AS 
 IF @pmTipo='B'
  SELECT SUM(tmCantDevg) AS SCANDEV,SUM(tmCantDed) AS SCANDED,SUM(tmVrTotDevg) AS SVALDEV,SUM(tmVrTotDed) AS SVALDED
,SUM(tmDiasCalc) AS SCANDIA,SUM(tmDiasCalc-tmDiasNov) AS SDIASNET
  FROM tm_Nomina
  WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND  tmClaseLiq IN ('NOMINA','NOVEDAD') AND tmItem<>@pmtmItem
  AND tmIdConcepto IN ('BAS','LIC','IGE','IRP','VAC','DSR')
 ELSE
 BEGIN
  IF  @pmTipo='H'
   SELECT SUM(tmCantDevg) AS SCANDEV,SUM(tmCantDed) AS SCANDED,SUM(tmVrTotDevg) AS SVALDEV  ,SUM(tmVrTotDed) AS SVALDED
 ,SUM(tmDiasCalc) AS SCANDIA,SUM(tmDiasCalc-tmDiasNov) AS SDIASNET
   FROM tm_Nomina
   WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND  tmClaseLiq IN ('NOMINA','NOVEDAD') AND tmItem<>@pmtmItem
   AND tmIdConcepto IN ('HHD','HHN')--,'HFD','HFN')
  ELSE
   BEGIN
    IF  @pmTipo='E'
     SELECT SUM(tmCantDevg) AS SCANDEV,SUM(tmCantDed) AS SCANDED,SUM(tmVrTotDevg) AS SVALDEV  ,SUM(tmVrTotDed) AS SVALDED
   ,SUM(tmDiasCalc) AS SCANDIA,SUM(tmDiasCalc-tmDiasNov) AS SDIASNET
     FROM tm_Nomina
     WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND  tmClaseLiq IN ('NOMINA','NOVEDAD') AND tmItem<>@pmtmItem
     AND tmIdConcepto IN ('HED','HEN','HXD','HXN')
    ELSE
     SELECT SUM(tmCantDevg) AS SCANDEV,SUM(tmCantDed) AS SCANDED,SUM(tmVrTotDevg) AS SVALDEV  ,SUM(tmVrTotDed) AS SVALDED
   ,SUM(tmDiasCalc) AS SCANDIA,SUM(tmDiasCalc-tmDiasNov) AS SDIASNET
     FROM tm_Nomina
     WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND  tmClaseLiq IN ('NOMINA','NOVEDAD') AND tmItem<>@pmtmItem
     AND tmIdConcepto=@pmtmIdConcepto
   END
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaDsa @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmItem,tmIdConcepto,tmDetalle,tmClaseCon,tmCantDevg,tmUnidad,tmVrUnitario,tmVrTotDevg,tmCantDed,tmVrUnitario AS VrUndDed
,tmVrTotDed,tmNPrestamo,tmNCuota,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmDiasCalc-tmDiasNov AS Dias,tmCodFondo,tmVrBaseLiq
 FROM tm_Nomina AS N INNER JOIN Empleados AS E ON N.tmIdEmpleado=E.IdEmpleado
 WHERE tmNumero=@pmtmNumero  AND tmItem>=@pmtmItem
 AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')  --AND tmClaseLiq IN ('NOMINA','NOVEDAD')
 ORDER BY Apellidos,Nombres,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaDsl @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT tmItem,tmIdConcepto,tmDetalle,tmClaseCon,tmCantDevg,tmUnidad,tmVrUnitario,tmVrTotDevg,tmCantDed,tmVrUnitario AS VrUndDed
,tmVrTotDed,tmNPrestamo,tmNCuota,tmVrOrigen,tmTarifa,tmFecUltLiq,tmFecIni,tmFecFin,tmDiasCalc,tmDiasNov,tmDiasCalc-tmDiasNov AS Dias
,tmLogPmdio,tmClaseLiq,tmFecIniPmd,tmFecFinPmd,tmIdEmpleado
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmClaseLiq NOT IN ('PROVISION','APORTE') AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaDso @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT tmItem,tmIdConcepto,tmDetalle,tmClaseCon,tmCantDevg,tmUnidad,tmVrUnitario,tmVrTotDevg,tmCantDed,tmVrUnitario AS VrUndDed
,tmVrTotDed,tmNPrestamo,tmNCuota,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmDiasCalc-tmDiasNov AS Dias,tmIdEmpleado
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq IN ('NOMINA','NOVEDAD')
 AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
ORDER BY tmIdEmpleado,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaEdv @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmClaseLiq VARCHAR(10)
,@pmtmItem INT
--valida si existe el primer concepto devengado
AS 
  SELECT Count(tmItem) AS SITEM
  FROM tm_Nomina
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado
  AND tmIdConcepto IN ('BAS','LIC','IGE','IRP','HHD','HHN','HFD','HFN','DSR','VAC')
 AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')  AND tmItem<>@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaEitm @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmItem INT,@pmtmClaseLiq VARCHAR(10)=Null
 AS 
 SELECT COUNT(tmItem) AS SITEM
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaGCon @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmIdConcepto VARCHAR(4)=Null,@pmtmClaseLiq VARCHAR(10)=Null
AS 
  SELECT tmIdConcepto,tmClaseLiq,Count(tmItem) AS SITEM,SUM(tmVrBaseLiq) AS STOTBAS,SUM(tmVrTotDevg) AS STOTDEV,SUM(tmVrTotDed) AS STOTDED
,SUM(tmCantDevg) AS SCANDEV,SUM(tmCantDed) AS SCANDED,SUM(tmVrOrigen) AS SBASORIG
  FROM tm_Nomina
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND tmIdConcepto LIKE ISNULL(@pmtmIdConcepto,'%')
 AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%') 
 GROUP BY tmIdConcepto,tmClaseLiq
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaId @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT MAX(tmItem) AS MXITEM
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaItm @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT
 AS 
 SELECT COUNT(tmItem) AS SITEM
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 AND  tmClaseLiq IN ('NOMINA','NOVEDAD')
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaLInt @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmClaseLiq VARCHAR(10)=Null
 AS 
 SELECT tmNumero,tmIdEmpleado,tmItem,tmIdConcepto,tmClaseCon,tmDetalle,tmCantDevg,tmCantDed,tmVrUnitario,tmVrTotDevg,tmVrTotDed,tmUnidad,tmClaseLiq
,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmCodFondo,tmNPrestamo,tmNCuota,tmOrigCargue,tmVrBaseLiq,tmNContto,tmFecIni,tmFecFin,tmIdPeriodo
,tmFecUltLiq,tmFecIniPmd,tmFecFinPmd,tmLogPmdio
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero AND tmClaseLiq IN ('NOMINA','PROVISION','APORTE','NOVEDAD',@pmClaseLiq)
 AND  tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaLpre @pmtmNumero VARCHAR(5),@pmtmIdConcepto VARCHAR(4)=Null,@pmtmIdEmpleado VARCHAR(16)=Null
,@pmtmClaseLiq VARCHAR(10)=Null
AS 
  SELECT tmNumero,tmIdEmpleado,tmItem,tmIdConcepto,tmCantDed,tmVrUnitario,tmVrTotDed,tmNPrestamo,tmNCuota,TotPrestamo,TotAbonado,VrPrestamo,NumCuotas
  FROM tm_Nomina AS N  INNER JOIN Trn_NomPrest AS P ON N.tmNPrestamo=P.NPrestamo
 WHERE  tmNumero=@pmtmNumero AND  tmNPrestamo>0 AND  tmIdEmpleado LIKE ISNULL(@pmtmIdEmpleado,'%')
 AND  tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')  AND tmIdConcepto LIKE ISNULL(@pmtmIdConcepto,'%')
ORDER BY tmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaNov @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmNContto INT
 AS 
 SELECT Count(tmItem) AS SCANT,SUM(tmCantDevg) AS SCANDEV,SUM(tmVrTotDevg) AS STOTDEV
,SUM(tmCantDed) AS SCANDED,SUM(tmVrTotDed) AS STOTDED
,SUM(tmDiasCalc-tmDiasNov) AS SDIASNET,SUM(tmVrBaseLiq) AS SVALLIQ
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmItem>=0 AND tmClaseLiq='NOVEDAD' AND tmIdEmpleado=@pmtmIdEmpleado
 AND tmNContto=@pmtmNContto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaPre @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmItem INT,@pmtmNPrestamo INT
 AS 
 --VALIDA SI EXISTE EL PRESTAMO EN EL TEMPORAL
 SELECT COUNT(tmItem) AS SITEM
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq='NOMINA'  AND tmIdEmpleado=@pmtmIdEmpleado
 AND tmNPrestamo=@pmtmNPrestamo AND tmItem<>@pmtmItem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaRet @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT COUNT(tmItem) AS SITEM,SUM(tmCantDed) AS tCantDed,SUM(tmVrTotDed) AS tVrTotDed
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq='NOMINA' AND tmIdConcepto='RET'
 AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaSBas @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmClaseLiq VARCHAR(10)
 AS 
 SELECT tmIdConcepto,SUM(tmCantDevg) AS SCANDEV,SUM(tmVrTotDevg) AS STOTDEV,SUM(tmCantDed) AS SCANDED
,SUM(tmVrTotDed) AS STOTDED,SUM(tmVrBaseLiq) AS SBASLIQ
 FROM tm_Nomina 
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado 
 AND tmIdConcepto IN ('BAS','AXT','LIC','IGE','IRP','HHD','HHN','DSR','VAC')
 AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%')
 GROUP BY tmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaTCon @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
,@pmtmIdConcepto VARCHAR(4),@pmtmClaseLiq VARCHAR(10)=Null
AS 
  SELECT Count(tmItem) AS SITEM,SUM(tmVrBaseLiq) AS STOTBAS,SUM(tmVrTotDevg) AS STOTDEV,SUM(tmVrTotDed) AS STOTDED
,SUM(tmCantDevg) AS SCANDEV,SUM(tmCantDed) AS SCANDED
  FROM tm_Nomina
 WHERE  tmNumero=@pmtmNumero AND  tmIdEmpleado=@pmtmIdEmpleado AND tmIdConcepto=@pmtmIdConcepto
 AND tmClaseLiq LIKE ISNULL(@pmtmClaseLiq,'%') 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaTPen @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmIdPeriodo VARCHAR(8)=Null
 AS 
 SELECT COUNT(tmItem) AS SITEM,SUM(tmCantDed) AS tCantDed,SUM(tmVrTotDed) AS tVrTotDed
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq IN  ('NOMINA','NOVEDAD') AND tmIdConcepto IN ('DCP','FSP','FSU')
  AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')  AND  tmIdPeriodo LIKE ISNULL(@pmtmIdPeriodo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaTot @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT COUNT(tmItem) AS SITEM,SUM(tmCantDevg) AS tCantDevg,SUM(tmVrTotDevg) AS tVrTotDevg
 ,SUM(tmCantDed) AS tCantDed,SUM(tmVrTotDed) AS tVrTotDed,SUM(tmVrBaseLiq) AS tVrBaseLiq
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq IN  ('NOMINA','NOVEDAD')
  AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaVac @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT tmItem,tmIdConcepto,tmDetalle,tmClaseCon,tmCantDed,tmUnidad,tmVrUnitario,tmVrTotDed,tmVrOrigen,tmTarifa
,tmNPrestamo,tmNCuota,tmDiasCalc,tmVrPension AS VrApoPension,tmVrBaseLiq AS VrCertf,tmIdEmpleado,tmIdPeriodo
,tmVrSalud
 FROM tm_Nomina
 WHERE tmNumero=@pmtmNumero  AND tmClaseLiq IN ('NOMINA','NOVEDAD')
 AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
ORDER BY tmIdEmpleado,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_NominaVint @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)
 AS 
 SELECT tmIdConcepto,Concepto,SUM(tmCantDed) AS tCantDed,SUM(tmVrTotDed) AS tVrTotDed,SUM(tmCantDevg) AS tCantDevg,SUM(tmVrTotDevg) AS tVrTotDevg
 ,SUM(tmVrBaseLiq) AS tVrBaseLiq,SUM(tmVrOrigen) AS tVrOrigen
 FROM  tm_Nomina AS N INNER JOIN NomConceptos AS C ON N.tmIdConcepto=C.IdConcepto
 WHERE  tmNumero=@pmtmNumero  AND tmClaseLiq IN  ('NOMINA','NOVEDAD')
  AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 GROUP BY tmIdConcepto,Concepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Nomina_Cr @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT tmIdEmpleado,Apellidos,Nombres,tmItem,tmIdConcepto,tmDetalle,Concepto,tmClaseCon,tmCantDevg,tmUnidad,tmVrUnitario,tmVrTotDevg,tmCantDed,tmVrUnitario AS VrUndDed
,tmVrTotDed,tmNPrestamo,tmNCuota,tmVrOrigen,tmTarifa,tmDiasCalc,tmDiasNov,tmDiasCalc-tmDiasNov AS Dias,tmCodFondo,FD.Fondo AS tm_Fondo,tmVrBaseLiq,tmFecIni,tmFecFin,tmIdPeriodo
,tmFecUltLiq,tmFecIniPmd,tmFecFinPmd,tmLogPmdio,tmClaseLiq,tmOrigCargue
--Datos empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,FecNacmto,EsCondtor,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
--datos del contrato
,E.NContrato AS NumContrato,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdDep AS CodDep,Dependencia,C.IdInstala AS CodInst,Instlacion,C.IdCCosto AS CodCcto,CCosto
,C.IdSubCos AS CodSubcen,SubCosto,C.IdNom AS CodNom,TipoNomina,C.TipoLiquida AS TipLiq,Asistencia,NoDevenga,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie,VrSalario,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes
,SegFinMes,DchDotacion,DchCafeteria,ModoRet,C.IdEstado AS IdEstdo,Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,C.IdUsuario AS Con_IdUsuari,Usuario
,pVehiculo,NumVeh,TipoAdmon,VehPropio
 FROM tm_Nomina AS N INNER JOIN Empleados AS E ON N.tmIdEmpleado=E.IdEmpleado
 INNER JOIN NomConceptos AS CN ON N.tmIdConcepto=CN.IdConcepto
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN  Emp_Contrato AS C ON E.NContrato=C.NContrato  INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon
 INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo  INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea
 INNER JOIN Dependencias AS DP ON C.IdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON C.IdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON C.IdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON C.IdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON C.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EN ON C.IdEstado=EN.IdEstado
 INNER JOIN adm_Usuarios AS U ON C.IdUsuario=U.IdUsuario  INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase 
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada  LEFT JOIN Fondos AS FD ON N.tmCodFondo=FD.IdFondo
 LEFT JOIN NomVehiculos AS VH ON E.pVehiculo=VH.IdVehiculo
 WHERE tmNumero=@pmtmNumero  AND tmIdEmpleado like ISNULL(@pmtmIdEmpleado,'%')
 ORDER BY Apellidos,Nombres,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_OccComp @pmtmTipDoc VARCHAR(3),@pmtmOCargueIni INT
,@pmtmOCargueFin INT,@pmtmIdCia CHAR(2)
 AS 
 SELECT tmNumComp,tmIdProducto,DescripProd,DescripAbrv,Referencia,tmCantidad
,tmTipDoc,tmOCargue,tmIdCia
 FROM tm_OccComp AS C INNER JOIN ProdMcias AS P ON C.tmIdProducto=P.IdProducto
 WHERE tmTipDoc=@pmtmTipDoc AND tmOCargue BETWEEN @pmtmOCargueIni AND @pmtmOCargueFin AND tmIdCia=@pmtmIdCia
 ORDER BY tmNumComp
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_OccCompExi @pmtmTipDoc VARCHAR(3),@pmtmOCargue INT,@pmtmIdCia CHAR(2)
 AS 
 SELECT COUNT(*) AS Scant
 FROM tm_OccComp
 WHERE tmTipDoc=@pmtmTipDoc AND tmOCargue=@pmtmOCargue AND tmIdCia=@pmtmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Pagos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdForma,tmDetalle,tmVrPagado,tmVrCambio,tmEsCaja,tmIdBanco,tmNitCliente,tmCdAgencia,tmNumForma
,tmFecForma,tmCtaForma,tmBeneficiario,tmReferncia1,tmReferncia2,tmCdLocal,tmNumAutoriza,tmVrDenom,tmCantidad,tmTipDenom
,tmTipRef,tmDocRef,tmIdCiaRef,tmItemDoc,tmCdCta,tmCliente,tmTipDoc,tmDocumento,tmIdCia,tmTipAplica
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosChe @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmNumero,tmItem,tmIdForma,tmDetalle,tmVrPagado,tmVrCambio,tmEsCaja,tmIdBanco,tmNitCliente,tmCdAgencia,tmNumForma
,tmFecForma,tmCtaForma,tmBeneficiario,tmReferncia1,tmReferncia2,tmCdLocal,tmNumAutoriza,tmTipAplica,tmIdCia
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero AND (tmIdForma IN ('CHE','BON','CON','TRA') OR tmIdForma LIKE 'CH%')
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosCmp @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmIdForma,FormaPago,tmDetalle,tmNumForma,tmFecForma,tmCtaForma,tmReferncia1
,tmReferncia2,tmCdCta,tmNumAutoriza,tmNitCliente,tmCdAgencia
,SUM(tmVrPagado) AS STOTPAG,SUM(tmVrCambio) AS STOTCAM
 FROM tm_Pagos AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 WHERE tmNumero=@pmtmNumero
 GROUP BY tmIdForma,FormaPago,tmDetalle,tmNumForma,tmFecForma,tmCtaForma,tmReferncia1
,tmReferncia2,tmCdCta,tmNumAutoriza,tmNitCliente,tmCdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosCue @pmtmNumero VARCHAR(5),@pmIdConcepto VARCHAR(4)
 AS 
 SELECT tmIdForma,IdConcepto,IdCuenta
 FROM tm_Pagos AS P INNER JOIN FormasCue AS F ON P.tmIdForma=F.IdForma
 WHERE tmNumero=@pmtmNumero AND IdConcepto=@pmIdConcepto
 ORDER BY  tmIdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmIdForma,FormaPago,tmVrPagado,tmNumForma,tmIdBanco,Banco,tmCtaForma,tmBeneficiario,tmFecForma
,tmCdLocal,tmDetalle,tmReferncia1,tmReferncia2,tmNumAutoriza,tmCdCta,tmVrDenom,tmCantidad
,tmVrDenom*tmCantidad AS TotalDenm,tmTipDenom,tmEsCaja,tmIdCia,tmTipRef,tmDocRef,tmIdCiaRef,tmItemDoc,tmNumero
 FROM tm_Pagos AS P INNER JOIN Formaspago AS F ON P.tmIdForma=F.IdForma
 LEFT JOIN Bancos AS B ON P.tmIdBanco=B.IdBanco
 WHERE tmNumero=@pmtmNumero
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosFor @pmtmNumero VARCHAR(5),@pmtmIdForma VARCHAR(4),@pmtmIdBanco VARCHAR(4)
,@pmtmNumForma VARCHAR(20),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmIdForma,tmVrPagado,tmVrCambio,tmIdBanco,tmNumForma
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero AND tmIdForma=@pmtmIdForma AND tmIdBanco=@pmtmIdBanco
 AND tmNumForma=@pmtmNumForma AND tmItem<>@pmtmItem
 ORDER BY  tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosTEfe @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrPagado) AS STOTPAG,SUM(tmVrCambio) AS SCAMB
,SUM(tmVrDenom*tmCantidad) AS STOTDEN
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero AND tmIdForma='EFE'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PagosTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCANT,SUM(tmVrPagado) AS STOTPAG,SUM(tmVrCambio) AS SCAMB
,SUM(tmVrDenom*tmCantidad) AS STOTDEN
 FROM tm_Pagos
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Plasueldos @pmtmIdPeriodo VARCHAR(8),@pmMes VARCHAR(6),@pmtmKeyRegistro VARCHAR(30)=Null,@pmIdEmpleado VARCHAR(16)=Null
 AS 
 SELECT N.IdEmpleado AS IdEmpldo,Apellidos,Nombres,N.VrSalario AS VrSalBasico,tmDias,tmVrBasico,tmVrAuxtrans,tmVrHextras,tmVrRecargos,tmVrComision,tmVrIncapac,tmVrLicencia
,tmVrVacLiquida,tmVrOtrosDev,VrDevengado+tmVrVacLiquida AS Devengado,tmVrDctoPen,tmVrDctoSal,tmVrDctoFsp,tmVrRetencion,tmVrPrestmos,tmVrSanciones,tmVrOtrosDed,tmVrVacPen,tmVrVacSal,tmVrVacFsp,tmVrVacOtr
,VrDeducido+tmVrVacPen+tmVrVacSal+tmVrVacFsp+tmVrVacOtr AS Deducido
,(VrDevengado+tmVrVacLiquida) -(VrDeducido+tmVrVacPen+tmVrVacSal+tmVrVacFsp+tmVrVacOtr) AS VrNetoPagar,tmVrVacacnes,tmObserv,tmIdPeriodo,FecInicial,FecFinal,tmKeyRegistro
--datos nomina
,N.IdDep AS CodDep,Dependencia,N.IdInstala AS CodInst,Instlacion,N.IdCCosto AS CodCcto,CCosto,N.IdSubCos AS CodSubcen,SubCosto,N.IdNom AS CodNom,TipoNomina,Asistencia,NoDevenga
,TipoLiq,N.pVehiculo AS PlacaVeh,TipCom,Comprobante,IdCiaCom,N.Observacion AS Observ,N.IdEstado AS Nom_IdEstdo,EN.Estado AS Nom_Estado,EsRetiro,NLiqRet,Cantidad,CantNoRem
,TimeSys,N.FecUpdate AS FecUpd,N.IdUsuario AS IdUsuari,Usuario
--datos del contrato
,N.NContrato AS NumContrato,C.IdTipCon AS CodTipCon,TipoContrato,FecIngreso,FecRetiro,Indefinido,C.IdCargo AS CodCargo,Cargo,C.IdArea AS CodArea,Area,C.IdClase AS CodClaRie,ClaseRiesgo,CR.Tarifa AS TarifaRie
,VrAuxTrans,SalMinimo,SalIntegral,FPnomina,ATFinMes,SegFinMes,DchDotacion,DchCafeteria,C.IdEstado AS Con_IdEstdo,ECO.Estado AS Con_Estado,C.Inactivo AS Con_inactivo,C.IdJornada AS CodJnada,Jornada,TipoTrabj
--datos del empleado
,Codigo,IdLugarCed,Localidad,E.Direccion AS Dirccion,Telefono,TelMovil,e_mail,E.IdProf AS CodProf,Profesion,E.IdClase AS CodClaCta,ClaseCuenta,NumCuenta,E.IdBanco AS CodBanco,Banco,IdFonPen,FP.Fondo AS FondoPen
,IdFonEps,FS.Fondo AS FondoEps,IdFonArp,FR.Fondo AS FondoArp,IdCajaCom,CC.Fondo AS CajaComp,EsCondtor,FecNacmto,Licencia,CatLicencia,VigLicencia,E.IdLugar AS CodLugar,LugarLic,TallaCam,TallaPan,TallaZap,E.Inactivo AS Emp_Inactvo
,NumVeh,TipoAdmon,VehPropio
 FROM tm_Plasueldos AS PS INNER JOIN Trn_Nomina AS N ON PS.tmIdPeriodo=N.IdPeriodo AND PS.tmKeyRegistro=N.KeyRegistro
 INNER JOIN Empleados AS E ON N.IdEmpleado=E.IdEmpleado INNER JOIN  Emp_Contrato AS C ON N.NContrato=C.NContrato
 INNER JOIN Dependencias AS DP ON N.IdDep=DP.IdDep  INNER JOIN Instalaciones AS I ON N.IdInstala=I.IdInstala
 INNER JOIN CentroCosto AS CO ON N.IdCCosto=CO.IdCCosto  INNER JOIN SubCentros AS SC ON N.IdSubCos=SC.IdSubCos
 INNER JOIN TiposNom AS TN ON N.IdNom=TN.IdNom INNER JOIN EstadoDoc AS EN ON N.IdEstado=EN.IdEstado 
 INNER JOIN adm_Usuarios AS U ON N.IdUsuario=U.IdUsuario INNER JOIN Localidades AS L ON E.IdLugarCed=L.IdLocal
 INNER JOIN Profesiones AS P ON E.IdProf=P.IdProf   INNER JOIN ClaseCta AS CT ON E.IdClase=CT.IdClase
 INNER JOIN Bancos AS B ON E.IdBanco=B.IdBanco  INNER JOIN Fondos AS FP ON E.IdFonPen=FP.IdFondo
 INNER JOIN Fondos AS FS ON E.IdFonEps=FS.IdFondo  INNER JOIN Fondos AS FR ON E.IdFonArp=FR.IdFondo
 INNER JOIN Fondos AS CC ON E.IdCajaCom=CC.IdFondo INNER JOIN ExpLicencias AS EL ON E.IdLugar=EL.IdLugar
 INNER JOIN TiposCon AS TC ON C.IdTipCon=TC.IdTipCon  INNER JOIN Cargos AS CG ON C.IdCargo=CG.IdCargo
 INNER JOIN Areas AS AR ON C.IdArea=AR.IdArea INNER JOIN ClaseRie AS CR ON C.IdClase=CR.IdClase
 INNER JOIN Jornadas AS J ON C.IdJornada=J.IdJornada  INNER JOIN EstadoDoc AS ECO ON C.IdEstado=ECO.IdEstado
 LEFT JOIN NomVehiculos AS VH ON N.pVehiculo=VH.IdVehiculo
 WHERE SUBSTRING(tmIdPeriodo,1,6)=@pmMes AND tmIdPeriodo LIKE ISNULL(@pmtmIdPeriodo,'%') 
 AND tmKeyRegistro LIKE ISNULL(@pmtmKeyRegistro,'%')  AND N.IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 ORDER BY Apellidos,Nombres,N.IdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PlasueldosKey @pmtmIdPeriodo VARCHAR(8),@pmtmKeyRegistro VARCHAR(30)
 AS 
 SELECT tmIdPeriodo,tmKeyRegistro,tmDias
 FROM tm_Plasueldos 
 WHERE tmKeyRegistro=@pmtmKeyRegistro AND SUBSTRING(tmIdPeriodo,1,6)=@pmtmIdPeriodo
 ORDER BY tmIdPeriodo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PreAbonos @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipDoc,tmIdPrestamo,tmIdCiaPre,tmItemPre,tmVrAbono,tmVrBaseCms,tmIdCliente,tmIdAgencia,tmIdVend,tmComision,tmDetalle,tmGalones
,tmTipoAplica,tmCodTarCom,tmTipRec,tmRecibo,tmCiaRec,tmItemRec,tmFecPago
 FROM tm_PreAbonos
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PreCartera @pmtmEst CHAR(2),@pmSaldo DECIMAL(14,2),@pmFecActual SMALLDATETIME
 AS 
 SELECT T.RazonSocial AS NomCliente,C.IdCliente AS NitCliente,NomAgencia AS Agencia,C.IdAgencia AS CdAgencia,C.TipDoc AS CdTipoDoc,TipoDoc,IdPrestamo,C.IdCia AS CdCia,Compania,Item
,VrTotal,VrAbonado,CASE C.TipDoc WHEN 'SAF' THEN C.VrSaldo*-1  ELSE  C.VrSaldo END AS ValorSaldo,Fecha,FechaVence,DATEDIFF(day,FechaVence,@pmFecActual) AS DiasMora
,C.Concepto AS ConceptoCuota,NumCuota,Detalle,C.IdConcepto AS CdConcepto,CO.Concepto AS NomConcepto,C.IdVend AS CdVend,VN.RazonSocial AS Vendedor,TipoTasa,DTF_EA,TasaNom,Spread_TA,TasaEfe,TipoCuota
,NumCausacion,IdCiaCau,FecUltPago,FecLiqMora,C.FechaCrea,CtaForma,Beneficiario,RefCheque,NumCheque,CdBanco
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdVend AS CdVendClie,VNC.RazonSocial AS NomVendClie
,DirAgencia,CLI.IdLocalAge  AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
 FROM tm_PreCartera  AS C INNER JOIN Terceros AS T ON C.IdCliente=T.IdTercero  INNER JOIN  Companias AS CI ON C.IdCia=CI.IdCia
 INNER JOIN Terceros AS VN ON C.IdVend=VN.IdTercero
 INNER JOIN TercCliePrestamo AS CLI ON C.IdCliente=CLI.IdClie AND C.IdAgencia=CLI.IdAgencia
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona   INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo INNER JOIN Terceros AS VNC ON CLI.IdVend=VNC.IdTercero
 INNER JOIN  TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Conceptos AS CO ON C.IdConcepto=CO.IdConcepto  LEFT JOIN Sys_TiposDoc AS TD ON C.TipDoc=TD.IdDoc
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE tmEst=@pmtmEst AND C.VrSaldo>@pmSaldo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQrytm_PreCuotasLta @pmtmNumero VARCHAR(5)
AS
SELECT  tmItem, tmTipDoc, tmPrestamo, tmIdCiaPre, tmItemPre, tmCuota, tmConcepto, tmFecCausa, tmFecVence, tmVrTotal
,tmTipoTasa, tmDTF_EA,tmTasaNom, tmSpread, tmTasaEfe, tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,NomAgencia AS Agencia
,tmIdVend,V.RazonSocial AS NomVendedor,tmEstado, tmDetalle,tmLinCred,tmFactura,tmDiasLiq,tmVrIntCorr, tmTotAbono,tmVrSaldo
,tmUltCausac,tmVrAcumCap,tmNitConv
--datos del prestamo
,FecPrestamo,VrPrestamo,NPlazos,Causacion,P.NContrato AS NumContrato,CdCiaCon,NumCuotaIni,P.IdEstado AS CdEstado
 FROM   tm_PreCuotas AS PC  INNER JOIN Terceros AS T ON PC.tmIdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON PC.tmIdVend=V.IdTercero
 INNER JOIN Trn_FinPrestamo AS P ON PC.tmTipDoc=P.TipDoc AND PC.tmPrestamo=P.IdPrestamo AND PC.tmIdCiaPre=P.IdCia
 LEFT JOIN TercCliePrestamo AS A ON  PC.tmIdCliente=A.IdClie AND PC.tmIdAgencia=A.IdAgencia
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQrytm_PreIntMoraLta @pmtmNumero VARCHAR(5)
AS
 SELECT      tmItem, tmTipDoc, tmPrestamo, tmIdCia, tmItemPre,tmNCuota, tmIdCliente,T.RazonSocial AS NomCliente, tmIdAgencia,NomAgencia AS Agencia,tmIdVend,V.RazonSocial AS NomVendedor
, tmFecVence, tmVrTotal, tmVrAbonado, tmFecUltLiq,tmFecIniLiq, tmFecFinLiq, tmDiasLiq, tmDiasGracia, tmBaseLiq
,tmTasaEA, tmTasaEM, tmTasaDia, tmVrLiquida, tmTarifaIva, tmVrIva, tmEstado,tmDetalle,tmLinCred
 FROM  tm_PreIntMora AS IM INNER JOIN Terceros AS T ON IM.tmIdCliente=T.IdTercero
 INNER JOIN Terceros AS V ON IM.tmIdVend=V.IdTercero
 LEFT JOIN TercCliePrestamo AS A ON  IM.tmIdCliente=A.IdClie AND IM.tmIdAgencia=A.IdAgencia
  WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_PreMovi @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCliente,T.RazonSocial AS NomCliente,tmIdAgencia,NomAgencia AS Agencia,tmIdCia,Compania,tmTipDoc,TipoDoc,tmNumero,tmItem,tmFecha,tmCargos,tmAbonos,tmConcepto,tmDetalle,tmReferencia
 ,tmNumCuota,tmNumForma,tmTipRef,tmDocRef,tmIdCiaRef,tmCdConcepto,Concepto,tmIdVend,VN.RazonSocial AS Vendedor,tmSanAge,tmSanAgeCia,tmSanClie,tmSanClieCia
,tmTimeSys,tmFecVence,tmTipoTasa,tmDTF_EA,tmTasaNom,tmPuntos,tmTasaEfe,tmCdBanco,Banco,tmBeneficiario,tmCdForma
,tmEnEfectivo,tmTotalAbono,tmVrAfavor,tmTipCom,tmComprobante,tmIdCiaCom,tmAnulado,tmNumDev,tmFecDev,tmIdUsuario,Usuario
--Datos del cliente
,T.TipoId AS TercTipo,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.NomCial AS TercNomCial,T.SiglaRaz AS TercSigal,T.Direccion AS TercDireccion
,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,DP.Departamento AS TercDpto,T.Telefono AS TercTelefono,T.Fax AS TercFax,T.e_mail AS TercEmail
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie
--agencias
,DirAgencia,CLI.IdLocalAge  AS AgeIdCiudad,LA.Localidad AS AgeCiudad,LA.IdDep AS AgeCodDep,DA.Departamento AS AgeDpto,CLI.IdTipoTerc AS CdTipoTerc,TipoTercero,EsExento,Comentarios
 FROM tm_PreMovi  AS  M  INNER JOIN Terceros AS T ON M.tmIdCliente=T.IdTercero  INNER JOIN  Companias AS CI ON M.tmIdCia=CI.IdCia
 INNER JOIN Terceros AS VN ON M.tmIdVend=VN.IdTercero INNER JOIN  TercCliePrestamo AS CLI ON M.tmIdCliente=CLI.IdClie AND M.tmIdAgencia=CLI.IdAgencia
 INNER JOIN adm_Usuarios AS U ON M.tmIdUsuario=U.IdUsuario
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 INNER JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona   INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo  INNER JOIN  TiposTerc AS TTC ON CLI.IdTipoTerc=TTC.IdTipoTerc
 LEFT JOIN Conceptos AS C ON M.tmCdConcepto=C.IdConcepto  LEFT JOIN Sys_TiposDoc AS TD ON M.tmTipDoc=TD.IdDoc
 LEFT JOIN Bancos AS B ON M.tmCdBanco =B.IdBanco 
 LEFT JOIN Localidades AS LA ON  CLI.IdLocalAge=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE tmEst=@pmtmEst
 ORDER BY T.RazonSocial,tmIdAgencia,tmTimeSys
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQrytm_RecAplica @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipoDoc,tmDocumento,tmIdCiaDoc,tmItemDoc,tmIdClase,tmIdCargo,tmValor,tmIdVehiculo,tmVehPropio,tmIdConductor
,tmIdCliente,tmIdAgencia,tmReferencia,tmFactura,tmDetalle,tmComision,tmDescuento,tmConductor,tmTipRec,tmRecibo,tmCiaRec,tmFecRec,tmItemRec,tmAplAnt
 FROM tm_RecAplica
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,-1)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaCmp @pmtmNumero VARCHAR(5),@pmtmAplAnt BIT=Null
AS
 SELECT  tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor,sum(tmValor) AS SValAbo
 FROM    tm_RecAplica  
 WHERE tmNumero=@pmtmNumero
 AND (tmAplAnt=ISNULL(@pmtmAplAnt,0) or tmAplAnt=ISNULL(@pmtmAplAnt,1))
 GROUP BY  tmIdCliente,tmIdVehiculo,tmVehPropio,tmIdConductor

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaCr @pmtmNumero VARCHAR(5),@pmtmAplAnt BIT=Null
AS
 SELECT     tmNumero, tmItem, tmTipoDoc, tmDocumento, tmIdCiaDoc, tmItemDoc, tmIdClase, tmIdCargo, tmValor, tmIdVehiculo, tmVehPropio, tmIdConductor, 
                      tmIdCliente, tmIdAgencia, tmReferencia, tmFactura, tmDetalle, tmComision, tmDescuento, tmConductor,tmTipRec,tmRecibo, tmCiaRec, tmFecRec, tmItemRec,tmAplAnt
 FROM    tm_RecAplica  
 WHERE tmNumero=@pmtmNumero 
 AND (tmAplAnt=ISNULL(@pmtmAplAnt,0) or tmAplAnt=ISNULL(@pmtmAplAnt,1))
 ORDER BY tmItem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaDoc @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipoDoc VARCHAR(3),@pmtmDocumento INT
,@pmtmIdCiaDoc CHAR(2),@pmtmItemDoc INT
AS
 IF @pmtmItem IS NULL
  SELECT  tmNumero, tmItem, tmTipoDoc, tmDocumento, tmIdCiaDoc, tmItemDoc, tmValor  FROM    tm_RecAplica  
  WHERE tmNumero=@pmtmNumero AND tmTipoDoc=@pmtmTipoDoc AND tmDocumento=@pmtmDocumento AND tmIdCiaDoc=@pmtmIdCiaDoc
  AND tmItemDoc=@pmtmItemDoc 
 ORDER BY tmItem
 ELSE
  SELECT  tmNumero, tmItem, tmTipoDoc, tmDocumento, tmIdCiaDoc, tmItemDoc, tmValor  FROM    tm_RecAplica  
  WHERE tmNumero=@pmtmNumero AND tmTipoDoc=@pmtmTipoDoc AND tmDocumento=@pmtmDocumento AND tmIdCiaDoc=@pmtmIdCiaDoc
  AND tmItemDoc=@pmtmItemDoc  AND tmItem<>@pmtmItem
 ORDER BY tmItem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaDso @pmtmNumero VARCHAR(5)
AS
 SELECT   tmItem, tmTipoDoc, tmDocumento, tmIdCiaDoc, tmItemDoc,tmValor,tmDescuento,tmDetalle,tmIdCliente,tmIdVehiculo,tmVehPropio
,tmIdConductor,tmConductor,tmReferencia,tmFactura
 FROM    tm_RecAplica  
 WHERE  tmNumero=@pmtmNumero
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaGap @pmtmNumero VARCHAR(5),@pmtmAplAnt BIT=Null
AS
 SELECT tmIdCliente,tmIdVehiculo,SUM( tmValor) AS STotApl, SUM(tmDescuento) AS STotDct
 FROM    tm_RecAplica  
 WHERE tmNumero=@pmtmNumero
 AND (tmAplAnt=ISNULL(@pmtmAplAnt,0) or tmAplAnt=ISNULL(@pmtmAplAnt,1))
 GROUP BY  tmIdCliente,tmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaGru @pmtmNumero VARCHAR(5)
AS
 SELECT   tmTipoDoc, tmDocumento, tmIdCiaDoc, tmItemDoc,SUM( tmValor) AS STotApl, SUM(tmDescuento) AS STotDct
 FROM    tm_RecAplica  
 WHERE tmNumero=@pmtmNumero
 GROUP BY tmTipoDoc, tmDocumento, tmIdCiaDoc, tmItemDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_RecAplicaRec @pmtmNumero VARCHAR(5)
AS
 SELECT tmTipRec,tmRecibo, tmCiaRec,SUM(tmValor) AS StotVal
 FROM    tm_RecAplica  
 WHERE tmNumero=@pmtmNumero
 GROUP BY tmTipRec,tmRecibo, tmCiaRec

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ReqDetalle @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmItem,tmCdProducto,tmDescripcion,tmIdSubgrupo,tmCdBodega,tmCantidad,tmIdUnd,tmVrUnitario
,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal,tmNumero
 FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ReqDetalleDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmCdProducto,tmDescripcion,tmCantidad,tmIdUnd,Unidad,tmVrUnitario,tmCantidad*tmVrUnitario AS ValorTotal
,tmTipOrd,tmNumOrden,tmIdCiaOrd,tmTipSal,tmNSalida,tmIdCiaSal,tmFechaSal,tmIdSubgrupo,tmCdBodega,tmNumero
 FROM tm_ReqDetalle AS D INNER JOIN UndMed AS U ON D.tmIdUnd=U.IdUnd
 WHERE tmNumero=@pmtmNumero
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ReqDetalleItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ReqDetalleTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS SCOUNT,SUM(tmCantidad) AS SCANT,SUM(tmCantidad*tmVrUnitario) AS SVALTOTAL
 FROM tm_ReqDetalle
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ResMensualCli @pmtmEst CHAR(2),@pmtmTipo INT
 AS 
 SELECT tmIdTercero,tmCodigo,T.RazonSocial AS NomCliente,tmIdCia,Compania,tmCdAgencia,Agencia
,tmEneSaldoAnt,tmEneDebitos,tmEneCreditos,tmFebSaldoAnt,tmFebDebitos,tmFebCreditos,tmMarSaldoAnt,tmMarDebitos,tmMarCreditos,tmAbrSaldoAnt,tmAbrDebitos,tmAbrCreditos
,tmMaySaldoAnt,tmMayDebitos,tmMayCreditos,tmJunSaldoAnt,tmJunDebitos,tmJunCreditos,tmJulSaldoAnt,tmJulDebitos,tmJulCreditos,tmAgoSaldoAnt,tmAgoDebitos,tmAgoCreditos
,tmSepSaldoAnt,tmSepDebitos,tmSepCreditos,tmOctSaldoAnt,tmOctDebitos,tmOctCreditos,tmNovSaldoAnt,tmNovDebitos,tmNovCreditos,tmDicSaldoAnt,tmDicDebitos,tmDicCreditos
--DATOS DEL CLIENTE
,CLI.IdSzona AS CdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,CLI.IdGrupo AS CdGrupoCli,GrupoClie,CLI.IdVend AS NitVend,VN.RazonSocial AS NomVendedor
,CLI.CdBandera AS CodBandera,TipoBandera
,T.Dv AS TercDv,T.Codigo AS TercCodigo,T.IdLocal AS TercCdCiudad,L.Localidad AS NomCiudad,L.IdDep AS CdDep,D.Departamento AS TercDpto,T.IdSector AS CdSector,SectorEco
 FROM tm_ResMensual  AS TR INNER JOIN Terceros AS T ON  TR.tmCodigo=T.IdTercero INNER JOIN TercCliente AS CLI ON TR.tmCodigo=CLI.IdClie
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal  INNER JOIN Departamentos AS D ON L.IdDep=D.IdDep 
 INNER JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 INNER JOIN Subzonas AS SZ ON CLI.IdSzona=SZ.IdSzona  INNER JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona 
 INNER JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo  LEFT JOIN  Terceros AS VN ON CLI.IdVend=VN.IdTercero 
 LEFT  JOIN Companias AS CN ON  TR.tmIdCia=CN.IdCia LEFT JOIN Agencias  AS A ON TR.tmCdAgencia=A.IdAgencia
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera
 WHERE tmEst=@pmtmEst AND  tmTipo=@pmtmTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ResMensualCue @pmtmEst CHAR(2),@pmtmTipo INT
 AS 
 SELECT  tmCodigo,NomCuenta,tmIdCia,Compania,tmIdTercero,RazonSocial,tmCdAgencia,A.Agencia AS NomAgencia
,tmCdCCosto,CCosto,tmCdSubCos,SubCosto,tmpVehiculo
,tmEneSaldoAnt,tmEneDebitos,tmEneCreditos,tmFebSaldoAnt,tmFebDebitos,tmFebCreditos,tmMarSaldoAnt,tmMarDebitos,tmMarCreditos,tmAbrSaldoAnt,tmAbrDebitos,tmAbrCreditos
,tmMaySaldoAnt,tmMayDebitos,tmMayCreditos,tmJunSaldoAnt,tmJunDebitos,tmJunCreditos,tmJulSaldoAnt,tmJulDebitos,tmJulCreditos,tmAgoSaldoAnt,tmAgoDebitos,tmAgoCreditos
,tmSepSaldoAnt,tmSepDebitos,tmSepCreditos,tmOctSaldoAnt,tmOctDebitos,tmOctCreditos,tmNovSaldoAnt,tmNovDebitos,tmNovCreditos,tmDicSaldoAnt,tmDicDebitos,tmDicCreditos
,tmM13SaldoAnt,tmM13Debitos,tmM13Creditos
 FROM tm_ResMensual  AS TR INNER JOIN Puc AS P ON TR.tmCodigo=P.IdCuenta
 LEFT  JOIN Companias AS CN ON  TR.tmIdCia=CN.IdCia  LEFT JOIN Terceros AS T ON TR.tmIdTercero=T.IdTercero
 LEFT JOIN Agencias  AS A ON TR.tmCdAgencia=A.IdAgencia
 LEFT JOIN CentroCosto AS CC ON TR.tmCdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON TR.tmCdSubCos=SC.IdSubCos
 WHERE tmEst=@pmtmEst AND  tmTipo=@pmtmTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ResMensualCueN @pmtmEst CHAR(2),@pmtmTipo INT
 AS 
 SELECT  tmCodigo,NomCuenta,tmIdCia,Compania,tmIdTercero,RazonSocial,tmCdAgencia,A.Agencia AS NomAgencia
,tmCdCCosto,CCosto,tmCdSubCos,SubCosto,tmpVehiculo
,tmEneSaldoAnt,tmEneDebitos,tmEneCreditos,tmFebSaldoAnt,tmFebDebitos,tmFebCreditos,tmMarSaldoAnt,tmMarDebitos,tmMarCreditos,tmAbrSaldoAnt,tmAbrDebitos,tmAbrCreditos
,tmMaySaldoAnt,tmMayDebitos,tmMayCreditos,tmJunSaldoAnt,tmJunDebitos,tmJunCreditos,tmJulSaldoAnt,tmJulDebitos,tmJulCreditos,tmAgoSaldoAnt,tmAgoDebitos,tmAgoCreditos
,tmSepSaldoAnt,tmSepDebitos,tmSepCreditos,tmOctSaldoAnt,tmOctDebitos,tmOctCreditos,tmNovSaldoAnt,tmNovDebitos,tmNovCreditos,tmDicSaldoAnt,tmDicDebitos,tmDicCreditos
,tmM13SaldoAnt,tmM13Debitos,tmM13Creditos
,tmNivel1,tmNivel2,tmNivel4,tmNivel6,tmNivel9,tmNivel12
 FROM tm_ResMensual  AS TR INNER JOIN Puc AS P ON TR.tmCodigo=P.IdCuenta
 LEFT  JOIN Companias AS CN ON  TR.tmIdCia=CN.IdCia  LEFT JOIN Terceros AS T ON TR.tmIdTercero=T.IdTercero
 LEFT JOIN Agencias  AS A ON TR.tmCdAgencia=A.IdAgencia
 LEFT JOIN CentroCosto AS CC ON TR.tmCdCCosto=CC.IdCCosto
 LEFT JOIN SubCentros AS SC ON TR.tmCdSubCos=SC.IdSubCos
 LEFT JOIN tm_NivCue AS NV ON TR.tmCodigo=NV.tmIdCuenta
 WHERE tmEst=@pmtmEst AND  tmTipo=@pmtmTipo 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ResMensualInv @pmtmEst CHAR(2),@pmtmTipo INT
 AS 
 SELECT tmCodigo,DescripProd,tmIdCia,Compania,tmCdBodega,Bodega,tmCdTanque,T.tmEneSaldoAnt AS CostoAntEne,tmEneDebitos,tmEneCreditos,T.tmFebSaldoAnt AS CostoAntFeb,tmFebDebitos,tmFebCreditos,T.tmMarSaldoAnt AS CostoAntMar,tmMarDebitos,tmMarCreditos
,T.tmAbrSaldoAnt AS CostoAntAbr,tmAbrDebitos,tmAbrCreditos,T.tmMaySaldoAnt AS CostoAntMay,tmMayDebitos,tmMayCreditos,T.tmJunSaldoAnt AS CostoAntJun,tmJunDebitos,tmJunCreditos,T.tmJulSaldoAnt AS CostoAntJul,tmJulDebitos,tmJulCreditos
,T.tmAgoSaldoAnt AS CostoAntAgo,tmAgoDebitos,tmAgoCreditos,T.tmSepSaldoAnt AS CostoAntSep,tmSepDebitos,tmSepCreditos,T.tmOctSaldoAnt AS CostoAntOct,tmOctDebitos,tmOctCreditos,T.tmNovSaldoAnt AS CostoAntNov,tmNovDebitos,tmNovCreditos
,T.tmDicSaldoAnt AS CostoAntDic,tmDicDebitos,tmDicCreditos
--datos de unidades
,U.tmEneSaldoAnt AS SaldoAntEne,tmEneEntradas,tmEneSalidas,U.tmFebSaldoAnt AS SaldoAntFeb,tmFebEntradas,tmFebSalidas,U.tmMarSaldoAnt AS SaldoAntMar,tmMarEntradas,tmMarSalidas,U.tmAbrSaldoAnt AS SaldoAntAbr
,tmAbrEntradas,tmAbrSalidas,U.tmMaySaldoAnt AS SaldoAntMay,tmMayEntradas,tmMaySalidas,U.tmJunSaldoAnt AS SaldoAntJun,tmJunEntradas,tmJunSalidas,U.tmJulSaldoAnt AS SaldoAntJul,tmJulEntradas
,tmJulSalidas,U.tmAgoSaldoAnt AS SaldoAntAgo,tmAgoEntradas,tmAgoSalidas,U.tmSepSaldoAnt AS SaldoAntSep,tmSepEntradas,tmSepSalidas,U.tmOctSaldoAnt AS SaldoAntOct,tmOctEntradas,tmOctSalidas,U.tmNovSaldoAnt AS SaldoAntNov,tmNovEntradas,tmNovSalidas,U.tmDicSaldoAnt AS SaldoAntDic,tmDicEntradas,tmDicSalidas
--datos de productos
,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,TipoRef,PM.IdMarca AS CdMarca,Marca,FecUltcom,FecUltVta,Tanques
,IdProv,RazonSocial
 FROM tm_ResMensual  AS T INNER JOIN tm_ResInvent AS U ON T.tmEst=U.tmEst AND T.tmTipo=U.tmTipo AND T.tmItem=U.tmItem
 INNER JOIN ProdMcias  AS PM ON T.tmCodigo=PM.IdProducto  INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo
 INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo  INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 INNER JOIN Terceros AS TP ON  PM.IdProv=TP.IdTercero
 LEFT  JOIN Companias AS CN ON  T.tmIdCia=CN.IdCia  LEFT JOIN Bodegas AS B ON T.tmCdBodega=B.IdBodega 
 WHERE T.tmEst=@pmtmEst AND T.tmTipo=@pmtmTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Saldos @pmtmEst CHAR(2)
 AS 
 SELECT tmIdCuenta,NomCuenta,tmIdTercero,RazonSocial,tmCdSubgrupo,Subgrupo,tmIdCia,Compania
,tmSanCue,tmDebitos,tmCreditos,tmCostoAnt,tmCostoEnt,tmCostoSal,tmSaldoAnt,tmEntradas,tmSalidas
--datos del subgrupo y cuenta
,G.IdLinea AS CdLinea,Linea,SG.IdGrupo AS CdGrupo,Grupo,Tercero,Agencia,Vencimiento
--datos del tercero
,Codigo,TipoId,Dv,SiglaRaz,NomCial
 FROM tm_Saldos AS S LEFT JOIN Puc AS P ON S.tmIdCuenta=P.IdCuenta
 LEFT  JOIN Companias AS CN ON  S.tmIdCia=CN.IdCia  LEFT JOIN Terceros AS T ON S.tmIdTercero=T.IdTercero
 LEFT JOIN SubGrupos AS SG ON S.tmCdSubgrupo=SG.IdSubgrupo  LEFT JOIN Grupos AS G ON SG.IdGrupo=G.IdGrupo 
 LEFT JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea
 WHERE tmEst=@pmtmEst 
 ORDER BY tmIdCuenta,RazonSocial,tmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_SaldosCue @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2)=Null
,@pmtmIdTercero VARCHAR(16)=Null
 AS 
 SELECT tmEst,tmIdCia,tmIdCuenta,tmIdTercero,tmCdSubgrupo,tmCostoEnt,tmCostoSal
 FROM tm_Saldos
 WHERE tmEst=@pmtmEst  AND tmIdCuenta=@pmtmIdCuenta
  AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')   AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_SaldosTer @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2)=Null
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmCdSubgrupo VARCHAR(8)=Null
 AS 
 SELECT tmEst,tmIdCia,tmIdCuenta,tmIdTercero,tmCdSubgrupo,tmCostoEnt,tmCostoSal,tmSaldoAnt,tmCostoAnt
 FROM tm_Saldos
 WHERE tmEst=@pmtmEst  AND tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')   AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%')
 AND tmCdSubgrupo LIKE ISNULL(@pmtmCdSubgrupo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_Ser @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 SELECT tmItem,tmIdProducto,DescripProd,tmNumSerial,tmIdCia,tmIdBodega,Bodega,tmCdUbic,tmIdEstado,Estado,tmLinea
,tmDetalle,tmReferencia,tmRef2,tmFecRef1,tmFecRef2,tmUbicacion,tmIdBodNue,tmUbicNue,tmNueEstado,tmInactivo
,tmTipMovi,tmNumero,Seriales
 FROM tm_Ser  AS S INNER JOIN ProdMcias AS P ON S.tmIdProducto=P.IdProducto
 INNER JOIN EstadoSer AS E ON S.tmIdEstado=E.IdEstado
 INNER JOIN Bodegas AS B ON S.tmIdBodega=B.IdBodega
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_SerId @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM  FROM tm_Ser 
 WHERE tmNumero=@pmtmNumero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_SerPro @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdProducto VARCHAR(16)
,@pmtmNumSerial VARCHAR(30)
 AS 
 SELECT tmNumero,tmItem,tmIdProducto,tmNumSerial,tmIdCia,tmIdBodega,tmCdUbic,tmLinea,tmTipMovi
 FROM tm_Ser
 WHERE tmNumero=@pmtmNumero AND tmIdProducto=@pmtmIdProducto
 AND tmNumSerial=@pmtmNumSerial AND tmItem<>@pmtmItem
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_SerTot @pmtmNumero VARCHAR(5),@pmtmIdProducto VARCHAR(16)=Null
,@pmtmIdBodega VARCHAR(4)=Null,@pmtmLinea INT=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT FROM tm_Ser
 WHERE tmNumero=@pmtmNumero AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND tmIdBodega LIKE ISNULL(@pmtmIdBodega,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_SerTotU @pmtmNumero VARCHAR(5),@pmtmCdUbic VARCHAR(10)
,@pmtmIdProducto VARCHAR(16)=Null,@pmtmLinea INT=Null
 AS 
 SELECT COUNT(tmItem) AS SCANT FROM tm_Ser
 WHERE tmNumero=@pmtmNumero AND tmCdUbic=@pmtmCdUbic
 AND tmIdProducto LIKE ISNULL(@pmtmIdProducto,'%')
 AND (tmLinea>=ISNULL(@pmtmLinea,0)  AND tmLinea<=ISNULL(@pmtmLinea,2147483647))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_Tanqueos @pmtmNumero VARCHAR(5),@pmtmItem INT
AS 
 SELECT  tmItem, tmDocumento,tmFecha, tmIdTipoCom,tmTipoCom, tmCantidad, tmValorUnitario
, tmCantidad * tmValorUnitario AS ValorTotal ,tmIdEstacion,tmEstacion ,tmNumero
 FROM     tm_Tanqueos
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ValesCred @pmtmNumero VARCHAR(5) 
 AS 
 SELECT tmItem,tmTipo,tmRango,tmNumSerie,tmNumVale,tmFecha,tmHoraServ,tmNumMovil,tmCentCosto,tmUnidades,tmHoraRec,tmHoraEspera
,tmVrPago,tmFactUnid,tmFactRutas,tmFactHoras,tmCantidad,tmIdCliente,RazonSocial,tmIdAgencia,Agencia,tmUsuarios,tmObservacion,tmIdCuenta,tmRutas,tmVrCobro
 FROM tm_ValesCred AS V INNER JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero LEFT JOIN Agencias AS A ON V.tmIdAgencia=A.IdAgencia
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_VentasCom @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdCliente,T.RazonSocial AS NomCliente,tmCdAgencia,Agencia,CodAgencia,tmIdVend,VN.RazonSocial AS Vendedor
,tmIdBodega,Bodega,tmTipDoc,TipoDoc,tmIdConcepto,Concepto,tmCdLocal,Localidad,L.IdDep AS CodDep,Departamento
,tmCdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,tmPeriodo
,tmUnidades,tmVentaNeto,tmCostoNeto,tmImpuestos,tmImpGlobal,tmSoldicom,tmVolumen,tmUnidades2,tmVentaNeto2,tmCostoNeto2,tmImpuestos2,tmImpGlobal2,tmSoldicom2,tmVolumen2
--Datos del producto
,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,TipoRef,PM.IdMarca AS CdMarca,Marca
,PM.IdUnd AS CdUnid,Unidad,FecUltcom,FecUltVta,Tanques
--datos del tercero
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
 FROM tm_Ventas AS V INNER JOIN tm_VentasCom AS D ON V.tmEst=D.tmEst AND V.tmItem=D.tmItem
 INNER JOIN ProdMcias  AS PM ON V.tmIdProducto=PM.IdProducto  INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 LEFT JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero LEFT JOIN Agencias  AS A ON V.tmCdAgencia=A.IdAgencia
 LEFT JOIN TercCliente AS CLI ON V.tmIdCliente=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Subzonas AS SZ ON V.tmCdSubzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Terceros AS VN ON V.tmIdVend=VN.IdTercero   LEFT JOIN Bodegas AS B ON V.tmIdBodega=B.IdBodega 
 LEFT  JOIN Companias AS CN ON  V.tmIdCia=CN.IdCia  LEFT JOIN Sys_TiposDoc AS TD ON V.tmTipDoc=TD.IdDoc
 LEFT JOIN  Conceptos AS C ON V.tmIdConcepto=C.IdConcepto
 LEFT JOIN Localidades AS L ON V.tmCdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera 
 WHERE V.tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_VentasDet @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdCliente,T.RazonSocial AS NomCliente,tmCdAgencia,Agencia,CodAgencia,tmIdVend,VN.RazonSocial AS Vendedor
,tmIdBodega,Bodega,tmTipDoc,TipoDoc,tmIdConcepto,Concepto,tmCdLocal,Localidad,L.IdDep AS CodDep,Departamento
,tmCdSubzona,Subzona,SZ.IdZona AS CdZona,Zona,tmPeriodo
,tmSalidas,tmEntradas,tmVtaBrutoSal,tmVtaBrutoEnt,tmDctosSal,tmDctosEnt,tmIvaSal,tmIvaEnt,tmCostoSal,tmCostoEnt
,tmReteFteSal,tmReteFteEnt,tmReteIcaSal,tmReteIcaEnt,tmSobtasaSal,tmSobtasaEnt,tmImpGlobalSal,tmImpGlobalEnt
,tmSoldicomSal,tmSoldicomEnt,tmUnidVolumen,tmComision
--Datos del producto
,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,TipoRef,PM.IdMarca AS CdMarca,Marca
,PM.IdUnd AS CdUnid,Unidad,FecUltcom,FecUltVta,Tanques
--datos del tercero
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera
,VrCupo,DiasPago
 FROM tm_Ventas AS V INNER JOIN tm_VentasDet AS D ON V.tmEst=D.tmEst AND V.tmItem=D.tmItem
 INNER JOIN ProdMcias  AS PM ON V.tmIdProducto=PM.IdProducto  INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 LEFT JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero LEFT JOIN Agencias  AS A ON V.tmCdAgencia=A.IdAgencia
 LEFT JOIN TercCliente AS CLI ON V.tmIdCliente=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Subzonas AS SZ ON V.tmCdSubzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Terceros AS VN ON V.tmIdVend=VN.IdTercero   LEFT JOIN Bodegas AS B ON V.tmIdBodega=B.IdBodega 
 LEFT  JOIN Companias AS CN ON  V.tmIdCia=CN.IdCia  LEFT JOIN Sys_TiposDoc AS TD ON V.tmTipDoc=TD.IdDoc
 LEFT JOIN  Conceptos AS C ON V.tmIdConcepto=C.IdConcepto
 LEFT JOIN Localidades AS L ON V.tmCdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera 
 LEFT JOIN Plazos AS PZ ON CLI.IdPlazo=PZ.IdPlazo
 WHERE V.tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_VentasRes @pmtmEst CHAR(2)
 AS 
 SELECT tmIdProducto,DescripProd,tmIdCia,Compania,tmIdCliente,T.RazonSocial AS NomCliente,tmCdAgencia,Agencia,CodAgencia,tmIdVend,VN.RazonSocial AS Vendedor
,tmIdBodega,Bodega,tmTipDoc,TipoDoc,tmIdConcepto,Concepto,tmCdLocal,L.Localidad AS NomCiudad,L.IdDep AS CodDep,DP.Departamento AS NomDpto
,tmCdSubzona,Subzona,SZ.IdZona AS CdZona,Zona
,tmEnero,tmFebrero,tmMarzo,tmAbril,tmMayo,tmJunio,tmJulio,tmAgosto,tmSeptiembre,tmOctubre,tmNoviembre,tmDiciembre
--Datos del producto
,G.IdLinea AS CdLinea,Linea,S.IdGrupo AS CdGrupo,Grupo,PM.IdSubgrupo AS CdSubgrupo,Subgrupo,TipoRef,PM.IdMarca AS CdMarca,Marca
,PM.IdUnd AS CdUnid,Unidad,FecUltcom,FecUltVta,Tanques
--datos del tercero
,T.IdSector AS CdSector,SectorEco,T.IdRegimen AS CdRegimen,Regimen,T.TipEnte AS TercTipEnte,CLI.IdGrupo AS CdGrupoCli,GrupoClie
,CLI.CdBandera AS CliCdBandera,TB.TipoBandera AS CliBandera,A.CdBandera AS AgeCdBandera,TBA.TipoBandera AS AgeBandera,A.IdLocal AS AgeCdCiu,LA.Localidad AS AgeNomCiudad
 FROM tm_Ventas AS V INNER JOIN tm_VentasRes AS D ON V.tmEst=D.tmEst AND V.tmItem=D.tmItem
 INNER JOIN ProdMcias  AS PM ON V.tmIdProducto=PM.IdProducto  INNER JOIN UndMed AS UM ON PM.IdUnd=UM.IdUnd
 INNER JOIN SubGrupos AS S ON PM.IdSubgrupo=S.IdSubgrupo INNER JOIN Grupos AS G ON S.IdGrupo=G.IdGrupo 
 INNER JOIN Lineas AS LN ON G.IdLinea=LN.IdLinea INNER JOIN Marcas AS M ON PM.IdMarca=M.IdMarca
 LEFT JOIN Terceros AS T ON V.tmIdCliente=T.IdTercero LEFT JOIN Agencias  AS A ON V.tmCdAgencia=A.IdAgencia
 LEFT JOIN TercCliente AS CLI ON V.tmIdCliente=CLI.IdClie LEFT JOIN GruposCli AS GC ON CLI.IdGrupo=GC.IdGrupo
 LEFT JOIN  RegimenDian AS RG ON T.IdRegimen=RG.IdRegimen LEFT JOIN SectoresEco AS SE ON T.IdSector=SE.IdSector
 LEFT JOIN Subzonas AS SZ ON V.tmCdSubzona=SZ.IdSzona  LEFT JOIN  Zonas AS Z ON SZ.IdZona=Z.IdZona  
 LEFT JOIN Terceros AS VN ON V.tmIdVend=VN.IdTercero   LEFT JOIN Bodegas AS B ON V.tmIdBodega=B.IdBodega 
 LEFT  JOIN Companias AS CN ON  V.tmIdCia=CN.IdCia  LEFT JOIN Sys_TiposDoc AS TD ON V.tmTipDoc=TD.IdDoc
 LEFT JOIN  Conceptos AS C ON V.tmIdConcepto=C.IdConcepto
 LEFT JOIN Localidades AS L ON V.tmCdLocal=L.IdLocal  LEFT JOIN Departamentos AS DP ON L.IdDep=DP.IdDep 
 LEFT JOIN TiposBan AS TB ON CLI.CdBandera=TB.IdBandera LEFT JOIN TiposBan AS TBA ON A.CdBandera=TBA.IdBandera 
 LEFT JOIN Localidades AS LA ON A.IdLocal=LA.IdLocal  LEFT JOIN Departamentos AS DA ON LA.IdDep=DA.IdDep 
 WHERE V.tmEst=@pmtmEst
 ORDER BY DescripProd,tmIdCia,T.RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_ccCupones @pmtmEst CHAR(2) 
 AS 
 SELECT tmItem,tmTipoReg,tmTipDoc,tmDocumento,tmIdCia,Compania,tmItemDoc,tmFecEmision,tmFecVence,tmValorFactura,tmValorAbono
,tmIdPropietario,T.RazonSocial AS Propietario,tmIdVehiculo,NumVeh,tmIdConductor,CD.RazonSocial AS Conductor,tmIdCargo,Concepto,NCuotas,TipoDcto,DctoPpago
,tmIdClase,ClaseCuenta,tmFactura,tmReferencia,tmDetalle,tmDescuento
--datos del tercero
,T.TipoId AS TercTipoId,T.Dv AS TercDv,T.Direccion AS TercDireccion,T.IdLocal AS TercIdCiu,Localidad,T.Telefono AS TercTel,T.Fax AS TercFax,T.TelMovil AS TercMovil,T.e_mail AS TercEmail
 FROM tm_ccCupones AS C INNER JOIN  Companias  AS CI ON C.tmIdCia=CI.IdCia
 INNER JOIN  Terceros AS T ON C.tmIdPropietario=T.IdTercero 
 INNER JOIN Localidades AS LC ON T.IdLocal=LC.IdLocal
 INNER JOIN Vehiculos AS V ON C.tmIdVehiculo=V.IdVehiculo
 INNER JOIN   Terceros AS CD  ON C.tmIdConductor=CD.IdTercero
 LEFT JOIN   Fijos AS  F ON C.tmIdCargo=F.IdCargo  LEFT JOIN  BusClaseCue AS B ON C.tmIdClase=B.IdClase
 WHERE tmEst=@pmtmEst 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_cheq @pmtmNumero VARCHAR(5),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipForma,tmNumForma,tmIdBanco,tmFecha,tmFecPago,tmVrForma,tmCtaForma,tmBeneficiario,tmReferncia1,tmReferncia2
,tmDetalle,tmNitCliente,tmCdAgencia,tmCdVend,tmIdLocal,tmCdCta
 FROM tm_cheq
 WHERE tmNumero=@pmtmNumero 
 AND (tmItem>=ISNULL(@pmtmItem,0)  AND tmItem<=ISNULL(@pmtmItem,2147483647))
 ORDER BY tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_cheqDso @pmtmNumero VARCHAR(5)
 AS 
 SELECT tmItem,tmTipForma,tmNumForma,tmIdBanco,Banco,tmVrForma,tmDetalle,tmFecha,tmFecPago,tmCtaForma
,tmBeneficiario,tmReferncia1,tmReferncia2,tmIdLocal,Localidad,tmCdCta
,tmNitCliente,tmCdAgencia,tmCdVend,tmNumero
 FROM tm_cheq AS C INNER JOIN Bancos AS B ON C.tmIdBanco=B.IdBanco
 LEFT JOIN Localidades AS L ON C.tmIdLocal=L.IdLocal
 WHERE tmNumero=@pmtmNumero 
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_cheqItm @pmtmNumero VARCHAR(5)
 AS 
 SELECT MAX(tmItem) AS MXITM
 FROM tm_cheq
 WHERE tmNumero=@pmtmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_cheqNum @pmtmNumero VARCHAR(5),@pmtmTipForma VARCHAR(3),@pmtmNumForma VARCHAR(20)
,@pmtmIdBanco VARCHAR(4),@pmtmItem INT
 AS 
 SELECT tmNumero,tmItem,tmTipForma,tmNumForma,tmIdBanco,tmVrForma
 FROM tm_cheq
 WHERE tmNumero=@pmtmNumero AND tmTipForma=@pmtmTipForma AND tmNumForma=@pmtmNumForma
 AND tmIdBanco=@pmtmIdBanco AND tmItem<>@pmtmItem
 ORDER BY tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_cheqTot @pmtmNumero VARCHAR(5)
 AS 
 SELECT COUNT(tmItem) AS STOTCAN,SUM(tmVrForma) AS STOTCHE
 FROM tm_cheq
 WHERE tmNumero=@pmtmNumero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paQrytm_vce @pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmItem INT
 AS 
 SELECT tmTipDoc,tmDocumento,tmIdCia,tmItem,tmValorFactura,tmValorAbono,tmIdVehiculo,tmFecUltPago
 FROM tm_vce
 WHERE tmTipDoc=@pmtmTipDoc AND tmDocumento=@pmtmDocumento AND tmIdCia=@pmtmIdCia AND tmItem=@pmtmItem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paQrytm_vceLta
 AS 
 SELECT tmTipDoc,tmDocumento,tmIdCia,tmItem,tmValorFactura,tmValorAbono,tmIdVehiculo,tmFecUltPago
 FROM tm_vce
 ORDER BY tmTipDoc,tmIdCia,tmDocumento,tmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paQrytm_vence @pmtmEst CHAR(2),@pmFechaAct SMALLDATETIME
AS
 SELECT  tmIdPropietario,T.RazonSocial AS Propietario, tmIdVehiculo, tmIdCia,tmTipDoc,tmDocumento,tmItem,tmFecEmision,tmFecVence,DATEDIFF(day,tmFecVence,@pmFechaAct) AS DiasMora
 ,tmValorFactura, tmValorAbono,tmValorFactura-tmValorAbono AS TotalSaldo,tmVencido,tmVence1, tmVence2, tmVence3, tmVence4,tmPorVencer,tmIdCargo,F.Concepto AS ConceptoCargo,tmIdClase,ClaseCuenta,tmFecUltPago,tmFactura,tmReferencia
,tmDetalle,tmObservacion,tmIdConcepto,COC.Concepto AS ConceptoDoc,tmIdConductor,NC.RazonSocial AS Conductor,tmModalidad, tmTipCom, tmComprobante, tmIdCiaCom,tmTipDcm,tmDocmto,tmIdCiaDcm
,tmIdEstado,ED.Estado AS EstadoDoc,tmIdUsuario,Usuario,tmVehPropio,tmNotaInt,tmFecUltLiquida,Compania,TipoDoc,tmTimeSys
,NumVeh,V.IdTipoVeh AS CdTipoVeh,TipoVehiculo,V.IdMarca AS CdMarca,Marca,V.IdColor AS CdColor,NomColor,Modelo,FecRep,ClaseMat,V.IdPropietario AS VehNitProp,VNP.RazonSocial AS VehPropietario,V.IdConductor AS VehCedConductor,VNC.RazonSocial AS VehConductor
,V.IdPpd AS CdTipProp,TipoProp,TipoAfil,NContrato,V.IdAdmon AS CdAdmon,TipoAdmon,V.IdNiv AS CdNivel,NivelServicio,V.IdGrupo AS CdGrupo,GrupoProp,CdTarifa,TB.Descripcion AS TipoTarifa,FecIngreso,FecVigencia,FecRetiro,NumSoat,FecSoat,VigSoat
,VrLmtCred,VrSaldoAct,V.IdEstado AS VehCdEstado,EV.Estado AS VehEstado,V.Inactivo AS VehInactivo
 FROM  tm_vence  AS C INNER JOIN Companias  AS CI ON C.tmIdCia=CI.IdCia
 INNER JOIN  Terceros AS T ON C.tmIdPropietario=T.IdTercero  INNER JOIN Vehiculos AS V ON C.tmIdVehiculo=V.IdVehiculo
 INNER JOIN  Fijos AS F ON C.tmIdCargo=F.IdCargo INNER JOIN BusClaseCue  AS CU ON C.tmIdClase=CU.IdClase
 INNER JOIN  adm_Usuarios AS U ON C.tmIdUsuario=U.IdUsuario INNER JOIN  EstadoDoc AS ED ON C.tmIdEstado=ED.IdEstado  
 INNER JOIN  Terceros AS NC ON C.tmIdConductor=NC.IdTercero
 INNER JOIN TiposCol AS CO ON V.IdColor=CO.IdColor 
 INNER JOIN TiposVeh AS TV  ON V.IdTipoVeh=TV.IdTipoVeh INNER JOIN Marcas AS M ON V.IdMarca=M.IdMarca
 INNER JOIN GruposPro AS GP ON V.IdGrupo=GP.IdGrupo INNER JOIN EstadoVeh AS EV ON V.IdEstado=EV.IdEstado
 INNER JOIN Terceros AS VNP ON V.IdPropietario=VNP.IdTercero INNER JOIN Terceros AS VNC ON V.IdConductor=VNC.IdTercero
 LEFT JOIN TiposPpt AS TPR  ON V.IdPpd=TPR.IdPpd
  LEFT JOIN TiposAdm AS TA ON V.IdAdmon=TA.IdAdmon LEFT JOIN TiposNivs AS VNS ON V.IdNiv=VNS.IdNiv
  LEFT JOIN  TarifBuses AS TB ON V.CdTarifa =TB.IdTarifa 
 LEFT JOIN  Conceptos AS COC ON C.tmIdConcepto=COC.IdConcepto  LEFT JOIN Sys_TiposDoc  AS TD ON C.tmTipDoc=TD.IdDoc
 WHERE tmEst=@pmtmEst 
 ORDER BY T.RazonSocial,tmIdVehiculo,tmFecVence
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paQrytm_vence_Ed @pmtmEst CHAR(2)
 --Cartera edades
 AS
 SELECT  tmEst, tmTipDoc, tmDocumento, tmIdCia, tmItem, tmFecEmision, tmFecVence, tmValorFactura, tmValorAbono
, tmVencido, tmPorVencer, tmVence1,tmVence2, tmVence3, tmVence4, tmIdPropietario, tmIdVehiculo, tmIdClase
 FROM   tm_vence
 WHERE tmEst=@pmtmEst AND tmTipDoc IN ('ADM','OTR','ND1','PRE','FVC')
 ORDER BY tmTipDoc,tmIdCia,tmDocumento,tmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpActivos @pmIdActivo VARCHAR(30),@pmNomActivo VARCHAR(250),@pmNumSerie VARCHAR(30),@pmCodInvent VARCHAR(16),@pmCodBarras VARCHAR(20),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdInstala VARCHAR(4),@pmIdDep VARCHAR(4),@pmIdRespons VARCHAR(16),@pmIdCia CHAR(2),@pmIdProv VARCHAR(16)
,@pmFecCompra SMALLDATETIME,@pmFecActivacion SMALLDATETIME,@pmNumOrden INT,@pmIdCiaOrden CHAR(2),@pmNumEntrada INT,@pmIdCiaEnt CHAR(2),@pmVrCompra MONEY,@pmVrComercial MONEY,@pmVrBaseDep MONEY,@pmVrMejoras MONEY,@pmVrDisminucion MONEY,@pmVidaUtil INT,@pmFecFinDep SMALLDATETIME,@pmMetodoDep VARCHAR(10),@pmAjustable BIT
,@pmContable BIT,@pmCueEnGrupo BIT,@pmVrAjuInfAcum MONEY,@pmVrDepAcuHis MONEY,@pmVrDepAcuAju MONEY,@pmVrAjuInfDep MONEY,@pmNumMesesDep INT,@pmFecUltDep SMALLDATETIME,@pmMarca VARCHAR(30),@pmModelo VARCHAR(10),@pmNumPlaca VARCHAR(20),@pmNumDoc VARCHAR(20),@pmReferencia VARCHAR(50),@pmIdEstado VARCHAR(4)
,@pmFecRetiro SMALLDATETIME,@pmIdCausal VARCHAR(4),@pmVrVenta MONEY
 AS 
 UPDATE Activos SET NomActivo=@pmNomActivo,NumSerie=@pmNumSerie,CodInvent=@pmCodInvent,CodBarras=@pmCodBarras,IdGrupo=@pmIdGrupo,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdInstala=@pmIdInstala,IdDep=@pmIdDep,IdRespons=@pmIdRespons,IdCia=@pmIdCia,IdProv=@pmIdProv,FecCompra=@pmFecCompra,FecActivacion=@pmFecActivacion
,NumOrden=@pmNumOrden,IdCiaOrden=@pmIdCiaOrden,NumEntrada=@pmNumEntrada,IdCiaEnt=@pmIdCiaEnt,VrCompra=@pmVrCompra,VrComercial=@pmVrComercial,VrBaseDep=@pmVrBaseDep,VrMejoras=@pmVrMejoras,VrDisminucion=@pmVrDisminucion,VidaUtil=@pmVidaUtil,FecFinDep=@pmFecFinDep,MetodoDep=@pmMetodoDep,Ajustable=@pmAjustable
,Contable=@pmContable,CueEnGrupo=@pmCueEnGrupo,VrAjuInfAcum=@pmVrAjuInfAcum,VrDepAcuHis=@pmVrDepAcuHis,VrDepAcuAju=@pmVrDepAcuAju,VrAjuInfDep=@pmVrAjuInfDep,NumMesesDep=@pmNumMesesDep,FecUltDep=@pmFecUltDep,Marca=@pmMarca,Modelo=@pmModelo,NumPlaca=@pmNumPlaca,NumDoc=@pmNumDoc,Referencia=@pmReferencia,IdEstado=@pmIdEstado,FecRetiro=@pmFecRetiro,IdCausal=@pmIdCausal,VrVenta=@pmVrVenta
 WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpActivosAcu @pmIdActivo VARCHAR(30),@pmVrAjuInfAcum MONEY,@pmVrDepAcuHis MONEY,@pmVrDepAcuAju MONEY,@pmVrAjuInfDep MONEY,@pmNumMesesDep INT
 AS 
 UPDATE Activos SET VrAjuInfAcum=@pmVrAjuInfAcum,VrDepAcuHis=@pmVrDepAcuHis
,VrDepAcuAju=@pmVrDepAcuAju,VrAjuInfDep=@pmVrAjuInfDep,NumMesesDep=@pmNumMesesDep
 WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpActivosDis @pmIdActivo VARCHAR(30),@pmVrDisminucion MONEY,@pmAuto BIT
 AS 
 IF @pmAuto<>0 
  UPDATE Activos SET  VrDisminucion=(VrDisminucion+@pmVrDisminucion)  WHERE IdActivo=@pmIdActivo
 ELSE
  UPDATE Activos SET  VrDisminucion=@pmVrDisminucion  WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpActivosMej @pmIdActivo VARCHAR(30),@pmVrMejoras MONEY,@pmAuto BIT
 AS 
 IF @pmAuto<>0 
  UPDATE Activos SET  VrMejoras=(VrMejoras+@pmVrMejoras)  WHERE IdActivo=@pmIdActivo
 ELSE
  UPDATE Activos SET  VrMejoras=@pmVrMejoras  WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpActivosRet @pmIdActivo VARCHAR(30),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME
,@pmIdCausal VARCHAR(4),@pmVrVenta MONEY
 AS 
 UPDATE Activos SET  IdEstado=@pmIdEstado,FecRetiro=@pmFecRetiro,IdCausal=@pmIdCausal,VrVenta=@pmVrVenta
 WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpActivos_Anexo @pmIdActivo VARCHAR(30),@pmIdCuenta VARCHAR(16),@pmIdCueDep VARCHAR(16),@pmIdCueGasto VARCHAR(16),@pmIdCueAjuste VARCHAR(16),@pmIdCueCorr VARCHAR(16),@pmIdCueAjuDep VARCHAR(16),@pmIdCueCorrAD VARCHAR(16),@pmNumPoliza VARCHAR(30)
,@pmNitCiaAseg VARCHAR(16),@pmFecPoliza SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmVrAsegurado MONEY,@pmVrCuotaMes MONEY,@pmVendPoliza VARCHAR(150),@pmTeleVend VARCHAR(30),@pmDiasGarantia INT,@pmComentGarantia VARCHAR(250),@pmPignorado BIT
,@pmFecIniPignor SMALLDATETIME,@pmFecFinPignor SMALLDATETIME,@pmGarantPignor VARCHAR(150),@pmMantenimiento BIT,@pmNumContrato VARCHAR(20),@pmFecIniCont SMALLDATETIME,@pmFecFinCont SMALLDATETIME,@pmPeriodicidad INT,@pmFecUltMnmto SMALLDATETIME
,@pmNitEmpMnmto VARCHAR(16),@pmComentMnmto VARCHAR(250),@pmDesUbicacion VARCHAR(100),@pmComentarios VARCHAR(250),@pmComentRetiro VARCHAR(250),@pmDatosTecnicos VARCHAR(4000)
 AS 
 UPDATE Activos_Anexo SET IdCuenta=@pmIdCuenta,IdCueDep=@pmIdCueDep,IdCueGasto=@pmIdCueGasto,IdCueAjuste=@pmIdCueAjuste,IdCueCorr=@pmIdCueCorr,IdCueAjuDep=@pmIdCueAjuDep,IdCueCorrAD=@pmIdCueCorrAD,NumPoliza=@pmNumPoliza,NitCiaAseg=@pmNitCiaAseg
,FecPoliza=@pmFecPoliza,FecVigencia=@pmFecVigencia,VrAsegurado=@pmVrAsegurado,VrCuotaMes=@pmVrCuotaMes,VendPoliza=@pmVendPoliza,TeleVend=@pmTeleVend,DiasGarantia=@pmDiasGarantia,ComentGarantia=@pmComentGarantia,Pignorado=@pmPignorado
,FecIniPignor=@pmFecIniPignor,FecFinPignor=@pmFecFinPignor,GarantPignor=@pmGarantPignor,Mantenimiento=@pmMantenimiento,NumContrato=@pmNumContrato,FecIniCont=@pmFecIniCont,FecFinCont=@pmFecFinCont,Periodicidad=@pmPeriodicidad,FecUltMnmto=@pmFecUltMnmto
,NitEmpMnmto=@pmNitEmpMnmto,ComentMnmto=@pmComentMnmto,DesUbicacion=@pmDesUbicacion,Comentarios=@pmComentarios,ComentRetiro=@pmComentRetiro,DatosTecnicos=@pmDatosTecnicos
 WHERE IdActivo=@pmIdActivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCaja @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11),@pmSaldoAnt MONEY,@pmFacturas MONEY,@pmDevFacturas MONEY,@pmPlanillas MONEY,@pmDevPlanillas MONEY
,@pmRecibos MONEY,@pmDevRecibos MONEY,@pmIngCaja MONEY,@pmDevIngCaja MONEY,@pmEgrCaja MONEY,@pmDevEgrCaja MONEY,@pmConsignac MONEY,@pmDevConsig MONEY,@pmNotasCalibra MONEY,@pmDevCalibra MONEY,@pmOtrosIng MONEY,@pmOtrosEgr MONEY
 AS 
 UPDATE AcuCaja SET SaldoAnt=@pmSaldoAnt,Facturas=@pmFacturas,DevFacturas=@pmDevFacturas,Planillas=@pmPlanillas,DevPlanillas=@pmDevPlanillas,Recibos=@pmRecibos,DevRecibos=@pmDevRecibos,IngCaja=@pmIngCaja,DevIngCaja=@pmDevIngCaja
,EgrCaja=@pmEgrCaja,DevEgrCaja=@pmDevEgrCaja,Consignac=@pmConsignac,DevConsig=@pmDevConsig,NotasCalibra=@pmNotasCalibra,DevCalibra=@pmDevCalibra,OtrosIng=@pmOtrosIng,OtrosEgr=@pmOtrosEgr
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte AND IdCia=@pmIdCia AND IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCajaEgr @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
,@pmEgrCaja MONEY,@pmDevEgrCaja MONEY
 AS 
 UPDATE AcuCaja SET EgrCaja=@pmEgrCaja,DevEgrCaja=@pmDevEgrCaja
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte AND IdCia=@pmIdCia AND IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCajaOtr @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2),@pmIdUsuario VARCHAR(11)
,@pmOtrosIng MONEY,@pmOtrosEgr MONEY
 AS 
 UPDATE AcuCaja SET  OtrosIng=@pmOtrosIng,OtrosEgr=@pmOtrosEgr
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte AND IdCia=@pmIdCia AND IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCajaSan @pmFechaDia SMALLDATETIME,@pmNumCorte INT,@pmIdCia CHAR(2)
,@pmIdUsuario VARCHAR(11),@pmSaldoAnt MONEY
 AS 
 UPDATE AcuCaja SET SaldoAnt=@pmSaldoAnt
 WHERE FechaDia=@pmFechaDia AND NumCorte=@pmNumCorte AND IdCia=@pmIdCia AND IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuClientes @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmSaldoAnt MONEY
,@pmFacturas MONEY,@pmDevFacturas MONEY,@pmRecibos MONEY,@pmDevRecibos MONEY,@pmNotasDeb MONEY,@pmNotasCre MONEY
 AS 
 UPDATE AcuClientes SET SaldoAnt=@pmSaldoAnt,Facturas=@pmFacturas,DevFacturas=@pmDevFacturas,Recibos=@pmRecibos,DevRecibos=@pmDevRecibos,NotasDeb=@pmNotasDeb,NotasCre=@pmNotasCre
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCliente=@pmIdCliente AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuClientesSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmSaldoAnt MONEY
 AS 
 UPDATE AcuClientes SET SaldoAnt=@pmSaldoAnt
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCliente=@pmIdCliente AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCuentasAgeSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)
,@pmTipoAcum CHAR(2),@pmSaldoAnterior MONEY
 AS 
 UPDATE AcuCuentasAge SET SaldoAnterior=@pmSaldoAnterior
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia
 AND IdCuenta=@pmIdCuenta AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia
 AND TipoAcum=@pmTipoAcum
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCuentasAuxSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmnReg INT,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY
 AS 
 UPDATE AcuCuentasAux SET  SaldoAnterior=@pmSaldoAnterior
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND nReg=@pmnReg
 AND IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCuentasCcSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmSaldoAnterior MONEY
 AS 
 UPDATE AcuCuentasCc SET SaldoAnterior=@pmSaldoAnterior
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia
 AND IdCuenta=@pmIdCuenta AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCuentasNitSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmSaldoAnterior MONEY
 AS 
 UPDATE AcuCuentasNit SET SaldoAnterior=@pmSaldoAnterior
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia
 AND IdCuenta=@pmIdCuenta AND IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCuentasSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2)
,@pmIdCuenta VARCHAR(16),@pmSaldoAnterior MONEY
 AS 
 UPDATE AcuCuentas SET SaldoAnterior=@pmSaldoAnterior
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuCuentasVehSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmSaldoAnterior MONEY
 AS 
 UPDATE AcuCuentasVeh SET SaldoAnterior=@pmSaldoAnterior
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCuenta=@pmIdCuenta AND IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuInvLotesSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmNumLote VARCHAR(30),@pmSaldoAnt DECIMAL(14,4),@pmCostoAnt MONEY
 AS 
 UPDATE  AcuInvLotes SET SaldoAnt=@pmSaldoAnt,CostoAnt=@pmCostoAnt
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia 
 AND IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega AND NumLote=@pmNumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuInvTanquesSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdTanque VARCHAR(4),@pmSaldoAnt DECIMAL(14,4),@pmCostoAnt MONEY
 AS 
 UPDATE  AcuInvTanques SET SaldoAnt=@pmSaldoAnt,CostoAnt=@pmCostoAnt
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia 
 AND IdProducto=@pmIdProducto AND IdTanque=@pmIdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuInvUbicaSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdUbic VARCHAR(10),@pmSaldoAnt DECIMAL(14,4),@pmCostoAnt MONEY
 AS 
 UPDATE  AcuInvUbica SET SaldoAnt=@pmSaldoAnt,CostoAnt=@pmCostoAnt
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia 
 AND IdProducto=@pmIdProducto AND IdUbic=@pmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuInventarioSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdProducto VARCHAR(16)
,@pmIdBodega VARCHAR(4),@pmSaldoAnt DECIMAL(14,4),@pmCostoAnt MONEY
 AS 
 UPDATE  AcuInventario SET SaldoAnt=@pmSaldoAnt,CostoAnt=@pmCostoAnt
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia 
 AND IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuPrestamos @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmSaldoAnt MONEY,@pmPrestamos MONEY,@pmDevPrestamos MONEY,@pmRecibos MONEY,@pmDevRecibos MONEY,@pmNotasDeb MONEY,@pmNotasCre MONEY
,@pmFacturas MONEY,@pmDevFacturas MONEY
 AS 
 UPDATE AcuPrestamos SET SaldoAnt=@pmSaldoAnt,Prestamos=@pmPrestamos,DevPrestamos=@pmDevPrestamos,Recibos=@pmRecibos,DevRecibos=@pmDevRecibos
,NotasDeb=@pmNotasDeb,NotasCre=@pmNotasCre,Facturas=@pmFacturas,DevFacturas=@pmDevFacturas
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCliente=@pmIdCliente AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAcuPrestamosSan @pmnAnno INT,@pmnMes INT,@pmIdCia CHAR(2),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmSaldoAnt MONEY
 AS 
 UPDATE AcuPrestamos SET SaldoAnt=@pmSaldoAnt
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND IdCia=@pmIdCia AND IdCliente=@pmIdCliente AND IdAgencia=@pmIdAgencia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpAcuPropietarios @pmnAnno INT,@pmIdReg INT,@pmnMes INT,@pmIdPropietario VARCHAR(16),@pmIdVehiculo VARCHAR(10)
,@pmIdClase VARCHAR(4), @pmIdCia CHAR(2),@pmSaldoAnterior MONEY, @pmTotalDebitos MONEY, @pmTotalCreditos MONEY
AS
 UPDATE  AcuPropietarios  SET  SaldoAnterior=@pmSaldoAnterior, TotalDebitos=@pmTotalDebitos, TotalCreditos=@pmTotalCreditos
 WHERE  nAnno=@pmnAnno AND IdReg=@pmIdReg AND  nMes=@pmnMes AND IdPropietario=@pmIdPropietario

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfAmortiza @pmNumero INT,@pmFecha SMALLDATETIME,@pmEstado INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_AfAmortiza SET Fecha=@pmFecha,Estado=@pmEstado,IdCia=@pmIdCia
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfCausales @pmIdCausal VARCHAR(4),@pmCausal VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE AfCausales SET Causal=@pmCausal,Inactivo=@pmInactivo
 WHERE IdCausal=@pmIdCausal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfClasesCue @pmIdClase VARCHAR(4),@pmClaseCue VARCHAR(50),@pmConcepto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE AfClasesCue SET ClaseCue=@pmClaseCue,Concepto=@pmConcepto,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfDeprecia @pmNumero INT,@pmFecha SMALLDATETIME,@pmEstado INT
,@pmIdUsuario VARCHAR(11),@pmFechaCrea SMALLDATETIME,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_AfDeprecia SET Fecha=@pmFecha,Estado=@pmEstado,IdUsuario=@pmIdUsuario,FechaCrea=@pmFechaCrea,IdCia=@pmIdCia
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfEstados @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE AfEstados SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfGrupos @pmIdGrupo VARCHAR(4),@pmGrupo VARCHAR(50),@pmIdLinea VARCHAR(4),@pmVidaUtil INT,@pmInactivo BIT
 AS 
 UPDATE AfGrupos SET Grupo=@pmGrupo,IdLinea=@pmIdLinea,VidaUtil=@pmVidaUtil,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfGruposCue @pmIdGrupo VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16)
 AS 
 UPDATE AfGruposCue SET IdCuenta=@pmIdCuenta
 WHERE IdGrupo=@pmIdGrupo AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfLineas @pmIdLinea VARCHAR(4),@pmLinea VARCHAR(50),@pmVidaUtil INT,@pmTarfDep DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE AfLineas SET Linea=@pmLinea,VidaUtil=@pmVidaUtil,TarfDep=@pmTarfDep,Inactivo=@pmInactivo
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfMantnmto @pmNumero INT,@pmIdCia CHAR(2),@pmIdActivo VARCHAR(30),@pmFecha SMALLDATETIME,@pmNumContrato VARCHAR(20)
,@pmNitEmpresa VARCHAR(16),@pmNomEmpresa VARCHAR(150),@pmFinContrato BIT,@pmDiagnostico VARCHAR(2000),@pmComentarios VARCHAR(4000)
 AS 
 UPDATE Trn_AfMantnmto SET IdCia=@pmIdCia,IdActivo=@pmIdActivo,Fecha=@pmFecha,NumContrato=@pmNumContrato,NitEmpresa=@pmNitEmpresa,NomEmpresa=@pmNomEmpresa
,FinContrato=@pmFinContrato,Diagnostico=@pmDiagnostico,Comentarios=@pmComentarios
 WHERE Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfTiposDif @pmIdTipDif VARCHAR(4),@pmTipoDiferido VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE AfTiposDif SET TipoDiferido=@pmTipoDiferido,Inactivo=@pmInactivo
 WHERE IdTipDif=@pmIdTipDif
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfTiposMov @pmIdMov VARCHAR(4),@pmTipoMov VARCHAR(50),@pmConsecutivo INT,@pmInactivo BIT
 AS 
 UPDATE AfTiposMov SET TipoMov=@pmTipoMov,Consecutivo=@pmConsecutivo,Inactivo=@pmInactivo
 WHERE IdMov=@pmIdMov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAfTiposMovCon @pmIdMov VARCHAR(4),@pmConsecutivo INT,@pmAuto BIT
 AS 
 IF @pmAuto <>0 
  UPDATE AfTiposMov SET Consecutivo=Consecutivo+1 WHERE IdMov=@pmIdMov
ELSE
  UPDATE AfTiposMov SET Consecutivo=@pmConsecutivo  WHERE IdMov=@pmIdMov

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAgencias @pmIdAgencia VARCHAR(16),@pmCodAgencia VARCHAR(16),@pmIdClie VARCHAR(16),@pmAgencia VARCHAR(150),@pmDirAgncia VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelAgncia VARCHAR(20),@pmFaxAgncia VARCHAR(20),@pmNitCont VARCHAR(16),@pmNomCont VARCHAR(150)
,@pmemlCont VARCHAR(100),@pmCargoCont VARCHAR(50),@pmIdSzona VARCHAR(4),@pmIdPlazo VARCHAR(4),@pmCdPlazoComb VARCHAR(4),@pmIdForma VARCHAR(4),@pmCdBandera VARCHAR(4),@pmIdVend VARCHAR(16),@pmCdCms VARCHAR(4),@pmCdDct VARCHAR(4),@pmIntMora DECIMAL(14,4),@pmDiasGracia INT,@pmFactFletes BIT,@pmFactSold BIT
,@pmVrCupoCre MONEY,@pmVrSaldoAct MONEY,@pmFecUpCupo SMALLDATETIME,@pmNContrato INT,@pmCiaCont CHAR(2),@pmFecIngreso SMALLDATETIME,@pmReferencia VARCHAR(50),@pmComentarios VARCHAR(250),@pmCupoGalones DECIMAL(14,4),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTipoZona VARCHAR(10),@pmCodSicom VARCHAR(20)
,@pmCdTipAgenc VARCHAR(4),@pmCdSubAgenc VARCHAR(4),@pmCdGrupAgenc VARCHAR(4), @pmCdSubGrupAgenc VARCHAR(4), @pmCdFntePago VARCHAR(4), @pmPlazoSobtasa VARCHAR(20)
,@pmDocFinanc VARCHAR(3), @pmVolContMes DECIMAL(14,4), @pmVolContTotal DECIMAL(14,4), @pmVolViabMes DECIMAL(14,4),@pmVolViabTotal DECIMAL(14,4), @pmFecPriVenta SMALLDATETIME, @pmInvCtbObra MONEY, @pmInvCtbCapital MONEY, @pmInvCtbEfectivo MONEY, @pmInvCtbImagen MONEY, @pmInvCtbMntmto MONEY
,@pmFecDesemb SMALLDATETIME, @pmInvContObra MONEY, @pmInvContCapital MONEY, @pmInvContEfectivo MONEY, @pmInvContImagen MONEY, @pmCrtlAprovInv INT, @pmCrtlAprovImg INT,@pmFecVigCond SMALLDATETIME,@pmTasaIntArranq DECIMAL(14,4),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Agencias SET CodAgencia=@pmCodAgencia,IdClie=@pmIdClie,Agencia=@pmAgencia,DirAgncia=@pmDirAgncia,IdLocal=@pmIdLocal,TelAgncia=@pmTelAgncia,FaxAgncia=@pmFaxAgncia,NitCont=@pmNitCont,NomCont=@pmNomCont,emlCont=@pmemlCont,CargoCont=@pmCargoCont,IdSzona=@pmIdSzona
,IdPlazo=@pmIdPlazo,CdPlazoComb=@pmCdPlazoComb,IdForma=@pmIdForma,CdBandera=@pmCdBandera,IdVend=@pmIdVend,CdCms=@pmCdCms,CdDct=@pmCdDct,IntMora=@pmIntMora,DiasGracia=@pmDiasGracia,FactFletes=@pmFactFletes,FactSold=@pmFactSold,VrCupoCre=@pmVrCupoCre,VrSaldoAct=@pmVrSaldoAct
,FecUpCupo=@pmFecUpCupo,FecIngreso=@pmFecIngreso,Referencia=@pmReferencia,Comentarios=@pmComentarios,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,NContrato=@pmNContrato,CiaCont=@pmCiaCont,CupoGalones=@pmCupoGalones,FechaUpdate=@pmFechaUpdate,TipoZona=@pmTipoZona,CodSicom=@pmCodSicom
,CdTipAgenc=@pmCdTipAgenc,CdSubAgenc=@pmCdSubAgenc,CdGrupAgenc=@pmCdGrupAgenc,CdSubGrupAgenc=@pmCdSubGrupAgenc,CdFntePago=@pmCdFntePago,PlazoSobtasa=@pmPlazoSobtasa,DocFinanc=@pmDocFinanc,VolContMes=@pmVolContMes,VolContTotal=@pmVolContTotal,VolViabMes=@pmVolViabMes,VolViabTotal=@pmVolViabTotal
,FecPriVenta=@pmFecPriVenta,InvCtbObra=@pmInvCtbObra,InvCtbCapital=@pmInvCtbCapital,InvCtbEfectivo=@pmInvCtbEfectivo,InvCtbImagen=@pmInvCtbImagen,InvCtbMntmto=@pmInvCtbMntmto,FecDesemb=@pmFecDesemb,InvContObra=@pmInvContObra,InvContCapital=@pmInvContCapital,InvContEfectivo=@pmInvContEfectivo,InvContImagen=@pmInvContImagen
,CrtlAprovInv=@pmCrtlAprovInv,CrtlAprovImg=@pmCrtlAprovImg,FecVigCond=@pmFecVigCond,TasaIntArranq=@pmTasaIntArranq
 WHERE IdAgencia=@pmIdAgencia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAgenciasCon @pmIdAgencia VARCHAR(16),@pmNContrato INT,@pmCiaCont CHAR(2),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Agencias SET NContrato=@pmNContrato,CiaCont=@pmCiaCont,FechaUpdate=@pmFechaUpdate
 WHERE IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAgenciasEst @pmIdAgencia VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmComentarios VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Agencias SET  IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Comentarios=@pmComentarios,FechaUpdate=@pmFechaUpdate
 WHERE IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAjuInv @pmTipDoc VARCHAR(3),@pmAjuste INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16)
,@pmVrSubTotal MONEY,@pmCantidad DECIMAL(14,4),@pmBaseAjuInf MONEY,@pmTarifPaag DECIMAL(14,4),@pmNumDcmto INT,@pmNumOrden INT,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_AjuInv SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdTercero=@pmIdTercero,VrSubTotal=@pmVrSubTotal,Cantidad=@pmCantidad,BaseAjuInf=@pmBaseAjuInf,TarifPaag=@pmTarifPaag
,NumDcmto=@pmNumDcmto,NumOrden=@pmNumOrden,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Modalidad=@pmModalidad,TipCom=@pmTipCom
,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Ajuste=@pmAjuste AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpAlmBodegas @pmIdBodega VARCHAR(4),@pmBodega VARCHAR(100),@pmDireccion VARCHAR(100),@pmTelBodega VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE AlmBodegas SET Bodega=@pmBodega,Direccion=@pmDireccion,TelBodega=@pmTelBodega,Inactivo=@pmInactivo
 WHERE IdBodega=@pmIdBodega

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAprobacion @pmTipDoc VARCHAR(3),@pmAprobacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmTipoPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2)
,@pmFecPedido SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmVrCupoCred MONEY,@pmVrSaldoAct MONEY,@pmChequesDev BIT
,@pmClienteMora BIT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Aprobacion SET Fecha=@pmFecha,TipoPed=@pmTipoPed,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,FecPedido=@pmFecPedido,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia
,pVehiculo=@pmpVehiculo,VrCupoCred=@pmVrCupoCred,VrSaldoAct=@pmVrSaldoAct,ChequesDev=@pmChequesDev,ClienteMora=@pmClienteMora
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Aprobacion=@pmAprobacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAprobacionAnu @pmTipDoc VARCHAR(3),@pmAprobacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Aprobacion SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Aprobacion=@pmAprobacion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAreas @pmIdArea VARCHAR(4),@pmArea VARCHAR(50),@pmJefeArea VARCHAR(50)
,@pmIdCCosto VARCHAR(16),@pmInactivo BIT
 AS 
 UPDATE Areas SET Area=@pmArea,JefeArea=@pmJefeArea,IdCCosto=@pmIdCCosto,Inactivo=@pmInactivo
 WHERE IdArea=@pmIdArea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAutoriza @pmTipAut VARCHAR(3),@pmAutoriza INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNumAutoriza INT,@pmIdUsuarioAut VARCHAR(11),@pmIdEstacion CHAR(2),@pmNitTercero VARCHAR(16)
,@pmpVehiculo VARCHAR(10),@pmTipoDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFechaDoc SMALLDATETIME,@pmDocRef INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Autoriza SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,NumAutoriza=@pmNumAutoriza,IdUsuarioAut=@pmIdUsuarioAut,IdEstacion=@pmIdEstacion,NitTercero=@pmNitTercero,pVehiculo=@pmpVehiculo
,TipoDoc=@pmTipoDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,FechaDoc=@pmFechaDoc,DocRef=@pmDocRef,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAutorizaAnu @pmTipAut VARCHAR(3),@pmAutoriza INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Autoriza SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=@pmIdEstado
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpAutorizaDoc @pmTipAut VARCHAR(3),@pmAutoriza INT,@pmIdCia CHAR(2)
,@pmTipoDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFechaDoc SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Autoriza SET TipoDoc=@pmTipoDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc
,FechaDoc=@pmFechaDoc,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipAut=@pmTipAut AND Autoriza=@pmAutoriza AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpBancos @pmIdBanco VARCHAR(4),@pmBanco VARCHAR(50),@pmIdClase VARCHAR(4),@pmCodEntidad VARCHAR(20),@pmDireccion VARCHAR(50)
,@pmContacto VARCHAR(50),@pmComision DECIMAL(10,4),@pmInactivo BIT
 AS 
 UPDATE Bancos SET Banco=@pmBanco,IdClase=@pmIdClase,CodEntidad=@pmCodEntidad,Direccion=@pmDireccion,Contacto=@pmContacto,Comision=@pmComision,Inactivo=@pmInactivo
 WHERE IdBanco=@pmIdBanco
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpBodUbic @pmIdUbic VARCHAR(10),@pmUbicacion VARCHAR(50),@pmIdBodega VARCHAR(4),@pmPasillo VARCHAR(10),@pmNivel VARCHAR(10),@pmProfdad VARCHAR(10),@pmDimAltura DECIMAL(14,4),@pmDimAnch DECIMAL(14,4)
,@pmDimLong DECIMAL(14,4),@pmCapcVolm DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmCapcPeso DECIMAL(14,4),@pmUndPes VARCHAR(10),@pmCdTmcia VARCHAR(4),@pmCdMnjo VARCHAR(4),@pmNotas VARCHAR(250),@pmInactivo BIT
 AS 
 UPDATE BodUbic SET Ubicacion=@pmUbicacion,IdBodega=@pmIdBodega,Pasillo=@pmPasillo,Nivel=@pmNivel,Profdad=@pmProfdad,DimAltura=@pmDimAltura,DimAnch=@pmDimAnch,DimLong=@pmDimLong,CapcVolm=@pmCapcVolm,UndCapc=@pmUndCapc
,CapcPeso=@pmCapcPeso,UndPes=@pmUndPes,CdTmcia=@pmCdTmcia,CdMnjo=@pmCdMnjo,Notas=@pmNotas,Inactivo=@pmInactivo
 WHERE IdUbic=@pmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpBodegas @pmIdBodega VARCHAR(4),@pmBodega VARCHAR(50),@pmDescrip VARCHAR(100)
,@pmLtaPre CHAR(1),@pmTipoInv VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Bodegas SET Bodega=@pmBodega,Descrip=@pmDescrip
,LtaPre=@pmLtaPre,Inactivo=@pmInactivo,TipoInv=@pmTipoInv
 WHERE IdBodega=@pmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpBusClaseCue @pmIdClase VARCHAR(4),@pmClaseCuenta VARCHAR(50),@pmPrioridad INT,@pmInactivo BIT
 AS 
 UPDATE BusClaseCue SET ClaseCuenta=@pmClaseCuenta,Prioridad=@pmPrioridad,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCalndario @pmFechaDia SMALLDATETIME,@pmTipoFtvo VARCHAR(3)
 AS 
 UPDATE Calndario SET TipoFtvo=@pmTipoFtvo
 WHERE FechaDia=@pmFechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCargos @pmIdCargo VARCHAR(4),@pmCargo VARCHAR(50),@pmFunciones VARCHAR(3000),@pmInactivo BIT
 AS 
 UPDATE Cargos SET Cargo=@pmCargo,Funciones=@pmFunciones,Inactivo=@pmInactivo
 WHERE IdCargo=@pmIdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCausaVales @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY
,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4)
,@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_CausaVales SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrCargos=@pmVrCargos
,VrOtrDcto=@pmVrOtrDcto,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom
,CodTarCom=@pmCodTarCom,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate 
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCausaValesAnu @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_CausaVales SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacVence @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecEmision SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmValorFactura MONEY,@pmValorAbono MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10)
,@pmVehPropio BIT,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdConcepto VARCHAR(4),@pmFecUltPago SMALLDATETIME,@pmNotaInt INT,@pmFecUltLiquida SMALLDATETIME,@pmFactura VARCHAR(15),@pmReferencia VARCHAR(20),@pmDetalle VARCHAR(100)
,@pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCiaRec CHAR(2),@pmValorAtcpo MONEY,@pmIdCuenta VARCHAR(16),@pmCombCant DECIMAL(14,4),@pmCombValor MONEY
 AS 
 UPDATE Trn_CausacVence SET FecEmision=@pmFecEmision,FecVence=@pmFecVence,ValorFactura=@pmValorFactura,ValorAbono=@pmValorAbono,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdVehiculo=@pmIdVehiculo,VehPropio=@pmVehPropio,IdVendedor=@pmIdVendedor,Comision=@pmComision,IdCargo=@pmIdCargo,IdClase=@pmIdClase
,IdConcepto=@pmIdConcepto,FecUltPago=@pmFecUltPago,NotaInt=@pmNotaInt,FecUltLiquida=@pmFecUltLiquida,Factura=@pmFactura,Referencia=@pmReferencia,Detalle=@pmDetalle,TipRec=@pmTipRec,Recibo=@pmRecibo,IdCiaRec=@pmIdCiaRec,ValorAtcpo=@pmValorAtcpo,IdCuenta=@pmIdCuenta,CombCant=@pmCombCant,CombValor=@pmCombValor
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND Item=@pmItem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacVenceAbo @pmTipDoc VARCHAR(3), @pmDocumento INT, @pmIdCia CHAR(2), @pmItem INT
,@pmValorAbono MONEY,@pmFecUltPago SMALLDATETIME
AS
UPDATE  Trn_CausacVence  SET  ValorAbono=@pmValorAbono,FecUltPago=ISNULL(@pmFecUltPago,FecUltPago)
WHERE  TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND  Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacVenceAcp @pmTipDoc VARCHAR(3),@pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null
AS
--limpia información de anticipos en los vencimientos
 UPDATE  Trn_CausacVence  SET TipRec='',Recibo=0,IdCiaRec='',ValorAtcpo=0
 WHERE  Recibo>0 AND TipDoc LIKE ISNULL(@pmTipDoc,'%') 
  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacVenceApl @pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdClase VARCHAR(4)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
 UPDATE  Trn_CausacVence  SET Trn_CausacVence.ValorAbono=(SELECT ISNULL(SUM(Trn_RecFactura.ValorAbono-Trn_RecFactura.Devolucion),0)
 FROM Trn_RecFactura WHERE Trn_CausacVence.TipDoc=Trn_RecFactura.TipDoc AND Trn_CausacVence.Documento =Trn_RecFactura.Documento
 AND Trn_CausacVence.IdCia=Trn_RecFactura.IdCiaDoc AND Trn_CausacVence.Item=Trn_RecFactura.ItemDoc)
 WHERE FecEmision BETWEEN @pmFechaIni AND @pmFechaFin 
 AND  IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdCliente like ISNULL(@pmIdCliente,'%')
 AND IdCia like ISNULL(@pmIdCia,'%%')  AND IdClase like ISNULL(@pmIdClase,'%')  AND TipDoc like ISNULL(@pmTipDoc,'%')  
ELSE
 UPDATE  Trn_CausacVence  SET Trn_CausacVence.ValorAbono=(SELECT ISNULL(SUM(Trn_RecFactura.ValorAbono-Trn_RecFactura.Devolucion),0)
 FROM Trn_RecFactura WHERE Trn_CausacVence.TipDoc=Trn_RecFactura.TipDoc AND Trn_CausacVence.Documento =Trn_RecFactura.Documento
 AND Trn_CausacVence.IdCia=Trn_RecFactura.IdCiaDoc AND Trn_CausacVence.Item=Trn_RecFactura.ItemDoc)
 WHERE  IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdCliente like ISNULL(@pmIdCliente,'%')
 AND IdCia like ISNULL(@pmIdCia,'%%')  AND IdClase like ISNULL(@pmIdClase,'%')  AND TipDoc like ISNULL(@pmTipDoc,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacVenceCero @pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdClase VARCHAR(4)=Null
,@pmTipDoc VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
  IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE  Trn_CausacVence  SET  ValorAbono=0
   WHERE FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND  IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdCliente like ISNULL(@pmIdCliente,'%')
  AND IdCia like ISNULL(@pmIdCia,'%%')   AND  TipDoc like ISNULL(@pmTipDoc,'%')  AND IdClase like ISNULL(@pmIdClase,'%')
 ELSE
    UPDATE  Trn_CausacVence  SET  ValorAbono=0
    WHERE  IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdCliente like ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
   AND  TipDoc LIKE ISNULL(@pmTipDoc,'%')  AND  IdClase LIKE ISNULL(@pmIdClase,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpCausacion @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmFactura VARCHAR(15),@pmIdProveedor VARCHAR(16),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmReferencia VARCHAR(20),@pmModalidad VARCHAR(10),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16)
,@pmVehPropio BIT,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorIva MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorFletes MONEY,@pmValorNeto MONEY,@pmValorCCobrar MONEY,@pmBaseIva MONEY,@pmBaseRet MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmIdTarIva VARCHAR(4),@pmIdTarRet VARCHAR(4)
,@pmIdTarIca VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmCxcVarios BIT,@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmCierreCte BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Causacion SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,Factura=@pmFactura,IdProveedor=@pmIdProveedor,FecFactura=@pmFecFactura,FecVence=@pmFecVence,Referencia=@pmReferencia,Modalidad=@pmModalidad,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor,VehPropio=@pmVehPropio,ValorTotal=@pmValorTotal,ValorDcto=@pmValorDcto,ValorIva=@pmValorIva,ValorReteFte=@pmValorReteFte
,ValorReteIva=@pmValorReteIva,ValorReteIca=@pmValorReteIca,ValorOtros=@pmValorOtros,ValorFletes=@pmValorFletes,ValorNeto=@pmValorNeto,ValorCCobrar=@pmValorCCobrar,BaseIva=@pmBaseIva,BaseRet=@pmBaseRet,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,IdTarIva=@pmIdTarIva,IdTarRet=@pmIdTarRet,IdTarIca=@pmIdTarIca,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CxPagar=@pmCxPagar,CxcVarios=@pmCxcVarios
,TipEgr=@pmTipEgr,Egreso=@pmEgreso,IdCiaEgr=@pmIdCiaEgr,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,CierreCte=@pmCierreCte,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacionAnu @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmAnulado BIT, @pmNumDev INT, @pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250), @pmIdEstado VARCHAR(4)
AS
UPDATE Trn_Causacion SET   Anulado=@pmAnulado, NumDev=@pmNumDev, FecDev=@pmFecDev,Observacion=@pmObservacion, IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacionCmp @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmTipCom VARCHAR(3),@pmComprobante INT, @pmIdCiaCom CHAR(2),@pmIdEstado VARCHAR(4), @pmFecUpdate SMALLDATETIME
AS
UPDATE  Trn_Causacion SET  TipCom=@pmTipCom,Comprobante=@pmComprobante, IdCiaCom=@pmIdCiaCom,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpCausacionNoInt @pmTipDoc VARCHAR(3),@pmModalidad VARCHAR(10), @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmIdEstado VARCHAR(4)
AS
 UPDATE  Trn_Causacion SET Comprobante=0,IdCiaCom='',IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Modalidad=@pmModalidad AND  TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCiaCom=@pmIdCiaCom



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCentroCosto @pmIdCCosto VARCHAR(16),@pmCCosto VARCHAR(100)
,@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT
 AS 
 UPDATE CentroCosto SET CCosto=@pmCCosto,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo
 WHERE IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCentrosServ @pmIdCenSer VARCHAR(4),@pmCentroServ VARCHAR(50),@pmDirCentro VARCHAR(100),@pmIdLocal VARCHAR(8)
,@pmTelefonos VARCHAR(30),@pmTipoCentro VARCHAR(10),@pmNitResp VARCHAR(16),@pmRespnsable VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE CentrosServ SET CentroServ=@pmCentroServ,DirCentro=@pmDirCentro,IdLocal=@pmIdLocal,Telefonos=@pmTelefonos,TipoCentro=@pmTipoCentro
,NitResp=@pmNitResp,Respnsable=@pmRespnsable,Inactivo=@pmInactivo
 WHERE IdCenSer=@pmIdCenSer
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCheques @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmVrForma MONEY,@pmCtaForma VARCHAR(30)
,@pmBeneficiario VARCHAR(150),@pmReferncia1 VARCHAR(50),@pmReferncia2 VARCHAR(50),@pmDetalle VARCHAR(150),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmCdVend VARCHAR(16),@pmIdLocal VARCHAR(8),@pmTipRec VARCHAR(3)
,@pmRecibo INT,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmTipCon VARCHAR(3),@pmNumConsig INT,@pmIdCiaCon CHAR(2),@pmFecConsig SMALLDATETIME,@pmCdCta VARCHAR(4),@pmAnulado BIT
,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Cheques SET Fecha=@pmFecha,FecPago=@pmFecPago,VrForma=@pmVrForma,CtaForma=@pmCtaForma,Beneficiario=@pmBeneficiario,Referncia1=@pmReferncia1,Referncia2=@pmReferncia2,Detalle=@pmDetalle,NitCliente=@pmNitCliente
,CdAgencia=@pmCdAgencia,CdVend=@pmCdVend,IdLocal=@pmIdLocal,TipRec=@pmTipRec,Recibo=@pmRecibo,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,TipCon=@pmTipCon,NumConsig=@pmNumConsig
,IdCiaCon=@pmIdCiaCon,FecConsig=@pmFecConsig,CdCta=@pmCdCta,Anulado=@pmAnulado,FecDev=@pmFecDev,IdEstado=@pmIdEstado
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesACns @pmTipCon VARCHAR(3),@pmNumConsig INT,@pmIdCiaCon CHAR(2),@pmFecConsig SMALLDATETIME
 AS 
 UPDATE  Trn_Cheques SET TipCon='0',NumConsig=0,IdCiaCon='00',FecConsig=@pmFecConsig
 WHERE  Anulado=0 AND TipCon=@pmTipCon AND NumConsig=@pmNumConsig AND IdCiaCon=@pmIdCiaCon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesAnuRec @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET Anulado=@pmAnulado,FecDev=@pmFecDev,IdEstado=@pmIdEstado
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesApl @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc
,FecDoc=@pmFecDoc,IdEstado=@pmIdEstado
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesCns @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
,@pmTipCon VARCHAR(3),@pmNumConsig INT,@pmIdCiaCon CHAR(2),@pmFecConsig SMALLDATETIME,@pmCdCta VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET TipCon=@pmTipCon,NumConsig=@pmNumConsig,IdCiaCon=@pmIdCiaCon,FecConsig=@pmFecConsig,CdCta=@pmCdCta
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesDDoc @pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCiaDoc CHAR(2),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET  Documento=0,IdCiaDoc='00',IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
 AND  Recibo>0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesDap @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET  Documento=0,IdCiaDoc='00',IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
 AND TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesEod @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmAnulado BIT,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc
,Anulado=@pmAnulado,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesNdb @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE  Trn_Cheques SET Anulado=@pmAnulado,FecDev=@pmFecDev,IdEstado=@pmIdEstado
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpChequesOdb @pmTipForma VARCHAR(3),@pmNumero VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCia CHAR(2)
,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE  Trn_Cheques SET  Documento=0,IdCiaDoc='00',Anulado=@pmAnulado,FecDev=@pmFecDev,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipForma=@pmTipForma AND Numero=@pmNumero AND IdBanco=@pmIdBanco AND IdCia=@pmIdCia
 AND TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseBan @pmIdClase VARCHAR(4),@pmClaseBanco VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseBan SET ClaseBanco=@pmClaseBanco,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseCta @pmIdClase VARCHAR(4),@pmClaseCuenta VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseCta SET ClaseCuenta=@pmClaseCuenta,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseDiv @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseDiv SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseFon @pmIdClase VARCHAR(4),@pmClaseFondo VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseFon SET ClaseFondo=@pmClaseFondo,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClasePol @pmIdClase VARCHAR(4),@pmClasePoliza VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClasePol SET ClasePoliza=@pmClasePoliza,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseRie @pmIdClase VARCHAR(4),@pmClaseRiesgo VARCHAR(50),@pmTarifa DECIMAL(10,4),@pmInactivo BIT
 AS 
 UPDATE ClaseRie SET ClaseRiesgo=@pmClaseRiesgo,Tarifa=@pmTarifa,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseSub @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseSub SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClaseTar @pmIdClase VARCHAR(4),@pmNomClase VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ClaseTar SET NomClase=@pmNomClase,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpClieContado @pmIdCliente VARCHAR(16),@pmNombre VARCHAR(150),@pmTipoId CHAR(1),@pmDv CHAR(1)
,@pmDireccion VARCHAR(150),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmemail VARCHAR(100)
,@pmExcento BIT,@pmPosIniNom INT,@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE ClieContado SET Nombre=@pmNombre,TipoId=@pmTipoId,Dv=@pmDv,Direccion=@pmDireccion,IdLocal=@pmIdLocal,Telefono=@pmTelefono
,email=@pmemail,Excento=@pmExcento,PosIniNom=@pmPosIniNom,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE IdCliente=@pmIdCliente
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCnxextna @pmIdReg INT,@pmCnxEngine VARCHAR(10),@pmCnxDso VARCHAR(250),@pmCnxDbname VARCHAR(50),@pmCnxAutentic VARCHAR(3)
,@pmCnxUid VARCHAR(50),@pmCnxPwd VARCHAR(50),@pmCnxOwner VARCHAR(20),@pmCnxObserv VARCHAR(250),@pmCnxTipo VARCHAR(10),@pmCnxPpties VARCHAR(250)
,@pmCnxDriver VARCHAR(100),@pmCnxPort VARCHAR(30)
 AS 
 UPDATE Sys_Cnxextna SET CnxEngine=@pmCnxEngine,CnxDso=@pmCnxDso,CnxDbname=@pmCnxDbname,CnxAutentic=@pmCnxAutentic,CnxUid=@pmCnxUid,CnxPwd=@pmCnxPwd
,CnxOwner=@pmCnxOwner,CnxObserv=@pmCnxObserv,CnxTipo=@pmCnxTipo,CnxPpties=@pmCnxPpties,CnxDriver=@pmCnxDriver,CnxPort=@pmCnxPort
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComDetalle @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmIdCuenta VARCHAR(16),@pmDetalle VARCHAR(250),@pmVrDebito MONEY,@pmVrCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmVehPropio BIT,@pmVrBase MONEY,@pmTarifaBase DECIMAL(10,4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmCodConce VARCHAR(4),@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15)
,@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmFecVence SMALLDATETIME,@pmCodCta VARCHAR(4),@pmNumCheque VARCHAR(20),@pmIntegrado BIT,@pmTipoAplica CHAR(1),@pmConsolida BIT,@pmCodCargo VARCHAR(4),@pmNitOtros VARCHAR(16),@pmCodSubgpo VARCHAR(8),@pmCiuOrigen VARCHAR(8),@pmCodAgncia VARCHAR(16),@pmReferncia VARCHAR(50),@pmNitDoc VARCHAR(16),@pmTipDocRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_ComDetalle SET Fecha=@pmFecha,IdCuenta=@pmIdCuenta,Detalle=@pmDetalle,VrDebito=@pmVrDebito,VrCredito=@pmVrCredito,IdTercero=@pmIdTercero,CodAgncia=@pmCodAgncia,IdVehiculo=@pmIdVehiculo,IdCCosto=@pmIdCCosto,VehPropio=@pmVehPropio,VrBase=@pmVrBase,TarifaBase=@pmTarifaBase,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,CodConce=@pmCodConce,TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,ItemFac=@pmItemFac,FecVence=@pmFecVence,CodCta=@pmCodCta,NumCheque=@pmNumCheque
,IdSubCos=@pmIdSubCos,Integrado=@pmIntegrado,TipoAplica=@pmTipoAplica,Consolida=@pmConsolida,CodCargo=@pmCodCargo,NitOtros=@pmNitOtros,CodSubgpo=@pmCodSubgpo,CiuOrigen=@pmCiuOrigen,Referncia=@pmReferncia,NitDoc=@pmNitDoc,TipDocRef=@pmTipDocRef,DocRef=@pmDocRef,IdCiaRef=@pmIdCiaRef,FecUpdate=@pmFecUpdate
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComDetallePla @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2)
,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmNumCheque VARCHAR(20),@pmItemFac INT,@pmCodCargo VARCHAR(4)
,@pmIdCuenta VARCHAR(16),@pmVrDebito MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmCodAgncia VARCHAR(16)
,@pmReferncia VARCHAR(50),@pmFecUpdate SMALLDATETIME,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
 AS 
 UPDATE Trn_ComDetalle SET IdCuenta=@pmIdCuenta,VrDebito=@pmVrDebito,IdTercero=@pmIdTercero,CodAgncia=@pmCodAgncia,IdVehiculo=@pmIdVehiculo
,Referncia=@pmReferncia,FecUpdate=@pmFecUpdate,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
 AND TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCiaDoc=@pmIdCiaDoc 
 AND NumCheque=@pmNumCheque AND ItemFac=@pmItemFac AND CodCargo=@pmCodCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComFactura @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCia CHAR(2),@pmItem INT,@pmIdProveedor VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecEmision SMALLDATETIME,@pmFecVence SMALLDATETIME
,@pmVrFactura MONEY,@pmVrAbonado MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmItemCom INT,@pmReferencia VARCHAR(50),@pmDetalle VARCHAR(100),@pmpVehiculo VARCHAR(10),@pmVehPropio BIT
,@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmEstadoApr INT
 AS 
 UPDATE Trn_ComFactura SET IdCuenta=@pmIdCuenta,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,FecEmision=@pmFecEmision,FecVence=@pmFecVence,VrFactura=@pmVrFactura,VrAbonado=@pmVrAbonado,TipCom=@pmTipCom,Comprobante=@pmComprobante,ItemCom=@pmItemCom
,Referencia=@pmReferencia,Detalle=@pmDetalle,pVehiculo=@pmpVehiculo,VehPropio=@pmVehPropio,TipRef=@pmTipRef,DocRef=@pmDocRef,IdCiaRef=@pmIdCiaRef,EstadoApr=@pmEstadoApr
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura AND IdCia=@pmIdCia AND Item=@pmItem AND IdProveedor=@pmIdProveedor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComFacturaAbo @pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCia CHAR(2),@pmItem INT,@pmIdProveedor VARCHAR(16),@pmVrAbonado MONEY
AS
 UPDATE   Trn_ComFactura SET VrAbonado=@pmVrAbonado
 WHERE TipFac=@pmTipFac AND Factura=@pmFactura AND IdCia=@pmIdCia AND Item=@pmItem
 AND IdProveedor=@pmIdProveedor

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComFacturaApl @pmIdProveedor VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null,@pmTipFac VARCHAR(3)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
  UPDATE  Trn_ComFactura  SET Trn_ComFactura.VrAbonado=(SELECT ISNULL(SUM(Trn_EgrFactura.VrAbono),0)
  FROM Trn_EgrFactura WHERE Trn_ComFactura.TipFac=Trn_EgrFactura.TipFac AND Trn_ComFactura.Factura=Trn_EgrFactura.Factura AND Trn_ComFactura.IdCia=Trn_EgrFactura.IdCiaFac
  AND Trn_ComFactura.Item=Trn_EgrFactura.ItemFac AND Trn_ComFactura.IdProveedor=Trn_EgrFactura.IdProveedor)
  WHERE FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND  IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND  TipFac LIKE ISNULL(@pmTipFac,'%') 
 ELSE
  UPDATE  Trn_ComFactura  SET Trn_ComFactura.VrAbonado=(SELECT ISNULL(SUM(Trn_EgrFactura.VrAbono),0)
  FROM Trn_EgrFactura WHERE Trn_ComFactura.TipFac=Trn_EgrFactura.TipFac AND Trn_ComFactura.Factura=Trn_EgrFactura.Factura AND Trn_ComFactura.IdCia=Trn_EgrFactura.IdCiaFac
  AND Trn_ComFactura.Item=Trn_EgrFactura.ItemFac AND Trn_ComFactura.IdProveedor=Trn_EgrFactura.IdProveedor)
  WHERE IdProveedor LIKE ISNULL(@pmIdProveedor,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')
  AND  TipFac LIKE ISNULL(@pmTipFac,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComFacturaCero @pmIdProveedor VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdCuenta VARCHAR(16)=Null
,@pmTipFac VARCHAR(3)=Null,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null

AS
IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
 UPDATE  Trn_ComFactura  SET  VrAbonado=0
 WHERE FecEmision BETWEEN @pmFechaIni AND @pmFechaFin AND  IdProveedor LIKE ISNULL(@pmIdProveedor,'%') 
  AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')  AND  TipFac LIKE ISNULL(@pmTipFac,'%')
 ELSE
 UPDATE  Trn_ComFactura  SET  VrAbonado=0
 WHERE IdProveedor LIKE ISNULL(@pmIdProveedor,'%')   AND IdCia LIKE ISNULL(@pmIdCia,'%%') 
 AND IdCuenta LIKE ISNULL(@pmIdCuenta,'%')  AND  TipFac LIKE ISNULL(@pmTipFac,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCompCaja @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmVrEfectivo MONEY,@pmVrOtros MONEY,@pmVrTotal MONEY,@pmVrTotDebito MONEY
,@pmVrTotCredito MONEY,@pmEnEfectivo BIT,@pmCdCta VARCHAR(4),@pmIdTercero VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmNumForma VARCHAR(20)
,@pmCdBanco VARCHAR(4),@pmReferencia VARCHAR(50),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME,@pmNumVehiculo VARCHAR(10),@pmFacCalidad INT,@pmSemInicial INT,@pmSemFinal INT
 AS 
 UPDATE Trn_CompCaja SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,VrEfectivo=@pmVrEfectivo,VrOtros=@pmVrOtros,VrTotal=@pmVrTotal,VrTotDebito=@pmVrTotDebito,VrTotCredito=@pmVrTotCredito,EnEfectivo=@pmEnEfectivo,CdCta=@pmCdCta
,IdTercero=@pmIdTercero,CdAgencia=@pmCdAgencia,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdCajero=@pmIdCajero,NumForma=@pmNumForma,CdBanco=@pmCdBanco,Referencia=@pmReferencia,TipCom=@pmTipCom
,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,NumVehiculo=@pmNumVehiculo
,FacCalidad=@pmFacCalidad,SemInicial=@pmSemInicial,SemFinal=@pmSemFinal
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCompCajaDev @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_CompCaja SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpCompGastos @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdPlantilla VARCHAR(4),@pmIdTercero VARCHAR(16),@pmTipFact VARCHAR(3)
,@pmFactura VARCHAR(15),@pmFecFactura SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmVrTotal MONEY,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_CompGastos SET Fecha=@pmFecha,IdPlantilla=@pmIdPlantilla,IdTercero=@pmIdTercero,TipFact=@pmTipFact,Factura=@pmFactura,FecFactura=@pmFecFactura,FecVence=@pmFecVence,VrTotal=@pmVrTotal
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpCompanias @pmIdCia CHAR(2),@pmCompania VARCHAR(50),@pmDirInterface VARCHAR(255),@pmFechaActual SMALLDATETIME,@pmPeriodoActual SMALLDATETIME,@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmIdTercero VARCHAR(16),@pmCodSucsal VARCHAR(10),@pmDireccSuc VARCHAR(250),@pmTelefSuc VARCHAR(20),@pmFaxSuc VARCHAR(20),@pmIdLocal VARCHAR(8)
,@pmTipoEDS VARCHAR(10),@pmCodPlanta VARCHAR(20),@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT
 AS 
 UPDATE Companias SET Compania=@pmCompania,DirInterface=@pmDirInterface,FechaActual=@pmFechaActual,PeriodoActual=@pmPeriodoActual,IdCCosto=@pmIdCCosto
,IdSubCos=@pmIdSubCos,IdTercero=@pmIdTercero,CodSucsal=@pmCodSucsal,DireccSuc=@pmDireccSuc,TelefSuc=@pmTelefSuc,FaxSuc=@pmFaxSuc, IdLocal=@pmIdLocal,TipoEDS=@pmTipoEDS
,CodPlanta=@pmCodPlanta,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo
 WHERE IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCompaniasFec @pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmConta BIT
AS
IF @pmConta<>0 
  UPDATE Companias SET PeriodoActual=@pmFecha  WHERE IdCia=@pmIdCia
ELSE
 UPDATE Companias SET FechaActual=@pmFecha  WHERE IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComprobantes @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrTotal MONEY,@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmFecCheque SMALLDATETIME,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmTipEgr VARCHAR(3),@pmNEgreso INT,@pmpVehiculo VARCHAR(10),@pmVehPropio BIT,@pmCedCondtor VARCHAR(16),@pmCodConce VARCHAR(4),@pmBeneficiario VARCHAR(150),@pmVrDivisa1 MONEY,@pmVrDivisa2 MONEY,@pmVrDivisa3 MONEY,@pmPermEditar BIT,@pmIntegrado BIT,@pmEsEgreso BIT,@pmAnticipo BIT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmPropddes VARCHAR(50),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Comprobantes SET Fecha=@pmFecha,IdTercero=@pmIdTercero,VrTotal=@pmVrTotal,IdCta=@pmIdCta,EnEfectivo=@pmEnEfectivo,NumCheque=@pmNumCheque,FecCheque=@pmFecCheque,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,TipEgr=@pmTipEgr,NEgreso=@pmNEgreso,pVehiculo=@pmpVehiculo,VehPropio=@pmVehPropio
,CedCondtor=@pmCedCondtor,CodConce=@pmCodConce,Beneficiario=@pmBeneficiario,PermEditar=@pmPermEditar,Integrado=@pmIntegrado,EsEgreso=@pmEsEgreso,Anticipo=@pmAnticipo,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Propddes=@pmPropddes
,VrDivisa1=@pmVrDivisa1,VrDivisa2=@pmVrDivisa2,VrDivisa3=@pmVrDivisa3,FecUpdate=@pmFecUpdate
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpComprobantesAnu @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2), @pmAnulado BIT, @pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmIdEstado VARCHAR(4), @pmPermEditar BIT,@pmObservacion VARCHAR(250)
AS
 UPDATE  Trn_Comprobantes  SET  Anulado=@pmAnulado, NumDev=@pmNumDev,FecDev=@pmFecDev,IdEstado=@pmIdEstado,Observacion=@pmObservacion,PermEditar=@pmPermEditar
 WHERE TipCom=@pmTipCom AND  Comprobante=@pmComprobante AND  IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConcDivCue @pmIdConcepto VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmCdCueDev VARCHAR(16)
,@pmTipoMov CHAR(1),@pmTipoTerc CHAR(1),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE ConcDivCue SET IdCuenta=@pmIdCuenta,CdCueDev=@pmCdCueDev,TipoMov=@pmTipoMov,TipoTerc=@pmTipoTerc,FecUpdate=@pmFecUpdate
 WHERE IdConcepto=@pmIdConcepto AND IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConcDiversos @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmGrupo VARCHAR(20),@pmEsServicio BIT
,@pmDeManfsto BIT,@pmCdIva VARCHAR(4),@pmCdRet VARCHAR(4)
,@pmCdIca VARCHAR(4),@pmIdCueDeb VARCHAR(16),@pmIdCueCre VARCHAR(16),@pmInactivo BIT,@pmCdAutoret VARCHAR(4),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE ConcDiversos SET Concepto=@pmConcepto,Grupo=@pmGrupo,EsServicio=@pmEsServicio,DeManfsto=@pmDeManfsto,CdIva=@pmCdIva
,CdRet=@pmCdRet,CdIca=@pmCdIca,Inactivo=@pmInactivo,IdCueDeb=@pmIdCueDeb,IdCueCre=@pmIdCueCre,CdAutoret=@pmCdAutoret,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConceptos @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmIdDoc VARCHAR(3),@pmIdCom VARCHAR(3),@pmVcmto BIT,@pmImptos BIT,@pmRetfte BIT
,@pmRetIca BIT,@pmObservacion VARCHAR(150),@pmPdterminado INT,@pmTrfaSanc DECIMAL(14,4),@pmRndTotal INT,@pmDcmtoEquiv BIT,@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Conceptos SET Concepto=@pmConcepto,IdDoc=@pmIdDoc,IdCom=@pmIdCom,Vcmto=@pmVcmto,Imptos=@pmImptos,Retfte=@pmRetfte,RetIca=@pmRetIca,Observacion=@pmObservacion
,Pdterminado=@pmPdterminado,TrfaSanc=@pmTrfaSanc,RndTotal=@pmRndTotal,Inactivo=@pmInactivo,DcmtoEquiv=@pmDcmtoEquiv,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpConceptosAdmon @pmIdConcepto VARCHAR(4),@pmIdAdmon VARCHAR(4)
,@pmIdClase INT,@pmIdCuenta VARCHAR(16),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE ConceptosAdmon SET IdCuenta=@pmIdCuenta,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto AND IdAdmon=@pmIdAdmon AND IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConceptosCue @pmIdConcepto VARCHAR(4),@pmIdClase INT,@pmDescripcion VARCHAR(50),@pmIdCuenta VARCHAR(16)
,@pmTipoMov CHAR(1),@pmDetalleMov VARCHAR(150),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipoTerc CHAR(1),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE ConceptosCue SET Descripcion=@pmDescripcion,IdCuenta=@pmIdCuenta,TipoMov=@pmTipoMov,DetalleMov=@pmDetalleMov
,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,FecUpdate=@pmFecUpdate,TipoTerc=@pmTipoTerc
 WHERE IdConcepto=@pmIdConcepto AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConceptosPre  @pmIdConcepto VARCHAR(4),@pmPdterminado INT,@pmIdDoc VARCHAR(3)
 AS 
 UPDATE Conceptos SET Pdterminado=0
 WHERE IdConcepto<>@pmIdConcepto  AND  Pdterminado=@pmPdterminado  AND IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConsigna @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmVrEfectivo MONEY,@pmVrOtros MONEY
,@pmVrTotal MONEY,@pmEnEfectivo BIT,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmIdCliente VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11)
,@pmNitDeptante VARCHAR(16),@pmDepositante VARCHAR(150),@pmTelDeptante VARCHAR(20),@pmReferencia VARCHAR(50),@pmReferncia2 VARCHAR(50),@pmNumConsigna VARCHAR(20)
,@pmRangosDoc VARCHAR(100),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Consigna SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,VrEfectivo=@pmVrEfectivo,VrOtros=@pmVrOtros,VrTotal=@pmVrTotal,EnEfectivo=@pmEnEfectivo,IdCta=@pmIdCta,IdCuenta=@pmIdCuenta
,IdCliente=@pmIdCliente,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdCajero=@pmIdCajero,NitDeptante=@pmNitDeptante,Depositante=@pmDepositante,TelDeptante=@pmTelDeptante,Referencia=@pmReferencia
,Referncia2=@pmReferncia2,NumConsigna=@pmNumConsigna,RangosDoc=@pmRangosDoc,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpConsignaDev @pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Consigna SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Consignacion=@pmConsignacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCotizacion @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4)
,@pmDirContacto VARCHAR(250),@pmIdLocal VARCHAR(8),@pmDiasVigencia INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4)
,@pmDetallePago VARCHAR(250),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmModalidad VARCHAR(10)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Cotizacion SET Fecha=@pmFecha,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrFletes=@pmVrFletes
,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,DirContacto=@pmDirContacto,IdLocal=@pmIdLocal,DiasVigencia=@pmDiasVigencia,NitContac=@pmNitContac
,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargoContac=@pmCargoContac,IdForma=@pmIdForma,DetallePago=@pmDetallePago,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CdMney=@pmCdMney,TipFac=@pmTipFac,Factura=@pmFactura
,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact,Modalidad=@pmModalidad,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCotizacionAbr @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Cotizacion SET TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCotizacionAnu @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Cotizacion SET Anulado=@pmAnulado,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCotizacionApr @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2)
,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Cotizacion SET Observacion=@pmObservacion,IdEstado=@pmIdEstado,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCotizacionFac @pmTipDoc VARCHAR(3),@pmCotizacion INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Cotizacion SET  TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac
,FechaFact=@pmFechaFact,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Cotizacion=@pmCotizacion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCtasCorrientes @pmIdCta VARCHAR(4),@pmNumeroCta VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmConsCheques BIT,@pmRangoCons VARCHAR(20)
,@pmConsActual INT,@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT,@pmNumCopias INT,@pmInactivo BIT,@pmIdCiaDef CHAR(2)
 AS 
 UPDATE CtasCorrientes SET NumeroCta=@pmNumeroCta,IdBanco=@pmIdBanco,IdCuenta=@pmIdCuenta,ConsCheques=@pmConsCheques,RangoCons=@pmRangoCons,ConsActual=@pmConsActual,Formato=@pmFormato
,TipoPapel=@pmTipoPapel,Orientacion=@pmOrientacion,VistaPrevia=@pmVistaPrevia,VerSetup=@pmVerSetup,NumCopias=@pmNumCopias,Inactivo=@pmInactivo,IdCiaDef=@pmIdCiaDef
 WHERE IdCta=@pmIdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpCtasCorrientesChe @pmIdCta VARCHAR(4),@pmConsActual INT=Null
AS
 IF @pmConsActual =Null
  UPDATE  CtasCorrientes SET ConsActual=(ConsActual+1)  WHERE  IdCta=@pmIdCta
 ELSE
  UPDATE  CtasCorrientes SET ConsActual=@pmConsActual  WHERE  IdCta=@pmIdCta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDepartamentos @pmIdDep VARCHAR(8),@pmDepartamento VARCHAR(50)
,@pmCodigo VARCHAR(4),@pmIdPais VARCHAR(8),@pmInactivo BIT
 AS 
 UPDATE Departamentos SET Departamento=@pmDepartamento,Codigo=@pmCodigo,Inactivo=@pmInactivo
 ,IdPais=@pmIdPais
 WHERE IdDep=@pmIdDep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDependencias @pmIdDep VARCHAR(4),@pmDependencia VARCHAR(50),@pmJefeDep VARCHAR(50),@pmIdArea VARCHAR(4)
,@pmTipRiesgo DECIMAL(10,4),@pmIdSubCos VARCHAR(16),@pmInactivo BIT
 AS 
 UPDATE Dependencias SET Dependencia=@pmDependencia,JefeDep=@pmJefeDep,IdArea=@pmIdArea,TipRiesgo=@pmTipRiesgo
,Inactivo=@pmInactivo,IdSubCos=@pmIdSubCos
 WHERE IdDep=@pmIdDep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpDespachos @pmTipDoc VARCHAR(3),@pmCartulina INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdRuta VARCHAR(4),@pmNumOrden INT
,@pmFechaRec SMALLDATETIME,@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmIdConductor1 VARCHAR(16),@pmIdRelevador VARCHAR(16)
,@pmVehPropio BIT,@pmCentInicial INT,@pmCentFinal INT,@pmNPasajeros INT,@pmRecorridos INT,@pmValorProducido MONEY,@pmValorGastos MONEY,@pmValorAhorro MONEY
,@pmValorOtros MONEY,@pmValorVales MONEY,@pmValorAbono MONEY,@pmValorPasaje MONEY,@pmTipoAdmon VARCHAR(10),@pmIdTipRec VARCHAR(3),@pmReciboCaja INT,@pmIdCiaRec CHAR(2)
,@pmNumAutoriza VARCHAR(30),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Despachos SET Fecha=@pmFecha,IdRuta=@pmIdRuta,NumOrden=@pmNumOrden,FechaRec=@pmFechaRec,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor
,IdConductor1=@pmIdConductor1,IdRelevador=@pmIdRelevador,VehPropio=@pmVehPropio,CentInicial=@pmCentInicial,CentFinal=@pmCentFinal,NPasajeros=@pmNPasajeros,Recorridos=@pmRecorridos
,ValorProducido=@pmValorProducido,ValorGastos=@pmValorGastos,ValorAhorro=@pmValorAhorro,ValorOtros=@pmValorOtros,ValorVales=@pmValorVales,ValorAbono=@pmValorAbono
,ValorPasaje=@pmValorPasaje,TipoAdmon=@pmTipoAdmon,IdTipRec=@pmIdTipRec,ReciboCaja=@pmReciboCaja,IdCiaRec=@pmIdCiaRec,NumAutoriza=@pmNumAutoriza,Anulado=@pmAnulado
,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Cartulina=@pmCartulina AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpDespachosAnu @pmTipDoc VARCHAR(3),@pmCartulina INT,@pmIdCia CHAR(2)
,@pmAnulado BIT, @pmNumDev INT, @pmFecDev SMALLDATETIME, @pmObservacion VARCHAR(250), @pmIdEstado VARCHAR(4)
AS
 UPDATE Trn_Despachos  SET  Anulado=@pmAnulado, NumDev=@pmNumDev, FecDev=@pmFecDev, Observacion=@pmObservacion, IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Cartulina=@pmCartulina AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpDespachosRec @pmTipDoc VARCHAR(3),@pmCartulina INT,@pmIdCia CHAR(2),@pmCentInicial INT, @pmCentFinal INT,@pmNPasajeros INT, @pmRecorridos INT, @pmValorProducido MONEY, @pmValorGastos MONEY, @pmValorAhorro MONEY, @pmValorOtros MONEY, @pmValorVales MONEY
, @pmValorAbono MONEY, @pmValorPasaje MONEY, @pmIdTipRec VARCHAR(3), @pmReciboCaja INT, @pmIdCiaRec CHAR(2),@pmIdEstado VARCHAR(4),@pmIdRuta VARCHAR(4),@pmIdRelevador VARCHAR(16), @pmFecUpdate SMALLDATETIME

AS
 UPDATE Trn_Despachos  SET  CentInicial=@pmCentInicial, CentFinal=@pmCentFinal, NPasajeros=@pmNPasajeros, Recorridos=@pmRecorridos, ValorProducido=@pmValorProducido, ValorGastos=@pmValorGastos, ValorAhorro=@pmValorAhorro, ValorOtros=@pmValorOtros, ValorVales=@pmValorVales
, ValorAbono=@pmValorAbono, ValorPasaje=@pmValorPasaje, IdTipRec=@pmIdTipRec, ReciboCaja=@pmReciboCaja, IdCiaRec=@pmIdCiaRec,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,IdRuta=@pmIdRuta,IdRelevador=@pmIdRelevador
WHERE TipDoc=@pmTipDoc AND Cartulina=@pmCartulina AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevCaj @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmVrEfectivo MONEY
,@pmVrOtros MONEY,@pmVrTotal MONEY,@pmVrTotDebito MONEY,@pmVrTotCredito MONEY,@pmIdTercero VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmEnEfectivo BIT,@pmCdCta VARCHAR(4)
,@pmNumForma VARCHAR(20),@pmCdBanco VARCHAR(4),@pmReferencia VARCHAR(50),@pmNumAutoriza INT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmNumVehiculo VARCHAR(10),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevCaj SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Recibo=@pmRecibo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,VrEfectivo=@pmVrEfectivo,VrOtros=@pmVrOtros,VrTotal=@pmVrTotal,VrTotDebito=@pmVrTotDebito
,VrTotCredito=@pmVrTotCredito,IdTercero=@pmIdTercero,IdAgencia=@pmIdAgencia,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdCajero=@pmIdCajero,EnEfectivo=@pmEnEfectivo,CdCta=@pmCdCta,NumForma=@pmNumForma,CdBanco=@pmCdBanco
,Referencia=@pmReferencia,NumAutoriza=@pmNumAutoriza,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,NumVehiculo=@pmNumVehiculo
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpDevCausacion @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorIva MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY
,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorFletes MONEY,@pmValorNeto MONEY,@pmValorCxc MONEY,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmFactura VARCHAR(15),@pmIdProveedor VARCHAR(16),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_DevCausacion SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,IdPropietario=@pmIdPropietario,VehPropio=@pmVehPropio,ValorTotal=@pmValorTotal,ValorDcto=@pmValorDcto,ValorIva=@pmValorIva,ValorReteFte=@pmValorReteFte,ValorReteIva=@pmValorReteIva,ValorReteIca=@pmValorReteIca,ValorOtros=@pmValorOtros,ValorFletes=@pmValorFletes,ValorNeto=@pmValorNeto
,ValorCxc=@pmValorCxc,IdPlazo=@pmIdPlazo,CxPagar=@pmCxPagar,Factura=@pmFactura,IdProveedor=@pmIdProveedor,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevCns @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmConsignacion INT,@pmIdCiaDoc CHAR(2)
,@pmFecDoc SMALLDATETIME,@pmVrEfectivo MONEY,@pmVrOtros MONEY,@pmVrTotal MONEY,@pmEnEfectivo BIT,@pmIdCta VARCHAR(4),@pmIdCuenta VARCHAR(16),@pmIdCliente VARCHAR(16),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmReferencia VARCHAR(50),@pmReferncia2 VARCHAR(50),@pmNumConsigna VARCHAR(20),@pmModdDev VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevCns SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Consignacion=@pmConsignacion,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,VrEfectivo=@pmVrEfectivo,VrOtros=@pmVrOtros
,VrTotal=@pmVrTotal,EnEfectivo=@pmEnEfectivo,IdCta=@pmIdCta,IdCuenta=@pmIdCuenta,IdCliente=@pmIdCliente,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdCajero=@pmIdCajero,Referencia=@pmReferencia
,Referncia2=@pmReferncia2,NumConsigna=@pmNumConsigna,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevEnt @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY
,@pmVrImpGlobal MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmCdCCosto VARCHAR(16)
,@pmCdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmFletesCosto BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevEnt SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Entrada=@pmEntrada,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdProv=@pmIdProv,Factura=@pmFactura,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento
,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto
,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,NitFletes=@pmNitFletes,CxPagar=@pmCxPagar,DocEquiv=@pmDocEquiv,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,Modalidad=@pmModalidad,ModdDev=@pmModdDev
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,VrRetFlete=@pmVrRetFlete,VrIcaFlete=@pmVrIcaFlete,FletesCosto=@pmFletesCosto,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevFco @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmnClieCon VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4)
,@pmCantPuntos DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmPedido INT,@pmIdCiaPed CHAR(2),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFecPedido SMALLDATETIME,@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmNumAutoriza INT,@pmPlacaVehic VARCHAR(10),@pmKmtVehic INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevFco SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Factura=@pmFactura,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,nClieCon=@pmnClieCon,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion
,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,VrEfectivo=@pmVrEfectivo,VrPagosOtr=@pmVrPagosOtr,Cantidad=@pmCantidad,CantPuntos=@pmCantPuntos,BaseImp=@pmBaseImp
,BaseRet=@pmBaseRet,IdCajero=@pmIdCajero,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,NitCliente=@pmNitCliente,CdAgencia=@pmCdAgencia,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdLocal=@pmIdLocal,EnEfectivo=@pmEnEfectivo,PlacaVehic=@pmPlacaVehic,KmtVehic=@pmKmtVehic
,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,Remision=@pmRemision,IdCiaRem=@pmIdCiaRem,FecPedido=@pmFecPedido,Modalidad=@pmModalidad,ModdDev=@pmModdDev,NumAutoriza=@pmNumAutoriza,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevFcr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY
,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrFaltantes MONEY,@pmVrAnticipos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4)
,@pmCantPuntos DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdLocEnv VARCHAR(8),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmCotizacion INT
,@pmIdCiaCot CHAR(2),@pmFecPedido SMALLDATETIME,@pmModalidad VARCHAR(10),@pmPlacaVehic VARCHAR(10),@pmKmtVehic INT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevFcr SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Factura=@pmFactura,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA
,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrCostos=@pmVrCostos,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrFaltantes=@pmVrFaltantes,VrAnticipos=@pmVrAnticipos,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,CantPuntos=@pmCantPuntos,BaseImp=@pmBaseImp
,BaseRet=@pmBaseRet,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdLocEnv=@pmIdLocEnv,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,Remision=@pmRemision,IdCiaRem=@pmIdCiaRem,Cotizacion=@pmCotizacion,IdCiaCot=@pmIdCiaCot
,FecPedido=@pmFecPedido,Modalidad=@pmModalidad,ModdDev=@pmModdDev,PlacaVehic=@pmPlacaVehic,KmtVehic=@pmKmtVehic,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,ZonaFrontera=@pmZonaFrontera,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpDevFvc @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_DevFvc SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Factura=@pmFactura,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA
,VrReteIVA=@pmVrReteIVA,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,ModdDev=@pmModdDev
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevNpr @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmNumNota INT
,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY,@pmVrCostos MONEY,@pmCantEnt DECIMAL(14,4),@pmCantSal DECIMAL(14,4),@pmNumOrden INT
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevNpr SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,NumNota=@pmNumNota,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdTercero=@pmIdTercero,VrSubTotal=@pmVrSubTotal
,VrCostos=@pmVrCostos,CantEnt=@pmCantEnt,CantSal=@pmCantSal,NumOrden=@pmNumOrden,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Modalidad=@pmModalidad,ModdDev=@pmModdDev
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevPas @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmNitTercero VARCHAR(16),@pmCdJornada VARCHAR(4),@pmVrSubTotal MONEY,@pmVrSobretasa MONEY
,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmModdDev VARCHAR(10),@pmNumAutoriza INT
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevPas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,nNota=@pmnNota,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,NitTercero=@pmNitTercero,CdJornada=@pmCdJornada,VrSubTotal=@pmVrSubTotal,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrOtros=@pmVrOtros,VrCostos=@pmVrCostos
,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,IdCajero=@pmIdCajero,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Referencia=@pmReferencia,ModdDev=@pmModdDev,NumAutoriza=@pmNumAutoriza,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevPla @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmNitTercero VARCHAR(16),@pmIdJornada VARCHAR(4),@pmVrSubTotal MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY
,@pmVrOtros MONEY,@pmVrSobrantes MONEY,@pmVrFaltantes MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrCreditos MONEY,@pmVrGastComb MONEY,@pmVrGastOtros MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4),@pmVrConTotal MONEY,@pmVrConImp MONEY,@pmVrConDcto MONEY,@pmVrConCos MONEY,@pmConCant DECIMAL(14,4)
,@pmVrCreTotal MONEY,@pmVrCreImp MONEY,@pmVrCreDcto MONEY,@pmVrCreCos MONEY,@pmCreCant DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitRef VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmModalidad VARCHAR(10)
,@pmFecCorte SMALLDATETIME,@pmNumCorte INT,@pmModdDev VARCHAR(10),@pmNumAutoriza INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevPla SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Planilla=@pmPlanilla,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,NitTercero=@pmNitTercero,IdJornada=@pmIdJornada,VrSubTotal=@pmVrSubTotal,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrOtros=@pmVrOtros,VrSobrantes=@pmVrSobrantes,VrFaltantes=@pmVrFaltantes
,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,VrEfectivo=@pmVrEfectivo,VrCreditos=@pmVrCreditos,VrGastComb=@pmVrGastComb,VrGastOtros=@pmVrGastOtros,VrPagosOtr=@pmVrPagosOtr,Cantidad=@pmCantidad,VrConTotal=@pmVrConTotal,VrConImp=@pmVrConImp,VrConDcto=@pmVrConDcto,VrConCos=@pmVrConCos,ConCant=@pmConCant,VrCreTotal=@pmVrCreTotal
,VrCreImp=@pmVrCreImp,VrCreDcto=@pmVrCreDcto,VrCreCos=@pmVrCreCos,CreCant=@pmCreCant,IdCajero=@pmIdCajero,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,NitRef=@pmNitRef,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Referencia=@pmReferencia,Modalidad=@pmModalidad,FecCorte=@pmFecCorte
,NumCorte=@pmNumCorte,ModdDev=@pmModdDev,NumAutoriza=@pmNumAutoriza,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpDevPrestamos @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmPrestamo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFechaPrest SMALLDATETIME,@pmTipoPlazo VARCHAR(10),@pmNumCuotas INT,@pmTipoCuota VARCHAR(10)
,@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY,@pmTasaInteres DECIMAL(14,4),@pmCxPagar BIT,@pmFactura VARCHAR(20),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_DevPrestamos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Prestamo=@pmPrestamo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,FechaPrest=@pmFechaPrest,TipoPlazo=@pmTipoPlazo,NumCuotas=@pmNumCuotas,TipoCuota=@pmTipoCuota,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor
,VehPropio=@pmVehPropio,ValorTotal=@pmValorTotal,TasaInteres=@pmTasaInteres,CxPagar=@pmCxPagar,Factura=@pmFactura,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrDescuento MONEY
,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrAhorro MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmNumAutoriza INT,@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevRec SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Recibo=@pmRecibo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,FecPago=@pmFecPago,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros
,VrOtrDcto=@pmVrOtrDcto,VrAhorro=@pmVrAhorro,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrAnticipo=@pmVrAnticipo,VrEfectivo=@pmVrEfectivo,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdCajero=@pmIdCajero,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdLocal=@pmIdLocal
,EnEfectivo=@pmEnEfectivo,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,Modalidad=@pmModalidad,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,NumAutoriza=@pmNumAutoriza,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpDevRecBuses @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10)
,@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmModalidad VARCHAR(10),@pmValorRecibido MONEY,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorOtrosDct MONEY,@pmValorPagMas MONEY,@pmValorAhorro MONEY,@pmValorOConce MONEY
,@pmValorNeto MONEY,@pmValorAplicado MONEY,@pmValorAnticipo MONEY,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmIdTarCom VARCHAR(4),@pmIdCta VARCHAR(4),@pmEnEfectivo BIT,@pmNumCheque VARCHAR(20),@pmIdBanco VARCHAR(4),@pmEsParcial BIT,@pmIdClase VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_DevRecBuses SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Recibo=@pmRecibo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,VehPropio=@pmVehPropio,Modalidad=@pmModalidad,ValorRecibido=@pmValorRecibido
,ValorTotal=@pmValorTotal,ValorDcto=@pmValorDcto,ValorReteFte=@pmValorReteFte,ValorReteIva=@pmValorReteIva,ValorReteIca=@pmValorReteIca,ValorOtros=@pmValorOtros,ValorOtrosDct=@pmValorOtrosDct,ValorPagMas=@pmValorPagMas,ValorAhorro=@pmValorAhorro,ValorOConce=@pmValorOConce,ValorNeto=@pmValorNeto,ValorAplicado=@pmValorAplicado
,ValorAnticipo=@pmValorAnticipo,IdVendedor=@pmIdVendedor,Comision=@pmComision,IdTarCom=@pmIdTarCom,IdCta=@pmIdCta,EnEfectivo=@pmEnEfectivo,NumCheque=@pmNumCheque,IdBanco=@pmIdBanco,EsParcial=@pmEsParcial,IdClase=@pmIdClase,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevSal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCiaDoc CHAR(2)
,@pmFecDoc SMALLDATETIME,@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmBaseImp MONEY,@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16),@pmTipOrd VARCHAR(3),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmpVehiculo VARCHAR(10)
,@pmNContrato INT,@pmIdCiaCont CHAR(2),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmModRequis VARCHAR(10),@pmModalidad VARCHAR(10),@pmModdDev VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevSal SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Salida=@pmSalida,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdTercero=@pmIdTercero,VrSubTotal=@pmVrSubTotal,VrImpuesto=@pmVrImpuesto
,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,BaseImp=@pmBaseImp,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,TipOrd=@pmTipOrd,NumOrden=@pmNumOrden,IdCiaOrd=@pmIdCiaOrd,pVehiculo=@pmpVehiculo
,NContrato=@pmNContrato,IdCiaCont=@pmIdCiaCont,NitCliente=@pmNitCliente,CdAgencia=@pmCdAgencia,ModRequis=@pmModRequis
,Modalidad=@pmModalidad,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDevVal @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdTercero VARCHAR(16)
,@pmTotalPagos MONEY,@pmTotalCobros MONEY,@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmVrDescuento MONEY,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_DevVal SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,nRadica=@pmnRadica,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdTercero=@pmIdTercero,TotalPagos=@pmTotalPagos,TotalCobros=@pmTotalCobros,Referencia=@pmReferencia
,CxPagar=@pmCxPagar,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,VrDescuento=@pmVrDescuento,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate 
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiarios @pmIdDiario VARCHAR(4),@pmDiario VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Diarios SET Diario=@pmDiario,Inactivo=@pmInactivo
 WHERE IdDiario=@pmIdDiario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiferidos @pmIdDiferido VARCHAR(30),@pmNomDiferido VARCHAR(250),@pmIdTipDif VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdDep VARCHAR(4),@pmIdRespons VARCHAR(16),@pmIdCia CHAR(2),@pmFecInicio SMALLDATETIME,@pmValorTotal MONEY,@pmVrAdiciones MONEY,@pmVrDisminucion MONEY,@pmNumPeriodos INT
,@pmFecFinAmtza SMALLDATETIME,@pmAjustable BIT,@pmContable BIT,@pmVrAjuInfAcum MONEY,@pmVrAmtzaAcuHis MONEY,@pmVrAmtzaAcum MONEY,@pmVrAjuInfAmtza MONEY,@pmCuotasAmtza INT
,@pmVrCorrMonDifAcu MONEY,@pmCorrMonDiferida MONEY,@pmNumMesesDif INT,@pmFecUltAmtza SMALLDATETIME,@pmCueEnGrupo BIT,@pmIdCuenta VARCHAR(16),@pmIdCueAmortiza VARCHAR(16)
,@pmIdCueAjuDif VARCHAR(16),@pmIdCueCorrDif VARCHAR(16),@pmIdCueAjuAmtza VARCHAR(16),@pmIdCueCorrAA VARCHAR(16),@pmNumDoc VARCHAR(20),@pmReferencia VARCHAR(50)
,@pmDesUbicacion VARCHAR(150),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME,@pmIdCausal VARCHAR(4),@pmComentRetiro VARCHAR(250),@pmComentarios VARCHAR(250)
,@pmTipoAmtza CHAR(1),@pmNitCliente VARCHAR(16),@pmAgenCliente VARCHAR(16),@pmCantGalones DECIMAL (14,4),@pmIdProv VARCHAR(16),@pmNumPlaca  VARCHAR(20),@pmCodInstala VARCHAR(4)
 AS 
 UPDATE Diferidos SET NomDiferido=@pmNomDiferido,IdTipDif=@pmIdTipDif,IdGrupo=@pmIdGrupo,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdDep=@pmIdDep
,IdRespons=@pmIdRespons,IdCia=@pmIdCia,FecInicio=@pmFecInicio,ValorTotal=@pmValorTotal,VrAdiciones=@pmVrAdiciones,VrDisminucion=@pmVrDisminucion
,NumPeriodos=@pmNumPeriodos,FecFinAmtza=@pmFecFinAmtza,Ajustable=@pmAjustable,Contable=@pmContable,VrAjuInfAcum=@pmVrAjuInfAcum,VrAmtzaAcuHis=@pmVrAmtzaAcuHis
,VrAmtzaAcum=@pmVrAmtzaAcum,VrAjuInfAmtza=@pmVrAjuInfAmtza,CuotasAmtza=@pmCuotasAmtza,VrCorrMonDifAcu=@pmVrCorrMonDifAcu,CorrMonDiferida=@pmCorrMonDiferida,NumMesesDif=@pmNumMesesDif
,FecUltAmtza=@pmFecUltAmtza,CueEnGrupo=@pmCueEnGrupo,IdCuenta=@pmIdCuenta,IdCueAmortiza=@pmIdCueAmortiza,IdCueAjuDif=@pmIdCueAjuDif,IdCueCorrDif=@pmIdCueCorrDif,IdCueAjuAmtza=@pmIdCueAjuAmtza
,IdCueCorrAA=@pmIdCueCorrAA,NumDoc=@pmNumDoc,Referencia=@pmReferencia,DesUbicacion=@pmDesUbicacion,IdEstado=@pmIdEstado,FecRetiro=@pmFecRetiro,IdCausal=@pmIdCausal
,ComentRetiro=@pmComentRetiro,Comentarios=@pmComentarios,TipoAmtza=@pmTipoAmtza,NitCliente=@pmNitCliente,AgenCliente=@pmAgenCliente,CantGalones=@pmCantGalones,IdProv=@pmIdProv
,NumPlaca=@pmNumPlaca,CodInstala=@pmCodInstala
 WHERE IdDiferido=@pmIdDiferido

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiferidosAcu @pmIdDiferido VARCHAR(30),@pmVrAjuInfAcum MONEY,@pmVrAmtzaAcuHis MONEY,@pmVrAmtzaAcum MONEY
,@pmVrAjuInfAmtza MONEY,@pmCuotasAmtza INT,@pmVrCorrMonDifAcu MONEY
 AS 
 UPDATE Diferidos SET VrAjuInfAcum=@pmVrAjuInfAcum,VrAmtzaAcuHis=@pmVrAmtzaAcuHis,VrAmtzaAcum=@pmVrAmtzaAcum
,VrAjuInfAmtza=@pmVrAjuInfAmtza,CuotasAmtza=@pmCuotasAmtza,VrCorrMonDifAcu=@pmVrCorrMonDifAcu
 WHERE IdDiferido=@pmIdDiferido

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiferidosDis @pmIdDiferido VARCHAR(30),@pmVrDisminucion MONEY,@pmAuto BIT
 AS 
 IF @pmAuto<>0 
  UPDATE Diferidos SET  VrDisminucion=(VrDisminucion+@pmVrDisminucion)  WHERE IdDiferido=@pmIdDiferido
 ELSE
  UPDATE Diferidos SET  VrDisminucion=@pmVrDisminucion  WHERE IdDiferido=@pmIdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiferidosMej @pmIdDiferido VARCHAR(30),@pmVrAdiciones MONEY,@pmAuto BIT
 AS 
 IF @pmAuto<>0 
  UPDATE Diferidos SET  VrAdiciones=(VrAdiciones+@pmVrAdiciones)  WHERE IdDiferido=@pmIdDiferido
 ELSE
  UPDATE Diferidos SET  VrAdiciones=@pmVrAdiciones  WHERE IdDiferido=@pmIdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDiferidosRet @pmIdDiferido VARCHAR(30),@pmIdEstado VARCHAR(4),@pmFecRetiro SMALLDATETIME,@pmIdCausal VARCHAR(4)
,@pmComentRetiro VARCHAR(250)
 AS 
 UPDATE Diferidos SET IdEstado=@pmIdEstado,FecRetiro=@pmFecRetiro,IdCausal=@pmIdCausal
,ComentRetiro=@pmComentRetiro 
 WHERE IdDiferido=@pmIdDiferido
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpDisciplinario  @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecSuceso SMALLDATETIME,@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16)
,@pmIdRuta VARCHAR(4),@pmLugar VARCHAR(150),@pmNomReporta VARCHAR(150),@pmCedReporta VARCHAR(16),@pmVehiculoRep VARCHAR(10),@pmRepCond BIT,@pmCoordinador VARCHAR(150),@pmDespachador VARCHAR(150),@pmCitaDescargos BIT
,@pmFecCita SMALLDATETIME,@pmMemoCita VARCHAR(50),@pmImputacion VARCHAR(2500),@pmDescargos VARCHAR(3000),@pmFecDesc SMALLDATETIME,@pmRecibeDesc VARCHAR(150),@pmTipoSancion VARCHAR(10),@pmDescSancion VARCHAR(1000)
,@pmFecSancion SMALLDATETIME,@pmCantSancion DECIMAL(14,4),@pmCodInfracc INT,@pmReferencia VARCHAR(50),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Disciplinario SET Fecha=@pmFecha,FecSuceso=@pmFecSuceso,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor,IdRuta=@pmIdRuta,Lugar=@pmLugar,NomReporta=@pmNomReporta,CedReporta=@pmCedReporta
,VehiculoRep=@pmVehiculoRep,RepCond=@pmRepCond,Coordinador=@pmCoordinador,Despachador=@pmDespachador,CitaDescargos=@pmCitaDescargos,FecCita=@pmFecCita,MemoCita=@pmMemoCita,Imputacion=@pmImputacion,Descargos=@pmDescargos
,FecDesc=@pmFecDesc,RecibeDesc=@pmRecibeDesc,TipoSancion=@pmTipoSancion,DescSancion=@pmDescSancion,FecSancion=@pmFecSancion,CantSancion=@pmCantSancion,CodInfracc=@pmCodInfracc,Referencia=@pmReferencia,Observacion=@pmObservacion
,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDisciplinarioAnu @pmTipDoc VARCHAR(3),@pmNumero INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Disciplinario SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,IdEstado=@pmIdEstado,Observacion=@pmObservacion 
 WHERE TipDoc=@pmTipDoc AND Numero=@pmNumero AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpDocMemo @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmComentarios VARCHAR(6500)
,@pmNota1 VARCHAR(500),@pmNota2 VARCHAR(500),@pmNota3 VARCHAR(500)
 AS 
 UPDATE Trn_DocMemo SET Comentarios=@pmComentarios
,Nota1=@pmNota1,Nota2=@pmNota2,Nota3=@pmNota3
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsCalibra @pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitTercero VARCHAR(16),@pmCdJornada VARCHAR(4),@pmVrSubTotal MONEY
,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_EdsCalibra SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,NitTercero=@pmNitTercero,CdJornada=@pmCdJornada,VrSubTotal=@pmVrSubTotal,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal
,VrOtros=@pmVrOtros,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,IdCajero=@pmIdCajero,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdCCosto=@pmIdCCosto
,IdSubCos=@pmIdSubCos,Referencia=@pmReferencia,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev
,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND nNota=@pmnNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsCalibraDev @pmTipDoc VARCHAR(3),@pmnNota INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_EdsCalibra SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND nNota=@pmnNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsCortes @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmNitTercero VARCHAR(16),@pmIdJornada VARCHAR(4),@pmVrSubTotal MONEY
,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrOtros MONEY,@pmVrSobrantes MONEY,@pmVrFaltantes MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrCreditos MONEY,@pmVrGastComb MONEY
,@pmVrGastOtros MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4),@pmVrConTotal MONEY,@pmVrConImp MONEY,@pmVrConDcto MONEY,@pmVrConCos MONEY,@pmConCant DECIMAL(14,4),@pmVrCreTotal MONEY,@pmVrCreImp MONEY
,@pmVrCreDcto MONEY,@pmVrCreCos MONEY,@pmCreCant DECIMAL(14,4),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitRef VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmReferencia VARCHAR(50),@pmModalidad VARCHAR(10),@pmFecCorte SMALLDATETIME,@pmNumCorte INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_EdsCortes SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,NitTercero=@pmNitTercero,IdJornada=@pmIdJornada,VrSubTotal=@pmVrSubTotal,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrOtros=@pmVrOtros,VrSobrantes=@pmVrSobrantes
,VrFaltantes=@pmVrFaltantes,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,VrEfectivo=@pmVrEfectivo,VrCreditos=@pmVrCreditos,VrGastComb=@pmVrGastComb,VrGastOtros=@pmVrGastOtros,VrPagosOtr=@pmVrPagosOtr,Cantidad=@pmCantidad,VrConTotal=@pmVrConTotal
,VrConImp=@pmVrConImp,VrConDcto=@pmVrConDcto,VrConCos=@pmVrConCos,ConCant=@pmConCant,VrCreTotal=@pmVrCreTotal,VrCreImp=@pmVrCreImp,VrCreDcto=@pmVrCreDcto,VrCreCos=@pmVrCreCos,CreCant=@pmCreCant,IdCajero=@pmIdCajero,IdVend=@pmIdVend
,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Referencia=@pmReferencia,Modalidad=@pmModalidad,FecCorte=@pmFecCorte,NumCorte=@pmNumCorte,NitRef=@pmNitRef
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsCortesDev @pmTipDoc VARCHAR(3),@pmPlanilla INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_EdsCortes SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsFormas @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmPlanilla INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmVrTotal MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdCuenta VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmCdProducto VARCHAR(16)
,@pmCantidad DECIMAL(14,4),@pmIdForma VARCHAR(4),@pmItem INT,@pmReferencia VARCHAR(50),@pmCantPuntos DECIMAL(14,4),@pmObservacion VARCHAR(150),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmAgencDcm VARCHAR(16),@pmAnulado BIT
,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16)
 AS 
 UPDATE Trn_EdsFormas SET Planilla=@pmPlanilla,IdCia=@pmIdCia,Fecha=@pmFecha,VrTotal=@pmVrTotal,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdCuenta=@pmIdCuenta,pVehiculo=@pmpVehiculo,nVehiculo=@pmnVehiculo,IdCajero=@pmIdCajero,IdVend=@pmIdVend,CdProducto=@pmCdProducto,Cantidad=@pmCantidad,IdForma=@pmIdForma,Item=@pmItem,Referencia=@pmReferencia,CantPuntos=@pmCantPuntos
,Observacion=@pmObservacion,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,FechaDcm=@pmFechaDcm,AgencDcm=@pmAgencDcm,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsFormasAnu @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_EdsFormas SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango 
AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsFormasDfa  @pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2)
 AS 
 UPDATE Trn_EdsFormas SET  TipDcm='0',Documento=0,IdCiaDcm='00'
 WHERE  TipDcm=@pmTipDcm AND Documento=@pmDocumento AND IdCiaDcm=@pmIdCiaDcm
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsFormasFac @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
,@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFechaDcm SMALLDATETIME,@pmAgencDcm VARCHAR(16)
 AS 
 UPDATE Trn_EdsFormas SET TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,FechaDcm=@pmFechaDcm,AgencDcm=@pmAgencDcm
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsFormasPla @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT,@pmPlanilla INT,@pmIdCia CHAR(2),@pmVrTotal MONEY,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmIdCuenta VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmCdProducto VARCHAR(16),@pmCantidad DECIMAL(14,4)
,@pmIdForma VARCHAR(4),@pmReferencia VARCHAR(50),@pmCantPuntos DECIMAL(14,4),@pmObservacion VARCHAR(150),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16)
 AS 
 UPDATE Trn_EdsFormas SET VrTotal=@pmVrTotal,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdCuenta=@pmIdCuenta,pVehiculo=@pmpVehiculo,nVehiculo=@pmnVehiculo,IdCajero=@pmIdCajero
,IdVend=@pmIdVend,CdProducto=@pmCdProducto,Cantidad=@pmCantidad,IdForma=@pmIdForma,Referencia=@pmReferencia,CantPuntos=@pmCantPuntos,Observacion=@pmObservacion,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
 AND  Planilla=@pmPlanilla AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsRangos @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmIdForma VARCHAR(4),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmnVehiculo VARCHAR(10),@pmIdCuenta VARCHAR(16),@pmNumSerie VARCHAR(5),@pmNumInicial INT,@pmNumFinal INT,@pmNumTarjeta VARCHAR(30)
,@pmVrCupo MONEY,@pmVrConsumos MONEY,@pmTipoConsumo INT,@pmReqPlaca BIT,@pmReqProd BIT,@pmReqGalones BIT,@pmVldAgencia BIT,@pmVldPlaca VARCHAR(10),@pmModalidad VARCHAR(10)
,@pmAnulado BIT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmNumAuto BIT,@pmNumActual INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_EdsRangos SET IdForma=@pmIdForma,Fecha=@pmFecha,FecVence=@pmFecVence,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,pVehiculo=@pmpVehiculo,nVehiculo=@pmnVehiculo
,IdCuenta=@pmIdCuenta,NumSerie=@pmNumSerie,NumInicial=@pmNumInicial,NumFinal=@pmNumFinal,NumTarjeta=@pmNumTarjeta,VrCupo=@pmVrCupo,VrConsumos=@pmVrConsumos,TipoConsumo=@pmTipoConsumo
,ReqPlaca=@pmReqPlaca,ReqProd=@pmReqProd,ReqGalones=@pmReqGalones,VldAgencia=@pmVldAgencia,VldPlaca=@pmVldPlaca,Modalidad=@pmModalidad,Anulado=@pmAnulado,Observacion=@pmObservacion
,IdEstado=@pmIdEstado,NumAuto=@pmNumAuto,NumActual=@pmNumActual,FecUpdate=@pmFecUpdate
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEdsRangosCon @pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumActual INT
 AS 
 UPDATE Trn_EdsRangos SET  NumActual=@pmNumActual
 WHERE TipoRango=@pmTipoRango AND IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpEdsServicio @pmIdEstacion VARCHAR(4),@pmEstServicio VARCHAR(50),@pmDireccion VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE EdsServicio SET EstServicio=@pmEstServicio,Direccion=@pmDireccion,Inactivo=@pmInactivo
 WHERE IdEstacion=@pmIdEstacion


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEgrFactura @pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmTipFac VARCHAR(3),@pmFactura VARCHAR(15),@pmIdCiaFac CHAR(2),@pmItemFac INT,@pmIdProveedor VARCHAR(16),@pmIdCuenta VARCHAR(16)
,@pmVrAbono MONEY,@pmEsEgreso BIT,@pmItemCom INT,@pmpVehiculo VARCHAR(10),@pmVehPropio BIT,@pmTipDev VARCHAR(3),@pmNumDev INT,@pmDetAbono VARCHAR(100)
 AS 
 UPDATE Trn_EgrFactura SET Fecha=@pmFecha,TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,ItemFac=@pmItemFac,IdProveedor=@pmIdProveedor,IdCuenta=@pmIdCuenta,VrAbono=@pmVrAbono,EsEgreso=@pmEsEgreso
,ItemCom=@pmItemCom,pVehiculo=@pmpVehiculo,VehPropio=@pmVehPropio,TipDev=@pmTipDev,NumDev=@pmNumDev,DetAbono=@pmDetAbono
 WHERE TipCom=@pmTipCom AND Comprobante=@pmComprobante AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_Contrato @pmNContrato INT,@pmIdEmpleado VARCHAR(16),@pmIdTipCon VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmIndefinido BIT,@pmIdCargo VARCHAR(4),@pmIdJornada VARCHAR(4),@pmIdDep VARCHAR(4),@pmIdArea VARCHAR(4),@pmIdInstala VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdCia CHAR(2),@pmIdNom VARCHAR(4),@pmTipoLiquida CHAR(1),@pmModPromdio CHAR(1),@pmVrSalario MONEY,@pmVrAuxTrans MONEY,@pmSalMinimo BIT,@pmSalIntegral BIT,@pmFPnomina VARCHAR(10),@pmModoRet INT,@pmFecAfiSeg SMALLDATETIME,@pmATFinMes BIT,@pmSegFinMes BIT,@pmDenyPEN BIT,@pmDenyEPS BIT,@pmDenyARP BIT,@pmDenyCaj BIT,@pmDenyCes BIT,@pmDenyPri BIT
,@pmDenyVac BIT,@pmDenyHex BIT,@pmBasePrest CHAR(2),@pmBaseSeg CHAR(2),@pmDchDotacion BIT,@pmDchCafeteria BIT,@pmReclmto VARCHAR(50),@pmNotas VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmIdClase VARCHAR(4),@pmTipoTrabj INT,@pmIdCot VARCHAR(4),@pmVrUpc MONEY,@pmIdGrupo VARCHAR(4),@pmDenyDcto BIT,@pmSubTipoCot VARCHAR(3),@pmDsoSabado BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Emp_Contrato SET IdEmpleado=@pmIdEmpleado,IdTipCon=@pmIdTipCon,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia,FecRetiro=@pmFecRetiro,Indefinido=@pmIndefinido,IdCargo=@pmIdCargo,IdJornada=@pmIdJornada,IdDep=@pmIdDep,IdArea=@pmIdArea,IdInstala=@pmIdInstala,IdCCosto=@pmIdCCosto,IdCia=@pmIdCia,IdNom=@pmIdNom,TipoLiquida=@pmTipoLiquida,ModPromdio=@pmModPromdio
,VrSalario=@pmVrSalario,VrAuxTrans=@pmVrAuxTrans,SalMinimo=@pmSalMinimo,SalIntegral=@pmSalIntegral,FPnomina=@pmFPnomina,ModoRet=@pmModoRet,FecAfiSeg=@pmFecAfiSeg,ATFinMes=@pmATFinMes,SegFinMes=@pmSegFinMes,DenyPEN=@pmDenyPEN,DenyEPS=@pmDenyEPS,DenyARP=@pmDenyARP,DenyCaj=@pmDenyCaj,DenyCes=@pmDenyCes,DenyPri=@pmDenyPri,DenyVac=@pmDenyVac,DenyHex=@pmDenyHex
,BasePrest=@pmBasePrest,BaseSeg=@pmBaseSeg,DchDotacion=@pmDchDotacion,DchCafeteria=@pmDchCafeteria,Reclmto=@pmReclmto,Notas=@pmNotas,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,IdSubCos=@pmIdSubCos,IdClase=@pmIdClase,TipoTrabj=@pmTipoTrabj ,IdCot=@pmIdCot,VrUpc=@pmVrUpc,IdGrupo=@pmIdGrupo,DenyDcto=@pmDenyDcto,SubTipoCot =@pmSubTipoCot,DsoSabado=@pmDsoSabado
 WHERE NContrato=@pmNContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ContratoAre @pmIdArea VARCHAR(4),@pmIdDep VARCHAR(4)
 AS 
 UPDATE Emp_Contrato SET IdArea=@pmIdArea
 WHERE IdDep=@pmIdDep

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ContratoAum @pmNContrato INT,@pmIdEmpleado VARCHAR(16),@pmVrSalario MONEY,@pmVrAuxTrans MONEY,@pmSalMinimo BIT
,@pmFecUltAum SMALLDATETIME,@pmFecUpdate SMALLDATETIME
 AS 
 BEGIN
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnContrato'
 BEGIN TRANSACTION @TrnNombre
 UPDATE Emp_Contrato SET VrSalario=@pmVrSalario,VrAuxTrans=@pmVrAuxTrans,SalMinimo=@pmSalMinimo,FecUpdate=@pmFecUpdate
 WHERE NContrato=@pmNContrato AND IdEmpleado=@pmIdEmpleado
 UPDATE Empleados SET FecUltAum=@pmFecUltAum WHERE  IdEmpleado=@pmIdEmpleado
 COMMIT TRANSACTION  @TrnNombre
 END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ContratoCc  @pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
 AS 
 UPDATE Emp_Contrato SET IdCCosto=@pmIdCCosto
 WHERE IdSubCos=@pmIdSubCos

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ContratoIns @pmNContrato INT,@pmIdEmpleado VARCHAR(16),@pmIdInstala VARCHAR(4),@pmIdClase VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Emp_Contrato SET IdInstala=@pmIdInstala,IdClase=@pmIdClase,FecUpdate=@pmFecUpdate
 WHERE NContrato=@pmNContrato AND IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ContratoRet @pmNContrato INT,@pmFecRetiro SMALLDATETIME,@pmIdEstado VARCHAR(4)
,@pmInactivo BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Emp_Contrato SET FecRetiro=@pmFecRetiro,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate
 WHERE NContrato=@pmNContrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ContratoTlq @pmIdNom VARCHAR(4),@pmTipoLiquida CHAR(1)
 AS 
 UPDATE Emp_Contrato SET TipoLiquida=@pmTipoLiquida
 WHERE IdNom=@pmIdNom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_Estudios @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmEstblecmto VARCHAR(50)
,@pmTitulo VARCHAR(50),@pmFecha SMALLDATETIME
 AS 
 UPDATE Emp_Estudios SET Estblecmto=@pmEstblecmto,Titulo=@pmTitulo,Fecha=@pmFecha
 WHERE IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_ExpLaboral @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmEmpresa VARCHAR(50),@pmCargo VARCHAR(20)
,@pmFecInicio SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmDirEmpresa VARCHAR(100),@pmTelEmpresa VARCHAR(20)
 AS 
 UPDATE Emp_ExpLaboral SET Empresa=@pmEmpresa,Cargo=@pmCargo,FecInicio=@pmFecInicio,FecRetiro=@pmFecRetiro,DirEmpresa=@pmDirEmpresa,TelEmpresa=@pmTelEmpresa
 WHERE IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmp_Parientes @pmIdEmpleado VARCHAR(16),@pmItem INT,@pmParntsco VARCHAR(10),@pmNombre VARCHAR(50)
,@pmFecNacmto SMALLDATETIME,@pmUltGrado VARCHAR(20),@pmDirccion VARCHAR(100),@pmTelefno VARCHAR(20)
 AS 
 UPDATE Emp_Parientes SET Parntsco=@pmParntsco,Nombre=@pmNombre,FecNacmto=@pmFecNacmto,UltGrado=@pmUltGrado,Dirccion=@pmDirccion,Telefno=@pmTelefno
 WHERE IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpaques @pmIdEmp VARCHAR(4),@pmEmpaque VARCHAR(50)
,@pmCodMintra VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Empaques SET Empaque=@pmEmpaque,CodMintra=@pmCodMintra,Inactivo=@pmInactivo
 WHERE IdEmp=@pmIdEmp
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleados @pmIdEmpleado VARCHAR(16),@pmApellidos VARCHAR(80),@pmNombres VARCHAR(70),@pmCodigo VARCHAR(16),@pmTipoId CHAR(1),@pmDv CHAR(1),@pmIdLugarCed VARCHAR(8),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmTelMovil VARCHAR(20),@pme_mail VARCHAR(100),@pmVvdaPropia BIT,@pmFecNacmto SMALLDATETIME,@pmLugarNacmto VARCHAR(30),@pmTipoSangre CHAR(2),@pmFactorRh CHAR(1),@pmSexo CHAR(1)
,@pmIdEstCivil VARCHAR(4),@pmNHijos INT,@pmLibMilitar VARCHAR(25),@pmClaseLib INT,@pmFechaLib SMALLDATETIME,@pmDistMil VARCHAR(10),@pmCertJudicial VARCHAR(25),@pmFecJudicial SMALLDATETIME,@pmVigJudicial SMALLDATETIME,@pmPasporte VARCHAR(25),@pmVigPaspte SMALLDATETIME,@pmLicencia VARCHAR(25),@pmIdLugar VARCHAR(8),@pmCatLicencia INT,@pmFecLicencia SMALLDATETIME,@pmVigLicencia SMALLDATETIME,@pmIdProf VARCHAR(4),@pmTallaCam CHAR(2)
,@pmTallaPan CHAR(2),@pmTallaZap CHAR(2),@pmUsaGafas BIT,@pmConyugue VARCHAR(50),@pmConyFecNac SMALLDATETIME,@pmConyTrabaja BIT,@pmConyEmpresa VARCHAR(50),@pmConyCargo VARCHAR(20),@pmConyFing SMALLDATETIME,@pmConyTel VARCHAR(20),@pmIdFonPen VARCHAR(8),@pmIdFonEps VARCHAR(8),@pmIdFonArp VARCHAR(8),@pmIdFonCes VARCHAR(8),@pmIdCajaCom VARCHAR(8),@pmCdFonAvp VARCHAR(8),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(25),@pmIdBanco VARCHAR(4),@pmNContrato INT,@pmFecVincula SMALLDATETIME
,@pmFecUltAum SMALLDATETIME,@pmFecUltCes SMALLDATETIME,@pmFecUltPri SMALLDATETIME,@pmFecUltVac SMALLDATETIME,@pmFecUltIng SMALLDATETIME,@pmFecUltRet SMALLDATETIME,@pmFecUltDot SMALLDATETIME,@pmFecUltExm SMALLDATETIME,@pmFecUltCap SMALLDATETIME,@pmNCAnterior INT,@pmFecIngAnt SMALLDATETIME,@pmFecRetAnt SMALLDATETIME,@pmFotoEmp VARCHAR(30),@pmFotoLic VARCHAR(30),@pmFotoFirma VARCHAR(30),@pmEsCondtor BIT,@pmpVehiculo VARCHAR(10)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmCdLocCue VARCHAR(8),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Empleados SET Apellidos=@pmApellidos,Nombres=@pmNombres,Codigo=@pmCodigo,TipoId=@pmTipoId,Dv=@pmDv,IdLugarCed=@pmIdLugarCed,Direccion=@pmDireccion,IdLocal=@pmIdLocal,Telefono=@pmTelefono,TelMovil=@pmTelMovil,e_mail=@pme_mail,VvdaPropia=@pmVvdaPropia,FecNacmto=@pmFecNacmto,LugarNacmto=@pmLugarNacmto,TipoSangre=@pmTipoSangre,FactorRh=@pmFactorRh,Sexo=@pmSexo,IdEstCivil=@pmIdEstCivil,NHijos=@pmNHijos,LibMilitar=@pmLibMilitar,ClaseLib=@pmClaseLib,FechaLib=@pmFechaLib,DistMil=@pmDistMil
,CertJudicial=@pmCertJudicial,FecJudicial=@pmFecJudicial,VigJudicial=@pmVigJudicial,Pasporte=@pmPasporte,VigPaspte=@pmVigPaspte,Licencia=@pmLicencia,IdLugar=@pmIdLugar,CatLicencia=@pmCatLicencia,FecLicencia=@pmFecLicencia,VigLicencia=@pmVigLicencia,IdProf=@pmIdProf,TallaCam=@pmTallaCam,TallaPan=@pmTallaPan,TallaZap=@pmTallaZap,UsaGafas=@pmUsaGafas,Conyugue=@pmConyugue,ConyFecNac=@pmConyFecNac,ConyTrabaja=@pmConyTrabaja,ConyEmpresa=@pmConyEmpresa,ConyCargo=@pmConyCargo,FecVincula=@pmFecVincula
,ConyFing=@pmConyFing,ConyTel=@pmConyTel,IdFonPen=@pmIdFonPen,IdFonEps=@pmIdFonEps,IdFonArp=@pmIdFonArp,IdFonCes=@pmIdFonCes,IdCajaCom=@pmIdCajaCom,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,NContrato=@pmNContrato,FecUltAum=@pmFecUltAum,FecUltCes=@pmFecUltCes,FecUltPri=@pmFecUltPri,FecUltVac=@pmFecUltVac,FecUltIng=@pmFecUltIng,FecUltRet=@pmFecUltRet,FecUltDot=@pmFecUltDot,FecUltExm=@pmFecUltExm,FecUltCap=@pmFecUltCap,FotoEmp=@pmFotoEmp,CdFonAvp=@pmCdFonAvp
,NCAnterior=@pmNCAnterior, FotoLic=@pmFotoLic,FotoFirma=@pmFotoFirma,pVehiculo=@pmpVehiculo,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,EsCondtor=@pmEsCondtor,FecIngAnt=@pmFecIngAnt,FecRetAnt=@pmFecRetAnt,CdLocCue=@pmCdLocCue,FechaUpdate=@pmFechaUpdate
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleadosCon @pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmFecUltIng SMALLDATETIME,@pmFecUltRet SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmFecVincula SMALLDATETIME,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Empleados SET NContrato=@pmNContrato,FecUltIng=@pmFecUltIng,FecUltRet=@pmFecUltRet
,IdEstado=@pmIdEstado,FecVincula=@pmFecVincula,FechaUpdate=@pmFechaUpdate
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleadosFon @pmIdEmpleado VARCHAR(16),@pmTipo VARCHAR(3),@pmIdFondo VARCHAR(8),@pmFechaUpdate SMALLDATETIME
AS
IF @pmTipo='PEN'
 UPDATE Empleados SET IdFonPen=@pmIdFondo,FechaUpdate=@pmFechaUpdate WHERE IdEmpleado=@pmIdEmpleado
IF @pmTipo='EPS'
 UPDATE Empleados SET IdFonEps=@pmIdFondo,FechaUpdate=@pmFechaUpdate WHERE IdEmpleado=@pmIdEmpleado
IF @pmTipo='ARP' or @pmTipo='APR'
 UPDATE Empleados SET IdFonArp=@pmIdFondo,FechaUpdate=@pmFechaUpdate WHERE IdEmpleado=@pmIdEmpleado
IF @pmTipo='CES'
 UPDATE Empleados SET IdFonCes=@pmIdFondo,FechaUpdate=@pmFechaUpdate WHERE IdEmpleado=@pmIdEmpleado
IF @pmTipo='COM'
 UPDATE Empleados SET IdCajaCom=@pmIdFondo,FechaUpdate=@pmFechaUpdate WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleadosFul @pmIdEmpleado VARCHAR(16),@pmFecha SMALLDATETIME,@pmTipo VARCHAR(3)
 AS 
 IF @pmTipo='VAC'
  UPDATE Empleados SET FecUltVac=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='PRI'
  UPDATE Empleados SET FecUltPri=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='CES'
  UPDATE Empleados SET FecUltCes=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='SAL'
  UPDATE Empleados SET FecUltAum=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='DOT'
  UPDATE Empleados SET FecUltDot=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='EXM'
  UPDATE Empleados SET FecUltExm=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='CAP'
  UPDATE Empleados SET FecUltCap=@pmFecha  WHERE IdEmpleado=@pmIdEmpleado
 IF @pmTipo='RET'
   UPDATE Empleados SET FecUltCes=@pmFecha,FecUltPri=@pmFecha,FecUltVac=@pmFecha WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleadosRet @pmIdEmpleado VARCHAR(16),@pmFecUltRet SMALLDATETIME,@pmNCAnterior INT,@pmFecIngAnt SMALLDATETIME
,@pmFecRetAnt SMALLDATETIME,@pmFecUltCes SMALLDATETIME,@pmFecUltPri SMALLDATETIME,@pmFecUltVac SMALLDATETIME
 AS 
 UPDATE Empleados SET FecUltRet=@pmFecUltRet,NCAnterior=@pmNCAnterior,FecIngAnt=@pmFecIngAnt,FecRetAnt=@pmFecRetAnt
,FecUltCes=@pmFecUltCes,FecUltPri=@pmFecUltPri,FecUltVac=@pmFecUltVac,Inactivo=1,IdEstado='9999'
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleadosRint @pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmFecUltIng SMALLDATETIME,@pmFecUltRet SMALLDATETIME
,@pmNCAnterior INT,@pmFecIngAnt SMALLDATETIME,@pmFecRetAnt SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmFecUltCes SMALLDATETIME
,@pmFecUltPri SMALLDATETIME,@pmFecUltVac SMALLDATETIME,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Empleados SET NContrato=@pmNContrato,FecUltIng=@pmFecUltIng,FecUltRet=@pmFecUltRet,NCAnterior=@pmNCAnterior,FecIngAnt=@pmFecIngAnt
,FecRetAnt=@pmFecRetAnt,IdEstado=@pmIdEstado,FecUltCes=@pmFecUltCes,FecUltPri=@pmFecUltPri,FecUltVac=@pmFecUltVac,FechaUpdate=@pmFechaUpdate
,Inactivo=0
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEmpleadosVeh @pmIdEmpleado VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Empleados SET pVehiculo=@pmpVehiculo,FechaUpdate=@pmFechaUpdate
 WHERE IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntArticulosDve @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmSalidas DECIMAL(14,4)
 AS 
 UPDATE Trn_EntArticulos SET Salidas=Salidas+@pmSalidas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntArticulosDvs @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmIdProducto VARCHAR(16),@pmSalidas DECIMAL(14,4)
 AS 
 UPDATE Trn_EntArticulos SET Salidas=Salidas-@pmSalidas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
  AND Item=@pmItem AND IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntArticulosDvsp @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
,@pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4),@pmSalidas DECIMAL(14,4)
 AS 
 UPDATE Trn_EntArticulos SET Salidas=Salidas-@pmSalidas
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
  AND IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntArticulosSal @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmIdProducto VARCHAR(16),@pmSalidas DECIMAL(14,4),@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2)
 AS 
 UPDATE Trn_EntArticulos SET Salidas=@pmSalidas,TipSal=@pmTipSal,NumSalida=@pmNumSalida,IdCiaSal=@pmIdCiaSal
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia AND Item=@pmItem
 AND IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntidades @pmIdTabla VARCHAR(20),@pmNivelRel INT,@pmCamposPK VARCHAR(50),@pmFngKey BIT,@pmSp_Qry VARCHAR(30),@pmSp_Qpmt VARCHAR(50),@pmSp_Ins VARCHAR(30)
,@pmSp_Upd VARCHAR(30),@pmSp_Del VARCHAR(30)
 AS 
 UPDATE Sys_Entidades SET NivelRel=@pmNivelRel,CamposPK=@pmCamposPK,FngKey=@pmFngKey,Sp_Qry=@pmSp_Qry,Sp_Qpmt=@pmSp_Qpmt,Sp_Ins=@pmSp_Ins,Sp_Upd=@pmSp_Upd,Sp_Del=@pmSp_Del
 WHERE IdTabla=@pmIdTabla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntradas @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmFactura VARCHAR(15),@pmFechaFac SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY
,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmNitFletes VARCHAR(16),@pmTipOdc VARCHAR(3),@pmOCompra INT,@pmIdCiaOdc CHAR(2)
,@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCxPagar BIT,@pmDocEquiv VARCHAR(3),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4)
,@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmTipEgr VARCHAR(3),@pmEgreso INT,@pmIdCiaEgr CHAR(2),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmVrRetFlete MONEY,@pmVrIcaFlete MONEY,@pmCodTarRetFle VARCHAR(4),@pmCodTarIcaFle VARCHAR(4),@pmFletesCosto BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Entradas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdProv=@pmIdProv,Factura=@pmFactura,FechaFac=@pmFechaFac,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes
,VrOtros=@pmVrOtros,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,NitFletes=@pmNitFletes,TipOdc=@pmTipOdc,OCompra=@pmOCompra,IdCiaOdc=@pmIdCiaOdc,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CxPagar=@pmCxPagar,DocEquiv=@pmDocEquiv
,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,TipEgr=@pmTipEgr
,Egreso=@pmEgreso,IdCiaEgr=@pmIdCiaEgr,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,VrRetFlete=@pmVrRetFlete,VrIcaFlete=@pmVrIcaFlete,CodTarRetFle=@pmCodTarRetFle,CodTarIcaFle=@pmCodTarIcaFle,FletesCosto=@pmFletesCosto,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEntradasDev @pmTipDoc VARCHAR(3),@pmEntrada INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmObservacion VARCHAR(250)
 AS 
 UPDATE Trn_Entradas SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Entrada=@pmEntrada AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstaciones @pmIdEstacion CHAR(2),@pmEstacion VARCHAR(50)
 AS 
 UPDATE Sys_Estaciones SET Estacion=@pmEstacion
 WHERE IdEstacion=@pmIdEstacion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstadoCiv @pmIdEstado VARCHAR(4),@pmEstCivil VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE EstadoCiv SET EstCivil=@pmEstCivil,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstadoDoc @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT
,@pmCodDcmto VARCHAR(3),@pmInactivo BIT
 AS 
 UPDATE EstadoDoc SET Estado=@pmEstado,NColor=@pmNColor,CodDcmto=@pmCodDcmto,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstadoPro @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE EstadoPro SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstadoSer @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE EstadoSer SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstadoTer @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE EstadoTer SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstadoVeh @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmNColor INT,@pmOutDemand BIT,@pmInactivo BIT
 AS 
 UPDATE EstadoVeh SET Estado=@pmEstado,NColor=@pmNColor,OutDemand=@pmOutDemand,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpEstratos @pmIdEstrato VARCHAR(4),@pmEstrato VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Estratos SET Estrato=@pmEstrato,Inactivo=@pmInactivo
 WHERE IdEstrato=@pmIdEstrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpExpLicencias @pmIdLugar VARCHAR(8),@pmLugarLic VARCHAR(50)
,@pmCodDpto VARCHAR(8),@pmDpto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE ExpLicencias SET LugarLic=@pmLugarLic,CodDpto=@pmCodDpto,Dpto=@pmDpto,Inactivo=@pmInactivo
 WHERE IdLugar=@pmIdLugar
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFactcon @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmnClieCon VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY
,@pmVrReteIVA MONEY,@pmVrFletes MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmVrEfectivo MONEY,@pmVrPagosOtr MONEY,@pmCantidad DECIMAL(14,4),@pmCantPuntos DECIMAL(14,4),@pmPuntosAcum DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY
,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCajero VARCHAR(11)
,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20)
,@pmDetallePago VARCHAR(250),@pmReferncia1 VARCHAR(50),@pmReferncia2 VARCHAR(50),@pmCdBanco VARCHAR(4),@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFecPedido SMALLDATETIME,@pmModalidad VARCHAR(10)
,@pmPlacaVehic VARCHAR(10),@pmKmtVehic INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Factcon SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,nClieCon=@pmnClieCon,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes,VrCargos=@pmVrCargos
,VrOtrDcto=@pmVrOtrDcto,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,VrEfectivo=@pmVrEfectivo,VrPagosOtr=@pmVrPagosOtr,Cantidad=@pmCantidad,CantPuntos=@pmCantPuntos,PuntosAcum=@pmPuntosAcum,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv
,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCajero=@pmIdCajero,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom
,CodTarCom=@pmCodTarCom,NitCliente=@pmNitCliente,CdAgencia=@pmCdAgencia,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdLocal=@pmIdLocal,EnEfectivo=@pmEnEfectivo,CdForma=@pmCdForma,NumForma=@pmNumForma,DetallePago=@pmDetallePago,Referncia1=@pmReferncia1,Referncia2=@pmReferncia2
,TipPed=@pmTipPed,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,TipRem=@pmTipRem,Remision=@pmRemision,IdCiaRem=@pmIdCiaRem,FecPedido=@pmFecPedido,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado
,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,CdBanco=@pmCdBanco,FecUpdate=@pmFecUpdate
,PlacaVehic=@pmPlacaVehic,KmtVehic=@pmKmtVehic
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFactconDev @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
AS
 UPDATE Trn_Factcon SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFacturas @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrFletes MONEY
,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrCostos MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrFaltantes MONEY,@pmVrAnticipos MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmCantidad DECIMAL(14,4),@pmCantPuntos DECIMAL(14,4),@pmPuntosAcum DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4)
,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250),@pmIdLocEnv VARCHAR(8)
,@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmpVehiculo VARCHAR(10)
,@pmCdConductor VARCHAR(16),@pmCdRuta VARCHAR(4),@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmTipCot VARCHAR(3),@pmCotizacion INT,@pmIdCiaCot CHAR(2),@pmFecPedido SMALLDATETIME,@pmAutzaMora INT,@pmAutzaCupo INT,@pmModalidad VARCHAR(10),@pmKmtVehic INT
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Facturas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrCostos=@pmVrCostos
,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrFaltantes=@pmVrFaltantes,VrAnticipos=@pmVrAnticipos,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,Cantidad=@pmCantidad,CantPuntos=@pmCantPuntos,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva
,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,DirEnvio=@pmDirEnvio,IdLocEnv=@pmIdLocEnv,DiasEntraga=@pmDiasEntraga,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargoContac=@pmCargoContac
,IdForma=@pmIdForma,DetallePago=@pmDetallePago,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CdMney=@pmCdMney,NitEmpTrans=@pmNitEmpTrans,EmpTrans=@pmEmpTrans,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,CdRuta=@pmCdRuta,TipPed=@pmTipPed,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,TipRem=@pmTipRem,Remision=@pmRemision,IdCiaRem=@pmIdCiaRem,TipCot=@pmTipCot,Cotizacion=@pmCotizacion,IdCiaCot=@pmIdCiaCot
,FecPedido=@pmFecPedido,AutzaMora=@pmAutzaMora,AutzaCupo=@pmAutzaCupo,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,PuntosAcum=@pmPuntosAcum,KmtVehic=@pmKmtVehic,ZonaFrontera=@pmZonaFrontera,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFacturasDev @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Facturas SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFcNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmVrNota MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY
,@pmCdCCosto VARCHAR(16),@pmCdSubCos VARCHAR(16),@pmReferencia VARCHAR(50),@pmTipRec VARCHAR(3),@pmReciboFc INT,@pmIdCiaRec CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FcNotas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor,VehPropio=@pmVehPropio,VrNota=@pmVrNota,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos
,Referencia=@pmReferencia,TipRec=@pmTipRec,ReciboFc=@pmReciboFc,IdCiaRec=@pmIdCiaRec,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFcNotasDev @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_FcNotas SET  Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFcSemanas @pmIdVehiculo VARCHAR(10),@pmnSemana INT,@pmNumCargue INT,@pmCiaCargue CHAR(2),@pmTipoSem INT,@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmVrCargo MONEY,@pmVrOtros MONEY,@pmVrPagado MONEY
,@pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCiaRec CHAR(2),@pmFechaPago SMALLDATETIME,@pmObservacion VARCHAR(250),@pmFecSemIni SMALLDATETIME,@pmFecSemFin SMALLDATETIME
 AS 
 UPDATE Trn_FcSemanas SET NumCargue=@pmNumCargue,CiaCargue=@pmCiaCargue,TipoSem=@pmTipoSem,Fecha=@pmFecha,FecVence=@pmFecVence,VrCargo=@pmVrCargo,VrOtros=@pmVrOtros,VrPagado=@pmVrPagado
,TipRec=@pmTipRec,Recibo=@pmRecibo,IdCiaRec=@pmIdCiaRec,FechaPago=@pmFechaPago,Observacion=@pmObservacion,FecSemIni=@pmFecSemIni,FecSemFin=@pmFecSemFin
 WHERE IdVehiculo=@pmIdVehiculo AND nSemana=@pmnSemana
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFcSemanasAbo  @pmIdVehiculo VARCHAR(10)=Null
 AS 
  UPDATE Trn_FcSemanas SET VrPagado=0  WHERE  IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 
  UPDATE Trn_FcSemanas SET  Trn_FcSemanas.VrPagado=(SELECT ISNULL(SUM(Trn_FcAbonos.VrAbono),0)
   FROM Trn_FcAbonos  WHERE Trn_FcSemanas.IdVehiculo=Trn_FcAbonos.IdVehiculo
   AND Trn_FcSemanas.nSemana=Trn_FcAbonos.nSemana)
  WHERE IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') 



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFields @pmIdEntidad VARCHAR(30),@pmIdCampo VARCHAR(20),@pmDescripcion VARCHAR(50),@pmTipoDato VARCHAR(15),@pmLongMax INT,@pmRequerido BIT,@pmPmryKey BIT,@pmFmtValue VARCHAR(20)
,@pmDftValue VARCHAR(20),@pmAliasFld VARCHAR(20),@pmAliasQry2 VARCHAR(20),@pmIndField INT,@pmTipoEntidad VARCHAR(5),@pmLstTipo VARCHAR(5),@pmLstClnaValor VARCHAR(20),@pmLstClnaLista VARCHAR(50),@pmLstQuery VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE Sys_Fields SET Descripcion=@pmDescripcion,TipoDato=@pmTipoDato,LongMax=@pmLongMax,Requerido=@pmRequerido,PmryKey=@pmPmryKey,FmtValue=@pmFmtValue,DftValue=@pmDftValue,AliasFld=@pmAliasFld,AliasQry2=@pmAliasQry2
,IndField=@pmIndField,TipoEntidad=@pmTipoEntidad,LstTipo=@pmLstTipo,LstClnaValor=@pmLstClnaValor,LstClnaLista=@pmLstClnaLista,LstQuery=@pmLstQuery,Inactivo=@pmInactivo
 WHERE IdEntidad=@pmIdEntidad AND IdCampo=@pmIdCampo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFijos @pmIdCargo VARCHAR(4),@pmConcepto VARCHAR(100), @pmObligatorio BIT, @pmTipoAplica VARCHAR(10),  @pmDiasSemana VARCHAR(20), @pmDiasMes VARCHAR(100)
, @pmValorMensual MONEY, @pmValorDiario MONEY, @pmIdCuentaDeb VARCHAR(16), @pmIdCuentaCre VARCHAR(16), @pmIdCueDebVp VARCHAR(16), @pmIdCueCreVp VARCHAR(16),@pmInactivo BIT
,@pmNCuotas INT,@pmDiasVcmto VARCHAR(50),@pmOrdenAbono INT,@pmTipoDcto CHAR(1),@pmDctoPpago DECIMAL(14,4),@pmVrCargoAdic MONEY,@pmFecLimiteIni SMALLDATETIME,@pmFecLimiteFin SMALLDATETIME,@pmFechaUpdate SMALLDATETIME
AS
 UPDATE  Fijos  SET Concepto=@pmConcepto, Obligatorio=@pmObligatorio, TipoAplica=@pmTipoAplica, DiasSemana=@pmDiasSemana, DiasMes=@pmDiasMes, ValorMensual=@pmValorMensual, ValorDiario=@pmValorDiario, IdCuentaDeb=@pmIdCuentaDeb
, IdCuentaCre=@pmIdCuentaCre, IdCueDebVp=@pmIdCueDebVp, IdCueCreVp=@pmIdCueCreVp
,NCuotas=@pmNCuotas,DiasVcmto=@pmDiasVcmto,OrdenAbono=@pmOrdenAbono,TipoDcto=@pmTipoDcto,DctoPpago=@pmDctoPpago,VrCargoAdic=@pmVrCargoAdic,Inactivo=@pmInactivo,FecLimiteIni=@pmFecLimiteIni,FecLimiteFin=@pmFecLimiteFin,FechaUpdate= @pmFechaUpdate
 WHERE IdCargo=@pmIdCargo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpFijos_Cla @pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4),@pmValorMensual MONEY,@pmValorDiario MONEY

AS
 UPDATE  Fijos_Cla  SET ValorMensual=@pmValorMensual,ValorDiario =@pmValorDiario
 WHERE IdCargo=@pmIdCargo AND IdClase=@pmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpFijos_Cue  @pmIdCargo VARCHAR(4), @pmIdAdmon VARCHAR(4),@pmIdCuentaDeb VARCHAR(16), @pmIdCuentaCre VARCHAR(16)
, @pmIdCueDebVp VARCHAR(16), @pmIdCueCreVp VARCHAR(16)
AS
  UPDATE  Fijos_Cue  SET  IdCuentaDeb=@pmIdCuentaDeb, IdCuentaCre=@pmIdCuentaCre, IdCueDebVp=@pmIdCueDebVp, IdCueCreVp=@pmIdCueCreVp
 WHERE   IdCargo=@pmIdCargo AND  IdAdmon=@pmIdAdmon



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpFijos_Veh @pmIdCargo VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmValorMensual MONEY,@pmValorDiario MONEY,@pmExcluir BIT

AS
 UPDATE Fijos_Veh SET ValorMensual=@pmValorMensual, ValorDiario=@pmValorDiario,  Excluir=@pmExcluir
 WHERE IdCargo=@pmIdCargo AND IdVehiculo=@pmIdVehiculo



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpFinAfavorAbo @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmVrAbono MONEY
 AS 
 UPDATE Trn_FinAfavor SET VrAbono=@pmVrAbono
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpFinAfavorApl @pmIdCliente VARCHAR(16)=Null,@pmIdVend  VARCHAR(16)=Null
 AS 
 UPDATE Trn_FinAfavor SET  Trn_FinAfavor.VrAbono=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE  Trn_FinAfavor.TipDoc=Trn_FinAbonos.TipRec AND Trn_FinAfavor.Recibo=Trn_FinAbonos.Recibo
   AND Trn_FinAfavor.IdCia=Trn_FinAbonos.IdCia AND TipoAplica=2)
  WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpFinAfavorDoc @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinAfavor SET  Trn_FinAfavor.VrAbono=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE  Trn_FinAfavor.TipDoc=Trn_FinAbonos.TipRec AND Trn_FinAfavor.Recibo=Trn_FinAbonos.Recibo
   AND Trn_FinAfavor.IdCia=Trn_FinAbonos.IdCia AND TipoAplica=2)
  WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo  AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpFinAfavorOdb @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmVrAbono MONEY,@pmVrOtrosDb MONEY
 AS 
 UPDATE Trn_FinAfavor  SET VrAbono=@pmVrAbono,VrOtrosDb=@pmVrOtrosDb
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinAlmacenes @pmIdAlmacen VARCHAR(4),@pmAlmacen VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE FinAlmacenes SET Almacen=@pmAlmacen,Inactivo=@pmInactivo
 WHERE IdAlmacen=@pmIdAlmacen

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 UPDATE FinClieSaldos SET VrSaldo=@pmVrSaldo
 WHERE IdClie=@pmIdClie AND CdAgencia=@pmCdAgencia AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinClieSaldosAcu @pmIdClie VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE NAME LIKE '#FinClieSaldos%') 
 DROP TABLE #FinClieSaldos
 CREATE TABLE #FinClieSaldos (tmId INT PRIMARY KEY IDENTITY (1,1)
 ,IdClie VARCHAR(16),CdAgencia VARCHAR(16),IdCia CHAR(2),VrCuotas MONEY,VrAbonos MONEY,VrAFavor MONEY,VrAbonoSAF MONEY)
--cuotas
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrTotal),0,0,0
 FROM Trn_FinCuotas
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
--abonos de cuotas
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,SUM(TotalAbono),0,0
 FROM Trn_FinAbonos
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
--recibos a favor
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,0,SUM(VrAFavor-VrOtrosDb),0
 FROM Trn_FinAfavor
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
 --abonos de saldos a favor
 INSERT INTO #FinClieSaldos (IdClie,CdAgencia,IdCia,VrCuotas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,0,0,SUM(TotalAbono)
 FROM Trn_FinAbonos
 WHERE TipoAplica=2 AND IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia 
 --actualiza saldos
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnSaldos'
 BEGIN TRANSACTION @TrnNombre
 DELETE FROM FinClieSaldos WHERE IdClie LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 INSERT INTO FinClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
 SELECT IdClie,CdAgencia,IdCia,SUM((VrCuotas-VrAbonos)-(VrAFavor-VrAbonoSAF)) 
 FROM #FinClieSaldos
 GROUP BY IdClie,CdAgencia,IdCia
 COMMIT TRANSACTION  @TrnNombre
--DROP TABLE #FinClieSaldos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinClieSaldosLin @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
,@pmIdLinea VARCHAR(4),@pmIdCia CHAR(2),@pmVrSaldoCap MONEY,@pmVrSaldoInt MONEY,@pmVrSaldoOtro MONEY
 AS 
 UPDATE FinClieSaldosLin SET VrSaldoCap=@pmVrSaldoCap,VrSaldoInt=@pmVrSaldoInt,VrSaldoOtro=@pmVrSaldoOtro
 WHERE IdClie=@pmIdClie AND CdAgencia=@pmCdAgencia AND IdLinea=@pmIdLinea AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinConcFactura @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(250),@pmTipoConc VARCHAR(10),@pmGravado BIT,@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4)
,@pmIdCuenta VARCHAR(16),@pmCdCueIva VARCHAR(16),@pmCdCueRet VARCHAR(16),@pmCdCueIca VARCHAR(16),@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE FinConcFactura SET Concepto=@pmConcepto,TipoConc=@pmTipoConc,Gravado=@pmGravado,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,IdCuenta=@pmIdCuenta
,CdCueIva=@pmCdCueIva,CdCueRet=@pmCdCueRet,CdCueIca=@pmCdCueIca,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinCuotasAbo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmItem INT
,@pmVrAbonado MONEY,@pmFecUltPago SMALLDATETIME
 AS 
 UPDATE Trn_FinCuotas SET  VrAbonado=@pmVrAbonado,FecUltPago=@pmFecUltPago
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinCuotasApl  @pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE Trn_FinCuotas SET  Trn_FinCuotas.VrAbonado=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE Trn_FinCuotas.TipDoc=Trn_FinAbonos.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinAbonos.IdPrestamo
   AND Trn_FinCuotas.IdCia=Trn_FinAbonos.IdCiaPre  AND Trn_FinCuotas.Item=Trn_FinAbonos.ItemPre)
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
   AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ELSE
   UPDATE Trn_FinCuotas SET  Trn_FinCuotas.VrAbonado=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
   FROM Trn_FinAbonos  WHERE Trn_FinCuotas.TipDoc=Trn_FinAbonos.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinAbonos.IdPrestamo
   AND Trn_FinCuotas.IdCia=Trn_FinAbonos.IdCiaPre  AND Trn_FinCuotas.Item=Trn_FinAbonos.ItemPre)
   WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinCuotasCero  @pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
 AS 
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE Trn_FinCuotas SET VrAbonado=0
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
   AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 ELSE
   UPDATE Trn_FinCuotas SET VrAbonado=0 WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinDevFac @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmIdVend VARCHAR(16),@pmIdLocal VARCHAR(8),@pmModdDev VARCHAR(10)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinDevFac SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Factura=@pmFactura,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto
,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrNeto=@pmVrNeto,IdVend=@pmIdVend,IdLocal=@pmIdLocal,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpFinDevPre @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdVend VARCHAR(16),@pmCxPagar BIT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinDevPre SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,IdPrestamo=@pmIdPrestamo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrPrestamo=@pmVrPrestamo,IdVend=@pmIdVend,CxPagar=@pmCxPagar
,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpFinDevRec @pmTipDev VARCHAR(3),@pmDevolucion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCiaDoc CHAR(2),@pmFecDoc SMALLDATETIME,@pmFecPago SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY
,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmReferencia VARCHAR(50),@pmNumAutoriza INT,@pmModdDev VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinDevRec SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,TipDoc=@pmTipDoc,Recibo=@pmRecibo,IdCiaDoc=@pmIdCiaDoc,FecDoc=@pmFecDoc,FecPago=@pmFecPago,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado
,VrAnticipo=@pmVrAnticipo,VrEfectivo=@pmVrEfectivo,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,EnEfectivo=@pmEnEfectivo,Referencia=@pmReferencia,NumAutoriza=@pmNumAutoriza,ModdDev=@pmModdDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDev=@pmTipDev AND Devolucion=@pmDevolucion AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinFactint @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4)
,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdLocal VARCHAR(8),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinFactint SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrNeto=@pmVrNeto,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv
,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,IdLocal=@pmIdLocal,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE  paUpFinFactintDev @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev  SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_FinFactint SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
WHERE TipDoc=@pmTipDoc AND Factura=@pmFactura AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpFinNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrSancion MONEY,@pmVrOtros MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4)
,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFecDcm SMALLDATETIME,@pmCdForma VARCHAR(4),@pmNumCheque VARCHAR(20)
,@pmCdBanco VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinNotas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrSancion=@pmVrSancion,VrOtros=@pmVrOtros,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrAnticipo=@pmVrAnticipo,BaseImp=@pmBaseImp,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv
,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,FecDcm=@pmFecDcm,CdForma=@pmCdForma,NumCheque=@pmNumCheque,CdBanco=@pmCdBanco,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,Modalidad=@pmModalidad
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinPrestamo @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPrestamo SMALLDATETIME,@pmFecCorte SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrPrestamo MONEY,@pmIdTasa VARCHAR(4),@pmNPlazos INT,@pmCausacion VARCHAR(10),@pmIdMora VARCHAR(4)
,@pmNContrato INT,@pmCdCiaCon CHAR(2),@pmNActaJunta INT,@pmNumCredito VARCHAR(20),@pmIdVend VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoGarantia VARCHAR(10),@pmVrGarantia MONEY,@pmDescGarantia VARCHAR(250),@pmCxPagar BIT,@pmNumCuotaIni INT,@pmAcumCapital MONEY,@pmAcumIntereses MONEY,@pmAcumIntMora MONEY,@pmAcumCuotas INT,@pmPagosCapital MONEY
,@pmPagosIntereses MONEY,@pmPagosIntMora MONEY,@pmCuotasPagadas INT,@pmCuotasMora INT,@pmTipoAprob VARCHAR(20),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmDiaCausac INT,@pmDiaCausac2 INT,@pmDiasVence VARCHAR(50),@pmIdLinea VARCHAR(4),@pmTipoPlazo CHAR(1),@pmTipoCausac INT,@pmCdTasa2 VARCHAR(4),@pmVrSeguro MONEY,@pmVrIntereses MONEY,@pmVrNeto MONEY,@pmVrCuota MONEY,@pmTipoCredito VARCHAR(10),@pmCalcIntMora BIT,@pmNitEmpConv VARCHAR(16),@pmCuotasConv INT
,@pmRefinanciado INT,@pmNumPrestRef INT,@pmCiaPrestRef CHAR(2),@pmVrRefinanc MONEY,@pmDescRefinanc  VARCHAR(150),@pmCdAlmacen VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinPrestamo SET Fecha=@pmFecha,FecPrestamo=@pmFecPrestamo,FecCorte=@pmFecCorte,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrPrestamo=@pmVrPrestamo,IdTasa=@pmIdTasa,NPlazos=@pmNPlazos,Causacion=@pmCausacion,IdMora=@pmIdMora,NContrato=@pmNContrato,CdCiaCon=@pmCdCiaCon,NActaJunta=@pmNActaJunta,NumCredito=@pmNumCredito,IdVend=@pmIdVend
,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipoGarantia=@pmTipoGarantia,VrGarantia=@pmVrGarantia,DescGarantia=@pmDescGarantia,CxPagar=@pmCxPagar,NumCuotaIni=@pmNumCuotaIni,AcumCapital=@pmAcumCapital,AcumIntereses=@pmAcumIntereses,AcumIntMora=@pmAcumIntMora,AcumCuotas=@pmAcumCuotas,PagosCapital=@pmPagosCapital,PagosIntereses=@pmPagosIntereses,PagosIntMora=@pmPagosIntMora,CuotasPagadas=@pmCuotasPagadas
,CuotasMora=@pmCuotasMora,TipoAprob=@pmTipoAprob,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,CdTasa2=@pmCdTasa2,VrSeguro=@pmVrSeguro,VrIntereses=@pmVrIntereses,VrNeto=@pmVrNeto,VrCuota=@pmVrCuota,FecUpdate=@pmFecUpdate
,DiaCausac=@pmDiaCausac,DiaCausac2=@pmDiaCausac2,DiasVence=@pmDiasVence,IdLinea=@pmIdLinea,TipoPlazo=@pmTipoPlazo,TipoCausac=@pmTipoCausac
,TipoCredito=@pmTipoCredito,CalcIntMora=@pmCalcIntMora,NitEmpConv=@pmNitEmpConv,CuotasConv=@pmCuotasConv
,Refinanciado=@pmRefinanciado,NumPrestRef=@pmNumPrestRef,CiaPrestRef=@pmCiaPrestRef,VrRefinanc=@pmVrRefinanc,DescRefinanc=@pmDescRefinanc,CdAlmacen=@pmCdAlmacen
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinPrestamoAcum  @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinPrestamo SET Trn_FinPrestamo.AcumCapital=(SELECT ISNULL(SUM(Trn_FinCuotas.VrTotal),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCia=Trn_FinPrestamo.IdCia AND Concepto='CAPITAL')
 ,Trn_FinPrestamo.AcumCuotas=(SELECT ISNULL(COUNT(Trn_FinCuotas.NumCuota),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCia=Trn_FinPrestamo.IdCia AND Concepto='CAPITAL')
,Trn_FinPrestamo.AcumIntereses=(SELECT ISNULL(SUM(Trn_FinCuotas.VrTotal),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinCuotas.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCia=Trn_FinPrestamo.IdCia AND Concepto='INTERESES')
  WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinPrestamoDev @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_FinPrestamo SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinPrestamoMora  @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinPrestamo SET Trn_FinPrestamo.AcumIntMora=(SELECT ISNULL(SUM(Trn_FinCuotas.VrTotal),0)
 FROM Trn_FinCuotas  WHERE Trn_FinCuotas.TipDoc='ND2' AND  Trn_FinCuotas.NumPreMora=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinCuotas.IdCiaCau=Trn_FinPrestamo.IdCia AND Concepto='INTMORA')
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinPrestamoPag  @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_FinPrestamo SET Trn_FinPrestamo.PagosCapital=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
 FROM Trn_FinAbonos INNER JOIN Trn_FinCuotas ON Trn_FinAbonos.TipDoc=Trn_FinCuotas.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinCuotas.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinCuotas.IdCia AND  Trn_FinAbonos.ItemPre=Trn_FinCuotas.Item
  WHERE Trn_FinAbonos.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinPrestamo.IdCia  AND Concepto='CAPITAL')
--INTERESS
,Trn_FinPrestamo.PagosIntereses=(SELECT ISNULL(SUM(Trn_FinAbonos.TotalAbono),0)
 FROM Trn_FinAbonos INNER JOIN Trn_FinCuotas ON Trn_FinAbonos.TipDoc=Trn_FinCuotas.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinCuotas.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinCuotas.IdCia AND  Trn_FinAbonos.ItemPre=Trn_FinCuotas.Item
  WHERE Trn_FinAbonos.TipDoc=Trn_FinPrestamo.TipDoc AND Trn_FinAbonos.IdPrestamo=Trn_FinPrestamo.IdPrestamo
 AND Trn_FinAbonos.IdCiaPre=Trn_FinPrestamo.IdCia  AND Concepto='INTERESES')
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinPrestamoTot @pmTipDoc VARCHAR(3),@pmIdPrestamo INT,@pmIdCia CHAR(2)
,@pmAcumCapital MONEY,@pmAcumIntereses MONEY,@pmAcumIntMora MONEY,@pmAcumCuotas INT,@pmPagosCapital MONEY
,@pmPagosIntereses MONEY,@pmPagosIntMora MONEY,@pmCuotasPagadas INT,@pmCuotasMora INT
 AS 
 UPDATE Trn_FinPrestamo SET  AcumCapital=@pmAcumCapital,AcumIntereses=@pmAcumIntereses,AcumIntMora=@pmAcumIntMora
,AcumCuotas=@pmAcumCuotas,PagosCapital=@pmPagosCapital,PagosIntereses=@pmPagosIntereses,PagosIntMora=@pmPagosIntMora
,CuotasPagadas=@pmCuotasPagadas,CuotasMora=@pmCuotasMora
 WHERE TipDoc=@pmTipDoc AND IdPrestamo=@pmIdPrestamo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFinRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY
,@pmVrAplicado MONEY,@pmVrEfectivo MONEY,@pmVrAnticipo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4)
,@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipoCredito VARCHAR(10),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_FinRecibos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FecPago=@pmFecPago,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrEfectivo=@pmVrEfectivo,VrAnticipo=@pmVrAnticipo
,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,EnEfectivo=@pmEnEfectivo,CdForma=@pmCdForma,NumForma=@pmNumForma,DetallePago=@pmDetallePago,CdBanco=@pmCdBanco,CdCta=@pmCdCta
,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipoCredito=@pmTipoCredito,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFinRecibosDev @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_FinRecibos SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFmtDoc @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2),@pmDescripcion VARCHAR(100),@pmFormato VARCHAR(20),@pmPredtmnado BIT,@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT
,@pmVerSetup BIT,@pmNumCopias INT,@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50)
 AS 
 UPDATE Sys_FmtDoc SET Descripcion=@pmDescripcion,Formato=@pmFormato,Predtmnado=@pmPredtmnado,TipoPapel=@pmTipoPapel,Orientacion=@pmOrientacion,VistaPrevia=@pmVistaPrevia,VerSetup=@pmVerSetup
,NumCopias=@pmNumCopias,sp_Nombre=@pmsp_Nombre,Ind_Location=@pmInd_Location
 WHERE IdDoc=@pmIdDoc AND IdForma=@pmIdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFmtDocPre @pmIdDoc VARCHAR(3),@pmIdForma CHAR(2),@pmPredtmnado BIT
 AS 
 UPDATE Sys_FmtDoc SET Predtmnado=@pmPredtmnado
 WHERE IdDoc=@pmIdDoc AND IdForma<>@pmIdForma

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFmtFactura @pmnFormato VARCHAR(10),@pmIdDoc VARCHAR(3),@pmCdForma CHAR(2)
 AS 
 UPDATE Sys_FmtFactura SET IdDoc=@pmIdDoc,CdForma=@pmCdForma
 WHERE nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFondos @pmIdFondo VARCHAR(8),@pmFondo VARCHAR(100)
,@pmIdClase VARCHAR(4),@pmIdTercero VARCHAR(16),@pmCodigoEps VARCHAR(10),@pmCodigoPen VARCHAR(10),@pmCodigoArp VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Fondos SET Fondo=@pmFondo,IdClase=@pmIdClase,IdTercero=@pmIdTercero
,CodigoEps=@pmCodigoEps,CodigoPen=@pmCodigoPen,CodigoArp=@pmCodigoArp,Inactivo=@pmInactivo
 WHERE IdFondo=@pmIdFondo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFormasCue @pmIdForma VARCHAR(4),@pmIdConcepto VARCHAR(4)
,@pmIdCuenta VARCHAR(16),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE FormasCue SET IdCuenta=@pmIdCuenta,FechaUpdate=@pmFechaUpdate
 WHERE IdForma=@pmIdForma AND IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpFormaspago @pmIdForma VARCHAR(4),@pmFormaPago VARCHAR(50),@pmConsecutivo INT,@pmCuadreCaja BIT
,@pmRefDcmto BIT,@pmRefNit BIT,@pmRefBco BIT,@pmRefFech BIT,@pmRefAtza BIT,@pmRefOtro BIT,@pmVldRangos BIT,@pmInactivo BIT
 AS 
 UPDATE Formaspago SET FormaPago=@pmFormaPago,Consecutivo=@pmConsecutivo,CuadreCaja=@pmCuadreCaja,RefDcmto=@pmRefDcmto,RefNit=@pmRefNit
,RefBco=@pmRefBco,RefFech=@pmRefFech,RefAtza=@pmRefAtza,RefOtro=@pmRefOtro,VldRangos=@pmVldRangos,Inactivo=@pmInactivo
 WHERE IdForma=@pmIdForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpFuentesPago @pmIdFuente VARCHAR(4),@pmFuentePago VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE FuentesPago SET FuentePago=@pmFuentePago,Inactivo=@pmInactivo
 WHERE IdFuente=@pmIdFuente

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGrupos @pmIdGrupo VARCHAR(8),@pmGrupo VARCHAR(150),@pmIdLinea VARCHAR(8)
,@pmTipoInvent VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Grupos SET Grupo=@pmGrupo,IdLinea=@pmIdLinea,TipoInvent=@pmTipoInvent
,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGruposAR @pmIdGrupo VARCHAR(4),@pmAltoRiesgo VARCHAR(50),@pmTarfaTrab DECIMAL(10,4),@pmTarfaEmp DECIMAL(10,4),@pmInactivo BIT
 AS 
 UPDATE GruposAR SET AltoRiesgo=@pmAltoRiesgo,TarfaTrab=@pmTarfaTrab,TarfaEmp=@pmTarfaEmp,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpGruposAgenc @pmIdGrupo VARCHAR(4),@pmGrupoAgenc VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE GruposAgenc SET GrupoAgenc=@pmGrupoAgenc,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGruposCli @pmIdGrupo VARCHAR(4),@pmGrupoClie VARCHAR(50),@pmModApp VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE GruposCli SET GrupoClie=@pmGrupoClie,ModApp=@pmModApp,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpGruposMcia @pmIdGrupo VARCHAR(10),@pmGrupoMcia VARCHAR(250),@pmInactivo BIT
 AS 
 UPDATE GruposMcia SET GrupoMcia=@pmGrupoMcia,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGruposObj @pmIdGrupo VARCHAR(6),@pmGrupoObjeto VARCHAR(50),@pmnModulo VARCHAR(10)
 AS 
 UPDATE Sys_GruposObj SET GrupoObjeto=@pmGrupoObjeto,nModulo=@pmnModulo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGruposPro @pmIdGrupo VARCHAR(4),@pmGrupoProp VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE GruposPro SET GrupoProp=@pmGrupoProp,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGruposRot @pmIdGrupo VARCHAR(4),@pmGrupoRot VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE GruposRot SET GrupoRot=@pmGrupoRot,Inactivo=@pmInactivo
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGruposUsu @pmIdGrupo INT,@pmGrupo VARCHAR(50),@pmDescripcion VARCHAR(100)
 AS 
 UPDATE Sys_GruposUsu SET Grupo=@pmGrupo,Descripcion=@pmDescripcion
 WHERE IdGrupo=@pmIdGrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGuia @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaDesp SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmCantidad DECIMAL(14,4)
,@pmDirEnvio VARCHAR(250),@pmIdLocal VARCHAR(8),@pmLugarEnv VARCHAR(50),@pmHorasVig DECIMAL(14,4),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmCdRuta VARCHAR(4),@pmCdCiuOrigen VARCHAR(8)
,@pmReferncia VARCHAR(50),@pmTipOcc VARCHAR(3),@pmOCargue INT,@pmIdCiaOcc CHAR(2),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmZonaFrontera BIT,@pmTipoTemp VARCHAR(10),@pmTemperatura DECIMAL(14,4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Guia SET Fecha=@pmFecha,FechaDesp=@pmFechaDesp,FechaVence=@pmFechaVence,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,Cantidad=@pmCantidad,DirEnvio=@pmDirEnvio,IdLocal=@pmIdLocal,LugarEnv=@pmLugarEnv,HorasVig=@pmHorasVig,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor
,NitEmpTrans=@pmNitEmpTrans,EmpTrans=@pmEmpTrans,CdRuta=@pmCdRuta,Referncia=@pmReferncia,TipOcc=@pmTipOcc,OCargue=@pmOCargue,IdCiaOcc=@pmIdCiaOcc,TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact,Anulado=@pmAnulado,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,CdCiuOrigen=@pmCdCiuOrigen,ZonaFrontera=@pmZonaFrontera,TipoTemp=@pmTipoTemp,Temperatura=@pmTemperatura,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGuiaAbr @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Guia SET  TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGuiaAnu @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Guia SET Anulado=@pmAnulado,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpGuiaFac @pmTipDoc VARCHAR(3),@pmGuia INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Guia SET  TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Guia=@pmGuia AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpInstalaciones @pmIdInstala VARCHAR(4),@pmInstlacion VARCHAR(50),@pmDirInstala VARCHAR(100)
,@pmCodCiu VARCHAR(8),@pmCodCia CHAR(2),@pmIdClase VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE Instalaciones SET Instlacion=@pmInstlacion,DirInstala=@pmDirInstala,CodCia=@pmCodCia
,CodCiu=@pmCodCiu,IdClase=@pmIdClase,Inactivo=@pmInactivo
 WHERE IdInstala=@pmIdInstala
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpInvDetalleSal @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmItem INT
,@pmVrUnitario MONEY,@pmCantExist DECIMAL(14,4),@pmNoConteo BIT
 AS 
 UPDATE  Trn_InvDetalle SET VrUnitario=@pmVrUnitario,CantExist=@pmCantExist,NoConteo=@pmNoConteo
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpInvFisico @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmModalidad VARCHAR(10),@pmCdBodega VARCHAR(4),@pmCantFisico DECIMAL(14,4)
,@pmVrTotalFisico MONEY,@pmExistencias DECIMAL(14,4),@pmVrTotalCosto MONEY,@pmProcesado BIT,@pmFecProceso SMALLDATETIME,@pmNumAjuSob INT,@pmNumAjuFal INT
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_InvFisico SET Fecha=@pmFecha,Modalidad=@pmModalidad,CdBodega=@pmCdBodega,CantFisico=@pmCantFisico,VrTotalFisico=@pmVrTotalFisico,Existencias=@pmExistencias,VrTotalCosto=@pmVrTotalCosto
,Procesado=@pmProcesado,FecProceso=@pmFecProceso,NumAjuSob=@pmNumAjuSob,NumAjuFal=@pmNumAjuFal,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpInvFisicoAnu @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_InvFisico SET  Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpInvFisicoPro @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmProcesado BIT,@pmFecProceso SMALLDATETIME,@pmNumAjuSob INT,@pmNumAjuFal INT
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_InvFisico SET Procesado=@pmProcesado,FecProceso=@pmFecProceso
,NumAjuSob=@pmNumAjuSob,NumAjuFal=@pmNumAjuFal
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpInvFisicoTot @pmTipDoc VARCHAR(3),@pmnInventario INT,@pmIdCia CHAR(2)
,@pmCantFisico DECIMAL(14,4),@pmVrTotalFisico MONEY,@pmExistencias DECIMAL(14,4),@pmVrTotalCosto MONEY
 AS 
 UPDATE Trn_InvFisico SET CantFisico=@pmCantFisico,VrTotalFisico=@pmVrTotalFisico,Existencias=@pmExistencias
,VrTotalCosto=@pmVrTotalCosto
 WHERE TipDoc=@pmTipDoc AND nInventario=@pmnInventario AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpJornadas @pmIdJornada VARCHAR(4),@pmJornada VARCHAR(50),@pmHorasDia INT
,@pmRangos VARCHAR(25),@pmInactivo BIT
 AS 
 UPDATE Jornadas SET Jornada=@pmJornada,HorasDia=@pmHorasDia,Rangos=@pmRangos,Inactivo=@pmInactivo
 WHERE IdJornada=@pmIdJornada
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurAutoridades @pmIdAutoridad VARCHAR(4),@pmNomAutoridad VARCHAR(50),@pmDireccion VARCHAR(100),@pmInactivo BIT
 AS 
 UPDATE JurAutoridades SET NomAutoridad=@pmNomAutoridad,Direccion=@pmDireccion,Inactivo=@pmInactivo
 WHERE IdAutoridad=@pmIdAutoridad


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurCalifica @pmIdCalifica VARCHAR(4),@pmCalificaCaso VARCHAR(50),@pmIdClase VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE JurCalifica SET CalificaCaso=@pmCalificaCaso,IdClase=@pmIdClase,Inactivo=@pmInactivo
 WHERE IdCalifica=@pmIdCalifica


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpJurCasos @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmRadicacion VARCHAR(20),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmSinRecorrido BIT,@pmTipCar VARCHAR(3),@pmCartulina INT,@pmIdCiaCar CHAR(2),@pmFechaSuc SMALLDATETIME,@pmIdRuta VARCHAR(4),@pmIdAbogado VARCHAR(16),@pmIdAuxiliar VARCHAR(16),@pmIdAutoridad VARCHAR(4),@pmIdTipoPro VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdCalifica VARCHAR(4),@pmLugarAcc VARCHAR(100),@pmCausaAcc VARCHAR(3300)
,@pmComentario VARCHAR(3300),@pmObservacion VARCHAR(250),@pmContraparte VARCHAR(100),@pmDirContraparte VARCHAR(100),@pmTelContraparte VARCHAR(20),@pmValDanosCParte MONEY,@pmValDanosEmp MONEY,@pmValDeducible MONEY,@pmEmpAsegdora VARCHAR(100),@pmAuxMutuo BIT,@pmArchivoCaso VARCHAR(100),@pmDocumntos VARCHAR(250),@pmCroquis VARCHAR(20),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmReclamaciones VARCHAR(1000),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_JurCasos SET Fecha=@pmFecha,Radicacion=@pmRadicacion,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor,SinRecorrido=@pmSinRecorrido,TipCar=@pmTipCar,Cartulina=@pmCartulina,IdCiaCar=@pmIdCiaCar,FechaSuc=@pmFechaSuc,IdRuta=@pmIdRuta,IdAbogado=@pmIdAbogado,IdAuxiliar=@pmIdAuxiliar,IdAutoridad=@pmIdAutoridad,IdTipoPro=@pmIdTipoPro,IdClase=@pmIdClase,IdCalifica=@pmIdCalifica,LugarAcc=@pmLugarAcc,CausaAcc=@pmCausaAcc,Comentario=@pmComentario,Observacion=@pmObservacion,Contraparte=@pmContraparte,DirContraparte=@pmDirContraparte
,TelContraparte=@pmTelContraparte,ValDanosCParte=@pmValDanosCParte,ValDanosEmp=@pmValDanosEmp,ValDeducible=@pmValDeducible,EmpAsegdora=@pmEmpAsegdora,AuxMutuo=@pmAuxMutuo,ArchivoCaso=@pmArchivoCaso,Documntos=@pmDocumntos,Croquis=@pmCroquis,Anulado=@pmAnulado,FecDev=@pmFecDev,Reclamaciones=@pmReclamaciones,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurCasosAnu @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2), @pmAnulado BIT, @pmFecDev SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmObservacion VARCHAR(250)
AS 
 UPDATE Trn_JurCasos SET  Anulado=@pmAnulado, FecDev=@pmFecDev,IdEstado=@pmIdEstado
 ,Observacion=ISNULL(@pmObservacion,Observacion)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurCasosCie @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2),@pmIdEstado VARCHAR(4)
,@pmObservacion VARCHAR(250),@pmArchivoCaso VARCHAR(100),@pmFecUpdate SMALLDATETIME
AS 
 UPDATE Trn_JurCasos SET IdEstado=@pmIdEstado, FecUpdate=@pmFecUpdate,Observacion=ISNULL(@pmObservacion,Observacion)
 ,ArchivoCaso=ISNULL(@pmArchivoCaso,ArchivoCaso)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurCasosCpt @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2),@pmContraparte VARCHAR(100), @pmDirContraparte VARCHAR(100),@pmTelContraparte VARCHAR(20)
,@pmValDanosCParte MONEY,@pmValDanosEmp MONEY, @pmValDeducible MONEY, @pmEmpAsegdora VARCHAR(100), @pmAuxMutuo BIT, @pmArchivoCaso VARCHAR(100),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
AS 
 UPDATE Trn_JurCasos SET  Contraparte=@pmContraparte, DirContraparte=@pmDirContraparte,TelContraparte=@pmTelContraparte,ValDanosCParte=@pmValDanosCParte, ValDanosEmp=@pmValDanosEmp, ValDeducible=@pmValDeducible
, EmpAsegdora=@pmEmpAsegdora, AuxMutuo=@pmAuxMutuo, FecUpdate=@pmFecUpdate,ArchivoCaso=ISNULL(@pmArchivoCaso,ArchivoCaso),IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID  AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurCasosHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT,@pmFechaUp SMALLDATETIME,@pmHistorial VARCHAR(3500),@pmEstadoAct VARCHAR(3500),@pmCierreProc BIT,@pmArchivoProc VARCHAR(100),@pmNombreCparte VARCHAR(100),@pmDirCparte VARCHAR(100),@pmTelCparte VARCHAR(20),@pmDanosCParte MONEY,@pmDanosEmp MONEY,@pmValorDeducible MONEY,@pmAseguradora VARCHAR(100)
,@pmAuxilioMutuo BIT,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_JurCasosHis SET FechaUp=@pmFechaUp,Historial=@pmHistorial,EstadoAct=@pmEstadoAct,CierreProc=@pmCierreProc,ArchivoProc=@pmArchivoProc,NombreCparte=@pmNombreCparte,DirCparte=@pmDirCparte,TelCparte=@pmTelCparte,DanosCParte=@pmDanosCParte,DanosEmp=@pmDanosEmp,ValorDeducible=@pmValorDeducible,Aseguradora=@pmAseguradora,AuxilioMutuo=@pmAuxilioMutuo,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia AND ActualizaID=@pmActualizaID


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurCasosVal @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2),@pmValDanosCParte MONEY,@pmValDanosEmp MONEY, @pmValDeducible MONEY
, @pmEmpAsegdora VARCHAR(100), @pmAuxMutuo BIT,@pmArchivoCaso VARCHAR(100)
AS 
 UPDATE Trn_JurCasos SET  ValDanosCParte=ISNULL(@pmValDanosCParte,ValDanosCParte), ValDanosEmp=ISNULL(@pmValDanosEmp,ValDanosEmp), ValDeducible=ISNULL(@pmValDeducible,ValDeducible)
, EmpAsegdora=ISNULL(@pmEmpAsegdora,EmpAsegdora) , AuxMutuo=ISNULL(@pmAuxMutuo,AuxMutuo) ,ArchivoCaso=ISNULL(@pmArchivoCaso,ArchivoCaso)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID  AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurClaseAcc @pmIdClase VARCHAR(4),@pmClaseAccdte VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurClaseAcc SET ClaseAccdte=@pmClaseAccdte,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurDocumtos @pmIdDoc VARCHAR(4),@pmDocumnto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurDocumtos SET Documnto=@pmDocumnto,Inactivo=@pmInactivo
 WHERE IdDoc=@pmIdDoc


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurEstados @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50),@pmInactivo BIT,@pmNColor INT
 AS 
 UPDATE JurEstados SET Estado=@pmEstado,Inactivo=@pmInactivo,NColor=@pmNColor
 WHERE IdEstado=@pmIdEstado


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurInvesHis @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmActualizaID INT,@pmFechaUp SMALLDATETIME,@pmHistorial VARCHAR(3000),@pmEstadoAct VARCHAR(3000),@pmPruebas VARCHAR(1200),@pmDocumntos VARCHAR(250)
,@pmCierreProc BIT,@pmArchivoProc VARCHAR(100),@pmNombreCparte VARCHAR(100),@pmDirCparte VARCHAR(100),@pmTelCparte VARCHAR(20),@pmIdEstado VARCHAR(4) 
 AS 
 UPDATE Trn_JurInvesHis SET FechaUp=@pmFechaUp,Historial=@pmHistorial,EstadoAct=@pmEstadoAct,Pruebas=@pmPruebas,Documntos=@pmDocumntos,CierreProc=@pmCierreProc,ArchivoProc=@pmArchivoProc,NombreCparte=@pmNombreCparte,DirCparte=@pmDirCparte
,TelCparte=@pmTelCparte,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia AND ActualizaID=@pmActualizaID

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurInvestiga @pmTipDoc VARCHAR(3),@pmCasoID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmRadicacion VARCHAR(20),@pmIdAbogado VARCHAR(16),@pmIdAuxiliar VARCHAR(16),@pmIdAutoridad VARCHAR(4),@pmIdEmpresa VARCHAR(16)
,@pmObservacion VARCHAR(250),@pmContraparte VARCHAR(100),@pmDirContraparte VARCHAR(100),@pmTelContraparte VARCHAR(20),@pmArchivoCaso VARCHAR(100),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_JurInvestiga SET Fecha=@pmFecha,Radicacion=@pmRadicacion,IdAbogado=@pmIdAbogado,IdAuxiliar=@pmIdAuxiliar,IdAutoridad=@pmIdAutoridad,IdEmpresa=@pmIdEmpresa,Observacion=@pmObservacion,Contraparte=@pmContraparte,DirContraparte=@pmDirContraparte,TelContraparte=@pmTelContraparte
,ArchivoCaso=@pmArchivoCaso,Anulado=@pmAnulado,FecDev=@pmFecDev,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurInvestigaAnu @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2), @pmAnulado BIT, @pmFecDev SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmObservacion VARCHAR(250)
AS 
 UPDATE Trn_JurInvestiga SET  Anulado=@pmAnulado, FecDev=@pmFecDev,IdEstado=@pmIdEstado
 ,Observacion=ISNULL(@pmObservacion,Observacion)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpJurInvestigaCie @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2),@pmIdEstado VARCHAR(4)
,@pmObservacion VARCHAR(250),@pmArchivoCaso VARCHAR(100),@pmFecUpdate SMALLDATETIME
AS 
 UPDATE Trn_JurInvestiga SET IdEstado=@pmIdEstado, FecUpdate=@pmFecUpdate,Observacion=ISNULL(@pmObservacion,Observacion)
 ,ArchivoCaso=ISNULL(@pmArchivoCaso,ArchivoCaso)
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpJurInvestigaCpt  @pmTipDoc VARCHAR(3), @pmCasoID INT, @pmIdCia CHAR(2),@pmContraparte VARCHAR(100), @pmDirContraparte VARCHAR(100), @pmTelContraparte VARCHAR(20)
,@pmArchivoCaso VARCHAR(100),@pmIdEstado VARCHAR(4), @pmFecUpdate SMALLDATETIME
AS
 UPDATE  Trn_JurInvestiga  SET Contraparte=@pmContraparte, DirContraparte=@pmDirContraparte,TelContraparte=@pmTelContraparte,ArchivoCaso=ISNULL(@pmArchivoCaso,ArchivoCaso)
, IdEstado=ISNULL(@pmIdEstado,IdEstado), FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND CasoID=@pmCasoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurPreguntas @pmIdPregunta VARCHAR(4),@pmPreguntaBas VARCHAR(250),@pmIdTipoPro VARCHAR(4),@pmTipoDato VARCHAR(15),@pmInactivo BIT
 AS 
 UPDATE JurPreguntas SET PreguntaBas=@pmPreguntaBas,IdTipoPro=@pmIdTipoPro,TipoDato=@pmTipoDato,Inactivo=@pmInactivo
 WHERE IdPregunta=@pmIdPregunta


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpJurTipos @pmIdTipoPro VARCHAR(4),@pmTipoProceso VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE JurTipos SET TipoProceso=@pmTipoProceso,Inactivo=@pmInactivo
 WHERE IdTipoPro=@pmIdTipoPro


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLineas @pmIdLinea VARCHAR(8),@pmLinea VARCHAR(150)
,@pmTipoInvent VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Lineas SET Linea=@pmLinea,Inactivo=@pmInactivo,TipoInvent=@pmTipoInvent
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLocalidades @pmIdLocal VARCHAR(8),@pmLocalidad VARCHAR(50),@pmIdDep VARCHAR(8),@pmTipLocal VARCHAR(20)
,@pmDist_Kmt DECIMAL(10,4),@pmTime_hh VARCHAR(8),@pmCelRow DECIMAL(14,4),@pmCelCol DECIMAL(14,4),@pmInactivo BIT
,@pmDistSede DECIMAL(10,4),@pmTimeSede VARCHAR(8),@pmCodAlias VARCHAR(10)
 AS 
 UPDATE Localidades SET Localidad=@pmLocalidad,IdDep=@pmIdDep,TipLocal=@pmTipLocal,Dist_Kmt=@pmDist_Kmt,Time_hh=@pmTime_hh,CelRow=@pmCelRow
,CelCol=@pmCelCol,Inactivo=@pmInactivo,DistSede=@pmDistSede,TimeSede=@pmTimeSede,CodAlias=@pmCodAlias
 WHERE IdLocal=@pmIdLocal
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLog_Nomina @pmIdNom VARCHAR(4),@pmIdPeriodo VARCHAR(8),@pmCerrado BIT
,@pmIntCtaCte BIT,@pmLogCtaCte VARCHAR(100)
 AS 
 UPDATE Log_Nomina SET Cerrado=@pmCerrado,IntCtaCte=@pmIntCtaCte,LogCtaCte=@pmLogCtaCte
 WHERE IdNom=@pmIdNom AND IdPeriodo=@pmIdPeriodo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLog_Periodos  @pmAnno INT,@pmIdCia CHAR(2),@pmnMes INT,@pmLock BIT
AS
 IF @pmnMes=1
  UPDATE Log_Periodos  SET  Ene=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=2
  UPDATE Log_Periodos SET Feb=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=3
  UPDATE Log_Periodos SET  Mar=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=4
  UPDATE Log_Periodos SET  Abr=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=5
  UPDATE Log_Periodos SET  May=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=6
  UPDATE Log_Periodos SET Jun=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=7
  UPDATE Log_Periodos SET Jul=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=8
  UPDATE Log_Periodos  SET Ago=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=9
  UPDATE Log_Periodos  SET Sep=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=10
  UPDATE Log_Periodos  SET  Oct=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=11
  UPDATE Log_Periodos  SET  Nov=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=12
  UPDATE Log_Periodos  SET  Dic=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 IF @pmnMes=13
  UPDATE Log_Periodos  SET  CierreAnno=@pmLock WHERE Anno=@pmAnno AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLotes @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10)
,@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmSaldoActual DECIMAL(14,4),@pmNitProv VARCHAR(16),@pmTipDoc VARCHAR(3),@pmEntrada INT
 AS 
 UPDATE Trn_Lotes SET Fecha=@pmFecha,FecVence=@pmFecVence,SaldoActual=@pmSaldoActual,NitProv=@pmNitProv,TipDoc=@pmTipDoc,Entrada=@pmEntrada
 WHERE NumLote=@pmNumLote AND IdProducto=@pmIdProducto AND IdCia=@pmIdCia AND IdBodega=@pmIdBodega AND IdUbic=@pmIdUbic
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLotesCero @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16)
,@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4)
 AS 
 UPDATE Trn_Lotes SET SaldoActual=0
 WHERE NumLote LIKE ISNULL(@pmNumLote,'%') AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLotesSal @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16)
,@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4)
 AS 
 UPDATE Trn_Lotes SET SaldoActual=(SELECT ISNULL(SUM(Entradas-Salidas),0)
 FROM  Trn_LotMov 
 WHERE Trn_Lotes.NumLote=Trn_LotMov.NumLote AND Trn_Lotes.IdProducto=Trn_LotMov.IdProducto AND Trn_Lotes.IdBodega=Trn_LotMov.IdBodega
 AND Trn_Lotes.IdCia=Trn_LotMov.IdCia  AND Trn_Lotes.IdUbic=Trn_LotMov.CdUbic)
 WHERE NumLote LIKE ISNULL(@pmNumLote,'%') AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND IdBodega LIKE ISNULL(@pmIdBodega,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpLotesSdo @pmNumLote VARCHAR(30),@pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10)
,@pmSaldoActual DECIMAL(14,4)
 AS 
 UPDATE Trn_Lotes SET SaldoActual=@pmSaldoActual
 WHERE NumLote=@pmNumLote AND IdProducto=@pmIdProducto AND IdCia=@pmIdCia AND IdBodega=@pmIdBodega
 AND IdUbic LIKE ISNULL(@pmIdUbic,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMarcas @pmIdMarca VARCHAR(4),@pmMarca VARCHAR(50),@pmTipoMarca VARCHAR(20)
,@pmCodigoMT VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE Marcas SET Marca=@pmMarca,TipoMarca=@pmTipoMarca,CodigoMT=@pmCodigoMT,Inactivo=@pmInactivo
 WHERE IdMarca=@pmIdMarca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMarcasLin @pmIdLinea VARCHAR(10),@pmLineaVeh VARCHAR(50),@pmIdMarca VARCHAR(4)
,@pmCodMT VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE MarcasLin SET LineaVeh=@pmLineaVeh,IdMarca=@pmIdMarca,CodMT=@pmCodMT,Inactivo=@pmInactivo
 WHERE IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayAnotaciones @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME,@pmDetalle VARCHAR(5000),@pmIdUsuario VARCHAR(11)
 AS 
 UPDATE Trn_MayAnotaciones SET Fecha=@pmFecha,Detalle=@pmDetalle,IdUsuario=@pmIdUsuario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayBonCuentas @pmIdBonif VARCHAR(4),@pmTipoCue INT
,@pmCdTipo VARCHAR(4),@pmIdCuenta VARCHAR(16)
 AS 
 UPDATE MayBonCuentas SET IdCuenta=@pmIdCuenta
 WHERE IdBonif=@pmIdBonif AND TipoCue=@pmTipoCue AND CdTipo=@pmCdTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayBonif @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoProd VARCHAR(20),@pmVrInicial DECIMAL(14,4),@pmVrFinal DECIMAL(14,4),@pmUnidad VARCHAR(10)
,@pmTarifa DECIMAL(14,4),@pmTipoTarifa CHAR(1),@pmPeriodo VARCHAR(10),@pmCdProducto VARCHAR(16),@pmCdSubgrupo VARCHAR(8)
 AS 
 UPDATE Trn_MayBonif SET TipoProd=@pmTipoProd,VrInicial=@pmVrInicial,VrFinal=@pmVrFinal,Unidad=@pmUnidad,Tarifa=@pmTarifa,TipoTarifa=@pmTipoTarifa
,Periodo=@pmPeriodo,CdProducto=@pmCdProducto,CdSubgrupo=@pmCdSubgrupo
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayContDoc @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdDoc VARCHAR(4),@pmFechaRad SMALLDATETIME
,@pmDependencia VARCHAR(50),@pmEstado VARCHAR(10),@pmFechaAprob SMALLDATETIME,@pmComentarios VARCHAR(250),@pmCdUsuario VARCHAR(11)
 AS 
 UPDATE Trn_MayContDoc SET IdDoc=@pmIdDoc,FechaRad=@pmFechaRad,Dependencia=@pmDependencia,Estado=@pmEstado
,FechaAprob=@pmFechaAprob,Comentarios=@pmComentarios,CdUsuario=@pmCdUsuario
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayContratos @pmNContrato INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdTipo VARCHAR(4),@pmIdBonif VARCHAR(4),@pmConvBonif BIT
,@pmCuotaFija BIT,@pmCondPago BIT,@pmDiasGracia INT,@pmModPlazo VARCHAR(10),@pmPlazo INT,@pmFechaVence SMALLDATETIME,@pmModRecursos VARCHAR(150),@pmVrInversion MONEY,@pmVrInventario MONEY,@pmVrCuota MONEY
,@pmModCuota VARCHAR(10),@pmIdBandera VARCHAR(4),@pmAprobJunta INT,@pmFechaAprob SMALLDATETIME,@pmObservAprob VARCHAR(250),@pmObservacion VARCHAR(250),@pmIdVend VARCHAR(16),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
,@pmFechaInicio SMALLDATETIME,@pmCuotaMin DECIMAL(14,4),@pmTipoAprob VARCHAR(10),@pmNumInterno VARCHAR(20),@pmInvCombus DECIMAL(14,4),@pmCuotaBase DECIMAL(14,4),@pmFechaFirma SMALLDATETIME,@pmCdAprobNeg VARCHAR(4),@pmArchivoAprob VARCHAR(50)
,@pmClaseContrato VARCHAR(10),@pmDescTipoCont VARCHAR(250),@pmGarantHipoteca BIT,@pmGarantPrenda BIT,@pmGarantPoliza BIT,@pmGarantBanco BIT,@pmGarantPagare BIT,@pmGarantOtro BIT,@pmCdTipoBien VARCHAR(4),@pmVrAvaludo MONEY,@pmNroEscritura VARCHAR(50)
,@pmCdTipoPoliza VARCHAR(4),@pmDescPoliza VARCHAR(150),@pmVigencPoliza INT,@pmFecVencPoliza SMALLDATETIME,@pmValorPoliza MONEY,@pmEdoGarantias INT,@pmDescGarantOtro VARCHAR(250),@pmInverContab INT,@pmInverContrac INT,@pmVolContMes DECIMAL(14,4)
,@pmVolContTotal DECIMAL(14,4),@pmVolViabMes DECIMAL(14,4),@pmVolViabTotal DECIMAL(14,4),@pmInvCtbObra MONEY,@pmInvCtbCapital MONEY,@pmInvCtbEfectivo MONEY,@pmInvCtbImagen MONEY,@pmInvCtbMntmto MONEY,@pmFecDesemb SMALLDATETIME,@pmInvContObra MONEY
,@pmInvContCapital MONEY,@pmInvContEfectivo MONEY,@pmInvContImagen MONEY,@pmCrtlAprovInv INT,@pmCrtlAprovImg INT,@pmCdTipoBloq VARCHAR(4),@pmEdoDocmtos INT,@pmDescEdoDoc VARCHAR(250),@pmRentabInicial DECIMAL(14,4),@pmGraciaBonif INT
 AS 
 UPDATE Trn_MayContratos SET Fecha=@pmFecha,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdTipo=@pmIdTipo,IdBonif=@pmIdBonif,ConvBonif=@pmConvBonif,CuotaFija=@pmCuotaFija,CondPago=@pmCondPago,DiasGracia=@pmDiasGracia
,ModPlazo=@pmModPlazo,Plazo=@pmPlazo,FechaVence=@pmFechaVence,ModRecursos=@pmModRecursos,VrInversion=@pmVrInversion,VrInventario=@pmVrInventario,VrCuota=@pmVrCuota,ModCuota=@pmModCuota,IdBandera=@pmIdBandera
,AprobJunta=@pmAprobJunta,FechaAprob=@pmFechaAprob,ObservAprob=@pmObservAprob,Observacion=@pmObservacion,IdEstado=@pmIdEstado,IdVend=@pmIdVend,FechaInicio=@pmFechaInicio,CuotaMin=@pmCuotaMin,TipoAprob=@pmTipoAprob
,NumInterno=@pmNumInterno,InvCombus=@pmInvCombus,CuotaBase=@pmCuotaBase,FecUpdate=@pmFecUpdate
,FechaFirma=@pmFechaFirma,CdAprobNeg=@pmCdAprobNeg,ArchivoAprob=@pmArchivoAprob,ClaseContrato=@pmClaseContrato,DescTipoCont=@pmDescTipoCont,GarantHipoteca=@pmGarantHipoteca,GarantPrenda=@pmGarantPrenda,GarantPoliza=@pmGarantPoliza,GarantBanco=@pmGarantBanco
,GarantPagare=@pmGarantPagare,GarantOtro=@pmGarantOtro,CdTipoBien=@pmCdTipoBien,VrAvaludo=@pmVrAvaludo,NroEscritura=@pmNroEscritura,CdTipoPoliza=@pmCdTipoPoliza,DescPoliza=@pmDescPoliza,VigencPoliza=@pmVigencPoliza,FecVencPoliza=@pmFecVencPoliza,ValorPoliza=@pmValorPoliza
,EdoGarantias=@pmEdoGarantias,DescGarantOtro=@pmDescGarantOtro,InverContab=@pmInverContab,InverContrac=@pmInverContrac,VolContMes=@pmVolContMes,VolContTotal=@pmVolContTotal,VolViabMes=@pmVolViabMes,VolViabTotal=@pmVolViabTotal,InvCtbObra=@pmInvCtbObra
,InvCtbCapital=@pmInvCtbCapital,InvCtbEfectivo=@pmInvCtbEfectivo,InvCtbImagen=@pmInvCtbImagen,InvCtbMntmto=@pmInvCtbMntmto,FecDesemb=@pmFecDesemb,InvContObra=@pmInvContObra,InvContCapital=@pmInvContCapital,InvContEfectivo=@pmInvContEfectivo,InvContImagen=@pmInvContImagen,CrtlAprovInv=@pmCrtlAprovInv
,CrtlAprovImg=@pmCrtlAprovImg,CdTipoBloq=@pmCdTipoBloq,EdoDocmtos=@pmEdoDocmtos,DescEdoDoc=@pmDescEdoDoc,RentabInicial=@pmRentabInicial,GraciaBonif=@pmGraciaBonif
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayContratosApr @pmNContrato INT,@pmIdCia CHAR(2),@pmAprobJunta INT
,@pmFechaAprob SMALLDATETIME,@pmObservAprob VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmTipoAprob VARCHAR(10),@pmCdAprobNeg VARCHAR(4),@pmArchivoAprob VARCHAR(50)

 AS 
 UPDATE Trn_MayContratos SET AprobJunta=@pmAprobJunta,FechaAprob=@pmFechaAprob
,ObservAprob=@pmObservAprob,IdEstado=@pmIdEstado,TipoAprob=@pmTipoAprob
,CdAprobNeg=@pmCdAprobNeg,ArchivoAprob=@pmArchivoAprob
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayContratosEst @pmNContrato INT,@pmIdCia CHAR(2),@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME,@pmCdTipoBloq VARCHAR(4)
 AS 
 UPDATE Trn_MayContratos SET  IdEstado=@pmIdEstado,Observacion=@pmObservacion,FecUpdate=@pmFecUpdate
,CdTipoBloq=@pmCdTipoBloq
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayEstados @pmIdEstado VARCHAR(4),@pmEstado VARCHAR(50)
,@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE MayEstados SET Estado=@pmEstado,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdEstado=@pmIdEstado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayInventario @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmIdArticulo VARCHAR(16),@pmDetalle VARCHAR(150),@pmCantidad DECIMAL(14,4)
,@pmVrUnitario MONEY,@pmVrTotal MONEY,@pmTipoArticulo VARCHAR(10),@pmReferencia VARCHAR(50),@pmIdTipo VARCHAR(4),@pmTipDoc VARCHAR(3),@pmDocumento INT
,@pmIdCiaDoc CHAR(2),@pmCantEntrega DECIMAL(14,4),@pmFecEntrega SMALLDATETIME,@pmEstado VARCHAR(10),@pmCdProd VARCHAR(16),@pmNumReq INT,@pmIdCiaReq CHAR(2)
,@pmCantReq DECIMAL(14,4),@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmCantOrden DECIMAL(14,4)
 AS 
 UPDATE Trn_MayInventario SET IdArticulo=@pmIdArticulo,Detalle=@pmDetalle,Cantidad=@pmCantidad,VrUnitario=@pmVrUnitario,VrTotal=@pmVrTotal,TipoArticulo=@pmTipoArticulo
,Referencia=@pmReferencia,IdTipo=@pmIdTipo,TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,CantEntrega=@pmCantEntrega,FecEntrega=@pmFecEntrega,Estado=@pmEstado
,CdProd=@pmCdProd,NumReq=@pmNumReq,IdCiaReq=@pmIdCiaReq,CantReq=@pmCantReq,NumOrden=@pmNumOrden,IdCiaOrd=@pmIdCiaOrd,CantOrden=@pmCantOrden
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayItemsInv @pmIdArticulo VARCHAR(16),@pmDescrpcion VARCHAR(150)
,@pmUndMed VARCHAR(10),@pmGrupoArt VARCHAR(50),@pmCdProducto VARCHAR(16),@pmInactivo BIT
 AS 
 UPDATE MayItemsInv SET Descrpcion=@pmDescrpcion,UndMed=@pmUndMed,GrupoArt=@pmGrupoArt
,CdProducto=@pmCdProducto,Inactivo=@pmInactivo
 WHERE IdArticulo=@pmIdArticulo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayLiqTotalesNcr @pmNLiquida INT,@pmIdCia CHAR(2),@pmItem INT
,@pmNotaCred INT,@pmIdCiaNot CHAR(2)
 AS 
 UPDATE Trn_MayLiqTotales SET  NotaCred=@pmNotaCred,IdCiaNot=@pmIdCiaNot
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayLiquidaAnu @pmNLiquida INT,@pmIdCia CHAR(2)
,@pmEstado INT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_MayLiquida SET Estado=@pmEstado,FecDev=@pmFecDev
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayLiquidaCmp @pmNLiquida INT,@pmIdCia CHAR(2)
,@pmEstado INT,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
 AS 
 UPDATE Trn_MayLiquida SET TipCom=@pmTipCom,Comprobante=@pmComprobante
 ,IdCiaCom=@pmIdCiaCom,Estado=@pmEstado
 WHERE NLiquida=@pmNLiquida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayPspto @pmNContrato INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipoProd VARCHAR(20),@pmCantidad DECIMAL(14,4)
,@pmUnidad VARCHAR(10),@pmCantMes DECIMAL(14,4),@pmCdProducto VARCHAR(16),@pmCdSubgrupo VARCHAR(8)
 AS 
 UPDATE Trn_MayPspto SET TipoProd=@pmTipoProd,Cantidad=@pmCantidad,Unidad=@pmUnidad,CantMes=@pmCantMes
,CdProducto=@pmCdProducto,CdSubgrupo=@pmCdSubgrupo
 WHERE NContrato=@pmNContrato AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayTipos @pmIdTipo VARCHAR(4),@pmTipContrato VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTipos SET TipContrato=@pmTipContrato,Inactivo=@pmInactivo
 WHERE IdTipo=@pmIdTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpMayTiposAprob @pmIdAprob VARCHAR(4),@pmTipoAprob VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTiposAprob SET TipoAprob=@pmTipoAprob,Inactivo=@pmInactivo
 WHERE IdAprob=@pmIdAprob

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpMayTiposBienes @pmIdTipBien VARCHAR(4),@pmTipoBien VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTiposBienes SET TipoBien=@pmTipoBien,Inactivo=@pmInactivo
 WHERE IdTipBien=@pmIdTipBien

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayTiposBonif @pmIdBonif VARCHAR(4),@pmTipoBonif VARCHAR(50)
,@pmAboCartera BIT,@pmAboPstmo BIT,@pmInactivo BIT
 AS 
 UPDATE MayTiposBonif SET TipoBonif=@pmTipoBonif,Inactivo=@pmInactivo,AboCartera=@pmAboCartera
,AboPstmo=@pmAboPstmo
 WHERE IdBonif=@pmIdBonif
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpMayTiposDoc @pmIdDoc VARCHAR(4),@pmNombreDoc VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE MayTiposDoc SET NombreDoc=@pmNombreDoc,Inactivo=@pmInactivo
 WHERE IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpMayTiposPoliza @pmIdTipoPol VARCHAR(4),@pmTipoPoliza VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE MayTiposPoliza SET TipoPoliza=@pmTipoPoliza,Inactivo=@pmInactivo
 WHERE IdTipoPol=@pmIdTipoPol

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpMercancias @pmIdMercancia VARCHAR(16),@pmDescripMcia VARCHAR(150),@pmCodigoMcia VARCHAR(16),@pmIdGrupo VARCHAR(10),@pmUndMed VARCHAR(10),@pmIdUnd VARCHAR(4)
,@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmContenedor BIT,@pmIdProducto VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Mercancias SET DescripMcia=@pmDescripMcia,CodigoMcia=@pmCodigoMcia,IdGrupo=@pmIdGrupo,UndMed=@pmUndMed,IdUnd=@pmIdUnd,IdNat=@pmIdNat,IdMnjo=@pmIdMnjo,IdTmcia=@pmIdTmcia
,Contenedor=@pmContenedor,IdProducto=@pmIdProducto,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE IdMercancia=@pmIdMercancia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpModulos @pmIdMod VARCHAR(3),@pmConsecutivo INT,@pmDescripcion VARCHAR(50)
,@pmValor BIT,@pmnModulo VARCHAR(10)
 AS 
 IF @pmIdMod IS NULL
  UPDATE Sys_Modulos SET Valor=@pmValor WHERE nModulo LIKE ISNULL(@pmnModulo,'%')
 ELSE
  UPDATE Sys_Modulos SET Consecutivo=@pmConsecutivo,Descripcion=@pmDescripcion,Valor=@pmValor,nModulo=@pmnModulo
  WHERE IdMod=@pmIdMod
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAbonos @pmNPrestamo INT,@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmFechaAbo SMALLDATETIME,@pmVrAbono MONEY,@pmnCuotas INT,@pmIdPeriodo VARCHAR(8)
,@pmDscto VARCHAR(3),@pmDetalle VARCHAR(50),@pmNLiquida INT
 AS 
 UPDATE Trn_NomAbonos SET IdEmpleado=@pmIdEmpleado,FechaAbo=@pmFechaAbo,VrAbono=@pmVrAbono,nCuotas=@pmnCuotas,IdPeriodo=@pmIdPeriodo,Dscto=@pmDscto,Detalle=@pmDetalle,NLiquida=@pmNLiquida
 WHERE NPrestamo=@pmNPrestamo AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAlqDet @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmNContrato INT,@pmApellido1 VARCHAR(50),@pmApellido2 VARCHAR(50),@pmNombre1 VARCHAR(50),@pmNombre2 VARCHAR(50),@pmIdFonPen VARCHAR(8),@pmIdFonSal VARCHAR(8),@pmIdFonRie VARCHAR(8),@pmIdCajCom VARCHAR(8),@pmSalBasico MONEY,@pmVrIBCPen MONEY
,@pmDiasPen DECIMAL(10,4),@pmTarifPen DECIMAL(10,4),@pmVrPension MONEY,@pmVrApoFsp MONEY,@pmVrApoFsu MONEY,@pmVrApoVolAfi MONEY,@pmVrApoVolApt MONEY,@pmVrNoReten MONEY,@pmVrIBCSal MONEY,@pmDiasSal DECIMAL(10,4),@pmTarifSal DECIMAL(10,4),@pmVrSalud MONEY,@pmVrUpc MONEY,@pmVrIncap MONEY,@pmVrLicencia MONEY,@pmNumAutInc VARCHAR(30),@pmNumAutLic VARCHAR(30),@pmVrIBCRie MONEY
,@pmDiasRie DECIMAL(10,4),@pmTarifRie DECIMAL(10,4),@pmVrRiesgos MONEY,@pmVrIncIrp MONEY,@pmNumAutIrp VARCHAR(30),@pmIdInstala VARCHAR(4),@pmVrIBCRef MONEY,@pmVrIBCCajas MONEY,@pmDiasCom DECIMAL(10,4),@pmTarifCom DECIMAL(10,4),@pmVrCajas MONEY,@pmTarifSen DECIMAL(10,4),@pmVrSena MONEY,@pmTarifIcb DECIMAL(10,4),@pmVrICBF MONEY,@pmTarifEsap DECIMAL(10,4),@pmVrESAP MONEY
,@pmTarifEdu DECIMAL(10,4),@pmVrMinEdu MONEY,@pmTarifFsp DECIMAL(10,4),@pmTarifFsu DECIMAL(10,4),@pmCdFonAvp VARCHAR(8)
 AS 
 UPDATE Trn_NomAlqDet SET IdEmpleado=@pmIdEmpleado,NContrato=@pmNContrato,Apellido1=@pmApellido1,Apellido2=@pmApellido2,Nombre1=@pmNombre1,Nombre2=@pmNombre2,IdFonPen=@pmIdFonPen,IdFonSal=@pmIdFonSal,IdFonRie=@pmIdFonRie,IdCajCom=@pmIdCajCom,SalBasico=@pmSalBasico,VrIBCPen=@pmVrIBCPen,DiasPen=@pmDiasPen,TarifPen=@pmTarifPen,VrPension=@pmVrPension,VrApoFsp=@pmVrApoFsp,VrApoFsu=@pmVrApoFsu
,VrApoVolAfi=@pmVrApoVolAfi,VrApoVolApt=@pmVrApoVolApt,VrNoReten=@pmVrNoReten,VrIBCSal=@pmVrIBCSal,DiasSal=@pmDiasSal,TarifSal=@pmTarifSal,VrSalud=@pmVrSalud,VrUpc=@pmVrUpc,VrIncap=@pmVrIncap,VrLicencia=@pmVrLicencia,NumAutInc=@pmNumAutInc,NumAutLic=@pmNumAutLic,VrIBCRie=@pmVrIBCRie,DiasRie=@pmDiasRie,TarifRie=@pmTarifRie,VrRiesgos=@pmVrRiesgos,VrIncIrp=@pmVrIncIrp,NumAutIrp=@pmNumAutIrp,IdInstala=@pmIdInstala
,VrIBCRef=@pmVrIBCRef,VrIBCCajas=@pmVrIBCCajas,DiasCom=@pmDiasCom,TarifCom=@pmTarifCom,VrCajas=@pmVrCajas,TarifSen=@pmTarifSen,VrSena=@pmVrSena,TarifIcb=@pmTarifIcb,VrICBF=@pmVrICBF,TarifEsap=@pmTarifEsap,VrESAP=@pmVrESAP,TarifEdu=@pmTarifEdu,VrMinEdu=@pmVrMinEdu,TarifFsp=@pmTarifFsp,TarifFsu=@pmTarifFsu,CdFonAvp=@pmCdFonAvp
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAlqNov @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmNovIng INT,@pmNovRet INT,@pmNovTde INT,@pmNovTae INT,@pmNovTdp INT,@pmNovTap INT
,@pmNovVsp INT,@pmNovVte INT,@pmNovVst INT,@pmNovSln INT,@pmNovIge INT,@pmNovLma INT,@pmNovVac INT,@pmNovAvp INT,@pmNovVct INT,@pmNovIrp INT,@pmCodNewPen VARCHAR(8),@pmCodNewSal VARCHAR(8)
,@pmCodNewIns VARCHAR(4),@pmCodNewRie VARCHAR(4),@pmCodNewGru VARCHAR(4),@pmDiasNov INT
 AS 
 UPDATE Trn_NomAlqNov SET IdEmpleado=@pmIdEmpleado,NovIng=@pmNovIng,NovRet=@pmNovRet,NovTde=@pmNovTde,NovTae=@pmNovTae,NovTdp=@pmNovTdp,NovTap=@pmNovTap,NovVsp=@pmNovVsp,NovVte=@pmNovVte
,NovVst=@pmNovVst,NovSln=@pmNovSln,NovIge=@pmNovIge,NovLma=@pmNovLma,NovVac=@pmNovVac,NovAvp=@pmNovAvp,NovVct=@pmNovVct,NovIrp=@pmNovIrp,CodNewPen=@pmCodNewPen,CodNewSal=@pmCodNewSal
,CodNewIns=@pmCodNewIns,CodNewRie=@pmCodNewRie,CodNewGru=@pmCodNewGru,DiasNov=@pmDiasNov
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAlqTot @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmItem INT,@pmTipAdm VARCHAR(3),@pmIdFondo VARCHAR(8),@pmCodTipoApo CHAR(2),@pmVrTotalCot MONEY,@pmVrTotalFsp MONEY,@pmVrTotalFsu MONEY,@pmVrTotalAva MONEY,@pmVrTotalAve MONEY,@pmVrTotalUpc MONEY
,@pmVrTotalLic MONEY,@pmVrTotalInc MONEY,@pmVrTotalOtr MONEY,@pmDiasMora INT,@pmTarifMora DECIMAL(10,4),@pmVrIntMora MONEY,@pmVrIMoraOtr MONEY,@pmVrTotalSAF MONEY,@pmVrSAFUpc MONEY,@pmVrTotalPagar MONEY,@pmVrTotalFon MONEY,@pmTotalAfi INT,@pmNumAutLic VARCHAR(30),@pmNumAutInc VARCHAR(30)
,@pmNumPlaSAF VARCHAR(20),@pmTipoReg INT
 AS 
 UPDATE Trn_NomAlqTot SET TipAdm=@pmTipAdm,IdFondo=@pmIdFondo,CodTipoApo=@pmCodTipoApo,VrTotalCot=@pmVrTotalCot,VrTotalFsp=@pmVrTotalFsp,VrTotalFsu=@pmVrTotalFsu,VrTotalAva=@pmVrTotalAva,VrTotalAve=@pmVrTotalAve,VrTotalUpc=@pmVrTotalUpc,VrTotalLic=@pmVrTotalLic,VrTotalInc=@pmVrTotalInc
,VrTotalOtr=@pmVrTotalOtr,DiasMora=@pmDiasMora,TarifMora=@pmTarifMora,VrIntMora=@pmVrIntMora,VrIMoraOtr=@pmVrIMoraOtr,VrTotalSAF=@pmVrTotalSAF,VrSAFUpc=@pmVrSAFUpc,VrTotalPagar=@pmVrTotalPagar,VrTotalFon=@pmVrTotalFon,TotalAfi=@pmTotalAfi,NumAutLic=@pmNumAutLic,NumAutInc=@pmNumAutInc,NumPlaSAF=@pmNumPlaSAF
,TipoReg=@pmTipoReg
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAportante @pmIdAportnte VARCHAR(16),@pmRazonSocial VARCHAR(200),@pmTipoId CHAR(1),@pmDvNit CHAR(1),@pmIdRepLegal VARCHAR(16),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pme_mail VARCHAR(100),@pmClaseApo CHAR(1),@pmTipoEco CHAR(1),@pmTipoPersna CHAR(1)
,@pmCIIUDane VARCHAR(8),@pmFecIniCndato SMALLDATETIME,@pmTipAccCndato INT,@pmFecFinCndato SMALLDATETIME,@pmCodOperInfo VARCHAR(10),@pmTipoApo CHAR(1),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE NomAportante SET RazonSocial=@pmRazonSocial,TipoId=@pmTipoId,DvNit=@pmDvNit,IdRepLegal=@pmIdRepLegal,Direccion=@pmDireccion,IdLocal=@pmIdLocal,Telefono=@pmTelefono,Fax=@pmFax,e_mail=@pme_mail,ClaseApo=@pmClaseApo,TipoEco=@pmTipoEco,TipoPersna=@pmTipoPersna,CIIUDane=@pmCIIUDane,FecIniCndato=@pmFecIniCndato,TipAccCndato=@pmTipAccCndato
,FecFinCndato=@pmFecFinCndato,CodOperInfo=@pmCodOperInfo,FecUpdate=@pmFecUpdate,TipoApo=@pmTipoApo
 WHERE IdAportnte=@pmIdAportnte
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAstncia @pmIdEmpleado VARCHAR(16),@pmFechaDia SMALLDATETIME,@pmHHDiurno DECIMAL(10,4),@pmHHNocturno DECIMAL(10,4),@pmHEDiurno DECIMAL(10,4),@pmHENocturno DECIMAL(10,4)
,@pmTipoJrnada VARCHAR(10),@pmEsDmncal BIT,@pmHorario VARCHAR(30),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomAstncia SET HHDiurno=@pmHHDiurno,HHNocturno=@pmHHNocturno,HEDiurno=@pmHEDiurno,HENocturno=@pmHENocturno,TipoJrnada=@pmTipoJrnada,EsDmncal=@pmEsDmncal,Horario=@pmHorario,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND FechaDia=@pmFechaDia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAutoliq @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmFecha SMALLDATETIME,@pmPerAutoliq VARCHAR(6),@pmPerAutoSal VARCHAR(6),@pmFechaPago SMALLDATETIME,@pmIdAportnte VARCHAR(16),@pmPresentacion CHAR(1),@pmCodCia CHAR(2),@pmCodFondo VARCHAR(8),@pmCodArp VARCHAR(8),@pmCodOper VARCHAR(10),@pmEsCorr BIT,@pmPerCorr VARCHAR(6),@pmNumCorr VARCHAR(20),@pmTotalEmp INT,@pmVrNomina MONEY,@pmVrPension MONEY,@pmVrSalud MONEY,@pmVrRiesgos MONEY,@pmVrCajas MONEY
,@pmVrSena MONEY,@pmVrIcbf MONEY,@pmVrEsap MONEY,@pmVrMinEduc MONEY,@pmTotalAfipen INT,@pmTotalAfiSal INT,@pmTotalAfiRie INT,@pmTotalAfiCaj INT,@pmTotalAfiSen INT,@pmTotalAfiIcb INT,@pmTotalAfiEsa INT,@pmTotalAfiEduc INT,@pmTotAdmPen INT,@pmTotAdmSal INT,@pmTotAdmRie INT,@pmTotAdmCom INT,@pmObservacion VARCHAR(250),@pmLogAdmon VARCHAR(250),@pmRedoMiles BIT,@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME,@pmTipoPlanilla VARCHAR(3)
 AS 
 UPDATE Trn_NomAutoliq SET Fecha=@pmFecha,PerAutoliq=@pmPerAutoliq,PerAutoSal=@pmPerAutoSal,FechaPago=@pmFechaPago,IdAportnte=@pmIdAportnte,Presentacion=@pmPresentacion,CodCia=@pmCodCia,CodFondo=@pmCodFondo,CodArp=@pmCodArp,CodOper=@pmCodOper,EsCorr=@pmEsCorr,PerCorr=@pmPerCorr,NumCorr=@pmNumCorr,TotalEmp=@pmTotalEmp,VrNomina=@pmVrNomina,VrPension=@pmVrPension,VrSalud=@pmVrSalud,VrRiesgos=@pmVrRiesgos,VrCajas=@pmVrCajas,VrSena=@pmVrSena,VrIcbf=@pmVrIcbf,VrEsap=@pmVrEsap,VrMinEduc=@pmVrMinEduc
,TotalAfipen=@pmTotalAfipen,TotalAfiSal=@pmTotalAfiSal,TotalAfiRie=@pmTotalAfiRie,TotalAfiCaj=@pmTotalAfiCaj,TotalAfiSen=@pmTotalAfiSen,TotalAfiIcb=@pmTotalAfiIcb,TotalAfiEsa=@pmTotalAfiEsa,TotalAfiEduc=@pmTotalAfiEduc,TotAdmPen=@pmTotAdmPen,TotAdmSal=@pmTotAdmSal,TotAdmRie=@pmTotAdmRie,TotAdmCom=@pmTotAdmCom,Observacion=@pmObservacion,LogAdmon=@pmLogAdmon,RedoMiles=@pmRedoMiles,Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,TipoPlanilla=@pmTipoPlanilla
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomAutoliqAnu @pmTipoLiq VARCHAR(3),@pmNumero VARCHAR(20),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomAutoliq SET Observacion=@pmObservacion,Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomCAdmon @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmClaseAdmon VARCHAR(10),@pmFecIniNov SMALLDATETIME,@pmNNovedad INT
,@pmIdFondo VARCHAR(8),@pmIdFonNew VARCHAR(8),@pmObservacion VARCHAR(100),@pmAnulado BIT
 AS 
 UPDATE Trn_NomCAdmon SET Fecha=@pmFecha,NContrato=@pmNContrato,ClaseAdmon=@pmClaseAdmon,FecIniNov=@pmFecIniNov,NNovedad=@pmNNovedad,IdFondo=@pmIdFondo,IdFonNew=@pmIdFonNew
,Observacion=@pmObservacion,Anulado=@pmAnulado
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomCAdmonAnu @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmAnulado BIT
,@pmObservacion VARCHAR(100)
 AS 
 UPDATE Trn_NomCAdmon SET Observacion=@pmObservacion,Anulado=@pmAnulado
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomCausalesRet @pmIdCausa VARCHAR(4),@pmCausalRet VARCHAR(100),@pmIndmnizar BIT,@pmInactivo BIT
 AS 
 UPDATE NomCausalesRet SET CausalRet=@pmCausalRet,Indmnizar=@pmIndmnizar,Inactivo=@pmInactivo
 WHERE IdCausa=@pmIdCausa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomCertf @pmIdEmpleado VARCHAR(16),@pmNumCertf VARCHAR(20),@pmnAnno INT,@pmnMes INT,@pmVrCertf MONEY
,@pmObservacion VARCHAR(100),@pmClaseCert VARCHAR(10),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomCertf SET nAnno=@pmnAnno,nMes=@pmnMes,VrCertf=@pmVrCertf,Observacion=@pmObservacion,ClaseCert=@pmClaseCert,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND NumCertf=@pmNumCertf
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomConAuto @pmIdReg INT,@pmIdConcepto VARCHAR(4),@pmFecInicio SMALLDATETIME,@pmVrAplicar MONEY,@pmModalidad CHAR(1),@pmOrigenBase CHAR(2),@pmFrecAplica VARCHAR(10)
,@pmCedEmpleado VARCHAR(16),@pmCodNom VARCHAR(4),@pmCodDep VARCHAR(4),@pmCodCcto VARCHAR(16),@pmCritSelect VARCHAR(4000),@pmFecFinal SMALLDATETIME,@pmExceptuar VARCHAR(8),@pmObservacion VARCHAR(50)
,@pmCodSubCos VARCHAR(16),@pmInactivo BIT,@pmNumPerMes INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomConAuto SET IdConcepto=@pmIdConcepto,FecInicio=@pmFecInicio,VrAplicar=@pmVrAplicar,Modalidad=@pmModalidad,OrigenBase=@pmOrigenBase,FrecAplica=@pmFrecAplica,CedEmpleado=@pmCedEmpleado,CodNom=@pmCodNom
,CodDep=@pmCodDep,CodCcto=@pmCodCcto,CritSelect=@pmCritSelect,FecFinal=@pmFecFinal,Exceptuar=@pmExceptuar,Observacion=@pmObservacion,Inactivo=@pmInactivo,CodSubCos=@pmCodSubCos,NumPerMes=@pmNumPerMes, FecUpdate=@pmFecUpdate
 WHERE IdReg=@pmIdReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomConCuentas @pmIdConcepto VARCHAR(4),@pmItem INT,@pmNClase INT,@pmIdCuenta VARCHAR(16)
,@pmTipoMov CHAR(1),@pmCodNom VARCHAR(4),@pmCodDep VARCHAR(4),@pmCodFon VARCHAR(8),@pmNitTerc CHAR(1)
 AS 
 UPDATE NomConCuentas SET NClase=@pmNClase,IdCuenta=@pmIdCuenta,TipoMov=@pmTipoMov,CodNom=@pmCodNom
,CodDep=@pmCodDep,CodFon=@pmCodFon,NitTerc=@pmNitTerc
 WHERE IdConcepto=@pmIdConcepto AND  Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomConOpciones @pmIdConcepto VARCHAR(4),@pmIdOpc INT,@pmNomOpcion VARCHAR(50),@pmAsignado BIT
 AS 
 UPDATE NomConOpciones SET NomOpcion=@pmNomOpcion,Asignado=@pmAsignado
 WHERE IdConcepto=@pmIdConcepto AND IdOpc=@pmIdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomConceptos @pmIdConcepto VARCHAR(4),@pmConcepto VARCHAR(100),@pmClaseCon VARCHAR(10),@pmUnidad VARCHAR(10),@pmTarifa DECIMAL(10,4)
,@pmBaseTrfa CHAR(2),@pmPdtermnado BIT,@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE NomConceptos SET Concepto=@pmConcepto,ClaseCon=@pmClaseCon,Unidad=@pmUnidad,Tarifa=@pmTarifa,BaseTrfa=@pmBaseTrfa,Pdtermnado=@pmPdtermnado,Inactivo=@pmInactivo
,FechaUpdate=@pmFechaUpdate
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomDefault @pmnAnno INT,@pmIdOpc VARCHAR(3),@pmDescripcion VARCHAR(100),@pmValor MONEY
 AS 
 UPDATE NomDefault SET Descripcion=@pmDescripcion,Valor=@pmValor
 WHERE nAnno=@pmnAnno AND IdOpc=@pmIdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomDetalle @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmItem INT,@pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantDevg DECIMAL(10,4),@pmCantDed DECIMAL(10,4),@pmVrUnitario MONEY,@pmVrTotDevg MONEY
,@pmVrTotDed MONEY,@pmUnidad VARCHAR(10),@pmClaseLiq VARCHAR(10),@pmVrOrigen MONEY,@pmTarifa DECIMAL(10,4),@pmDiasCalc DECIMAL(10,4),@pmDiasNov DECIMAL(10,4),@pmCodFondo VARCHAR(8),@pmNPrestamo INT,@pmNCuota INT,@pmOrigCargue VARCHAR(10),@pmVrBaseLiq MONEY
 AS 
 UPDATE Trn_NomDetalle SET IdEmpleado=@pmIdEmpleado,IdConcepto=@pmIdConcepto,Detalle=@pmDetalle,CantDevg=@pmCantDevg,CantDed=@pmCantDed,VrUnitario=@pmVrUnitario,VrTotDevg=@pmVrTotDevg,VrTotDed=@pmVrTotDed,Unidad=@pmUnidad,ClaseLiq=@pmClaseLiq
,VrOrigen=@pmVrOrigen,Tarifa=@pmTarifa,DiasCalc=@pmDiasCalc,DiasNov=@pmDiasNov,CodFondo=@pmCodFondo,NPrestamo=@pmNPrestamo,NCuota=@pmNCuota,OrigCargue=@pmOrigCargue,VrBaseLiq=@pmVrBaseLiq
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomFijos @pmIdEmpleado VARCHAR(16),@pmnAnno INT,@pmnMes INT,@pmTarifa DECIMAL(10,4),@pmVrBase MONEY,@pmVrSalarios MONEY,@pmVrPrima MONEY,@pmVrVacacnes MONEY,@pmVrSIntExcto MONEY,@pmVrOtrosIng MONEY,@pmVrIngresos MONEY
,@pmVrPensiones MONEY,@pmVrDedcido MONEY,@pmVrExcento MONEY,@pmVrCertEdu MONEY,@pmVrCertVda MONEY,@pmVrCertAFC MONEY,@pmVrCertfcado MONEY,@pmPeriodos DECIMAL(10,4),@pmVrTotalBase MONEY,@pmOrignPorc VARCHAR(10),@pmFecUpdate SMALLDATETIME
,@pmVrSalud MONEY 
 AS 
 UPDATE Trn_NomFijos SET Tarifa=@pmTarifa,VrBase=@pmVrBase,VrSalarios=@pmVrSalarios,VrPrima=@pmVrPrima,VrVacacnes=@pmVrVacacnes,VrSIntExcto=@pmVrSIntExcto,VrOtrosIng=@pmVrOtrosIng,VrIngresos=@pmVrIngresos,VrPensiones=@pmVrPensiones,VrDedcido=@pmVrDedcido
,VrExcento=@pmVrExcento,VrCertEdu=@pmVrCertEdu,VrCertVda=@pmVrCertVda,VrCertAFC=@pmVrCertAFC,VrCertfcado=@pmVrCertfcado,Periodos=@pmPeriodos,VrTotalBase=@pmVrTotalBase,OrignPorc=@pmOrignPorc,FecUpdate=@pmFecUpdate
,VrSalud=@pmVrSalud
 WHERE IdEmpleado=@pmIdEmpleado AND nAnno=@pmnAnno AND nMes=@pmnMes
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomLiquida @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmFecha SMALLDATETIME,@pmSemstre INT,@pmVrTotal MONEY,@pmVrTotDed MONEY,@pmVrAplicado MONEY
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4), @pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomLiquida SET Fecha=@pmFecha,Semstre=@pmSemstre,VrTotal=@pmVrTotal,VrTotDed=@pmVrTotDed,VrAplicado=@pmVrAplicado,TipCom=@pmTipCom,Comprobante=@pmComprobante
,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomLiquidaAnu @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmAnulado BIT,@pmIdEstado VARCHAR(4), @pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomLiquida SET Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipoLiq=@pmTipoLiq AND Numero=@pmNumero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomLiquidaPar @pmTipoLiq VARCHAR(3),@pmNumero INT,@pmIdEstado VARCHAR(4),@pmAplica BIT
 AS 
 IF @pmAplica<>0
  UPDATE Trn_NomLiquida SET VrAplicado=VrTotal,IdEstado=@pmIdEstado  WHERE  TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Anulado=0
 ELSE
  UPDATE Trn_NomLiquida SET VrAplicado=0,IdEstado=@pmIdEstado  WHERE  TipoLiq=@pmTipoLiq AND Numero=@pmNumero AND Anulado=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomLogVeh @pmIdEmpleado VARCHAR(16),@pmNumReg INT,@pmFecha SMALLDATETIME,@pmpVehAnterior VARCHAR(10)
,@pmNitPropAnt VARCHAR(16),@pmpVehNew VARCHAR(10),@pmNitPropNew VARCHAR(16),@pmObservacion VARCHAR(100),@pmNitCondAnt VARCHAR(16)
 AS 
 UPDATE Trn_NomLogVeh SET Fecha=@pmFecha,pVehAnterior=@pmpVehAnterior,NitPropAnt=@pmNitPropAnt,pVehNew=@pmpVehNew,NitPropNew=@pmNitPropNew
,Observacion=@pmObservacion,NitCondAnt=@pmNitCondAnt
 WHERE IdEmpleado=@pmIdEmpleado AND NumReg=@pmNumReg
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomNov @pmIdPeriodo VARCHAR(8),@pmIdEmpleado VARCHAR(16),@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantidad DECIMAL(10,4),@pmVrUnitario MONEY
,@pmVrTotal MONEY,@pmVrOrigen MONEY,@pmTarifa DECIMAL(10,4),@pmOrigCargue VARCHAR(10),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomNov SET IdConcepto=@pmIdConcepto,Detalle=@pmDetalle,Cantidad=@pmCantidad,VrUnitario=@pmVrUnitario,VrTotal=@pmVrTotal,VrOrigen=@pmVrOrigen
,Tarifa=@pmTarifa,OrigCargue=@pmOrigCargue,FecUpdate=@pmFecUpdate
 WHERE IdPeriodo=@pmIdPeriodo AND IdEmpleado=@pmIdEmpleado AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomNovedad @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmIdConcepto VARCHAR(4),@pmFecInicio SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCantDias INT,@pmDiaInicio INT
,@pmModalidad VARCHAR(10),@pmIdNov VARCHAR(4),@pmRemnrado BIT,@pmObservacion VARCHAR(250),@pmCodInstla VARCHAR(4),@pmInactivo BIT,@pmCodClsRie VARCHAR(4),@pmNumAutza VARCHAR(30),@pmCodNewFon VARCHAR(8)
,@pmCodOldFon VARCHAR(8),@pmVrCotVolAfi MONEY,@pmVrCotVolApo MONEY,@pmVrNoRetenido MONEY,@pmTipoIncap CHAR(1),@pmFechaDcto SMALLDATETIME,@pmVrDescuento MONEY,@pmTipoBaseLiq VARCHAR(3),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomNovedad SET IdConcepto=@pmIdConcepto,FecInicio=@pmFecInicio,FecFinal=@pmFecFinal,CantDias=@pmCantDias,DiaInicio=@pmDiaInicio,Modalidad=@pmModalidad,IdNov=@pmIdNov,Remnrado=@pmRemnrado
,Observacion=@pmObservacion,Inactivo=@pmInactivo,CodInstla=@pmCodInstla,CodClsRie=@pmCodClsRie,NumAutza=@pmNumAutza,CodNewFon=@pmCodNewFon,FecUpdate=@pmFecUpdate
,VrCotVolAfi=@pmVrCotVolAfi,VrCotVolApo=@pmVrCotVolApo,VrNoRetenido=@pmVrNoRetenido,TipoIncap=@pmTipoIncap,CodOldFon=@pmCodOldFon,FechaDcto=@pmFechaDcto,VrDescuento=@pmVrDescuento,TipoBaseLiq=@pmTipoBaseLiq
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomNovedadAnu @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmInactivo BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomNovedad SET Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrest @pmNPrestamo INT,@pmFecha SMALLDATETIME,@pmIdEmpleado VARCHAR(16),@pmIdConcepto VARCHAR(4),@pmVrPrestamo MONEY,@pmNumCuotas INT,@pmVrCuota MONEY
,@pmFecInicio SMALLDATETIME,@pmFrecDcto VARCHAR(10),@pmExceptuar VARCHAR(8),@pmTotPrestamo MONEY,@pmTotAbonado MONEY,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3)
,@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmCxpagar BIT,@pmDsctNom INT,@pmDsctPri INT,@pmDsctVac INT,@pmDsctEspPer VARCHAR(8),@pmDsctEspVal MONEY
,@pmVrTmAbono MONEY,@pmNumPerMes INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomPrest SET Fecha=@pmFecha,IdEmpleado=@pmIdEmpleado,IdConcepto=@pmIdConcepto,VrPrestamo=@pmVrPrestamo,NumCuotas=@pmNumCuotas,VrCuota=@pmVrCuota,FecInicio=@pmFecInicio
,FrecDcto=@pmFrecDcto,Exceptuar=@pmExceptuar,TotPrestamo=@pmTotPrestamo,TotAbonado=@pmTotAbonado,Anulado=@pmAnulado,FecDev=@pmFecDev,TipCom=@pmTipCom,Comprobante=@pmComprobante
,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Cxpagar=@pmCxpagar,DsctNom=@pmDsctNom,DsctPri=@pmDsctPri,DsctVac=@pmDsctVac,DsctEspPer=@pmDsctEspPer,DsctEspVal=@pmDsctEspVal
,NumPerMes=@pmNumPerMes,VrTmAbono=@pmVrTmAbono,FecUpdate=@pmFecUpdate
 WHERE NPrestamo=@pmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestAbn @pmNPrestamo INT,@pmTotAbonado MONEY
 AS 
 UPDATE Trn_NomPrest SET TotAbonado=@pmTotAbonado
 WHERE NPrestamo=@pmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestAbo @pmIdEmpleado VARCHAR(16)=Null,@pmNPrestamo INT=Null
 AS 
 UPDATE Trn_NomPrest 
 SET TotAbonado=(SELECT ISNULL(SUM(VrAbono),0) 
 FROM Trn_NomAbonos  WHERE  Trn_NomPrest.NPrestamo=Trn_NomAbonos.NPrestamo)
 WHERE Anulado=0 AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestAnu @pmNPrestamo INT,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomPrest SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=ISNULL(@pmObservacion,Observacion)
 ,FecUpdate=@pmFecUpdate
 WHERE NPrestamo=@pmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestCan @pmNPrestamo INT,@pmTotAbonado MONEY,@pmObservacion VARCHAR(250),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomPrest SET TotAbonado=@pmTotAbonado,Observacion=@pmObservacion,FecUpdate=@pmFecUpdate
 WHERE NPrestamo=@pmNPrestamo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestCero @pmIdEmpleado VARCHAR(16)=Null,@pmNPrestamo INT=Null
 AS 
 UPDATE Trn_NomPrest SET TotAbonado=0,VrTmAbono=0
 WHERE Anulado=0 AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestTAbo @pmIdEmpleado VARCHAR(16)=Null,@pmIdPeriodo VARCHAR(8)=Null
,@pmDscto VARCHAR(3)=Null,@pmNPrestamo INT=Null
 AS 
 UPDATE Trn_NomPrest 
 SET VrTmAbono=(SELECT ISNULL(SUM(VrAbono),0) 
           FROM Trn_NomAbonos
          WHERE Trn_NomPrest.NPrestamo=Trn_NomAbonos.NPrestamo
         AND IdPeriodo LIKE ISNULL(@pmIdPeriodo,'%') AND Dscto LIKE ISNULL(@pmDscto,'%') )
 WHERE Anulado=0 AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
 AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomPrestTCero @pmIdEmpleado VARCHAR(16)=Null,@pmNPrestamo INT=Null
 AS 
 UPDATE Trn_NomPrest SET VrTmAbono=0
 WHERE Anulado=0 AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%') 
AND (NPrestamo>=ISNULL(@pmNPrestamo,0)  AND NPrestamo<=ISNULL(@pmNPrestamo,2147483647))
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomRangosFsu @pmIdRango INT,@pmVrInicial DECIMAL(12,2),@pmVrFinal DECIMAL(12,2),@pmTarifa DECIMAL(10,4)
 AS 
 UPDATE NomRangosFsu SET VrInicial=@pmVrInicial,VrFinal=@pmVrFinal,Tarifa=@pmTarifa
 WHERE IdRango=@pmIdRango
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomRet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmIdCausa VARCHAR(4),@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmDiasTrab INT,@pmDiasNoRem INT,@pmVrTotDev MONEY,@pmVrTotDed MONEY,@pmVrTotCes MONEY,@pmVrTotPri MONEY,@pmVrTotVac MONEY,@pmVrTotInt MONEY
,@pmVrTotInd MONEY,@pmVrNeto MONEY,@pmVrBaseCes MONEY,@pmVrBaseVac MONEY,@pmIdPeriodo VARCHAR(8),@pmKeyReg VARCHAR(30),@pmNPerVac INT,@pmVrSalario MONEY,@pmIdDep VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdNom VARCHAR(4),@pmTipoLiq CHAR(1),@pmpVehiculo VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmVrParCes MONEY,@pmVrParInt MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomRet SET Fecha=@pmFecha,NContrato=@pmNContrato,IdCausa=@pmIdCausa,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,DiasTrab=@pmDiasTrab,DiasNoRem=@pmDiasNoRem,VrTotDev=@pmVrTotDev,VrTotDed=@pmVrTotDed,VrTotCes=@pmVrTotCes,VrTotPri=@pmVrTotPri,VrTotVac=@pmVrTotVac,VrTotInt=@pmVrTotInt,VrTotInd=@pmVrTotInd,VrNeto=@pmVrNeto,VrBaseCes=@pmVrBaseCes,TipoLiq=@pmTipoLiq
,VrBaseVac=@pmVrBaseVac,IdPeriodo=@pmIdPeriodo,KeyReg=@pmKeyReg,NPerVac=@pmNPerVac,VrSalario=@pmVrSalario,IdDep=@pmIdDep,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdNom=@pmIdNom,pVehiculo=@pmpVehiculo,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,Anulado=@pmAnulado,IdEstado=@pmIdEstado
, VrParCes=@pmVrParCes,VrParInt=@pmVrParInt,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomRetAnu @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmAnulado BIT,@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomRet SET Anulado=@pmAnulado,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomSueldos @pmNumero INT,@pmIdEmpleado VARCHAR(16),@pmFecha SMALLDATETIME,@pmNContrato INT,@pmVrSueAnterior MONEY,@pmVrSueNuevo MONEY,@pmVrAtAnterior MONEY,@pmVrAtNuevo MONEY
,@pmMetodoUp CHAR(1),@pmReemplzado BIT,@pmFecUltAumto SMALLDATETIME,@pmCritSelect VARCHAR(250),@pmObservacion VARCHAR(100),@pmTimeSys SMALLDATETIME,@pmIdUsuario VARCHAR(11)
 AS 
 UPDATE Trn_NomSueldos SET Fecha=@pmFecha,NContrato=@pmNContrato,VrSueAnterior=@pmVrSueAnterior,VrSueNuevo=@pmVrSueNuevo,VrAtAnterior=@pmVrAtAnterior,VrAtNuevo=@pmVrAtNuevo,MetodoUp=@pmMetodoUp
,Reemplzado=@pmReemplzado,FecUltAumto=@pmFecUltAumto,CritSelect=@pmCritSelect,Observacion=@pmObservacion
 WHERE Numero=@pmNumero AND IdEmpleado=@pmIdEmpleado
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomTablaRet @pmnAnno INT,@pmnIntrvalo INT,@pmVrInicial MONEY,@pmVrFinal MONEY,@pmTarifa DECIMAL(10,4),@pmVrRetncion MONEY
,@pmTarExcdnte DECIMAL(10,4),@pmVrBaseExc MONEY,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE NomTablaRet SET VrInicial=@pmVrInicial,VrFinal=@pmVrFinal,Tarifa=@pmTarifa,VrRetncion=@pmVrRetncion,TarExcdnte=@pmTarExcdnte,VrBaseExc=@pmVrBaseExc
,FechaUpdate=@pmFechaUpdate
 WHERE nAnno=@pmnAnno AND nIntrvalo=@pmnIntrvalo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomVac @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmFecha SMALLDATETIME,@pmNContrato INT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCantidad DECIMAL(10,4),@pmVrLiquida MONEY,@pmVrBase MONEY,@pmFecIniPmd SMALLDATETIME,@pmFecFinPmd SMALLDATETIME,@pmDiasBase INT,@pmDiasNoRem INT
,@pmDiasVac INT,@pmNNovedad INT,@pmNPeriodos INT,@pmCantRem INT,@pmVrRemndo MONEY,@pmVrTotDed MONEY,@pmVrSalario MONEY,@pmpVehiculo VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmLogPmdio VARCHAR(600),@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomVac SET Fecha=@pmFecha,NContrato=@pmNContrato,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,Cantidad=@pmCantidad,VrLiquida=@pmVrLiquida,VrBase=@pmVrBase,FecIniPmd=@pmFecIniPmd,FecFinPmd=@pmFecFinPmd,DiasBase=@pmDiasBase,DiasNoRem=@pmDiasNoRem,DiasVac=@pmDiasVac,NNovedad=@pmNNovedad,NPeriodos=@pmNPeriodos
,CantRem=@pmCantRem,VrRemndo=@pmVrRemndo,VrTotDed=@pmVrTotDed,VrSalario=@pmVrSalario,pVehiculo=@pmpVehiculo,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,LogPmdio=@pmLogPmdio,IdEstado=@pmIdEstado,Anulado=@pmAnulado,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomVacAnu @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmIdEstado VARCHAR(4),@pmAnulado BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NomVac SET IdEstado=@pmIdEstado,Anulado=@pmAnulado,FecUpdate=@pmFecUpdate
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomVacDet @pmIdEmpleado VARCHAR(16),@pmNumero INT,@pmItem INT,@pmIdConcepto VARCHAR(4),@pmDetalle VARCHAR(100),@pmCantidad INT,@pmVrDevgado MONEY,@pmVrDeducido MONEY
,@pmTarifa DECIMAL(10,4),@pmVrBaseLiq MONEY,@pmVrCertfcado MONEY,@pmVrPension MONEY,@pmNPrestamo INT,@pmNCuota INT,@pmIdPeriodo VARCHAR(8),@pmVrSalud MONEY
 AS 
 UPDATE Trn_NomVacDet SET IdConcepto=@pmIdConcepto,Detalle=@pmDetalle,Cantidad=@pmCantidad,VrDevgado=@pmVrDevgado,VrDeducido=@pmVrDeducido,Tarifa=@pmTarifa,VrBaseLiq=@pmVrBaseLiq,VrCertfcado=@pmVrCertfcado
,VrPension=@pmVrPension,VrSalud=@pmVrSalud,NPrestamo=@pmNPrestamo,NCuota=@pmNCuota,IdPeriodo=@pmIdPeriodo
 WHERE IdEmpleado=@pmIdEmpleado AND Numero=@pmNumero AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomVehiculos @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmClaseVeh VARCHAR(50),@pmTipoAdmon VARCHAR(50)
,@pmGrupoProp VARCHAR(50),@pmVehPropio BIT,@pmCedCond VARCHAR(16),@pmConductor VARCHAR(50)
 AS 
 UPDATE NomVehiculos SET NumVeh=@pmNumVeh,ClaseVeh=@pmClaseVeh,TipoAdmon=@pmTipoAdmon,GrupoProp=@pmGrupoProp
,VehPropio=@pmVehPropio,CedCond=@pmCedCond,Conductor=@pmConductor
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNomina @pmIdPeriodo VARCHAR(8),@pmKeyRegistro VARCHAR(30),@pmIdEmpleado VARCHAR(16),@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCantidad DECIMAL(10,4),@pmVrDevengado MONEY,@pmVrDeducido MONEY,@pmNContrato INT,@pmIdDep VARCHAR(4)
,@pmIdInstala VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdNom VARCHAR(4),@pmVrSalario MONEY,@pmVrATransMes MONEY,@pmpVehiculo VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmEsRetiro BIT,@pmNLiqRet INT,@pmTipoLiq CHAR(1),@pmCantNoRem DECIMAL(10,4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Nomina SET IdEmpleado=@pmIdEmpleado,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,Cantidad=@pmCantidad,VrDevengado=@pmVrDevengado,VrDeducido=@pmVrDeducido,NContrato=@pmNContrato,IdDep=@pmIdDep,IdInstala=@pmIdInstala,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdNom=@pmIdNom
,VrSalario=@pmVrSalario,VrATransMes=@pmVrATransMes,pVehiculo=@pmpVehiculo,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,EsRetiro=@pmEsRetiro,FecUpdate=@pmFecUpdate,NLiqRet=@pmNLiqRet,TipoLiq=@pmTipoLiq,CantNoRem=@pmCantNoRem
 WHERE IdPeriodo=@pmIdPeriodo AND KeyRegistro=@pmKeyRegistro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNominaCmp @pmIdPeriodo VARCHAR(8),@pmIdNom VARCHAR(4),@pmEsRetiro BIT,@pmIdEmpleado VARCHAR(16)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Nomina SET TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom
,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE IdPeriodo=@pmIdPeriodo  AND IdNom=@pmIdNom  AND IdEmpleado LIKE ISNULL(@pmIdEmpleado,'%')
 AND  (EsRetiro=ISNULL(@pmEsRetiro,0) or EsRetiro=ISNULL(@pmEsRetiro,1))

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNotaProd @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16)
,@pmVrSubTotal MONEY,@pmVrCostos MONEY,@pmCantEnt DECIMAL(14,4),@pmCantSal DECIMAL(14,4),@pmNumOrden INT,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NotaProd SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdTercero=@pmIdTercero,VrSubTotal=@pmVrSubTotal,VrCostos=@pmVrCostos,CantEnt=@pmCantEnt,CantSal=@pmCantSal
,NumOrden=@pmNumOrden,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante
,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNotaProdDev @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_NotaProd SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpNotas @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFechaVence SMALLDATETIME,@pmVrSubTotal MONEY,@pmVrImpuesto MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrCosto MONEY,@pmVrSancion MONEY,@pmVrOtros MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY
,@pmVrAnticipo MONEY,@pmCantidad DECIMAL(14,4),@pmBaseImp MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4)
,@pmCodTarCom VARCHAR(4),@pmCdCajero VARCHAR(11),@pmIdLocal VARCHAR(8),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2),@pmFecDcm SMALLDATETIME,@pmCdProducto VARCHAR(16),@pmCdForma VARCHAR(4),@pmNumCheque VARCHAR(20),@pmCdBanco VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmPrestCaja BIT
,@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Notas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FechaVence=@pmFechaVence,VrSubTotal=@pmVrSubTotal,VrImpuesto=@pmVrImpuesto,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrCosto=@pmVrCosto,VrSancion=@pmVrSancion,VrOtros=@pmVrOtros,VrNeto=@pmVrNeto,VrAplicado=@pmVrAplicado,VrAnticipo=@pmVrAnticipo,Cantidad=@pmCantidad,BaseImp=@pmBaseImp
,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarIva=@pmCodTarIva,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,CdCajero=@pmCdCajero,IdLocal=@pmIdLocal,Referencia=@pmReferencia
,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,FecDcm=@pmFecDcm,CdProducto=@pmCdProducto,CdForma=@pmCdForma,NumCheque=@pmNumCheque,CdBanco=@pmCdBanco,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,PrestCaja=@pmPrestCaja,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion
,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpNotasBuses @pmTipDoc VARCHAR(3),@pmNumNota INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmValorTotal MONEY,@pmValorIva MONEY,@pmValorReteFte MONEY,@pmValorReteIca MONEY,@pmValorCosto MONEY,@pmValorSancion MONEY,@pmValorNeto MONEY,@pmValorAplicado MONEY
,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmIdTarCom VARCHAR(4),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmPrestCaja BIT,@pmIntMora BIT,@pmNumCheque VARCHAR(20),@pmIdBanco VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdClase VARCHAR(4),@pmIdCargo VARCHAR(4),@pmReferencia VARCHAR(20),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmBaseIva MONEY,@pmBaseRet MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmIdTarIva VARCHAR(4)
,@pmIdTarRet VARCHAR(4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_NotasBuses SET Fecha=@pmFecha,FecVence=@pmFecVence,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,VehPropio=@pmVehPropio,ValorTotal=@pmValorTotal,ValorIva=@pmValorIva,ValorReteFte=@pmValorReteFte,ValorReteIca=@pmValorReteIca,ValorCosto=@pmValorCosto,ValorSancion=@pmValorSancion,ValorNeto=@pmValorNeto,ValorAplicado=@pmValorAplicado,IdVendedor=@pmIdVendedor,Comision=@pmComision
,IdTarCom=@pmIdTarCom,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,PrestCaja=@pmPrestCaja,IntMora=@pmIntMora,NumCheque=@pmNumCheque,IdBanco=@pmIdBanco,IdCCosto=@pmIdCCosto,IdClase=@pmIdClase,IdCargo=@pmIdCargo,Referencia=@pmReferencia,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,BaseIva=@pmBaseIva,BaseRet=@pmBaseRet,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,IdTarIva=@pmIdTarIva,IdTarRet=@pmIdTarRet,TipCom=@pmTipCom
,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND NumNota=@pmNumNota AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOComb @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaDesp SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdVend VARCHAR(16)
,@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250),@pmIdLocal VARCHAR(8),@pmLugarEnv VARCHAR(50),@pmDiasEntraga INT,@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150)
,@pmCdRuta VARCHAR(4),@pmReferncia VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(150),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmTipGui VARCHAR(3),@pmGuia INT,@pmIdCiaGui CHAR(2),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2)
,@pmFechaFact SMALLDATETIME,@pmAutzaMora INT,@pmAutzaCupo INT,@pmAprobacion INT,@pmFechaAprob SMALLDATETIME,@pmDetalleAprob VARCHAR(250),@pmCdUsuAprob VARCHAR(11),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_OComb SET Fecha=@pmFecha,FechaDesp=@pmFechaDesp,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrCargos=@pmVrCargos
,VrOtrDcto=@pmVrOtrDcto,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,DirEnvio=@pmDirEnvio,IdLocal=@pmIdLocal,LugarEnv=@pmLugarEnv,DiasEntraga=@pmDiasEntraga,IdVehiculo=@pmIdVehiculo
,IdConductor=@pmIdConductor,NitEmpTrans=@pmNitEmpTrans,EmpTrans=@pmEmpTrans,CdRuta=@pmCdRuta,Referncia=@pmReferncia,IdForma=@pmIdForma,DetallePago=@pmDetallePago,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,TipGui=@pmTipGui,Guia=@pmGuia,IdCiaGui=@pmIdCiaGui,TipFac=@pmTipFac,Factura=@pmFactura
,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact,TipPed=@pmTipPed,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,AutzaMora=@pmAutzaMora,AutzaCupo=@pmAutzaCupo,Aprobacion=@pmAprobacion,FechaAprob=@pmFechaAprob,DetalleAprob=@pmDetalleAprob,CdUsuAprob=@pmCdUsuAprob,Anulado=@pmAnulado
,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,ZonaFrontera=@pmZonaFrontera,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCombAbr @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
,@pmTipGui VARCHAR(3),@pmGuia INT,@pmIdCiaGui CHAR(2),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2)
,@pmFechaFact SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_OComb SET  TipGui=@pmTipGui,Guia=@pmGuia,IdCiaGui=@pmIdCiaGui,TipFac=@pmTipFac,Factura=@pmFactura
,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCombAnu @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OComb SET  Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCombApr @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
,@pmAprobacion INT,@pmFechaAprob SMALLDATETIME,@pmDetalleAprob VARCHAR(250),@pmCdUsuAprob VARCHAR(11)
,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OComb SET Aprobacion=@pmAprobacion,FechaAprob=@pmFechaAprob,DetalleAprob=@pmDetalleAprob
,CdUsuAprob=@pmCdUsuAprob,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCombFac @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OComb SET TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCombGui @pmTipDoc VARCHAR(3),@pmOCargue INT,@pmIdCia CHAR(2)
,@pmTipGui VARCHAR(3),@pmGuia INT,@pmIdCiaGui CHAR(2)
 AS 
 UPDATE Trn_OComb SET TipGui=@pmTipGui,Guia=@pmGuia,IdCiaGui=@pmIdCiaGui
 WHERE TipDoc=@pmTipDoc AND OCargue=@pmOCargue AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCompra @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdProv VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY
,@pmVrOtros MONEY,@pmVrCargos MONEY,@pmVrOtrDcto MONEY,@pmVrSobtasa MONEY,@pmVrImpGlobal MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdEmpresa VARCHAR(16),@pmDirEntrega VARCHAR(250),@pmIdLocEtga VARCHAR(8),@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150)
,@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmLiqFletes BIT
,@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipEnt VARCHAR(3),@pmEntrada INT,@pmIdCiaEnt CHAR(2),@pmNumAutoriza INT,@pmModalidad VARCHAR(10),@pmVigencia VARCHAR(10),@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME
,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4),@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmVrReteIVA MONEY,@pmCodTarRiv VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_OCompra SET Fecha=@pmFecha,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdProv=@pmIdProv,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrSobtasa=@pmVrSobtasa,VrImpGlobal=@pmVrImpGlobal
,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,IdEmpresa=@pmIdEmpresa,DirEntrega=@pmDirEntrega,IdLocEtga=@pmIdLocEtga,DiasEntraga=@pmDiasEntraga,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdForma=@pmIdForma,DetallePago=@pmDetallePago
,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CdMney=@pmCdMney,NitEmpTrans=@pmNitEmpTrans,EmpTrans=@pmEmpTrans,LiqFletes=@pmLiqFletes,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipEnt=@pmTipEnt,Entrada=@pmEntrada,IdCiaEnt=@pmIdCiaEnt,NumAutoriza=@pmNumAutoriza,Modalidad=@pmModalidad,Vigencia=@pmVigencia
,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob,IdEstado=@pmIdEstado
,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,FecUpdate=@pmFecUpdate,VrReteIVA=@pmVrReteIVA,CodTarRiv=@pmCodTarRiv
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCompraAbr @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
,@pmEntrada INT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_OCompra SET IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,Observacion=@pmObservacion
,Entrada=@pmEntrada
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCompraAnu @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OCompra SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCompraApb @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OCompra SET Observacion=@pmObservacion,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob
,CdUsuAprob=@pmCdUsuAprob,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOCompraApl @pmTipDoc VARCHAR(3),@pmOCompra INT,@pmIdCia CHAR(2)
,@pmTipEnt VARCHAR(3),@pmEntrada INT,@pmIdCiaEnt CHAR(2),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_OCompra 
 SET  TipEnt=@pmTipEnt,Entrada=@pmEntrada,IdCiaEnt=@pmIdCiaEnt,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND OCompra=@pmOCompra AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpObjetosApp @pmIdObj VARCHAR(10),@pmIdGrupo VARCHAR(6),@pmSubModulo VARCHAR(3),@pmIndObj INT,@pmNombre VARCHAR(100)
,@pmFormulario VARCHAR(10),@pmPermisos VARCHAR(20),@pmNivelMinimo INT,@pmNomArchivo VARCHAR(30)
 AS 
 UPDATE Sys_ObjetosApp SET IdGrupo=@pmIdGrupo,SubModulo=@pmSubModulo,IndObj=@pmIndObj,Nombre=@pmNombre
,Formulario=@pmFormulario,Permisos=@pmPermisos,NivelMinimo=@pmNivelMinimo,NomArchivo=@pmNomArchivo
 WHERE IdObj=@pmIdObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpciones @pmIdOpc VARCHAR(3),@pmOpcion VARCHAR(100),@pmTipoDato VARCHAR(15)
,@pmValor VARCHAR(100),@pmNivUp INT,@pmnModulo VARCHAR(10)
 AS 
 UPDATE adm_Opciones SET Opcion=@pmOpcion,TipoDato=@pmTipoDato,Valor=@pmValor
,NivUp=@pmNivUp,nModulo=@pmnModulo
 WHERE IdOpc=@pmIdOpc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedido @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdClieFact VARCHAR(16),@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrCargos MONEY
,@pmVrOtrDcto MONEY,@pmVrSobretasa MONEY,@pmVrImpGlobal MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250),@pmIdLocEnv VARCHAR(8),@pmLugarEnvio VARCHAR(50),@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20)
,@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT,@pmIdPlazo VARCHAR(4),@pmCdMney VARCHAR(5),@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmAsignarVeh BIT,@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmCdRuta VARCHAR(4),@pmListaPrec CHAR(1),@pmRefPedido VARCHAR(50)
,@pmModalidad VARCHAR(10),@pmVigencia VARCHAR(10),@pmNumAutoriza INT,@pmNumAutCupo INT,@pmNumAutCheq INT,@pmNumAprob INT,@pmIdCiaApr CHAR(2),@pmFecAprob SMALLDATETIME,@pmDetalleAprob VARCHAR(250),@pmCdUsuAprob VARCHAR(11),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2)
,@pmFechaRem SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmZonaFrontera BIT,@pmTipoTrans INT,@pmTipoOrden VARCHAR(3),@pmTipoModifica VARCHAR(10),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Opedido SET Fecha=@pmFecha,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdClieFact=@pmIdClieFact,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros,VrCargos=@pmVrCargos,VrOtrDcto=@pmVrOtrDcto,VrSobretasa=@pmVrSobretasa,VrImpGlobal=@pmVrImpGlobal,VrNeto=@pmVrNeto
,Cantidad=@pmCantidad,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,DirEnvio=@pmDirEnvio,IdLocEnv=@pmIdLocEnv,LugarEnvio=@pmLugarEnvio,DiasEntraga=@pmDiasEntraga,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargoContac=@pmCargoContac,IdForma=@pmIdForma,DetallePago=@pmDetallePago,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo
,CdMney=@pmCdMney,NitEmpTrans=@pmNitEmpTrans,EmpTrans=@pmEmpTrans,AsignarVeh=@pmAsignarVeh,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,CdRuta=@pmCdRuta,ListaPrec=@pmListaPrec,RefPedido=@pmRefPedido,Modalidad=@pmModalidad,Vigencia=@pmVigencia,NumAutoriza=@pmNumAutoriza,NumAutCupo=@pmNumAutCupo,NumAutCheq=@pmNumAutCheq,NumAprob=@pmNumAprob,IdCiaApr=@pmIdCiaApr
,FecAprob=@pmFecAprob,DetalleAprob=@pmDetalleAprob,CdUsuAprob=@pmCdUsuAprob,TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact,TipRem=@pmTipRem,Remision=@pmRemision,IdCiaRem=@pmIdCiaRem,FechaRem=@pmFechaRem,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,TipoModifica=@pmTipoModifica,IdEstado=@pmIdEstado
,ZonaFrontera=@pmZonaFrontera,TipoTrans=@pmTipoTrans,TipoOrden=@pmTipoOrden, FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoAbf @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmRemision INT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
AS 
 UPDATE Trn_Opedido SET TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Remision=@pmRemision,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoAnu @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Opedido SET Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoApr @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11)
,@pmIdEstado VARCHAR(4),@pmIdCiaApr CHAR(2),@pmDetalleAprob VARCHAR(250)
 AS 
 UPDATE Trn_Opedido SET IdEstado=@pmIdEstado,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob
,CdUsuAprob=@pmCdUsuAprob,IdCiaApr=@pmIdCiaApr,DetalleAprob=@pmDetalleAprob
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoBlo @pmTipDoc VARCHAR(3),@pmIdCia CHAR(2),@pmModalidad VARCHAR(10)
,@pmFecha SMALLDATETIME,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Opedido SET Anulado=@pmAnulado,FecDev=@pmFecDev,IdEstado=@pmIdEstado
,NumAprob=0,IdCiaApr='00'
 WHERE TipDoc=@pmTipDoc AND IdEstado='0001' AND Anulado=0 AND NumAprob>0 AND Factura<=0
 AND FechaVence<=@pmFecha
 AND Modalidad LIKE ISNULL(@pmModalidad,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoDvs @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmIdEstado VARCHAR(4)
AS 
 UPDATE Trn_Opedido SET TipFac='0',Factura=0,IdCiaFac='00',IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia AND TipFac='SAL'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoFac @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmIdEstado VARCHAR(4)
AS 
 UPDATE Trn_Opedido SET TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac
,FechaFact=@pmFechaFact,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpOpedidoRem @pmTipDoc VARCHAR(3),@pmPedido INT,@pmIdCia CHAR(2)
,@pmTipRem VARCHAR(3),@pmRemision INT,@pmIdCiaRem CHAR(2),@pmFechaRem SMALLDATETIME
 AS 
 UPDATE Trn_Opedido SET TipRem=@pmTipRem,Remision=@pmRemision,IdCiaRem=@pmIdCiaRem,FechaRem=@pmFechaRem
 WHERE TipDoc=@pmTipDoc AND Pedido=@pmPedido AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPagosACns @pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
 AS 
 UPDATE  Trn_Pagos SET  DocRef=0,IdCiaRef='00',ItemDoc=0
 WHERE TipDoc<>'CNS' AND TipRef=@pmTipRef AND DocRef=@pmDocRef AND IdCiaRef=@pmIdCiaRef
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPagosCnChe @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmIdForma VARCHAR(4),@pmIdBanco VARCHAR(4),@pmNumForma VARCHAR(20)
,@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmItemDoc INT
 AS 
 UPDATE  Trn_Pagos SET  TipRef=@pmTipRef,DocRef=@pmDocRef,IdCiaRef=@pmIdCiaRef,ItemDoc=@pmItemDoc
 WHERE TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdForma=@pmIdForma AND Item=ISNULL(@pmItem,Item)
 AND IdBanco=@pmIdBanco AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPagosCnEfe @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmIdForma VARCHAR(4),@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2),@pmItemDoc INT
 AS 
 UPDATE  Trn_Pagos SET  TipRef=@pmTipRef,DocRef=@pmDocRef,IdCiaRef=@pmIdCiaRef,ItemDoc=@pmItemDoc
 WHERE TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdForma=@pmIdForma AND Item=ISNULL(@pmItem,Item)
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPagosDoc @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
 AS 
 UPDATE  Trn_Pagos SET  TipRef=@pmTipRef,DocRef=@pmDocRef,IdCiaRef=@pmIdCiaRef
 WHERE TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPagosEod @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmTipRef VARCHAR(3)
 AS 
 UPDATE  Trn_Pagos SET  TipRef='0',DocRef=0,IdCiaRef='00'
 WHERE TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia AND TipRef=@pmTipRef
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPagosFor @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2)
,@pmIdForma VARCHAR(4),@pmIdBanco VARCHAR(4),@pmNumForma VARCHAR(20)
,@pmTipRef VARCHAR(3),@pmDocRef INT,@pmIdCiaRef CHAR(2)
 AS 
 UPDATE  Trn_Pagos SET  TipRef=@pmTipRef,DocRef=@pmDocRef,IdCiaRef=@pmIdCiaRef
 WHERE TipDoc=@pmTipDoc AND  Documento=@pmDocumento AND IdCia=@pmIdCia
 AND IdForma=@pmIdForma AND IdBanco=@pmIdBanco AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPaises @pmIdPais VARCHAR(8),@pmCodISO VARCHAR(3),@pmNombrePais VARCHAR(50),@pmIdMneda VARCHAR(5)
 AS 
 UPDATE adm_Paises SET CodISO=@pmCodISO,NombrePais=@pmNombrePais,IdMneda=@pmIdMneda
 WHERE IdPais=@pmIdPais
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPeajesCat @pmIdCat VARCHAR(4),@pmCatpeaje VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE PeajesCat SET Catpeaje=@pmCatpeaje,Inactivo=@pmInactivo
 WHERE IdCat=@pmIdCat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPeajesClase @pmIdClase VARCHAR(4),@pmClasePeaje VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE PeajesClase SET ClasePeaje=@pmClasePeaje,Inactivo=@pmInactivo
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPeajesTarf @pmIdCat VARCHAR(4),@pmIdClase VARCHAR(4),@pmVrTarifa MONEY
 AS 
 UPDATE PeajesTarf SET VrTarifa=@pmVrTarifa
 WHERE IdCat=@pmIdCat AND IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPermisos @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10),@pmConsecutivo INT,@pmPerAdd BIT,@pmPerUpdate BIT
,@pmPerDelete BIT,@pmPerAnular BIT,@pmPerRead BIT,@pmPerRename BIT
 AS 
 UPDATE adm_Permisos SET PerAdd=@pmPerAdd,PerUpdate=@pmPerUpdate
,PerDelete=@pmPerDelete,PerAnular=@pmPerAnular,PerRead=@pmPerRead,PerRename=@pmPerRename
 ,Consecutivo=ISNULL(@pmConsecutivo,Consecutivo)
 WHERE IdUsuario=@pmIdUsuario AND IdObj=@pmIdObj
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPermisosRoles @pmIdUsuario VARCHAR(11),@pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3),@pmAsignado BIT
 AS 
 UPDATE adm_PermisosRoles SET Asignado=@pmAsignado
 WHERE IdUsuario=@pmIdUsuario AND IdObj=@pmIdObj AND IdRole LIKE ISNULL(@pmIdRole,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpPicoPlaca @pmFecha SMALLDATETIME,@pmUltimoDig VARCHAR(50)
 AS 
 UPDATE PicoPlaca SET UltimoDig=@pmUltimoDig
 WHERE Fecha=@pmFecha


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPlazos @pmIdPlazo VARCHAR(4),@pmPlazo VARCHAR(50),@pmNVmto INT
,@pmDiasPago VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Plazos SET Plazo=@pmPlazo,NVmto=@pmNVmto,DiasPago=@pmDiasPago,Inactivo=@pmInactivo
 WHERE IdPlazo=@pmIdPlazo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPolizas @pmNumPoliza VARCHAR(30),@pmFecha SMALLDATETIME,@pmVigencia SMALLDATETIME,@pmNitCompania VARCHAR(16),@pmCompania VARCHAR(150),@pmNitTomador VARCHAR(16)
,@pmTomador VARCHAR(150),@pmNitAseg VARCHAR(16),@pmAsegurado VARCHAR(150),@pmNitBenef VARCHAR(16),@pmBeneficiario VARCHAR(150),@pmDescFirma VARCHAR(2000),@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Polizas SET Fecha=@pmFecha,Vigencia=@pmVigencia,NitCompania=@pmNitCompania,Compania=@pmCompania,NitTomador=@pmNitTomador,Tomador=@pmTomador,NitAseg=@pmNitAseg
,Asegurado=@pmAsegurado,NitBenef=@pmNitBenef,Beneficiario=@pmBeneficiario,Inactivo=@pmInactivo,DescFirma=@pmDescFirma,FechaUpdate=@pmFechaUpdate
 WHERE NumPoliza=@pmNumPoliza
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpPreCausacionAnu @pmCausacionID INT,@pmIdCia CHAR(2),@pmAnulado BIT, @pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250)
AS
 UPDATE  Trn_PreCausacion SET Anulado=@pmAnulado, FecDev=@pmFecDev,Observacion=@pmObservacion
 WHERE  CausacionID=@pmCausacionID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpPrestamos @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFecPrestamo SMALLDATETIME,@pmFecInicio SMALLDATETIME,@pmTipoPlazo VARCHAR(10),@pmNumCuotas INT,@pmDiasCausa VARCHAR(5),@pmDiasVence VARCHAR(5),@pmTipoCuota VARCHAR(10),@pmValorPrestamo MONEY,@pmTasaInteres DECIMAL(14,4)
,@pmTotalCausado MONEY,@pmTotalInteres MONEY,@pmUltimaCuota INT,@pmCancelado BIT,@pmIdConcepto VARCHAR(4),@pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdConductor VARCHAR(16),@pmVehPropio BIT,@pmCxPagar BIT,@pmReferencia VARCHAR(20),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmTipCom VARCHAR(3),@pmComprobante INT
,@pmIdCiaCom CHAR(2),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Prestamos SET Fecha=@pmFecha,FecPrestamo=@pmFecPrestamo,FecInicio=@pmFecInicio,TipoPlazo=@pmTipoPlazo,NumCuotas=@pmNumCuotas,DiasCausa=@pmDiasCausa,DiasVence=@pmDiasVence,TipoCuota=@pmTipoCuota,ValorPrestamo=@pmValorPrestamo,TasaInteres=@pmTasaInteres,TotalCausado=@pmTotalCausado,TotalInteres=@pmTotalInteres,UltimaCuota=@pmUltimaCuota,Cancelado=@pmCancelado
,IdConcepto=@pmIdConcepto,IdVehiculo=@pmIdVehiculo,IdPropietario=@pmIdPropietario,IdConductor=@pmIdConductor,VehPropio=@pmVehPropio,CxPagar=@pmCxPagar,Referencia=@pmReferencia,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpPrestamosAcu @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmTotalCausado MONEY, @pmTotalInteres MONEY, @pmUltimaCuota INT
,@pmIdEstado VARCHAR(4), @pmFecUpdate  SMALLDATETIME,@pmCancelado BIT=Null
AS 
 UPDATE Trn_Prestamos SET TotalCausado=@pmTotalCausado, TotalInteres=@pmTotalInteres, UltimaCuota=@pmUltimaCuota,FecUpdate=@pmFecUpdate,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 ,Cancelado=ISNULL(@pmCancelado,Cancelado)
 WHERE TipDoc=@pmTipDoc  AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpPrestamosAnu @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmAnulado BIT, @pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250), @pmIdEstado VARCHAR(4)
AS 
 UPDATE Trn_Prestamos SET Anulado=@pmAnulado, NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion, IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc  AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpPrestamosCan @pmTipDoc VARCHAR(3),@pmPrestamoID INT,@pmIdCia CHAR(2),@pmTotalCausado MONEY, @pmTotalInteres MONEY, @pmUltimaCuota INT
,@pmIdEstado VARCHAR(4),@pmCancelado BIT, @pmFecUpdate  SMALLDATETIME
AS 
 UPDATE Trn_Prestamos SET Cancelado=@pmCancelado,TotalCausado=@pmTotalCausado,UltimaCuota=@pmUltimaCuota,IdEstado=@pmIdEstado
,FecUpdate=@pmFecUpdate,TotalInteres=ISNULL(@pmTotalInteres,TotalInteres)
 WHERE TipDoc=@pmTipDoc  AND PrestamoID=@pmPrestamoID AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpPresupuesto @pmNumero INT,@pmIdCia CHAR(2),@pmnAnno INT,@pmnMes INT,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
,@pmTipoPsto INT, @pmDetCentros BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Presupuesto SET nAnno=@pmnAnno,nMes=@pmnMes,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,TipoPsto=@pmTipoPsto, DetCentros=@pmDetCentros, FecUpdate=@pmFecUpdate
 WHERE Numero=@pmNumero AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdAditivos @pmIdProducto VARCHAR(16),@pmIdAditivo VARCHAR(16),@pmCant DECIMAL(14,4)
,@pmUnidad VARCHAR(10),@pmCdBodega VARCHAR(4),@pmCdTanq VARCHAR(4)
 AS 
 UPDATE ProdAditivos SET Cant=@pmCant,Unidad=@pmUnidad,CdBodega=@pmCdBodega,CdTanq=@pmCdTanq
 WHERE IdProducto=@pmIdProducto AND IdAditivo=@pmIdAditivo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdCombo @pmIdProducto VARCHAR(16),@pmItem INT,@pmIdProdBas VARCHAR(16),@pmCant DECIMAL(14,4)
,@pmCdBodega VARCHAR(4),@pmCdTanq VARCHAR(4),@pmFacMezcla VARCHAR(10),@pmExentoSob BIT
 AS 
 UPDATE ProdCombo SET IdProdBas=@pmIdProdBas,Cant=@pmCant,CdBodega=@pmCdBodega,CdTanq=@pmCdTanq
,FacMezcla=@pmFacMezcla,ExentoSob=@pmExentoSob
 WHERE IdProducto=@pmIdProducto AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdCostos @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmCostoAnt MONEY
,@pmCostoUlt MONEY,@pmCostoPmd MONEY,@pmFecUltPmd SMALLDATETIME
 AS 
 UPDATE ProdCostos SET CostoAnt=@pmCostoAnt,CostoUlt=@pmCostoUlt,CostoPmd=@pmCostoPmd,FecUltPmd=@pmFecUltPmd
 WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpProdImpFron @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmIdLocal VARCHAR(8),@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 UPDATE ProdImpFron SET IdLocal=@pmIdLocal,Tarifa=@pmTarifa,SimbTfa=@pmSimbTfa,CdTarifa=@pmCdTarifa,RngFecha=@pmRngFecha,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal
,LtaPre=@pmLtaPre,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,TarifZfe=@pmTarifZfe
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdImptos @pmIdProducto VARCHAR(16),@pmIdClase VARCHAR(4),@pmItem INT,@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmRngFecha BIT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmLtaPre CHAR(1),@pmInactivo BIT,@pmFecUpdate SMALLDATETIME,@pmTarifZfe DECIMAL(14,4)
 AS 
 UPDATE ProdImptos SET Tarifa=@pmTarifa,SimbTfa=@pmSimbTfa,CdTarifa=@pmCdTarifa,RngFecha=@pmRngFecha,FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,LtaPre=@pmLtaPre
,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,TarifZfe=@pmTarifZfe
 WHERE IdProducto=@pmIdProducto AND IdClase=@pmIdClase AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdMcias @pmIdProducto VARCHAR(16),@pmDescripProd VARCHAR(150),@pmDescripAbrv VARCHAR(50),@pmCodBarras VARCHAR(20),@pmReferencia VARCHAR(20),@pmTipoRef VARCHAR(10),@pmIdSubgrupo VARCHAR(8),@pmIdMarca VARCHAR(4),@pmColor VARCHAR(30),@pmTamano VARCHAR(30),@pmMedAlto DECIMAL(14,4),@pmMedAncho DECIMAL(14,4)
,@pmMedLargo DECIMAL(14,4),@pmMedVolm DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmIdUnd VARCHAR(4),@pmIdUndP VARCHAR(4),@pmCdUndS VARCHAR(4),@pmCdUndE VARCHAR(4),@pmCdUndEb VARCHAR(4),@pmIdEmp VARCHAR(4),@pmIdNat VARCHAR(4),@pmIdMnjo VARCHAR(4),@pmIdTmcia VARCHAR(4),@pmIdBodega VARCHAR(4),@pmIdUbic VARCHAR(10),@pmDesUbic VARCHAR(50)
,@pmIdProv VARCHAR(16),@pmGarProv INT,@pmGarClie INT,@pmCdDctCom VARCHAR(4),@pmVrCostAnt MONEY,@pmVrCosto MONEY,@pmVrCostPmd MONEY,@pmIdTarIva VARCHAR(4),@pmIvaInc VARCHAR(10),@pmLtPreDef CHAR(1),@pmPrecio1 MONEY,@pmPrecio2 MONEY,@pmPrecio3 MONEY,@pmPrecio4 MONEY,@pmPrecio5 MONEY,@pmCdMon1 VARCHAR(5),@pmCdMon2 VARCHAR(5)
,@pmCdMon3 VARCHAR(5),@pmCdMon4 VARCHAR(5),@pmCdMon5 VARCHAR(5),@pmBaseMgn VARCHAR(10),@pmCdMgn1 VARCHAR(4),@pmCdMgn2 VARCHAR(4),@pmCdMgn3 VARCHAR(4),@pmCdMgn4 VARCHAR(4),@pmCdMgn5 VARCHAR(4),@pmCdDct1 VARCHAR(4),@pmCdDct2 VARCHAR(4),@pmCdDct3 VARCHAR(4),@pmCdDct4 VARCHAR(4),@pmCdDct5 VARCHAR(4),@pmCdTarIca VARCHAR(4)
,@pmCdTarRet VARCHAR(4),@pmExtciaMin DECIMAL(14,4),@pmExtciaMax DECIMAL(14,4),@pmExtciaAct DECIMAL(14,4),@pmFactor1 DECIMAL(14,4),@pmFactor2 DECIMAL(14,4),@pmFactor3 DECIMAL(14,4),@pmFactor4 DECIMAL(14,4),@pmFactor5 DECIMAL(14,4),@pmSeriales BIT,@pmLotes BIT,@pmCombo BIT,@pmNoAjustes BIT,@pmTanques BIT,@pmValesComb BIT,@pmFecUltcom SMALLDATETIME,@pmFecUltVta SMALLDATETIME
,@pmCodMcia VARCHAR(10),@pmDescripLong VARCHAR(250),@pmCmntarios VARCHAR(250),@pmPathFoto VARCHAR(30),@pmCantTpv DECIMAL(14,4),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE ProdMcias SET DescripProd=@pmDescripProd,DescripAbrv=@pmDescripAbrv,CodBarras=@pmCodBarras,Referencia=@pmReferencia,TipoRef=@pmTipoRef,IdSubgrupo=@pmIdSubgrupo,IdMarca=@pmIdMarca,Color=@pmColor,Tamano=@pmTamano,MedAlto=@pmMedAlto,MedAncho=@pmMedAncho,MedLargo=@pmMedLargo,MedVolm=@pmMedVolm,UndMed=@pmUndMed,IdUnd=@pmIdUnd,IdUndP=@pmIdUndP,CdUndS=@pmCdUndS
,CdUndE=@pmCdUndE,CdUndEb=@pmCdUndEb,IdEmp=@pmIdEmp,IdNat=@pmIdNat,IdMnjo=@pmIdMnjo,IdTmcia=@pmIdTmcia,IdBodega=@pmIdBodega,IdUbic=@pmIdUbic,DesUbic=@pmDesUbic,IdProv=@pmIdProv,GarProv=@pmGarProv,GarClie=@pmGarClie,CdDctCom=@pmCdDctCom,VrCostAnt=@pmVrCostAnt,VrCosto=@pmVrCosto,VrCostPmd=@pmVrCostPmd,IdTarIva=@pmIdTarIva,IvaInc=@pmIvaInc,LtPreDef=@pmLtPreDef,Precio1=@pmPrecio1
,Precio2=@pmPrecio2,Precio3=@pmPrecio3,Precio4=@pmPrecio4,Precio5=@pmPrecio5,CdMon1=@pmCdMon1,CdMon2=@pmCdMon2,CdMon3=@pmCdMon3,CdMon4=@pmCdMon4,CdMon5=@pmCdMon5,BaseMgn=@pmBaseMgn,CdMgn1=@pmCdMgn1,CdMgn2=@pmCdMgn2,CdMgn3=@pmCdMgn3,CdMgn4=@pmCdMgn4,CdMgn5=@pmCdMgn5,CdDct1=@pmCdDct1,CdDct2=@pmCdDct2,CdDct3=@pmCdDct3,CdDct4=@pmCdDct4,CdDct5=@pmCdDct5
,CdTarIca=@pmCdTarIca,CdTarRet=@pmCdTarRet,ExtciaMin=@pmExtciaMin,ExtciaMax=@pmExtciaMax,ExtciaAct=@pmExtciaAct,Factor1=@pmFactor1,Factor2=@pmFactor2,Factor3=@pmFactor3,Factor4=@pmFactor4,Factor5=@pmFactor5,Seriales=@pmSeriales,Lotes=@pmLotes,Combo=@pmCombo,NoAjustes=@pmNoAjustes,Tanques=@pmTanques,FecUltcom=@pmFecUltcom,FecUltVta=@pmFecUltVta,CodMcia=@pmCodMcia,DescripLong=@pmDescripLong
,Cmntarios=@pmCmntarios,PathFoto=@pmPathFoto,CantTpv=@pmCantTpv,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,ValesComb=@pmValesComb,FechaUpdate=@pmFechaUpdate
 WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdMciasEst @pmIdProducto VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmCmntarios VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE ProdMcias SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Cmntarios=@pmCmntarios,FechaUpdate=@pmFechaUpdate
 WHERE IdProducto=@pmIdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdMciasIca @pmIdSubgrupo VARCHAR(8),@pmCdTarIca VARCHAR(4),@pmIdNewIca VARCHAR(4)
 AS 
 UPDATE ProdMcias SET  CdTarIca=@pmIdNewIca
 WHERE IdSubgrupo=@pmIdSubgrupo AND CdTarIca=@pmCdTarIca
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdMciasIva @pmIdSubgrupo VARCHAR(8),@pmIdTarIva VARCHAR(4),@pmIdNewTar VARCHAR(4)
 AS 
 UPDATE ProdMcias SET IdTarIva=@pmIdNewTar
 WHERE IdSubgrupo=@pmIdSubgrupo AND IdTarIva=@pmIdTarIva
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdMciasRet @pmIdSubgrupo VARCHAR(8),@pmCdTarRet VARCHAR(4),@pmIdNewRet VARCHAR(4)
 AS 
 UPDATE ProdMcias SET  CdTarRet=@pmIdNewRet
 WHERE IdSubgrupo=@pmIdSubgrupo AND CdTarRet=@pmCdTarRet
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdMciasSal @pmIdProducto VARCHAR(16)=Null
 AS 
 UPDATE ProdMcias 
 SET ExtciaAct=(SELECT ISNULL(SUM(SaldoActual),0) FROM ProdSaldos WHERE ProdMcias.IdProducto=ProdSaldos.IdProducto)
 WHERE TipoRef<>'SERVICIO' AND  IdProducto LIKE ISNULL(@pmIdProducto,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpProdPrecCom @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmNumero INT,@pmTipoTarif VARCHAR(3),@pmnMes INT,@pmnAnno INT
,@pmTarifa DECIMAL(14,4),@pmNitProv VARCHAR(16),@pmInactivo BIT
 AS 
 UPDATE ProdPrecCom SET TipoTarif=@pmTipoTarif,nMes=@pmnMes,nAnno=@pmnAnno,Tarifa=@pmTarifa,NitProv=@pmNitProv,Inactivo=@pmInactivo
 WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia AND Numero=@pmNumero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdPrecios @pmIdProducto VARCHAR(16),@pmIdCia CHAR(2),@pmVrPrecio1 MONEY,@pmVrPrecio2 MONEY,@pmVrPrecio3 MONEY,@pmVrPrecio4 MONEY,@pmVrPrecio5 MONEY
,@pmIvaInc VARCHAR(10),@pmCdTarIva VARCHAR(4),@pmCdMoney1 VARCHAR(5),@pmCdMoney2 VARCHAR(5),@pmCdMoney3 VARCHAR(5),@pmCdMoney4 VARCHAR(5),@pmCdMoney5 VARCHAR(5)
,@pmCdMargen1 VARCHAR(4),@pmCdMargen2 VARCHAR(4),@pmCdMargen3 VARCHAR(4),@pmCdMargen4 VARCHAR(4),@pmCdMargen5 VARCHAR(4),@pmBaseMargen VARCHAR(10),@pmCdDcto1 VARCHAR(4)
,@pmCdDcto2 VARCHAR(4),@pmCdDcto3 VARCHAR(4),@pmCdDcto4 VARCHAR(4),@pmCdDcto5 VARCHAR(4),@pmIdUnd VARCHAR(4),@pmIdBodega VARCHAR(4)
 AS 
 UPDATE ProdPrecios SET VrPrecio1=@pmVrPrecio1,VrPrecio2=@pmVrPrecio2,VrPrecio3=@pmVrPrecio3,VrPrecio4=@pmVrPrecio4,VrPrecio5=@pmVrPrecio5,IvaInc=@pmIvaInc,CdTarIva=@pmCdTarIva
,CdMoney1=@pmCdMoney1,CdMoney2=@pmCdMoney2,CdMoney3=@pmCdMoney3,CdMoney4=@pmCdMoney4,CdMoney5=@pmCdMoney5,CdMargen1=@pmCdMargen1,CdMargen2=@pmCdMargen2,CdMargen3=@pmCdMargen3
,CdMargen4=@pmCdMargen4,CdMargen5=@pmCdMargen5,BaseMargen=@pmBaseMargen,CdDcto1=@pmCdDcto1,CdDcto2=@pmCdDcto2,CdDcto3=@pmCdDcto3,CdDcto4=@pmCdDcto4,CdDcto5=@pmCdDcto5,IdUnd=@pmIdUnd,IdBodega=@pmIdBodega
 WHERE IdProducto=@pmIdProducto AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdProvee @pmIdProducto VARCHAR(16),@pmIdProv VARCHAR(16),@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmDiasGar INT
,@pmCdDct VARCHAR(4),@pmVrCostCom MONEY,@pmVrCostPmd MONEY,@pmRefProv VARCHAR(20),@pmDcmento INT
 AS 
 UPDATE ProdProvee SET Fecha=@pmFecha,DiasGar=@pmDiasGar,CdDct=@pmCdDct,VrCostCom=@pmVrCostCom,VrCostPmd=@pmVrCostPmd,RefProv=@pmRefProv,Dcmento=@pmDcmento
 WHERE IdProducto=@pmIdProducto AND IdProv=@pmIdProv AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdSaldoUbc @pmIdProducto VARCHAR(16),@pmIdUbic VARCHAR(10),@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 UPDATE ProdSaldoUbc SET SaldoActual=@pmSaldoActual
 WHERE IdProducto=@pmIdProducto AND IdUbic=@pmIdUbic AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdSaldoUbcKar @pmDelete BIT,@pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 IF @pmDelete<>0  
  BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SELECT @TrnNombre = 'TrnProdSaldos'
  BEGIN TRANSACTION @TrnNombre
   DELETE  FROM ProdSaldoUbc WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
   INSERT INTO ProdSaldoUbc  (IdProducto,IdUbic,IdCia,SaldoActual)
   SELECT K.IdProducto,K.IdUbic,IdCia,SUM(Entradas-Salidas)
   FROM  Trn_KarUbic AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
   WHERE TipDoc NOT IN ('PED','REM','COT','ODC','GUI','OCC','ODS') 
    AND TipoRef<>'SERVICIO' AND Tanques=0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
   GROUP BY K.IdProducto,K.IdUbic,IdCia
  COMMIT TRANSACTION  @TrnNombre
  END
 ELSE
   INSERT INTO ProdSaldoUbc  (IdProducto,IdUbic,IdCia,SaldoActual)
   SELECT K.IdProducto,K.IdUbic,IdCia,SUM(Entradas-Salidas)
   FROM  Trn_KarUbic AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
   WHERE TipDoc NOT IN ('PED','REM','COT','ODC','GUI','OCC','ODS') 
    AND TipoRef<>'SERVICIO' AND Tanques=0 AND IdCia LIKE ISNULL(@pmIdCia,'%%') AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%')
   GROUP BY K.IdProducto,K.IdUbic,IdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdSaldos @pmIdProducto VARCHAR(16),@pmIdBodega VARCHAR(4)
,@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 UPDATE ProdSaldos SET SaldoActual=@pmSaldoActual
 WHERE IdProducto=@pmIdProducto AND IdBodega=@pmIdBodega AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdSaldosKar @pmDelete BIT,@pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 IF @pmDelete<>0  
  BEGIN
  DECLARE @TrnNombre VARCHAR(20)
  SELECT @TrnNombre = 'TrnProdSaldos'
  BEGIN TRANSACTION @TrnNombre
   DELETE  FROM ProdSaldos WHERE IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
   INSERT INTO ProdSaldos  (IdProducto,IdBodega,IdCia,SaldoActual)
   SELECT K.IdProducto,K.IdBodega,IdCia,SUM(Entradas-Salidas)
   FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
   WHERE TipDoc NOT IN ('PED','REM','COT','ODC','GUI','OCC','ODS') AND TipoRef<>'SERVICIO'
   AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
   GROUP BY K.IdProducto,K.IdBodega,IdCia
  COMMIT TRANSACTION  @TrnNombre
  END
 ELSE
  INSERT INTO ProdSaldos  (IdProducto,IdBodega,IdCia,SaldoActual)
  SELECT K.IdProducto,K.IdBodega,IdCia,SUM(Entradas-Salidas)
  FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
  WHERE TipDoc NOT IN ('PED','REM','COT','ODC','GUI','OCC','ODS') AND TipoRef<>'SERVICIO'
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  GROUP BY K.IdProducto,K.IdBodega,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProdTarf @pmTipo VARCHAR(3),@pmNumero INT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmCdCia CHAR(2),@pmCdLinea VARCHAR(8),@pmCdGrupo VARCHAR(8),@pmCdSubgrupo VARCHAR(8),@pmCdMarca VARCHAR(4),@pmCdProducto VARCHAR(16),@pmTarifa DECIMAL(14,4),@pmSimbTfa CHAR(1),@pmLtaPre CHAR(1),@pmCdTarifa VARCHAR(4)
,@pmCdClie VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmCdVend VARCHAR(16),@pmCdCiudad VARCHAR(8),@pmCdZona VARCHAR(4)
,@pmCdSzona VARCHAR(4),@pmCdGruCli VARCHAR(4),@pmOtrosCrit VARCHAR(250),@pmAnulado BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_ProdTarf SET FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,CdCia=@pmCdCia,CdLinea=@pmCdLinea,CdGrupo=@pmCdGrupo,CdSubgrupo=@pmCdSubgrupo,CdMarca=@pmCdMarca,CdProducto=@pmCdProducto,Tarifa=@pmTarifa,SimbTfa=@pmSimbTfa,LtaPre=@pmLtaPre,CdTarifa=@pmCdTarifa,CdClie=@pmCdClie,CdVend=@pmCdVend,CdCiudad=@pmCdCiudad,CdZona=@pmCdZona
,CdSzona=@pmCdSzona,CdGruCli=@pmCdGruCli,OtrosCrit=@pmOtrosCrit,Anulado=@pmAnulado,FecUpdate=@pmFecUpdate,CdAgencia=@pmCdAgencia
 WHERE Tipo=@pmTipo AND Numero=@pmNumero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpProfesiones @pmIdProf VARCHAR(4),@pmProfesion VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Profesiones SET Profesion=@pmProfesion,Inactivo=@pmInactivo
 WHERE IdProf=@pmIdProf
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuc @pmIdCuenta VARCHAR(16),@pmNomCuenta VARCHAR(100),@pmMayor BIT,@pmMovimiento BIT,@pmTercero BIT,@pmSubCentro BIT,@pmVehiculo BIT,@pmCentroCosto BIT,@pmVencimiento BIT,@pmBaseGravable BIT
,@pmPorcntaje DECIMAL(10,4),@pmNaturaleza CHAR(1),@pmAjustable BIT,@pmCuentaAjuDeb VARCHAR(16),@pmCuentaAjuCre VARCHAR(16),@pmCheques BIT,@pmReferncias BIT
,@pmAgencia BIT,@pmFmtDian INT,@pmConDian INT,@pmRenDec INT,@pmCdTipDoc VARCHAR(3),@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT,@pmAfijos INT
 AS 
 UPDATE Puc SET NomCuenta=@pmNomCuenta,Mayor=@pmMayor,Movimiento=@pmMovimiento,Tercero=@pmTercero,SubCentro=@pmSubCentro,Vehiculo=@pmVehiculo,CentroCosto=@pmCentroCosto,Vencimiento=@pmVencimiento,BaseGravable=@pmBaseGravable
,Porcntaje=@pmPorcntaje,Naturaleza=@pmNaturaleza,Ajustable=@pmAjustable,CuentaAjuDeb=@pmCuentaAjuDeb,CuentaAjuCre=@pmCuentaAjuCre,Cheques=@pmCheques,Referncias=@pmReferncias,Agencia=@pmAgencia
,FmtDian=@pmFmtDian,ConDian=@pmConDian,RenDec=@pmRenDec,CdTipDoc=@pmCdTipDoc,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo,Afijos=@pmAfijos
 WHERE IdCuenta=@pmIdCuenta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuestos @pmIdPto VARCHAR(4),@pmPtoControl VARCHAR(100),@pmTagPuesto VARCHAR(50),@pmIdLocal VARCHAR(8)
,@pmUbicacion VARCHAR(100),@pmContacto VARCHAR(150),@pmTelefono VARCHAR(20),@pmTelMovil VARCHAR(20),@pmReferencia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE Puestos SET PtoControl=@pmPtoControl,TagPuesto=@pmTagPuesto,IdLocal=@pmIdLocal,Ubicacion=@pmUbicacion,Contacto=@pmContacto
,Telefono=@pmTelefono,TelMovil=@pmTelMovil,Referencia=@pmReferencia,Inactivo=@pmInactivo
 WHERE IdPto=@pmIdPto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntBases @pmIdProgrma INT,@pmCdSubgrupo VARCHAR(8),@pmCdProducto VARCHAR(16)
,@pmPuntos DECIMAL(14,4),@pmMonto DECIMAL(14,4)
 AS 
 UPDATE PuntBases SET Puntos=@pmPuntos,Monto=@pmMonto
 WHERE IdProgrma=@pmIdProgrma AND CdSubgrupo=@pmCdSubgrupo AND CdProducto=@pmCdProducto

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntFormas @pmIdProgrma INT,@pmIdForma VARCHAR(4),@pmPuntos DECIMAL(14,4)
 AS 
 UPDATE PuntFormas SET Puntos=@pmPuntos
 WHERE IdProgrma=@pmIdProgrma AND IdForma=@pmIdForma

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntPremios @pmIdProgrma INT,@pmItem INT,@pmDescripcion VARCHAR(100)
,@pmIdProducto VARCHAR(16),@pmPuntos DECIMAL(14,4),@pmVrBono MONEY
 AS 
 UPDATE PuntPremios SET Descripcion=@pmDescripcion,IdProducto=@pmIdProducto,Puntos=@pmPuntos
,VrBono=@pmVrBono
 WHERE IdProgrma=@pmIdProgrma AND Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntProg @pmIdProgrma INT,@pmFecInicial SMALLDATETIME,@pmFecFinal SMALLDATETIME,@pmMesesInact INT,@pmMesesVcmto INT
,@pmObservacion VARCHAR(250),@pmAnulado BIT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE PuntProg SET FecInicial=@pmFecInicial,FecFinal=@pmFecFinal,MesesInact=@pmMesesInact,MesesVcmto=@pmMesesVcmto
,Observacion=@pmObservacion,Anulado=@pmAnulado,FecUpdate=@pmFecUpdate
 WHERE IdProgrma=@pmIdProgrma

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntTarjetas @pmNumTarjeta VARCHAR(20),@pmIdClie VARCHAR(16),@pmFecha SMALLDATETIME,@pmTotalPuntos DECIMAL(14,4)
,@pmPuntRedim DECIMAL(14,4),@pmFecUltCompra SMALLDATETIME,@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE PuntTarjetas SET IdClie=@pmIdClie,Fecha=@pmFecha,TotalPuntos=@pmTotalPuntos,PuntRedim=@pmPuntRedim,FecUltCompra=@pmFecUltCompra
,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntTarjetasAcu @pmNumTarjeta VARCHAR(20)
,@pmTotalPuntos DECIMAL(14,4),@pmFecUltCompra SMALLDATETIME
 AS 
 UPDATE PuntTarjetas SET TotalPuntos=@pmTotalPuntos,FecUltCompra=@pmFecUltCompra
 WHERE NumTarjeta=@pmNumTarjeta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpPuntos @pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCia CHAR(2),@pmItem INT,@pmFecha SMALLDATETIME
,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrBase MONEY,@pmPuntFactura DECIMAL(14,4)
,@pmPuntExtra DECIMAL(14,4),@pmPuntRedim DECIMAL(14,4),@pmIdProgrma INT,@pmNumTarjeta VARCHAR(20),@pmDetalles VARCHAR(50),@pmReferncia VARCHAR(50)
 AS 
 UPDATE Trn_Puntos SET Fecha=@pmFecha,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrBase=@pmVrBase,PuntFactura=@pmPuntFactura
,PuntExtra=@pmPuntExtra,PuntRedim=@pmPuntRedim,IdProgrma=@pmIdProgrma,NumTarjeta=@pmNumTarjeta,Detalles=@pmDetalles,Referncia=@pmReferncia
 WHERE TipDoc=@pmTipDoc AND Documento=@pmDocumento AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRadicaVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16),@pmTotalPagos MONEY,@pmTotalCobros MONEY
,@pmReferencia VARCHAR(50),@pmCxPagar BIT,@pmVrDescuento MONEY,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_RadicaVales SET Fecha=@pmFecha,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdTercero=@pmIdTercero,TotalPagos=@pmTotalPagos,TotalCobros=@pmTotalCobros,Referencia=@pmReferencia,CxPagar=@pmCxPagar
,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate,VrDescuento=@pmVrDescuento
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRadicaValesDev @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmObservacion VARCHAR(250)
 AS 
 UPDATE Trn_RadicaVales SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpRangosPeso @pmIdRango VARCHAR(4),@pmDescripRango VARCHAR(50),@pmValorInicial DECIMAL(14,4),@pmValorFinal DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE RangosPeso SET DescripRango=@pmDescripRango,ValorInicial=@pmValorInicial,ValorFinal=@pmValorFinal,Inactivo=@pmInactivo
 WHERE IdRango=@pmIdRango

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecAfavorAbo @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmVrAbono MONEY
 AS 
 UPDATE Trn_RecAfavor SET VrAbono=@pmVrAbono
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecAfavorApl @pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 UPDATE Trn_RecAfavor SET Trn_RecAfavor.VrAbono=(SELECT ISNULL(SUM(Trn_VencAbonos.VrAbono),0)
   FROM Trn_VencAbonos  WHERE Trn_RecAfavor.TipDoc=Trn_VencAbonos.TipRec AND Trn_RecAfavor.Recibo=Trn_VencAbonos.Recibo
   AND Trn_RecAfavor.IdCia=Trn_VencAbonos.IdCia AND TipoAplica='ANTICIPO')
  WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecAfavorApld @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
 AS 
 UPDATE Trn_RecAfavor SET Trn_RecAfavor.VrAbono=(SELECT ISNULL(SUM(Trn_VencAbonos.VrAbono),0)
   FROM Trn_VencAbonos  WHERE Trn_RecAfavor.TipDoc=Trn_VencAbonos.TipRec AND Trn_RecAfavor.Recibo=Trn_VencAbonos.Recibo
   AND Trn_RecAfavor.IdCia=Trn_VencAbonos.IdCia AND TipoAplica='ANTICIPO')
  WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo  AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecAfavorCero @pmIdCliente VARCHAR(16)=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 UPDATE Trn_RecAfavor SET VrAbono=0
 WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecAfavorOdb @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmVrAbono MONEY,@pmVrOtrosDb MONEY
 AS 
 UPDATE Trn_RecAfavor SET VrAbono=@pmVrAbono,VrOtrosDb=@pmVrOtrosDb
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpRecBuses @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCar VARCHAR(3),@pmCartulina INT,@pmIdCiaCar CHAR(2),@pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16)
,@pmVehPropio BIT,@pmCentInicial INT,@pmCentFinal INT,@pmNPasajeros INT,@pmRecorridos INT,@pmValorRecibido MONEY,@pmValorTotal MONEY,@pmValorDcto MONEY,@pmValorReteFte MONEY,@pmValorReteIva MONEY,@pmValorReteIca MONEY,@pmValorOtros MONEY,@pmValorOtrosDct MONEY,@pmValorPagMas MONEY,@pmValorNeto MONEY,@pmValorAplicado MONEY
,@pmValorAnticipo MONEY,@pmValorAbono MONEY,@pmTotalProducido MONEY,@pmTotalGastos MONEY,@pmTotalAhorro MONEY,@pmTotalOtros MONEY,@pmTotalVales MONEY,@pmTotalAbono MONEY,@pmValorPasaje MONEY,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmIdTarCom VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdCta VARCHAR(4),@pmEnEfectivo BIT
,@pmNumCheque VARCHAR(20),@pmIdBanco VARCHAR(4),@pmReferencia VARCHAR(20),@pmBaseIva MONEY,@pmBaseRet MONEY,@pmBaseOtr MONEY,@pmTarifaIva DECIMAL(14,4),@pmTarifaRet DECIMAL(14,4),@pmTarifaOtr DECIMAL(14,4),@pmIdTarIva VARCHAR(4),@pmIdTarRet VARCHAR(4),@pmIdTarOtr VARCHAR(4),@pmIdClase VARCHAR(4),@pmIdRuta VARCHAR(4),@pmIdRelevador VARCHAR(16)
,@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_RecBuses SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,Modalidad=@pmModalidad,TipCar=@pmTipCar,Cartulina=@pmCartulina,IdCiaCar=@pmIdCiaCar,IdVehiculo=@pmIdVehiculo,IdConductor=@pmIdConductor,VehPropio=@pmVehPropio,CentInicial=@pmCentInicial,CentFinal=@pmCentFinal,NPasajeros=@pmNPasajeros,Recorridos=@pmRecorridos
,ValorRecibido=@pmValorRecibido,ValorTotal=@pmValorTotal,ValorDcto=@pmValorDcto,ValorReteFte=@pmValorReteFte,ValorReteIva=@pmValorReteIva,ValorReteIca=@pmValorReteIca,ValorOtros=@pmValorOtros,ValorOtrosDct=@pmValorOtrosDct,ValorPagMas=@pmValorPagMas,ValorNeto=@pmValorNeto,ValorAplicado=@pmValorAplicado,ValorAnticipo=@pmValorAnticipo,ValorAbono=@pmValorAbono,TotalProducido=@pmTotalProducido
,TotalGastos=@pmTotalGastos,TotalAhorro=@pmTotalAhorro,TotalOtros=@pmTotalOtros,TotalVales=@pmTotalVales,TotalAbono=@pmTotalAbono,ValorPasaje=@pmValorPasaje,IdVendedor=@pmIdVendedor,Comision=@pmComision,IdTarCom=@pmIdTarCom,IdCCosto=@pmIdCCosto,IdCta=@pmIdCta,EnEfectivo=@pmEnEfectivo,NumCheque=@pmNumCheque,IdBanco=@pmIdBanco,Referencia=@pmReferencia,BaseIva=@pmBaseIva,BaseRet=@pmBaseRet
,BaseOtr=@pmBaseOtr,TarifaIva=@pmTarifaIva,TarifaRet=@pmTarifaRet,TarifaOtr=@pmTarifaOtr,IdTarIva=@pmIdTarIva,IdTarRet=@pmIdTarRet,IdTarOtr=@pmIdTarOtr,IdClase=@pmIdClase,IdRuta=@pmIdRuta,IdRelevador=@pmIdRelevador,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpRecBusesAbo @pmTipDoc VARCHAR(3), @pmRecibo INT, @pmIdCia CHAR(2),@pmValorAbono MONEY
AS
 UPDATE  Trn_RecBuses  SET ValorAbono=@pmValorAbono
 WHERE TipDoc=@pmTipDoc  AND  Recibo=@pmRecibo  AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpRecBusesAcp @pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null
AS
 UPDATE  Trn_RecBuses  SET  ValorAnticipo=0,ValorAbono=0
 WHERE Anulado<>0 AND ValorAnticipo>0  AND IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdCliente LIKE ISNULL(@pmIdCliente,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpRecBusesAnu @pmTipDoc VARCHAR(3), @pmRecibo INT, @pmIdCia CHAR(2), @pmAnulado BIT
, @pmNumDev INT, @pmFecDev SMALLDATETIME, @pmObservacion VARCHAR(250), @pmIdEstado VARCHAR(4)
AS
 UPDATE  Trn_RecBuses  SET Anulado=@pmAnulado, NumDev=@pmNumDev, FecDev=@pmFecDev, Observacion=@pmObservacion, IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc  AND  Recibo=@pmRecibo  AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpRecBusesApl @pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdClase VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
  UPDATE  Trn_RecBuses  SET Trn_RecBuses.ValorAbono=ISNULL((SELECT SUM(ValorAbono)
  FROM  Trn_RecFactura  WHERE AplAntcpo<>0 AND Trn_RecFactura.TipRec=Trn_RecBuses.TipDoc AND Trn_RecFactura.Recibo=Trn_RecBuses.Recibo
   AND Trn_RecFactura.IdCia=Trn_RecBuses.IdCia),0) --HAVING SUM(ValorAbono)<=ValorAnticipo
  WHERE ValorAnticipo>0 AND   Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND  IdVehiculo like ISNULL(@pmIdVehiculo,'%')
  AND IdCliente like ISNULL(@pmIdCliente,'%')   AND IdCia like ISNULL(@pmIdCia,'%%')  AND IdClase like ISNULL(@pmIdClase,'%')
 ELSE
  UPDATE  Trn_RecBuses  SET Trn_RecBuses.ValorAbono=ISNULL((SELECT SUM(ValorAbono)
  FROM  Trn_RecFactura  WHERE AplAntcpo<>0 AND Trn_RecFactura.TipRec=Trn_RecBuses.TipDoc AND Trn_RecFactura.Recibo=Trn_RecBuses.Recibo
   AND Trn_RecFactura.IdCia=Trn_RecBuses.IdCia),0)
  WHERE ValorAnticipo>0 AND  IdVehiculo like ISNULL(@pmIdVehiculo,'%') AND IdCliente like ISNULL(@pmIdCliente,'%')
   AND IdCia like ISNULL(@pmIdCia,'%%')  AND IdClase like ISNULL(@pmIdClase,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpRecBusesAplVeh @pmIdVehiculo VARCHAR(10)
AS
  UPDATE  Trn_RecBuses  SET Trn_RecBuses.ValorAbono=ISNULL((SELECT SUM(ValorAbono)
  FROM  Trn_RecFactura  WHERE AplAntcpo<>0 AND Trn_RecFactura.TipRec=Trn_RecBuses.TipDoc AND Trn_RecFactura.Recibo=Trn_RecBuses.Recibo
   AND Trn_RecFactura.IdCia=Trn_RecBuses.IdCia),0)
  WHERE ValorAnticipo>0 AND  IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpRecBusesCero @pmIdVehiculo VARCHAR(10)=Null,@pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null,@pmIdClase VARCHAR(4)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null
AS
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
  UPDATE  Trn_RecBuses  SET  ValorAbono=0
  WHERE ValorAnticipo>0 AND Fecha BETWEEN @pmFechaIni AND @pmFechaFin AND   IdVehiculo like ISNULL(@pmIdVehiculo,'%')
  AND IdCliente like ISNULL(@pmIdCliente,'%')  AND IdCia like ISNULL(@pmIdCia,'%%')  AND  IdClase like ISNULL(@pmIdClase,'%')
 ELSE
  UPDATE  Trn_RecBuses  SET  ValorAbono=0
  WHERE ValorAnticipo>0 AND  IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%')
  AND IdCliente LIKE ISNULL(@pmIdCliente,'%')  AND IdCia LIKE ISNULL(@pmIdCia,'%%')  AND  IdClase LIKE ISNULL(@pmIdClase,'%')


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpRecBusesCeroVeh @pmIdVehiculo VARCHAR(10)
AS
  UPDATE  Trn_RecBuses  SET  ValorAbono=0
  WHERE ValorAnticipo>0 AND  IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpRecBusesTabo @pmTipDoc VARCHAR(3), @pmRecibo INT, @pmIdCia CHAR(2),@pmValorAplicado MONEY, @pmValorAnticipo MONEY
AS
 UPDATE  Trn_RecBuses  SET ValorAplicado=@pmValorAplicado, ValorAnticipo=@pmValorAnticipo,ValorAbono=0
 WHERE TipDoc=@pmTipDoc  AND  Recibo=@pmRecibo  AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecCheques @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16)
,@pmVrTotal MONEY,@pmCantidad DECIMAL(14,4),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmPlacaVehic VARCHAR(10),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_RecCheques SET Fecha=@pmFecha,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrTotal=@pmVrTotal,Cantidad=@pmCantidad,
TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,PlacaVehic=@pmPlacaVehic,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecChequesAnu @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_RecCheques SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpRecFactura @pmTipRec VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmItem INT,@pmTipDoc VARCHAR(3),@pmDocumento INT,@pmIdCiaDoc CHAR(2),@pmItemDoc INT,@pmFecha SMALLDATETIME,@pmIdCargo VARCHAR(4),@pmIdClase VARCHAR(4),@pmValorAbono MONEY,@pmDevolucion MONEY,@pmDescuento MONEY,@pmIdCliente VARCHAR(16)
,@pmIdAgencia VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmVehPropio BIT,@pmIdVendedor VARCHAR(16),@pmComision DECIMAL(14,4),@pmDetalle VARCHAR(100),@pmFecPago SMALLDATETIME,@pmAplAntcpo BIT
 AS 
 UPDATE Trn_RecFactura SET TipDoc=@pmTipDoc,Documento=@pmDocumento,IdCiaDoc=@pmIdCiaDoc,ItemDoc=@pmItemDoc,Fecha=@pmFecha,IdCargo=@pmIdCargo,IdClase=@pmIdClase,ValorAbono=@pmValorAbono,Devolucion=@pmDevolucion,Descuento=@pmDescuento,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,IdVehiculo=@pmIdVehiculo
,VehPropio=@pmVehPropio,IdVendedor=@pmIdVendedor,Comision=@pmComision,Detalle=@pmDetalle,FecPago=@pmFecPago,AplAntcpo=@pmAplAntcpo
 WHERE TipRec=@pmTipRec AND Recibo=@pmRecibo AND IdCia=@pmIdCia AND Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUpRecTanqueosAnu @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmCantidad DECIMAL(14,4)
AS
 UPDATE  Trn_RecTanqueos  SET  Cantidad=@pmCantidad
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND  IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecibos @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmFecPago SMALLDATETIME,@pmVrRecibido MONEY,@pmVrSubTotal MONEY,@pmVrDescuento MONEY,@pmVrRetencion MONEY,@pmVrReteICA MONEY,@pmVrReteIVA MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY
,@pmVrAhorro MONEY,@pmVrPagosMas MONEY,@pmVrNeto MONEY,@pmVrAplicado MONEY,@pmVrAnticipo MONEY,@pmVrEfectivo MONEY,@pmBaseRet MONEY,@pmBaseIca MONEY,@pmBaseRiv MONEY,@pmTarifaRet DECIMAL(14,4),@pmTarifaIca DECIMAL(14,4),@pmTarifaRiv DECIMAL(14,4),@pmCodTarRet VARCHAR(4),@pmCodTarIca VARCHAR(4),@pmCodTarRiv VARCHAR(4),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdCajero VARCHAR(11),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmIdLocal VARCHAR(8),@pmEnEfectivo BIT,@pmCdForma VARCHAR(4),@pmNumForma VARCHAR(20),@pmDetallePago VARCHAR(250),@pmCdBanco VARCHAR(4),@pmCdCta VARCHAR(4),@pmReferencia VARCHAR(50),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16),@pmTipDcm VARCHAR(3),@pmDocumento INT,@pmIdCiaDcm CHAR(2)
,@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Recibos SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,FecPago=@pmFecPago,VrRecibido=@pmVrRecibido,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrRetencion=@pmVrRetencion,VrReteICA=@pmVrReteICA,VrReteIVA=@pmVrReteIVA,VrOtros=@pmVrOtros,VrOtrDcto=@pmVrOtrDcto,VrAhorro=@pmVrAhorro,VrPagosMas=@pmVrPagosMas,VrNeto=@pmVrNeto
,VrAplicado=@pmVrAplicado,VrAnticipo=@pmVrAnticipo,VrEfectivo=@pmVrEfectivo,BaseRet=@pmBaseRet,BaseIca=@pmBaseIca,BaseRiv=@pmBaseRiv,TarifaRet=@pmTarifaRet,TarifaIca=@pmTarifaIca,TarifaRiv=@pmTarifaRiv,CodTarRet=@pmCodTarRet,CodTarIca=@pmCodTarIca,CodTarRiv=@pmCodTarRiv,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom
,IdLocal=@pmIdLocal,EnEfectivo=@pmEnEfectivo,CdForma=@pmCdForma,NumForma=@pmNumForma,DetallePago=@pmDetallePago,CdBanco=@pmCdBanco,CdCta=@pmCdCta,IdCajero=@pmIdCajero,Referencia=@pmReferencia,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,TipDcm=@pmTipDcm,Documento=@pmDocumento,IdCiaDcm=@pmIdCiaDcm,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante
,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRecibosDev @pmTipDoc VARCHAR(3),@pmRecibo INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Recibos SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev,Observacion=@pmObservacion
,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Recibo=@pmRecibo AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRegimenDian @pmIdRegimen VARCHAR(4),@pmRegimen VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE RegimenDian SET Regimen=@pmRegimen,Inactivo=@pmInactivo
 WHERE IdRegimen=@pmIdRegimen
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRegistro @pmIdCia CHAR(2),@pmIdEmpresa VARCHAR(16),@pmNitEmpresa VARCHAR(16),@pmDvNit CHAR(1),@pmRazonSocial VARCHAR(250),@pmDescripcion VARCHAR(250)
,@pmDireccion VARCHAR(250),@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pmIdLocal VARCHAR(8),@pmIdPais VARCHAR(8),@pme_mail VARCHAR(100),@pmArchivoLogo VARCHAR(30),@pmNumeroSerial VARCHAR(80)
,@pmRegimen VARCHAR(50),@pmLicencia VARCHAR(20),@pmVigenciaLic SMALLDATETIME,@pmPersoneria VARCHAR(40),@pmLeyenda VARCHAR(250),@pmFechaRegistro SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmCurrtyDate SMALLDATETIME
,@pmTipoId CHAR(1),@pmNitRepLegal VARCHAR(16),@pmRepLegal VARCHAR(150),@pmTipoPersna CHAR(1),@pmCIIUDane VARCHAR(8),@pmCurDate1 SMALLDATETIME,@pmFechaPrest SMALLDATETIME
 AS 
 UPDATE adm_Registro SET IdEmpresa=@pmIdEmpresa,NitEmpresa=@pmNitEmpresa,DvNit=@pmDvNit,RazonSocial=@pmRazonSocial,Descripcion=@pmDescripcion,Direccion=@pmDireccion,Telefono=@pmTelefono
,Fax=@pmFax,IdLocal=@pmIdLocal,IdPais=@pmIdPais,e_mail=@pme_mail,ArchivoLogo=@pmArchivoLogo,NumeroSerial=@pmNumeroSerial,Regimen=@pmRegimen,Licencia=@pmLicencia,VigenciaLic=@pmVigenciaLic
,Personeria=@pmPersoneria,Leyenda=@pmLeyenda,FechaRegistro=@pmFechaRegistro,FechaVence=@pmFechaVence,CurrtyDate=@pmCurrtyDate
,TipoId=@pmTipoId,NitRepLegal=@pmNitRepLegal,RepLegal=@pmRepLegal,TipoPersna=@pmTipoPersna ,CIIUDane=@pmCIIUDane,CurDate1=@pmCurDate1,FechaPrest=@pmFechaPrest
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRegistroFec  @pmCurrtyDate SMALLDATETIME,@pmUpCia BIT,@pmIdCia CHAR(2)=Null
 AS 
 IF @pmUpCia=1  
  BEGIN
  DECLARE @TrnCias VARCHAR(20)
  SELECT @TrnCias = 'TrnFecha'
  BEGIN TRANSACTION @TrnCias
  UPDATE adm_Registro SET CurrtyDate=@pmCurrtyDate
  UPDATE Companias SET FechaActual=@pmCurrtyDate WHERE IdCia=@pmIdCia
  COMMIT TRANSACTION  @TrnCias  
  END
 ELSE
  UPDATE adm_Registro SET CurrtyDate=@pmCurrtyDate
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRelaciones @pmIdTabla VARCHAR(20),@pmTablaForanea VARCHAR(20),@pmCampoForaneo VARCHAR(20)
,@pmBasedeDatos VARCHAR(10),@pmNoValidar BIT
 AS 
 UPDATE Sys_Relaciones SET BasedeDatos=@pmBasedeDatos,NoValidar=@pmNoValidar
 WHERE IdTabla=@pmIdTabla AND TablaForanea=@pmTablaForanea AND CampoForaneo=@pmCampoForaneo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRemision @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaDesp SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdCliente VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrDescuento MONEY,@pmVrImpuesto MONEY,@pmVrFletes MONEY,@pmVrOtros MONEY,@pmVrOtrDcto MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmUnidades DECIMAL(14,4),@pmIdVend VARCHAR(16),@pmTarifaCom DECIMAL(14,4),@pmCodTarCom VARCHAR(4),@pmDirEnvio VARCHAR(250)
,@pmIdLocEnv VARCHAR(8),@pmDiasEntraga INT,@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargoContac VARCHAR(50),@pmIdForma VARCHAR(4),@pmDetallePago VARCHAR(100),@pmMulPlazos BIT, @pmIdPlazo VARCHAR(4), @pmCdRuta VARCHAR(4)
,@pmNitEmpTrans VARCHAR(16),@pmEmpTrans VARCHAR(150),@pmpVehiculo VARCHAR(10),@pmCdConductor VARCHAR(16)
,@pmDetalleEnvio VARCHAR(250),@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME,@pmTipPed VARCHAR(3),@pmPedido INT,@pmIdCiaPed CHAR(2),@pmFechaPed SMALLDATETIME,@pmNumAutoriza INT,@pmModalidad VARCHAR(10),@pmAnulado BIT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Remision SET Fecha=@pmFecha,FechaDesp=@pmFechaDesp,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdCliente=@pmIdCliente,IdAgencia=@pmIdAgencia,VrSubTotal=@pmVrSubTotal,VrDescuento=@pmVrDescuento,VrImpuesto=@pmVrImpuesto,VrFletes=@pmVrFletes,VrOtros=@pmVrOtros
,VrOtrDcto=@pmVrOtrDcto,VrNeto=@pmVrNeto,Cantidad=@pmCantidad,Unidades=@pmUnidades,IdVend=@pmIdVend,TarifaCom=@pmTarifaCom,CodTarCom=@pmCodTarCom,DirEnvio=@pmDirEnvio,IdLocEnv=@pmIdLocEnv,DiasEntraga=@pmDiasEntraga,NitContac=@pmNitContac,NomContac=@pmNomContac
,TelContac=@pmTelContac,emlContac=@pmemlContac,CargoContac=@pmCargoContac,NitEmpTrans=@pmNitEmpTrans,EmpTrans=@pmEmpTrans,pVehiculo=@pmpVehiculo,CdConductor=@pmCdConductor,DetalleEnvio=@pmDetalleEnvio,TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,TipPed=@pmTipPed,Pedido=@pmPedido,IdCiaPed=@pmIdCiaPed,NumAutoriza=@pmNumAutoriza,Modalidad=@pmModalidad,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,IdForma=@pmIdForma,DetallePago=@pmDetallePago,MulPlazos=@pmMulPlazos,IdPlazo=@pmIdPlazo,CdRuta=@pmCdRuta,FechaPed=@pmFechaPed
,NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRemisionAbr @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Remision SET TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRemisionAnu @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Remision SET  Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRemisionApr @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2)
,@pmObservacion VARCHAR(250),@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Remision SET NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob
,IdEstado=@pmIdEstado,Observacion=@pmObservacion
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRemisionFac @pmTipDoc VARCHAR(3),@pmRemision INT,@pmIdCia CHAR(2)
,@pmTipFac VARCHAR(3),@pmFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFact SMALLDATETIME
,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Remision SET  TipFac=@pmTipFac,Factura=@pmFactura,IdCiaFac=@pmIdCiaFac,FechaFact=@pmFechaFact
 ,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Remision=@pmRemision AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpReport @pmTipoRep VARCHAR(3),@pmIdRep CHAR(2),@pmDescripcion VARCHAR(100),@pmFormato VARCHAR(20),@pmTipoPapel INT
,@pmOrientacion INT,@pmVistaPrevia BIT,@pmVerSetup BIT,@pmNumCopias INT,@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50),@pmNomRep VARCHAR(50)
 AS 
 UPDATE Sys_Report SET Descripcion=@pmDescripcion,Formato=@pmFormato,TipoPapel=@pmTipoPapel,Orientacion=@pmOrientacion,VistaPrevia=@pmVistaPrevia,VerSetup=@pmVerSetup
,NumCopias=@pmNumCopias,sp_Nombre=@pmsp_Nombre,Ind_Location=@pmInd_Location,NomRep=@pmNomRep
 WHERE TipoRep=@pmTipoRep AND IdRep=@pmIdRep
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpReportFmto @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmTipoOrigen VARCHAR(10),@pmCrVersion INT,@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50)
 AS 
 UPDATE Sys_ReportFmto SET TipoOrigen=@pmTipoOrigen,CrVersion=@pmCrVersion,sp_Nombre=@pmsp_Nombre,Ind_Location=@pmInd_Location
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpReportSub @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmNombreSub VARCHAR(20),@pmTipoOrigen VARCHAR(10),@pmsp_Nombre VARCHAR(50),@pmInd_Location VARCHAR(50)
 AS 
 UPDATE Sys_ReportSub SET TipoOrigen=@pmTipoOrigen,sp_Nombre=@pmsp_Nombre,Ind_Location=@pmInd_Location
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato AND NombreSub=@pmNombreSub
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpReportSubPmt @pmTipoRep VARCHAR(10),@pmFormato VARCHAR(20)
,@pmNombreSub VARCHAR(20),@pmIndParam INT,@pmNomParam VARCHAR(30),@pmTipoDato VARCHAR(15),@pmValorPred VARCHAR(100)
 AS 
 UPDATE Sys_ReportSubPmt SET NomParam=@pmNomParam,TipoDato=@pmTipoDato,ValorPred=@pmValorPred
 WHERE TipoRep=@pmTipoRep AND Formato=@pmFormato AND NombreSub=@pmNombreSub AND IndParam=@pmIndParam
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRequisicion @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmFechaVence SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdRespons VARCHAR(16),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
,@pmIdDep VARCHAR(4),@pmVrSubTotal MONEY,@pmCantidad DECIMAL(14,4),@pmNContrato INT,@pmIdCiaCont CHAR(2),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmModalidad VARCHAR(10),@pmDirEntrega VARCHAR(250),@pmIdLocEnt VARCHAR(8)
,@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2),@pmFechaSal SMALLDATETIME,@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmNomContacto VARCHAR(150),@pmTelsContacto VARCHAR(50),@pmEmailContacto VARCHAR(100),@pmAnulado BIT
,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Requisicion SET Fecha=@pmFecha,FechaVence=@pmFechaVence,IdConcepto=@pmIdConcepto,IdRespons=@pmIdRespons,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,IdDep=@pmIdDep,VrSubTotal=@pmVrSubTotal
,Cantidad=@pmCantidad,NContrato=@pmNContrato,IdCiaCont=@pmIdCiaCont,NitCliente=@pmNitCliente,CdAgencia=@pmCdAgencia,Modalidad=@pmModalidad,DirEntrega=@pmDirEntrega
,IdLocEnt=@pmIdLocEnt,TipSal=@pmTipSal,NumSalida=@pmNumSalida,IdCiaSal=@pmIdCiaSal,FechaSal=@pmFechaSal,NumAprob=@pmNumAprob
,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob,Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,NomContacto=@pmNomContacto,TelsContacto=@pmTelsContacto,EmailContacto=@pmEmailContacto,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRequisicionAnu @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Requisicion SET  Anulado=@pmAnulado,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRequisicionApr @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmNumAprob INT,@pmFecAprob SMALLDATETIME,@pmCdUsuAprob VARCHAR(11),@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Requisicion SET  NumAprob=@pmNumAprob,FecAprob=@pmFecAprob,CdUsuAprob=@pmCdUsuAprob
,Observacion=@pmObservacion,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRequisicionSal @pmTipDoc VARCHAR(3),@pmRequisicion INT,@pmIdCia CHAR(2)
,@pmTipSal VARCHAR(3),@pmNumSalida INT,@pmIdCiaSal CHAR(2),@pmFechaSal SMALLDATETIME,@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Requisicion SET TipSal=@pmTipSal,NumSalida=@pmNumSalida,IdCiaSal=@pmIdCiaSal,FechaSal=@pmFechaSal
,IdEstado=@pmIdEstado
 WHERE TipDoc=@pmTipDoc AND Requisicion=@pmRequisicion AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRoles @pmIdObj VARCHAR(10),@pmIdRole VARCHAR(3),@pmFuncion VARCHAR(100)
 AS 
 UPDATE Sys_Roles SET Funcion=@pmFuncion
 WHERE IdObj=@pmIdObj AND IdRole=@pmIdRole
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRutas @pmIdRuta VARCHAR(4),@pmRuta VARCHAR(100),@pmIdLocOri VARCHAR(8),@pmIdLocDes VARCHAR(8),@pmEsLocal BIT
,@pmDistkmt DECIMAL(14,4),@pmTmHoras VARCHAR(8),@pmCodigoMt VARCHAR(5),@pmFleteTon MONEY,@pmInactivo BIT
 AS 
 UPDATE Rutas SET Ruta=@pmRuta,IdLocOri=@pmIdLocOri,IdLocDes=@pmIdLocDes,EsLocal=@pmEsLocal,Distkmt=@pmDistkmt
,TmHoras=@pmTmHoras,CodigoMt=@pmCodigoMt,FleteTon=@pmFleteTon,Inactivo=@pmInactivo
 WHERE IdRuta=@pmIdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpRutasPlan @pmIdRuta VARCHAR(4),@pmIdPunto INT,@pmDescripcion VARCHAR(100),@pmDistancia DECIMAL(14,4)
,@pmTiempo VARCHAR(8),@pmClasePto VARCHAR(10),@pmCdPto VARCHAR(4),@pmIdLocal VARCHAR(8),@pmPosLat DECIMAL(14,4),@pmPosLon DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE RutasPlan SET Descripcion=@pmDescripcion,Distancia=@pmDistancia,Tiempo=@pmTiempo,ClasePto=@pmClasePto,CdPto=@pmCdPto
,IdLocal=@pmIdLocal,PosLat=@pmPosLat,PosLon=@pmPosLon,Inactivo=@pmInactivo
 WHERE IdRuta=@pmIdRuta AND IdPunto=@pmIdPunto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpRutasTramos @pmIdRuta VARCHAR(4),@pmNumTramo INT,@pmIdLocal VARCHAR(8),@pmkmstramo DECIMAL(14,4),@pmkmstotal DECIMAL(14,4)
 AS 
 UPDATE RutasTramos SET IdLocal=@pmIdLocal,kmstramo=@pmkmstramo,kmstotal=@pmkmstotal
 WHERE IdRuta=@pmIdRuta AND NumTramo=@pmNumTramo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSAutoriza @pmIdConcepto VARCHAR(4),@pmConceptoAut VARCHAR(150),@pmInactivo BIT
 AS 
 UPDATE Sys_Autoriza SET ConceptoAut=@pmConceptoAut,Inactivo=@pmInactivo
 WHERE IdConcepto=@pmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSalidas @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY
,@pmVrImpuesto MONEY,@pmVrCostos MONEY,@pmVrNeto MONEY,@pmCantidad DECIMAL(14,4),@pmBaseImp MONEY,@pmTarifaIva DECIMAL(14,4),@pmCodTarIva VARCHAR(4),@pmTipOrd VARCHAR(3)
,@pmNumOrden INT,@pmIdCiaOrd CHAR(2),@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmModalidad VARCHAR(10)
,@pmCdConductor VARCHAR(16),@pmCdDep VARCHAR(4),@pmNContrato INT,@pmIdCiaCont CHAR(2),@pmNitCliente VARCHAR(16),@pmCdAgencia VARCHAR(16),@pmModRequis VARCHAR(10)
,@pmDirEntrega VARCHAR(250),@pmIdLocEnt VARCHAR(8),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2),@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmGuiaComb INT,@pmCdCiaGuia CHAR(2),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Salidas SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdTercero=@pmIdTercero,VrSubTotal=@pmVrSubTotal,VrImpuesto=@pmVrImpuesto,VrCostos=@pmVrCostos,VrNeto=@pmVrNeto,Cantidad=@pmCantidad
,BaseImp=@pmBaseImp,TarifaIva=@pmTarifaIva,CodTarIva=@pmCodTarIva,TipOrd=@pmTipOrd,NumOrden=@pmNumOrden,IdCiaOrd=@pmIdCiaOrd,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,pVehiculo=@pmpVehiculo
,CdConductor=@pmCdConductor,CdDep=@pmCdDep,NContrato=@pmNContrato,IdCiaCont=@pmIdCiaCont,NitCliente=@pmNitCliente,CdAgencia=@pmCdAgencia,ModRequis=@pmModRequis
,DirEntrega=@pmDirEntrega,IdLocEnt=@pmIdLocEnt,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Anulado=@pmAnulado,NumDev=@pmNumDev
,FecDev=@pmFecDev,Observacion=@pmObservacion,IdEstado=@pmIdEstado,GuiaComb=@pmGuiaComb,CdCiaGuia=@pmCdCiaGuia,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSalidasDev @pmTipDoc VARCHAR(3),@pmSalida INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Trn_Salidas SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
,Observacion=@pmObservacion,IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE TipDoc=@pmTipDoc AND Salida=@pmSalida AND IdCia=@pmIdCia



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSectoresEco @pmIdSector VARCHAR(8),@pmSectorEco VARCHAR(250),@pmInactivo BIT
 AS 
 UPDATE SectoresEco SET SectorEco=@pmSectorEco,Inactivo=@pmInactivo
 WHERE IdSector=@pmIdSector
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSecurity @pmIdLla INT,@pmDescripcion VARCHAR(50),@pmPwd VARCHAR(10)
 AS 
 UPDATE Sys_Security SET Descripcion=@pmDescripcion,Pwd=@pmPwd
 WHERE IdLla=@pmIdLla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSeriales @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4),@pmCdUbic VARCHAR(10),@pmFecEntrada SMALLDATETIME
,@pmFecSalida SMALLDATETIME,@pmNitProv VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmObservacion VARCHAR(250),@pmReferncia1 VARCHAR(50),@pmReferncia2 VARCHAR(50)
,@pmFecRef1 SMALLDATETIME,@pmFecRef2 SMALLDATETIME,@pmUbicacion VARCHAR(50)
 AS 
 UPDATE Trn_Seriales SET IdBodega=@pmIdBodega,CdUbic=@pmCdUbic,FecEntrada=@pmFecEntrada,FecSalida=@pmFecSalida,NitProv=@pmNitProv,IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Observacion=@pmObservacion,Referncia1=@pmReferncia1,Referncia2=@pmReferncia2,FecRef1=@pmFecRef1,FecRef2=@pmFecRef2,Ubicacion=@pmUbicacion
 WHERE IdProducto=@pmIdProducto AND NumSerial=@pmNumSerial AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSerialesEtd @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE Trn_Seriales SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo
 WHERE IdProducto=@pmIdProducto AND NumSerial=@pmNumSerial AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSerialesSal @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2)
,@pmFecSalida SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmReferncia1 VARCHAR(50),@pmReferncia2 VARCHAR(50)
,@pmObservacion  VARCHAR(250),@pmFecRef1 SMALLDATETIME,@pmFecRef2 SMALLDATETIME
 AS 
 UPDATE Trn_Seriales SET FecSalida=@pmFecSalida,IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Observacion=@pmObservacion,Referncia1=@pmReferncia1,Referncia2=@pmReferncia2,FecRef1=@pmFecRef1,FecRef2=@pmFecRef2
 WHERE IdProducto=@pmIdProducto AND NumSerial=@pmNumSerial AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSerialesUbi @pmIdProducto VARCHAR(16),@pmNumSerial VARCHAR(30),@pmIdCia CHAR(2),@pmIdBodega VARCHAR(4),@pmCdUbic VARCHAR(10)
,@pmUbicacion VARCHAR(50),@pmObservacion VARCHAR(250)
 AS 
 UPDATE Trn_Seriales SET IdBodega=@pmIdBodega,CdUbic=@pmCdUbic
,Observacion=@pmObservacion,Ubicacion=@pmUbicacion
 WHERE IdProducto=@pmIdProducto AND NumSerial=@pmNumSerial AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobEntidades @pmIdEnterr VARCHAR(8),@pmTipoEnterr VARCHAR(10),@pmNitEntidad VARCHAR(16),@pmTipoId CHAR(1),@pmDv CHAR(1),@pmDirOficina VARCHAR(250)
,@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pmSitioWeb VARCHAR(100),@pme_mail VARCHAR(100),@pmCedRepLegal VARCHAR(16),@pmRepLegal VARCHAR(150)
,@pmFormaPago VARCHAR(10),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmCdLocCue VARCHAR(8)
,@pmCargoRep VARCHAR(50),@pmNomTitular VARCHAR(150),@pmNomEntidad VARCHAR(50),@pmInactivo BIT,@pmCodCtaPag VARCHAR(4)
 AS 
 UPDATE SobEntidades SET TipoEnterr=@pmTipoEnterr,NitEntidad=@pmNitEntidad,TipoId=@pmTipoId,Dv=@pmDv,DirOficina=@pmDirOficina,Telefono=@pmTelefono,Fax=@pmFax,SitioWeb=@pmSitioWeb
,e_mail=@pme_mail,CedRepLegal=@pmCedRepLegal,RepLegal=@pmRepLegal,FormaPago=@pmFormaPago,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,CdLocCue=@pmCdLocCue
,CargoRep=@pmCargoRep,NomTitular=@pmNomTitular,NomEntidad=@pmNomEntidad,Inactivo=@pmInactivo,CodCtaPag=@pmCodCtaPag
 WHERE IdEnterr=@pmIdEnterr
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobProductos @pmIdClase INT,@pmIdProducto VARCHAR(16),@pmClaseProd VARCHAR(20)
 AS 
 UPDATE SobProductos SET IdProducto=@pmIdProducto,ClaseProd=@pmClaseProd
 WHERE IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobTiposDec @pmIdDec VARCHAR(4),@pmDeclaracion VARCHAR(100),@pmTipoDec VARCHAR(20),@pmNitDec VARCHAR(16),@pmTipoIdDec CHAR(1),@pmNombreDec VARCHAR(250)
,@pmDvDec CHAR(1),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8),@pmTelefono VARCHAR(20),@pmCedRepLegal VARCHAR(16),@pmRepLegal VARCHAR(150),@pmTipoFirma INT,@pmNitRevisor VARCHAR(16)
,@pmNomRevisor VARCHAR(150),@pmTpRevisor VARCHAR(20),@pmTarifSubsidio DECIMAL(14,4),@pmConsecutivo INT
 AS 
 UPDATE SobTiposDec SET Declaracion=@pmDeclaracion,TipoDec=@pmTipoDec,NitDec=@pmNitDec,TipoIdDec=@pmTipoIdDec,NombreDec=@pmNombreDec,DvDec=@pmDvDec,Direccion=@pmDireccion
,IdLocal=@pmIdLocal,Telefono=@pmTelefono,CedRepLegal=@pmCedRepLegal,RepLegal=@pmRepLegal,TipoFirma=@pmTipoFirma,NitRevisor=@pmNitRevisor,NomRevisor=@pmNomRevisor
,TpRevisor=@pmTpRevisor,TarifSubsidio=@pmTarifSubsidio,Consecutivo=@pmConsecutivo
 WHERE IdDec=@pmIdDec
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobTiposDecNum @pmIdDec VARCHAR(4),@pmConsecutivo INT
 AS 
 IF @pmConsecutivo IS NULL
  UPDATE SobTiposDec SET  Consecutivo=Consecutivo+1 WHERE IdDec=@pmIdDec
 ELSE
  UPDATE SobTiposDec SET  Consecutivo=@pmConsecutivo WHERE IdDec=@pmIdDec 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobretasas @pmIdDec VARCHAR(4),@pmNumDeclara INT,@pmFecha SMALLDATETIME,@pmnAnno INT,@pmnMes INT,@pmIdEnterr VARCHAR(8),@pmTipoEntidad VARCHAR(10),@pmVrSobretasa MONEY,@pmVrSanciones MONEY,@pmVrCompensa MONEY,@pmVrIntMora MONEY
,@pmVrTotalPagar MONEY,@pmVrFondoSubs MONEY,@pmVrRecaudoDep MONEY,@pmNitDeclara VARCHAR(16),@pmDvDeclara CHAR(1),@pmCedDeclara VARCHAR(16),@pmDeclarante VARCHAR(250),@pmDirEntidad VARCHAR(250),@pmEmailEnt VARCHAR(100),@pmNomRepres VARCHAR(150)
,@pmCargoRepres VARCHAR(50),@pmTipoFirma INT,@pmNitRevisor VARCHAR(16),@pmNomRevisor VARCHAR(150),@pmTpRevisor VARCHAR(20),@pmNumCheque VARCHAR(30),@pmCodBanco VARCHAR(4),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4)
,@pmCdLocCue VARCHAR(8),@pmTitularCuenta VARCHAR(150),@pmVrEfectivo MONEY,@pmVrOtros MONEY,@pmVrTransf MONEY,@pmVrPagado MONEY,@pmEstado INT,@pmAdhesivo VARCHAR(30),@pmFechaPago SMALLDATETIME,@pmEsCorr BIT,@pmNumCorr VARCHAR(30)
,@pmFecCorr SMALLDATETIME,@pmObservacion VARCHAR(250),@pmFecUpdate SMALLDATETIME,@pmCodCta VARCHAR(4),@pmTipEgreso VARCHAR(3),@pmNumEgreso INT,@pmCiaEgreso CHAR(2)
 AS 
 UPDATE Trn_Sobretasas SET Fecha=@pmFecha,nAnno=@pmnAnno,nMes=@pmnMes,IdEnterr=@pmIdEnterr,TipoEntidad=@pmTipoEntidad,VrSobretasa=@pmVrSobretasa,VrSanciones=@pmVrSanciones,VrCompensa=@pmVrCompensa,VrIntMora=@pmVrIntMora,VrTotalPagar=@pmVrTotalPagar
,VrFondoSubs=@pmVrFondoSubs,VrRecaudoDep=@pmVrRecaudoDep,NitDeclara=@pmNitDeclara,DvDeclara=@pmDvDeclara,CedDeclara=@pmCedDeclara,Declarante=@pmDeclarante,DirEntidad=@pmDirEntidad,EmailEnt=@pmEmailEnt,NomRepres=@pmNomRepres
,CargoRepres=@pmCargoRepres,TipoFirma=@pmTipoFirma,NitRevisor=@pmNitRevisor,NomRevisor=@pmNomRevisor,TpRevisor=@pmTpRevisor,NumCheque=@pmNumCheque,CodBanco=@pmCodBanco,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco
,CdLocCue=@pmCdLocCue,TitularCuenta=@pmTitularCuenta,VrEfectivo=@pmVrEfectivo,VrOtros=@pmVrOtros,VrPagado=@pmVrPagado,VrTransf=@pmVrTransf,Estado=@pmEstado,Adhesivo=@pmAdhesivo,FechaPago=@pmFechaPago,EsCorr=@pmEsCorr,NumCorr=@pmNumCorr,FecCorr=@pmFecCorr
,Observacion=@pmObservacion,FecUpdate=@pmFecUpdate,CodCta=@pmCodCta,TipEgreso=@pmTipEgreso,NumEgreso=@pmNumEgreso,CiaEgreso=@pmCiaEgreso
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobretasasCie @pmIdDec VARCHAR(4),@pmNumDeclara INT,@pmEstado INT
,@pmObservacion VARCHAR(250),@pmAdhesivo VARCHAR(30),@pmFechaPago SMALLDATETIME,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Sobretasas SET Estado=@pmEstado,Observacion=@pmObservacion
,Adhesivo=@pmAdhesivo,FechaPago=@pmFechaPago,FecUpdate=@pmFecUpdate
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSobretasasEst @pmIdDec VARCHAR(4),@pmNumDeclara INT,@pmEstado INT
,@pmObservacion VARCHAR(250),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Sobretasas SET Estado=@pmEstado,Observacion=@pmObservacion,FecUpdate=@pmFecUpdate
 WHERE IdDec=@pmIdDec AND NumDeclara=@pmNumDeclara
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSubCentros @pmIdSubCos VARCHAR(16),@pmSubCosto VARCHAR(100),@pmIdCCosto VARCHAR(16),@pmFecUpdate SMALLDATETIME,@pmInactivo BIT
 AS 
 UPDATE SubCentros SET SubCosto=@pmSubCosto,IdCCosto=@pmIdCCosto,FecUpdate=@pmFecUpdate,Inactivo=@pmInactivo
 WHERE IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSubGrupos @pmIdSubgrupo VARCHAR(8),@pmSubgrupo VARCHAR(150),@pmIdGrupo VARCHAR(8)
,@pmCdTarIva VARCHAR(4),@pmCdTarAut VARCHAR(4),@pmCdTarRet VARCHAR(4)
,@pmCdTarIca VARCHAR(4),@pmTipoInvent VARCHAR(10),@pmInactivo BIT
 AS 
 UPDATE SubGrupos SET Subgrupo=@pmSubgrupo,IdGrupo=@pmIdGrupo,CdTarIva=@pmCdTarIva
,CdTarAut=@pmCdTarAut,CdTarRet=@pmCdTarRet,CdTarIca=@pmCdTarIca
,TipoInvent=@pmTipoInvent,Inactivo=@pmInactivo
 WHERE IdSubgrupo=@pmIdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpSubGruposAgenc @pmIdSubAgenc VARCHAR(4),@pmSubGrupoAgenc VARCHAR(50),@pmIdGrupo VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE SubGruposAgenc SET SubGrupoAgenc=@pmSubGrupoAgenc,IdGrupo=@pmIdGrupo,Inactivo=@pmInactivo
 WHERE IdSubAgenc=@pmIdSubAgenc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSubgruposCue @pmIdSubgrupo VARCHAR(8),@pmIdClase VARCHAR(4),@pmIdCuenta VARCHAR(16)
,@pmCdCueDev VARCHAR(16),@pmCdCueCom VARCHAR(16),@pmCdCueCdv VARCHAR(16),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE SubgruposCue SET IdCuenta=@pmIdCuenta,CdCueDev=@pmCdCueDev,CdCueCom=@pmCdCueCom,CdCueCdv=@pmCdCueCdv,FecUpdate=@pmFecUpdate
 WHERE IdSubgrupo=@pmIdSubgrupo AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpSubzonas @pmIdSzona VARCHAR(4),@pmSubzona VARCHAR(50),@pmIdZona VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE Subzonas SET Subzona=@pmSubzona,IdZona=@pmIdZona,Inactivo=@pmInactivo
 WHERE IdSzona=@pmIdSzona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTFmtos @pmnFormato VARCHAR(10),@pmFmtoFactura VARCHAR(50),@pmTipFac VARCHAR(3)
 AS 
 UPDATE Sys_TFmtos SET FmtoFactura=@pmFmtoFactura,TipFac=@pmTipFac
 WHERE nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTablapor @pmIdTarifa VARCHAR(4),@pmDescripcion VARCHAR(50),@pmIdClase VARCHAR(4),@pmTarifa DECIMAL(16,6)
,@pmSimbolo CHAR(1),@pmCdLocdad VARCHAR(8),@pmMinimo MONEY,@pmMaximo MONEY,@pmInactivo BIT
 AS 
 UPDATE Tablapor SET Descripcion=@pmDescripcion,IdClase=@pmIdClase,Tarifa=@pmTarifa,Simbolo=@pmSimbolo,CdLocdad=@pmCdLocdad
,Minimo=@pmMinimo,Maximo=@pmMaximo,Inactivo=@pmInactivo
 WHERE IdTarifa=@pmIdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanqAforo @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4),@pmCantidad DECIMAL(14,4)
 AS 
 UPDATE TanqAforo SET Cantidad=@pmCantidad
 WHERE IdTanque=@pmIdTanque AND UndAforo=@pmUndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanqAforoFrac @pmIdTanque VARCHAR(4),@pmUndAforo DECIMAL(14,4),@pmFraccion DECIMAL(14,4),@pmMilimetros DECIMAL(14,4)
 AS 
 UPDATE TanqAforoFrac SET Fraccion=@pmFraccion,Milimetros=@pmMilimetros
 WHERE IdTanque=@pmIdTanque AND UndAforo=@pmUndAforo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanqDispens @pmIdMngra VARCHAR(10),@pmNumDisp VARCHAR(4),@pmNumPos VARCHAR(4),@pmNumMngra VARCHAR(4),@pmIdTanque VARCHAR(4),@pmDescripcion VARCHAR(50),@pmLecGalAnt DECIMAL(14,4),@pmLecGalUlt DECIMAL(14,4)
,@pmLecPesAnt MONEY,@pmLecPesUlt MONEY,@pmLtaPrec CHAR(1),@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT,@pmLecGalMax DECIMAL(14,4)
 AS 
 UPDATE TanqDispens SET NumDisp=@pmNumDisp,NumPos=@pmNumPos,NumMngra=@pmNumMngra,IdTanque=@pmIdTanque,Descripcion=@pmDescripcion,LecGalAnt=@pmLecGalAnt,LecGalUlt=@pmLecGalUlt,LecPesAnt=@pmLecPesAnt,LecPesUlt=@pmLecPesUlt
,LtaPrec=@pmLtaPrec,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo,LecGalMax=@pmLecGalMax
 WHERE IdMngra=@pmIdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanqDispensLec @pmIdMngra VARCHAR(10),@pmLecGalAnt DECIMAL(14,4),@pmLecGalUlt DECIMAL(14,4)
 AS 
 UPDATE TanqDispens SET LecGalAnt=@pmLecGalAnt,LecGalUlt=@pmLecGalUlt
 WHERE IdMngra=@pmIdMngra
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanqSaldos @pmIdTanque VARCHAR(4),@pmIdCia CHAR(2),@pmSaldoActual DECIMAL(14,4)
 AS 
 UPDATE TanqSaldos SET SaldoActual=@pmSaldoActual
 WHERE IdTanque=@pmIdTanque AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanqSaldosKar @pmDelete BIT,@pmIdProducto VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
 AS 
 IF @pmDelete<>0  
  BEGIN
   DECLARE @TrnNombre VARCHAR(20)
   SELECT @TrnNombre = 'TrnProdSaldos'
   BEGIN TRANSACTION @TrnNombre
    DELETE TanqSaldos
    FROM TanqSaldos AS TS INNER JOIN Tanques AS T ON TS.IdTanque=T.IdTanque
    WHERE IdCia LIKE ISNULL(@pmIdCia,'%%')  AND IdProducto LIKE ISNULL(@pmIdProducto,'%')
    INSERT INTO  TanqSaldos  (IdTanque,IdCia,SaldoActual)
    SELECT ISNULL(CdTanque,'0'),IdCia,SUM(Entradas-Salidas)
    FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
    WHERE TipDoc NOT IN ('PED','REM','COT','ODC','GUI','OCC','ODS') AND TipoRef<>'SERVICIO' AND Tanques<>0
    AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
    GROUP BY CdTanque,IdCia
   COMMIT TRANSACTION  @TrnNombre
   END
 ELSE
  INSERT INTO  TanqSaldos  (IdTanque,IdCia,SaldoActual)
  SELECT ISNULL(CdTanque,'0'),IdCia,SUM(Entradas-Salidas)
  FROM Trn_Kardex AS K INNER JOIN ProdMcias AS P ON K.IdProducto=P.IdProducto
  WHERE TipDoc NOT IN ('PED','REM','COT','ODC','GUI','OCC','ODS') AND TipoRef<>'SERVICIO' AND Tanques<>0
  AND K.IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  GROUP BY CdTanque,IdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanques @pmIdTanque VARCHAR(4),@pmIdProducto VARCHAR(16),@pmDescripcion VARCHAR(50),@pmCapTanq DECIMAL(14,4),@pmNivAgua DECIMAL(14,4),@pmCapNeta DECIMAL(14,4)
,@pmSaldoActual DECIMAL(14,4),@pmTnqGas BIT,@pmTnqUnido BIT,@pmTnqVirtual BIT,@pmCdNueTan VARCHAR(4),@pmNColor INT
,@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT,@pmIdCiaDef CHAR(2),@pmCodigoApi VARCHAR(10)
 AS 
 UPDATE Tanques SET IdProducto=@pmIdProducto,Descripcion=@pmDescripcion,CapTanq=@pmCapTanq,NivAgua=@pmNivAgua,CapNeta=@pmCapNeta,SaldoActual=@pmSaldoActual
,TnqGas=@pmTnqGas,TnqUnido=@pmTnqUnido,TnqVirtual=@pmTnqVirtual,CdNueTan=@pmCdNueTan,NColor=@pmNColor,FechaUpdate=@pmFechaUpdate,Inactivo=@pmInactivo
,IdCiaDef =@pmIdCiaDef,CodigoApi=@pmCodigoApi
 WHERE IdTanque=@pmIdTanque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTanquesSal @pmIdProducto VARCHAR(16)=Null,@pmIdTanque VARCHAR(4)=Null
 AS 
 UPDATE Tanques 
 SET SaldoActual=(SELECT ISNULL(SUM(SaldoActual),0) FROM TanqSaldos WHERE Tanques.IdTanque=TanqSaldos.IdTanque)
 WHERE  IdProducto LIKE ISNULL(@pmIdProducto,'%') AND IdTanque LIKE ISNULL(@pmIdTanque,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTarifBuses @pmIdTarifa VARCHAR(4),@pmDescripcion VARCHAR(20),@pmVrNormal MONEY,@pmVrFestivo MONEY,@pmInactivo BIT
 AS 
 UPDATE TarifBuses SET Descripcion=@pmDescripcion,VrNormal=@pmVrNormal,VrFestivo=@pmVrFestivo,Inactivo=@pmInactivo
 WHERE IdTarifa=@pmIdTarifa
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTarifasFletes @pmIdOrigen VARCHAR(8),@pmIdDestino VARCHAR(8),@pmItem INT,@pmTipoTarifa VARCHAR(10),@pmTarifClie MONEY,@pmTarifPago MONEY,@pmTarifTabla MONEY,@pmIdMneda VARCHAR(5)
,@pmUndMed VARCHAR(10),@pmNitCliente VARCHAR(16),@pmCdNat VARCHAR(4),@pmCdTipoVeh VARCHAR(4),@pmCdRango VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE TarifasFletes SET TipoTarifa=@pmTipoTarifa,TarifClie=@pmTarifClie,TarifPago=@pmTarifPago,TarifTabla=@pmTarifTabla,IdMneda=@pmIdMneda,UndMed=@pmUndMed,NitCliente=@pmNitCliente
,CdNat=@pmCdNat,CdTipoVeh=@pmCdTipoVeh,CdRango=@pmCdRango,Inactivo=@pmInactivo
 WHERE IdOrigen=@pmIdOrigen AND IdDestino=@pmIdDestino AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpTasasDTF @pmnAnno INT,@pmnMes INT,@pmnSem INT,@pmDTF_EA DECIMAL(14,4),@pmTIBC_EA DECIMAL(14,4),@pmFactor_Mon DECIMAL(14,4)
 AS 
 UPDATE TasasDTF SET DTF_EA=@pmDTF_EA,TIBC_EA=@pmTIBC_EA,Factor_Mon=@pmFactor_Mon
 WHERE nAnno=@pmnAnno AND nMes=@pmnMes AND nSem=@pmnSem


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpTasasInteres @pmIdTasa VARCHAR(4),@pmDescTasa VARCHAR(50),@pmTipoInteres VARCHAR(10),@pmTipoPeriodo VARCHAR(10),@pmCausaInteres INT,@pmTipoDTF VARCHAR(10)
,@pmDTF_EA DECIMAL(14,4),@pmSpread_TA DECIMAL(14,4),@pmTasaEfectiva DECIMAL(14,4),@pmOpcCompra DECIMAL(14,4),@pmInactivo BIT
 AS 
 UPDATE TasasInteres SET DescTasa=@pmDescTasa,TipoInteres=@pmTipoInteres,TipoPeriodo=@pmTipoPeriodo,CausaInteres=@pmCausaInteres,TipoDTF=@pmTipoDTF,DTF_EA=@pmDTF_EA,Spread_TA=@pmSpread_TA
,TasaEfectiva=@pmTasaEfectiva,OpcCompra=@pmOpcCompra,Inactivo=@pmInactivo
 WHERE IdTasa=@pmIdTasa


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpTasasIntmora @pmIdMora VARCHAR(4),@pmDescMora VARCHAR(50),@pmTipoInteres VARCHAR(10)
,@pmTasaEA DECIMAL(14,4),@pmTasaEM DECIMAL(14,4),@pmDiasGracia INT,@pmInactivo BIT
 AS 
 UPDATE TasasIntmora SET DescMora=@pmDescMora,TipoInteres=@pmTipoInteres,TasaEA=@pmTasaEA,TasaEM=@pmTasaEM,DiasGracia=@pmDiasGracia,Inactivo=@pmInactivo
 WHERE IdMora=@pmIdMora


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTaxRutas @pmIdRuta VARCHAR(4),@pmNomRuta VARCHAR(100),@pmTipoRuta VARCHAR(10),@pmTarifPago MONEY
,@pmTarifCobro MONEY,@pmOrigen VARCHAR(30),@pmDestino VARCHAR(30),@pmCodRuta VARCHAR(20),@pmInactivo BIT
 AS 
 UPDATE TaxRutas SET NomRuta=@pmNomRuta,TipoRuta=@pmTipoRuta,TarifPago=@pmTarifPago,TarifCobro=@pmTarifCobro,Origen=@pmOrigen
,Destino=@pmDestino,CodRuta=@pmCodRuta,Inactivo=@pmInactivo
 WHERE IdRuta=@pmIdRuta
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTaxTarifClie @pmTipoTarif VARCHAR(10),@pmItem INT,@pmIdClie VARCHAR(16),@pmTipoHora INT,@pmIdRuta VARCHAR(4),@pmNumInicial DECIMAL(14,4)
,@pmNumFinal DECIMAL(14,4),@pmTarifPago MONEY,@pmTarifCobro MONEY
 AS 
 UPDATE TaxTarifClie SET IdClie=@pmIdClie,TipoHora=@pmTipoHora,IdRuta=@pmIdRuta,NumInicial=@pmNumInicial,NumFinal=@pmNumFinal,TarifPago=@pmTarifPago,TarifCobro=@pmTarifCobro
 WHERE TipoTarif=@pmTipoTarif AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTaxTarifas @pmTipoTarif VARCHAR(10),@pmItem INT,@pmTipoHora INT,@pmNumInicial DECIMAL(14,4)
,@pmNumFinal DECIMAL(14,4),@pmTarifPago MONEY,@pmTarifCobro MONEY
 AS 
 UPDATE TaxTarifas SET TipoHora=@pmTipoHora,NumInicial=@pmNumInicial,NumFinal=@pmNumFinal,TarifPago=@pmTarifPago,TarifCobro=@pmTarifCobro
 WHERE TipoTarif=@pmTipoTarif AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTercClieLineas @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmIdLinea VARCHAR(4),@pmIdTasa VARCHAR(4),@pmnPlazo INT,@pmTipoPlazo CHAR(1),@pmCupoCredito MONEY,@pmSaldoCapital MONEY,@pmSaldoInteres MONEY,@pmMaxCupoSmlv DECIMAL(14,4),@pmTipoCausac VARCHAR(10),@pmDiasVence VARCHAR(20)
,@pmFecPreAprob SMALLDATETIME,@pmPlazoAprob INT,@pmEtdoAprob INT,@pmTipoAprob VARCHAR(20),@pmDescAprob VARCHAR(250),@pmGarantias BIT,@pmTipoGarantia VARCHAR(10),@pmDescGarantia VARCHAR(250),@pmVrGarantia MONEY,@pmReqCheques BIT,@pmFecActCupo SMALLDATETIME,@pmEstadoAct INT,@pmLiqSeguro BIT
,@pmCdTasaConv VARCHAR(4),@pmNitConvenio VARCHAR(16),@pmCuotasConv INT,@pmFechaAct SMALLDATETIME
 AS 
 UPDATE TercClieLineas SET IdTasa=@pmIdTasa,nPlazo=@pmnPlazo,TipoPlazo=@pmTipoPlazo,CupoCredito=@pmCupoCredito,SaldoCapital=@pmSaldoCapital,SaldoInteres=@pmSaldoInteres,MaxCupoSmlv=@pmMaxCupoSmlv,TipoCausac=@pmTipoCausac,DiasVence=@pmDiasVence,FecPreAprob=@pmFecPreAprob,PlazoAprob=@pmPlazoAprob,EtdoAprob=@pmEtdoAprob
,TipoAprob=@pmTipoAprob,DescAprob=@pmDescAprob,Garantias=@pmGarantias,TipoGarantia=@pmTipoGarantia,DescGarantia=@pmDescGarantia,VrGarantia=@pmVrGarantia,ReqCheques=@pmReqCheques,FecActCupo=@pmFecActCupo,EstadoAct=@pmEstadoAct,LiqSeguro=@pmLiqSeguro,CdTasaConv=@pmCdTasaConv,NitConvenio=@pmNitConvenio,CuotasConv=@pmCuotasConv,FechaAct=@pmFechaAct
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND IdLinea=@pmIdLinea
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTercCliePreRef @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmItem INT,@pmTipoRef VARCHAR(10),@pmReferencia VARCHAR(150),@pmNitRefncia VARCHAR(16)
,@pmDirRefncia VARCHAR(150),@pmTelRefncia VARCHAR(30),@pmemlRefncia VARCHAR(100),@pmCargRefncia VARCHAR(50),@pmNumCuenta VARCHAR(30),@pmObservcion VARCHAR(250)
 AS 
 UPDATE TercCliePreRef SET TipoRef=@pmTipoRef,Referencia=@pmReferencia,NitRefncia=@pmNitRefncia,DirRefncia=@pmDirRefncia,TelRefncia=@pmTelRefncia,emlRefncia=@pmemlRefncia
,CargRefncia=@pmCargRefncia,NumCuenta=@pmNumCuenta,Observcion=@pmObservcion
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTercCliePrestamo @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50)
,@pmIdSzona VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdTipoTerc VARCHAR(4),@pmIdVend VARCHAR(16),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmEsExento BIT,@pmCalcIntMora BIT,@pmDiasGracia INT,@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME
,@pmFecRetiro SMALLDATETIME,@pmVrSalBasico MONEY,@pmMaxDcto DECIMAL(14,4),@pmMatMerc VARCHAR(20),@pmFecMat SMALLDATETIME,@pmNomAgencia VARCHAR(150),@pmDirAgencia VARCHAR(250),@pmIdLocalAge VARCHAR(8),@pmComentarios VARCHAR(2000),@pmIdEstado VARCHAR(4)
,@pmInactivo BIT,@pmTipoSalario CHAR(1),@pmFechaAct SMALLDATETIME
 AS 
 UPDATE TercCliePrestamo SET NitRepLeg=@pmNitRepLeg,NomRepLeg=@pmNomRepLeg,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargContac=@pmCargContac,IdSzona=@pmIdSzona,IdGrupo=@pmIdGrupo,IdTipoTerc=@pmIdTipoTerc,IdVend=@pmIdVend
,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,EsExento=@pmEsExento,CalcIntMora=@pmCalcIntMora,DiasGracia=@pmDiasGracia,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia,FecRetiro=@pmFecRetiro,VrSalBasico=@pmVrSalBasico,MaxDcto=@pmMaxDcto,MatMerc=@pmMatMerc,FecMat=@pmFecMat
,Comentarios=@pmComentarios,IdEstado=@pmIdEstado,NomAgencia=@pmNomAgencia,DirAgencia=@pmDirAgencia,IdLocalAge=@pmIdLocalAge,Inactivo=@pmInactivo,FechaAct=@pmFechaAct,TipoSalario=@pmTipoSalario
 WHERE IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClieRef @pmIdClie VARCHAR(16),@pmItem INT,@pmTipoRef VARCHAR(10),@pmReferencia VARCHAR(150)
,@pmNitRefncia VARCHAR(16),@pmDirRefncia VARCHAR(150),@pmTelRefncia VARCHAR(30),@pmemlRefncia VARCHAR(100)
,@pmCargRefncia VARCHAR(50),@pmNumCuenta VARCHAR(30),@pmObservcion VARCHAR(250)
 AS 
 UPDATE TercClieRef SET TipoRef=@pmTipoRef,Referencia=@pmReferencia,NitRefncia=@pmNitRefncia,DirRefncia=@pmDirRefncia
,TelRefncia=@pmTelRefncia,emlRefncia=@pmemlRefncia,CargRefncia=@pmCargRefncia,NumCuenta=@pmNumCuenta,Observcion=@pmObservcion
 WHERE IdClie=@pmIdClie AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClieSaldos @pmIdClie VARCHAR(16),@pmCdAgencia VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 UPDATE TercClieSaldos SET VrSaldo=@pmVrSaldo
 WHERE IdClie=@pmIdClie AND CdAgencia=@pmCdAgencia AND IdCia=@pmIdCia

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClieSaldosAcu @pmIdClie VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmUpClie BIT=Null,@pmUpAge BIT=Null
 AS 
 IF EXISTS(SELECT * FROM tempdb..sysobjects WHERE NAME LIKE '#ClieSaldos%') 
 DROP TABLE #ClieSaldos
 CREATE TABLE #ClieSaldos (tmId INT PRIMARY KEY IDENTITY (1,1)
 ,IdClie VARCHAR(16),CdAgencia VARCHAR(16),IdCia CHAR(2),VrFacturas MONEY,VrAbonos MONEY,VrAFavor MONEY,VrAbonoSAF MONEY)
--facturas y vencimientos
 INSERT INTO #ClieSaldos (IdClie,CdAgencia,IdCia,VrFacturas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,SUM(VrFactura),0,0,0
 FROM Trn_Vencimientos
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
--abonos de facturas
 INSERT INTO #ClieSaldos (IdClie,CdAgencia,IdCia,VrFacturas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,SUM(VrAbono),0,0
 FROM Trn_VencAbonos
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
--recibos a favor
 INSERT INTO #ClieSaldos (IdClie,CdAgencia,IdCia,VrFacturas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,0,SUM(VrAFavor-VrOtrosDb),0
 FROM Trn_RecAfavor
 WHERE IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia
 --abonos de saldos a favor
 INSERT INTO #ClieSaldos (IdClie,CdAgencia,IdCia,VrFacturas,VrAbonos,VrAFavor,VrAbonoSAF) 
 SELECT IdCliente,IdAgencia,IdCia,0,0,0,SUM(VrAbono)
 FROM Trn_VencAbonos
 WHERE TipoAplica='ANTICIPO' AND IdCliente LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 GROUP BY  IdCliente,IdAgencia,IdCia 
 --actualiza saldos
 DECLARE @TrnNombre VARCHAR(20)
 SET @TrnNombre = 'TrnSaldos'
 BEGIN TRANSACTION @TrnNombre
 DELETE FROM TercClieSaldos WHERE IdClie LIKE ISNULL(@pmIdClie,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
 INSERT INTO TercClieSaldos (IdClie,CdAgencia,IdCia,VrSaldo)
 SELECT IdClie,CdAgencia,IdCia,SUM((VrFacturas-VrAbonos)-(VrAFavor-VrAbonoSAF)) 
 FROM #ClieSaldos
 GROUP BY IdClie,CdAgencia,IdCia
 COMMIT TRANSACTION  @TrnNombre
--DROP TABLE #ClieSaldos
IF @pmUpClie=1  
 BEGIN 
 UPDATE TercCliente SET VrSaldo=(SELECT ISNULL(SUM(VrSaldo),0) FROM TercClieSaldos
 WHERE TercClieSaldos.IdClie=TercCliente.IdClie)
 WHERE  IdClie LIKE ISNULL(@pmIdClie,'%') 
 END
IF @pmUpAge=1
 BEGIN
 UPDATE Agencias SET VrSaldoAct=(SELECT ISNULL(SUM(VrSaldo),0) FROM TercClieSaldos
 WHERE TercClieSaldos.IdClie=Agencias.IdClie AND TercClieSaldos.CdAgencia=Agencias.IdAgencia)
 WHERE  IdClie LIKE ISNULL(@pmIdClie,'%')  
 END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClieVeh @pmIdClie VARCHAR(16),@pmpVehiculo VARCHAR(10)
,@pmCdConductor VARCHAR(16),@pmNumVehiculo VARCHAR(10),@pmDescripcion VARCHAR(100)
 AS 
 UPDATE TercClieVeh SET CdConductor=@pmCdConductor,NumVehiculo=@pmNumVehiculo,Descripcion=@pmDescripcion
 WHERE IdClie=@pmIdClie AND pVehiculo=@pmpVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCliente @pmIdClie VARCHAR(16),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20),@pmemlContac VARCHAR(100)
,@pmCargContac VARCHAR(50),@pmDirEnv VARCHAR(250),@pmIdLocEnv VARCHAR(8),@pmDiasEntga INT,@pmIdSzona VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmIdPlazo VARCHAR(4),@pmIdForma VARCHAR(4),@pmIdEstrato VARCHAR(4)
,@pmIdVend VARCHAR(16),@pmNitFact VARCHAR(16),@pmIdRuta VARCHAR(4),@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmCdMney VARCHAR(5),@pmCdDct VARCHAR(4),@pmCdRet VARCHAR(4)
,@pmCdRiv VARCHAR(4),@pmCdCms VARCHAR(4),@pmExcIva BIT,@pmTrfIntMora DECIMAL(14,4),@pmDiasGracia INT,@pmLiqFletes BIT,@pmFactSold BIT,@pmAutoret BIT,@pmIncRet BIT,@pmIncRiv BIT,@pmIncIca BIT,@pmFactTipo INT,@pmVrCupo MONEY
,@pmVrSaldo MONEY,@pmUidClie VARCHAR(16),@pmPwdClie VARCHAR(10),@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmMatMerc VARCHAR(20),@pmFecMat SMALLDATETIME
,@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmCmntario1 VARCHAR(250),@pmCmntario2 VARCHAR(250),@pmPlazosImp VARCHAR(20),@pmCdBandera VARCHAR(4),@pmContrato BIT,@pmNContrato INT,@pmCiaContMay CHAR(2)
,@pmCmntario3 VARCHAR(250),@pmPrendGarant VARCHAR(150),@pmFecUpCupo SMALLDATETIME,@pmTipoCliente VARCHAR(10),@pmRestric_Cia BIT,@pmCupoGalones DECIMAL(14,4)
,@pmCdPlazoComb VARCHAR(4),@pmCodClieSicom VARCHAR(20),@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFecPlazoDoc SMALLDATETIME,@pmEdoRadicaDoc INT,@pmCdTipBloq VARCHAR(4),@pmDescEdoDoc VARCHAR(250),@pmFechaUpdate SMALLDATETIME 
 AS 
 UPDATE TercCliente SET NitRepLeg=@pmNitRepLeg,NomRepLeg=@pmNomRepLeg,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargContac=@pmCargContac,DirEnv=@pmDirEnv,IdLocEnv=@pmIdLocEnv
,DiasEntga=@pmDiasEntga,IdSzona=@pmIdSzona,IdGrupo=@pmIdGrupo,IdPlazo=@pmIdPlazo,IdForma=@pmIdForma,IdEstrato=@pmIdEstrato,IdVend=@pmIdVend,NitFact=@pmNitFact,IdRuta=@pmIdRuta,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco
,CdMney=@pmCdMney,CdDct=@pmCdDct,CdRet=@pmCdRet,CdRiv=@pmCdRiv,CdCms=@pmCdCms,ExcIva=@pmExcIva,TrfIntMora=@pmTrfIntMora,DiasGracia=@pmDiasGracia,LiqFletes=@pmLiqFletes,FactSold=@pmFactSold,Autoret=@pmAutoret,IncRet=@pmIncRet,IncRiv=@pmIncRiv
,IncIca=@pmIncIca,FactTipo=@pmFactTipo,VrCupo=@pmVrCupo,VrSaldo=@pmVrSaldo,UidClie=@pmUidClie,PwdClie=@pmPwdClie,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia,FecRetiro=@pmFecRetiro,MatMerc=@pmMatMerc,FecMat=@pmFecMat,PathFoto=@pmPathFoto
,PathFirma=@pmPathFirma,Cmntario1=@pmCmntario1,Cmntario2=@pmCmntario2,PlazosImp=@pmPlazosImp,CdBandera=@pmCdBandera,Contrato=@pmContrato,NContrato=@pmNContrato,CiaContMay=@pmCiaContMay,Cmntario3=@pmCmntario3,PrendGarant=@pmPrendGarant,FecUpCupo=@pmFecUpCupo
,CdPlazoComb=@pmCdPlazoComb,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,TipoCliente=@pmTipoCliente,FechaUpdate=@pmFechaUpdate,Restric_Cia=@pmRestric_Cia,CupoGalones=@pmCupoGalones,CodClieSicom=@pmCodClieSicom,FecPlazoDoc=@pmFecPlazoDoc,EdoRadicaDoc=@pmEdoRadicaDoc
,CdTipBloq=@pmCdTipBloq,DescEdoDoc=@pmDescEdoDoc
 WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClienteCon @pmIdClie VARCHAR(16),@pmContrato BIT,@pmNContrato INT
,@pmCiaContMay CHAR(2),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercCliente SET Contrato=@pmContrato,NContrato=@pmNContrato
,CiaContMay=@pmCiaContMay,FechaUpdate=@pmFechaUpdate
 WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClienteCre  @pmIdClie VARCHAR(16),@pmFecUpCupo SMALLDATETIME
AS 
 UPDATE TercCliente SET FecUpCupo=@pmFecUpCupo,VrCupo=(SELECT ISNULL(SUM(VrCupoCre),0) FROM Agencias
 WHERE IdClie=@pmIdClie  AND  Inactivo=0)
 WHERE IdClie=@pmIdClie 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClienteEst @pmIdClie VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmCmntario1 VARCHAR(250),@pmFechaUpdate SMALLDATETIME 
 AS 
 UPDATE TercCliente SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo,Cmntario1=@pmCmntario1
,FechaUpdate=@pmFechaUpdate
 WHERE IdClie=@pmIdClie
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercClienteSdo @pmIdClie VARCHAR(16),@pmVrSaldo MONEY
 AS 
 UPDATE TercCliente SET VrSaldo=@pmVrSaldo
 WHERE IdClie=@pmIdClie

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpTercCndContrato @pmNumContrato INT,@pmIdConductor VARCHAR(16),@pmIdTipCon VARCHAR(4),@pmIdModd VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecRetiro SMALLDATETIME
,@pmFecVigencia SMALLDATETIME,@pmObservaciones VARCHAR(250),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE TercCndContrato SET IdConductor=@pmIdConductor,IdTipCon=@pmIdTipCon,IdModd=@pmIdModd,FecIngreso=@pmFecIngreso,FecRetiro=@pmFecRetiro,FecVigencia=@pmFecVigencia,Observaciones=@pmObservaciones,FecUpdate=@pmFecUpdate
 WHERE NumContrato=@pmNumContrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTercCndReferencias @pmIdConductor VARCHAR(16),@pmItem INT,@pmTipoRef VARCHAR(10),@pmEmpresaRef VARCHAR(150),@pmNitRef VARCHAR(16),@pmDireccionRef VARCHAR(150),@pmTelefonoRef VARCHAR(30),@pmContactoRef VARCHAR(150)
,@pmEmailRef VARCHAR(100),@pmCargoRef VARCHAR(50),@pmCuentaBanco VARCHAR(30),@pmFechaInicial SMALLDATETIME,@pmFechaFinal SMALLDATETIME,@pmVerificado VARCHAR(150),@pmObservRef VARCHAR(250)
 AS 
 UPDATE TercCndReferencias SET TipoRef=@pmTipoRef,EmpresaRef=@pmEmpresaRef,NitRef=@pmNitRef,DireccionRef=@pmDireccionRef,TelefonoRef=@pmTelefonoRef,ContactoRef=@pmContactoRef,EmailRef=@pmEmailRef,CargoRef=@pmCargoRef,CuentaBanco=@pmCuentaBanco
,FechaInicial=@pmFechaInicial,FechaFinal=@pmFechaFinal,Verificado=@pmVerificado,ObservRef=@pmObservRef
 WHERE IdConductor=@pmIdConductor AND Item=@pmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCndTarjetas @pmIdConductor VARCHAR(16),@pmItem INT,@pmNumeroTarj VARCHAR(30)
,@pmSecuencia INT,@pmFecha SMALLDATETIME,@pmFecVence SMALLDATETIME
 AS 
 UPDATE TercCndTarjetas SET NumeroTarj=@pmNumeroTarj,Secuencia=@pmSecuencia,Fecha=@pmFecha,FecVence=@pmFecVence
 WHERE IdConductor=@pmIdConductor AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCndtores @pmIdConductor VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmIdModd VARCHAR(4),@pmTipoSangre CHAR(2),@pmFactorRh CHAR(1),@pmLibMilitar VARCHAR(25),@pmClaseLib INT,@pmFechaLib SMALLDATETIME,@pmDistMil VARCHAR(10),@pmCertJudicial VARCHAR(25)
,@pmFecJudicial SMALLDATETIME,@pmVigJudicial SMALLDATETIME,@pmLicencia VARCHAR(25),@pmIdLugar VARCHAR(8),@pmCatLicencia INT,@pmFecLicencia SMALLDATETIME,@pmVigLicencia SMALLDATETIME
,@pmFecNacmto SMALLDATETIME,@pmLugarNacmto VARCHAR(30),@pmSexo CHAR(1),@pmIdEstCivil VARCHAR(4),@pmNHijos INT,@pmPasporte VARCHAR(25),@pmVigPaspte SMALLDATETIME,@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4)
,@pmCdLocCue VARCHAR(8),@pmCdFonPen VARCHAR(8),@pmCdFonEps VARCHAR(8),@pmCdFonArp VARCHAR(8),@pmpVehiculo VARCHAR(10),@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmPathLic VARCHAR(30),@pmCmntarios VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmNumTarjeta VARCHAR(30),@pmEdicTarjeta INT,@pmFecTarjeta SMALLDATETIME,@pmVigTarjeta SMALLDATETIME,@pmPasSeguridad VARCHAR(30),@pmFecPasSeg SMALLDATETIME,@pmVigPasSeg SMALLDATETIME,@pmNitEmpTrans VARCHAR(16),@pmFechaUpdate SMALLDATETIME
,@pmNum_Contrato INT,@pmContrato_Activo BIT,@pmCdTipCon VARCHAR(4),@pmFec_Ingreso SMALLDATETIME,@pmFec_Retiro SMALLDATETIME,@pmFec_Vigencia SMALLDATETIME,@pmFec_IngresoAnt SMALLDATETIME,@pmFec_UltimoAcc SMALLDATETIME
,@pmPasadoJudicial VARCHAR(250),@pmRestriccion VARCHAR(50),@pmFecPagoPen SMALLDATETIME,@pmFecPagoEps SMALLDATETIME,@pmFecPagoArp SMALLDATETIME
 AS 
 UPDATE TercCndtores SET IdGrupo=@pmIdGrupo,IdModd=@pmIdModd,TipoSangre=@pmTipoSangre,FactorRh=@pmFactorRh,LibMilitar=@pmLibMilitar,ClaseLib=@pmClaseLib,FechaLib=@pmFechaLib,DistMil=@pmDistMil,CertJudicial=@pmCertJudicial,FecJudicial=@pmFecJudicial,VigJudicial=@pmVigJudicial
,Licencia=@pmLicencia,IdLugar=@pmIdLugar,CatLicencia=@pmCatLicencia,FecLicencia=@pmFecLicencia,VigLicencia=@pmVigLicencia,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,CdLocCue=@pmCdLocCue,CdFonPen=@pmCdFonPen,CdFonEps=@pmCdFonEps
,CdFonArp=@pmCdFonArp,FecNacmto=@pmFecNacmto,LugarNacmto=@pmLugarNacmto,Sexo=@pmSexo,IdEstCivil=@pmIdEstCivil,NHijos=@pmNHijos,Pasporte=@pmPasporte,VigPaspte=@pmVigPaspte,pVehiculo=@pmpVehiculo,PathFoto=@pmPathFoto,PathFirma=@pmPathFirma
,PathLic=@pmPathLic,Cmntarios=@pmCmntarios,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,NumTarjeta=@pmNumTarjeta,EdicTarjeta=@pmEdicTarjeta,FecTarjeta=@pmFecTarjeta,VigTarjeta=@pmVigTarjeta,PasSeguridad=@pmPasSeguridad,FecPasSeg=@pmFecPasSeg,VigPasSeg=@pmVigPasSeg
,NitEmpTrans=@pmNitEmpTrans,FechaUpdate=@pmFechaUpdate,Num_Contrato=@pmNum_Contrato,Contrato_Activo=@pmContrato_Activo,CdTipCon=@pmCdTipCon,Fec_Ingreso=@pmFec_Ingreso,Fec_Retiro=@pmFec_Retiro,Fec_Vigencia=@pmFec_Vigencia,Fec_IngresoAnt=@pmFec_IngresoAnt
,Fec_UltimoAcc=@pmFec_UltimoAcc,PasadoJudicial=@pmPasadoJudicial,Restriccion=@pmRestriccion,FecPagoPen=@pmFecPagoPen,FecPagoEps=@pmFecPagoEps,FecPagoArp=@pmFecPagoArp
 WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpTercCndtoresCon @pmIdConductor VARCHAR(16),@pmNum_Contrato INT,@pmContrato_Activo BIT,@pmCdTipCon VARCHAR(4)
,@pmFec_Ingreso SMALLDATETIME,@pmFec_Retiro SMALLDATETIME,@pmFec_Vigencia SMALLDATETIME,@pmIdModd VARCHAR(4)
 AS 
 UPDATE TercCndtores SET IdModd=@pmIdModd,Contrato_Activo=@pmContrato_Activo,CdTipCon=@pmCdTipCon
,Fec_Ingreso=@pmFec_Ingreso,Fec_Retiro=@pmFec_Retiro,Fec_Vigencia=@pmFec_Vigencia
 WHERE IdConductor=@pmIdConductor AND Num_Contrato=@pmNum_Contrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpTercCndtoresCont @pmIdConductor VARCHAR(16),@pmNum_Contrato INT,@pmContrato_Activo BIT,@pmCdTipCon VARCHAR(4)
,@pmFec_Ingreso SMALLDATETIME,@pmFec_Retiro SMALLDATETIME,@pmFec_Vigencia SMALLDATETIME,@pmIdModd VARCHAR(4)
 AS 
 UPDATE TercCndtores SET Num_Contrato=@pmNum_Contrato,IdModd=@pmIdModd,Contrato_Activo=@pmContrato_Activo,CdTipCon=@pmCdTipCon
,Fec_Ingreso=@pmFec_Ingreso,Fec_Retiro=@pmFec_Retiro,Fec_Vigencia=@pmFec_Vigencia
 WHERE IdConductor=@pmIdConductor  

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCndtoresEst @pmIdConductor VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmCmntarios VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercCndtores SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Cmntarios=@pmCmntarios,FechaUpdate=@pmFechaUpdate
 WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCndtoresFtar @pmIdConductor VARCHAR(16),@pmNumTarjeta VARCHAR(30)
,@pmFecTarjeta SMALLDATETIME,@pmVigTarjeta SMALLDATETIME
 AS 
 UPDATE TercCndtores SET FecTarjeta=@pmFecTarjeta,VigTarjeta=@pmVigTarjeta
 WHERE IdConductor=@pmIdConductor AND NumTarjeta LIKE ISNULL(@pmNumTarjeta,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpTercCndtoresRet @pmIdConductor VARCHAR(16),@pmNum_Contrato INT,@pmContrato_Activo BIT
,@pmFec_Retiro SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmCmntarios VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercCndtores SET Num_Contrato=@pmNum_Contrato,Contrato_Activo=@pmContrato_Activo,Fec_Retiro=@pmFec_Retiro,IdEstado=@pmIdEstado
,Inactivo=@pmInactivo,Cmntarios=@pmCmntarios,FechaUpdate=@pmFechaUpdate
 WHERE IdConductor=@pmIdConductor 

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCndtoresTar @pmIdConductor VARCHAR(16),@pmNumTarjeta VARCHAR(30),@pmEdicTarjeta INT
,@pmFecTarjeta SMALLDATETIME,@pmVigTarjeta SMALLDATETIME
 AS 
 UPDATE TercCndtores SET NumTarjeta=@pmNumTarjeta,FecTarjeta=@pmFecTarjeta,VigTarjeta=@pmVigTarjeta
,EdicTarjeta=ISNULL(@pmEdicTarjeta,EdicTarjeta)
 WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercCndtoresVeh @pmIdConductor VARCHAR(16),@pmpVehiculo VARCHAR(10),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercCndtores SET pVehiculo=@pmpVehiculo,FechaUpdate=@pmFechaUpdate
 WHERE IdConductor=@pmIdConductor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpTercPropietario @pmIdPropietario VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmCupoCredito MONEY,@pmSaldoActual MONEY,@pmAvala BIT,@pmIdAdmon VARCHAR(4),@pmIdTipCon VARCHAR(4),@pmContratoActivo BIT,@pmFecIngreso SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmFecVigencia SMALLDATETIME
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercPropietario SET IdGrupo=@pmIdGrupo,CupoCredito=@pmCupoCredito,SaldoActual=@pmSaldoActual,Avala=@pmAvala,IdAdmon=@pmIdAdmon,IdTipCon=@pmIdTipCon
,ContratoActivo=@pmContratoActivo,FecIngreso=@pmFecIngreso,FecRetiro=@pmFecRetiro,FecVigencia=@pmFecVigencia,Observacion=@pmObservacion,IdEstado=@pmIdEstado
,FechaUpdate=@pmFechaUpdate
 WHERE IdPropietario=@pmIdPropietario


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercPropietarioSal @pmIdPropietario VARCHAR(16),@pmSaldoActual MONEY
AS
 IF @pmIdPropietario IS NULL
   UPDATE TercPropietario  SET SaldoActual=@pmSaldoActual
 ELSE
  UPDATE TercPropietario  SET SaldoActual=@pmSaldoActual  WHERE  IdPropietario=@pmIdPropietario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercProvee @pmIdProv VARCHAR(16),@pmIdGrupo VARCHAR(4),@pmNitRepLeg VARCHAR(16),@pmNomRepLeg VARCHAR(150),@pmNitContac VARCHAR(16),@pmNomContac VARCHAR(150),@pmTelContac VARCHAR(20)
,@pmemlContac VARCHAR(100),@pmCargContac VARCHAR(50),@pmDirOrdComp VARCHAR(250),@pmIdLocOrd VARCHAR(8),@pmDiasEntga INT,@pmIdPlazo VARCHAR(4),@pmIdForma VARCHAR(4),@pmCdMney VARCHAR(5),@pmCdDct VARCHAR(4),@pmIdClase VARCHAR(4)
,@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmAutoret BIT,@pmIncRet BIT,@pmIncRiv BIT,@pmIncIca BIT,@pmLiqFletes BIT,@pmCdRet VARCHAR(4),@pmCdRiv VARCHAR(4),@pmGartProd INT,@pmFecIngreso SMALLDATETIME,@pmPathFoto VARCHAR(30)
,@pmPathFirma VARCHAR(30),@pmCmntarios VARCHAR(250),@pmContrato BIT,@pmNContrato VARCHAR(20),@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmObsFinanc VARCHAR(250),@pmObsJuridica VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercProvee SET IdGrupo=@pmIdGrupo,NitRepLeg=@pmNitRepLeg,NomRepLeg=@pmNomRepLeg,NitContac=@pmNitContac,NomContac=@pmNomContac,TelContac=@pmTelContac,emlContac=@pmemlContac,CargContac=@pmCargContac,DirOrdComp=@pmDirOrdComp,IdLocOrd=@pmIdLocOrd
,DiasEntga=@pmDiasEntga,IdPlazo=@pmIdPlazo,IdForma=@pmIdForma,CdMney=@pmCdMney,CdDct=@pmCdDct,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,Autoret=@pmAutoret,IncRet=@pmIncRet,IncRiv=@pmIncRiv,IncIca=@pmIncIca,LiqFletes=@pmLiqFletes,CdRet=@pmCdRet
,CdRiv=@pmCdRiv,GartProd=@pmGartProd,FecIngreso=@pmFecIngreso,PathFoto=@pmPathFoto,PathFirma=@pmPathFirma,Cmntarios=@pmCmntarios,Contrato=@pmContrato,NContrato=@pmNContrato,FecVigencia=@pmFecVigencia,FecRetiro=@pmFecRetiro,ObsFinanc=@pmObsFinanc,ObsJuridica=@pmObsJuridica
,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE IdProv=@pmIdProv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercProveeEst @pmIdProv VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmCmntarios VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TercProvee SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo,Cmntarios=@pmCmntarios,FechaUpdate=@pmFechaUpdate
 WHERE IdProv=@pmIdProv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercVendedor @pmIdVend VARCHAR(16),@pmIdTarCms VARCHAR(4),@pmIdTarCm2 VARCHAR(4),@pmCdJorn VARCHAR(4),@pmIdSzona VARCHAR(4)
,@pmIdGrupo VARCHAR(4),@pmPathFoto VARCHAR(30),@pmPathFirma VARCHAR(30),@pmComntarios VARCHAR(250),@pmInactivo BIT
,@pmIdClase VARCHAR(4),@pmNumCuenta VARCHAR(30),@pmIdBanco VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE TercVendedor SET IdTarCms=@pmIdTarCms,IdTarCm2=@pmIdTarCm2,CdJorn=@pmCdJorn,IdSzona=@pmIdSzona,IdGrupo=@pmIdGrupo,PathFoto=@pmPathFoto
,PathFirma=@pmPathFirma,Comntarios=@pmComntarios,Inactivo=@pmInactivo,IdClase=@pmIdClase,NumCuenta=@pmNumCuenta,IdBanco=@pmIdBanco,FecUpdate=@pmFecUpdate
 WHERE IdVend=@pmIdVend
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTerceros @pmIdTercero VARCHAR(16),@pmRazonSocial VARCHAR(250),@pmCodigo VARCHAR(16),@pmTipoId CHAR(1),@pmDv CHAR(1),@pmDireccion VARCHAR(250),@pmIdLocal VARCHAR(8)
,@pmTelefono VARCHAR(20),@pmFax VARCHAR(20),@pmTelMovil VARCHAR(20),@pmSitioWeb VARCHAR(100),@pme_mail VARCHAR(100),@pmEsCliente BIT,@pmEsVendedor BIT,@pmEsConductor BIT
,@pmEsPropietario BIT,@pmEsProveedor BIT,@pmEsEmpleado BIT,@pmEsOperario BIT,@pmEsAccnista BIT,@pmEsCiaAseg BIT,@pmIdSector VARCHAR(8),@pmIdRegimen VARCHAR(4),@pmIdLugarCed VARCHAR(8),@pmObservacion VARCHAR(250)
,@pmIniStgNom INT,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmNomCial VARCHAR(250),@pmSiglaRaz VARCHAR(50),@pmIdProf VARCHAR(4),@pmTipEnte CHAR(1),@pmFecExpCed SMALLDATETIME,@pmEsCliePres BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Terceros SET RazonSocial=@pmRazonSocial,Codigo=@pmCodigo,TipoId=@pmTipoId,Dv=@pmDv,Direccion=@pmDireccion,IdLocal=@pmIdLocal,Telefono=@pmTelefono,Fax=@pmFax,TelMovil=@pmTelMovil,SitioWeb=@pmSitioWeb,e_mail=@pme_mail
,EsCliente=@pmEsCliente,EsVendedor=@pmEsVendedor,EsConductor=@pmEsConductor,EsPropietario=@pmEsPropietario,EsProveedor=@pmEsProveedor,EsEmpleado=@pmEsEmpleado,EsOperario=@pmEsOperario,EsAccnista=@pmEsAccnista,EsCiaAseg=@pmEsCiaAseg 
,EsCliePres=@pmEsCliePres,IdSector=@pmIdSector,IdRegimen=@pmIdRegimen,IdLugarCed=@pmIdLugarCed,Observacion=@pmObservacion,IniStgNom=@pmIniStgNom,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
,NomCial=@pmNomCial,SiglaRaz=@pmSiglaRaz,IdProf=@pmIdProf,TipEnte=@pmTipEnte,FecExpCed=@pmFecExpCed
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercerosCdt  @pmIdTercero VARCHAR(16),@pmEsConductor BIT
 AS 
 UPDATE Terceros SET EsConductor=@pmEsConductor
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercerosCli @pmIdTercero VARCHAR(16),@pmEsCliente BIT
 AS 
 UPDATE Terceros SET EsCliente=@pmEsCliente
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercerosEst @pmIdTercero VARCHAR(16),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmObservacion VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Terceros SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo,Observacion=@pmObservacion,FechaUpdate=@pmFechaUpdate
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercerosPrv @pmIdTercero VARCHAR(16),@pmEsProveedor BIT
 AS 
 UPDATE Terceros SET EsProveedor=@pmEsProveedor
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTercerosVen @pmIdTercero VARCHAR(16),@pmEsVendedor BIT
 AS 
 UPDATE Terceros SET EsVendedor=@pmEsVendedor
 WHERE IdTercero=@pmIdTercero
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposAdm @pmIdAdmon VARCHAR(4),@pmTipoAdmon VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposAdm SET TipoAdmon=@pmTipoAdmon,Inactivo=@pmInactivo
 WHERE IdAdmon=@pmIdAdmon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposAgenc @pmIdTipAgenc VARCHAR(4),@pmTipoAgencia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposAgenc SET TipoAgencia=@pmTipoAgencia,Inactivo=@pmInactivo
 WHERE IdTipAgenc=@pmIdTipAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposBan @pmIdBandera VARCHAR(4),@pmTipoBandera VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposBan SET TipoBandera=@pmTipoBandera,Inactivo=@pmInactivo
 WHERE IdBandera=@pmIdBandera
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposBloq @pmIdTipBloq VARCHAR(4),@pmTipoBloqueo VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposBloq SET TipoBloqueo=@pmTipoBloqueo,Inactivo=@pmInactivo
 WHERE IdTipBloq=@pmIdTipBloq

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposCar @pmIdCrceria VARCHAR(4),@pmTipoCar VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCar SET TipoCar=@pmTipoCar,Inactivo=@pmInactivo
 WHERE IdCrceria=@pmIdCrceria
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposCarga @pmIdTipCarga VARCHAR(4),@pmTipoCarga VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCarga SET TipoCarga=@pmTipoCarga,Inactivo=@pmInactivo
 WHERE IdTipCarga=@pmIdTipCarga

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposCnd @pmIdTipoCon VARCHAR(4),@pmTipoContndor VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCnd SET TipoContndor=@pmTipoContndor,Inactivo=@pmInactivo
 WHERE IdTipoCon=@pmIdTipoCon


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposCol @pmIdColor VARCHAR(4),@pmNomColor VARCHAR(50),@pmNColor INT,@pmInactivo BIT
 AS 
 UPDATE TiposCol SET NomColor=@pmNomColor,NColor=@pmNColor,Inactivo=@pmInactivo
 WHERE IdColor=@pmIdColor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposCom @pmIdCom VARCHAR(3),@pmTipoCom VARCHAR(50),@pmIdDiario VARCHAR(4)
,@pmEsEgreso BIT,@pmConsUnico BIT,@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TiposCom SET TipoCom=@pmTipoCom,IdDiario=@pmIdDiario,EsEgreso=@pmEsEgreso
,ConsUnico=@pmConsUnico,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE IdCom=@pmIdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposComAgenc @pmIdSubAgenc VARCHAR(4),@pmSubTipoAgenc VARCHAR(50),@pmIdTipAgenc VARCHAR(4),@pmInactivo BIT
 AS 
 UPDATE TiposComAgenc SET SubTipoAgenc=@pmSubTipoAgenc,IdTipAgenc=@pmIdTipAgenc,Inactivo=@pmInactivo
 WHERE IdSubAgenc=@pmIdSubAgenc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposComCons @pmIdCom VARCHAR(3),@pmIdCia CHAR(2),@pmLDesde INT,@pmLHasta INT,@pmNumero INT
,@pmNumManual BIT,@pmFormato VARCHAR(20),@pmConfigFecha VARCHAR(10),@pmTipoPapel INT,@pmOrientacion INT
,@pmVistaPrevia BIT,@pmVerSetup BIT,@pmNumCopias INT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TiposComCons SET LDesde=@pmLDesde,LHasta=@pmLHasta,Numero=@pmNumero,NumManual=@pmNumManual
,Formato=@pmFormato,ConfigFecha=@pmConfigFecha,TipoPapel=@pmTipoPapel,Orientacion=@pmOrientacion
,VistaPrevia=@pmVistaPrevia,VerSetup=@pmVerSetup,NumCopias=@pmNumCopias,FechaUpdate=@pmFechaUpdate
 WHERE IdCom=@pmIdCom AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposComConsNum @pmIdCom VARCHAR(3),@pmIdCia CHAR(2),@pmNumero INT
 AS 
 IF @pmNumero IS NULL
  UPDATE TiposComCons SET Numero=Numero+1
  WHERE IdCom=@pmIdCom AND IdCia=@pmIdCia
 ELSE
  UPDATE TiposComCons SET Numero=@pmNumero
  WHERE IdCom=@pmIdCom AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposCon @pmIdTipCon VARCHAR(4),@pmTipoContrato VARCHAR(100)
,@pmDuracion INT,@pmInactivo BIT
 AS 
 UPDATE TiposCon SET TipoContrato=@pmTipoContrato,Duracion=@pmDuracion,Inactivo=@pmInactivo
 WHERE IdTipCon=@pmIdTipCon
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposCot @pmIdCot VARCHAR(4),@pmCotizante VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposCot SET Cotizante=@pmCotizante,Inactivo=@pmInactivo
 WHERE IdCot=@pmIdCot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposDoc @pmIdDoc VARCHAR(3),@pmTipoDoc VARCHAR(50),@pmConsUnico BIT
,@pmLeyenda VARCHAR(250),@pmInactivo BIT,@pmIntegraCias BIT
 AS 
 UPDATE Sys_TiposDoc SET TipoDoc=@pmTipoDoc,ConsUnico=@pmConsUnico
,Leyenda=@pmLeyenda,Inactivo=@pmInactivo,IntegraCias=@pmIntegraCias
 WHERE IdDoc=@pmIdDoc
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposDocCons @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2),@pmLDesde INT,@pmLHasta INT,@pmResolucion VARCHAR(30),@pmRangoNum VARCHAR(50),@pmFechaRes SMALLDATETIME
,@pmPrefijo VARCHAR(5),@pmNumero INT,@pmNumManual BIT,@pmIntLotes BIT,@pmConfigFecha VARCHAR(10),@pmFormato VARCHAR(20),@pmTipoPapel INT,@pmOrientacion INT,@pmVistaPrevia BIT
,@pmVerSetup BIT,@pmNumCopias INT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE TiposDocCons SET LDesde=@pmLDesde,LHasta=@pmLHasta,Resolucion=@pmResolucion,RangoNum=@pmRangoNum,FechaRes=@pmFechaRes,Prefijo=@pmPrefijo
,Numero=@pmNumero,NumManual=@pmNumManual,IntLotes=@pmIntLotes,ConfigFecha=@pmConfigFecha,Formato=@pmFormato,TipoPapel=@pmTipoPapel
,Orientacion=@pmOrientacion,VistaPrevia=@pmVistaPrevia,VerSetup=@pmVerSetup,NumCopias=@pmNumCopias,FechaUpdate=@pmFechaUpdate
 WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposDocConsNum @pmIdDoc VARCHAR(3),@pmIdCia CHAR(2),@pmNumero INT
 AS 
 IF @pmNumero IS NULL
  UPDATE TiposDocCons SET Numero=Numero+1
  WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
 ELSE
  UPDATE TiposDocCons SET Numero=@pmNumero
  WHERE IdDoc=@pmIdDoc AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposDocCue @pmIdDoc VARCHAR(3),@pmIdClase INT
,@pmClaseCuenta VARCHAR(50),@pmTipoMov CHAR(1)
 AS 
 UPDATE Sys_TiposDocCue SET ClaseCuenta=@pmClaseCuenta,TipoMov=@pmTipoMov
 WHERE IdDoc=@pmIdDoc AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposEsc @pmIdTipoEsc VARCHAR(4),@pmTipoEscolta VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposEsc SET TipoEscolta=@pmTipoEscolta,Inactivo=@pmInactivo
 WHERE IdTipoEsc=@pmIdTipoEsc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposFil @pmIdFiltro VARCHAR(4),@pmTipoFiltro VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposFil SET TipoFiltro=@pmTipoFiltro,Inactivo=@pmInactivo
 WHERE IdFiltro=@pmIdFiltro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposFuel @pmIdCom VARCHAR(4),@pmTipoComb VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposFuel SET TipoComb=@pmTipoComb,Inactivo=@pmInactivo
 WHERE IdCom=@pmIdCom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposLla @pmIdTlla VARCHAR(4),@pmTipoLlanta VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposLla SET TipoLlanta=@pmTipoLlanta,Inactivo=@pmInactivo
 WHERE IdTlla=@pmIdTlla
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposLub @pmIdLub VARCHAR(4),@pmTipoLub VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposLub SET TipoLub=@pmTipoLub,Inactivo=@pmInactivo
 WHERE IdLub=@pmIdLub
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposMat @pmIdMat VARCHAR(4),@pmMaterial VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMat SET Material=@pmMaterial,Inactivo=@pmInactivo
 WHERE IdMat=@pmIdMat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposMcia @pmIdTmcia VARCHAR(4),@pmTipoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMcia SET TipoMcia=@pmTipoMcia,Inactivo=@pmInactivo
 WHERE IdTmcia=@pmIdTmcia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposMnjo @pmIdMnjo VARCHAR(4),@pmManejoMcia VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMnjo SET ManejoMcia=@pmManejoMcia,Inactivo=@pmInactivo
 WHERE IdMnjo=@pmIdMnjo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposMod @pmIdModd VARCHAR(4),@pmModalidad VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMod SET Modalidad=@pmModalidad,Inactivo=@pmInactivo
 WHERE IdModd=@pmIdModd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposMot @pmIdTipoMot VARCHAR(4),@pmTipoMotor VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposMot SET TipoMotor=@pmTipoMotor,Inactivo=@pmInactivo
 WHERE IdTipoMot=@pmIdTipoMot
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposNat @pmIdNat VARCHAR(4),@pmNatlzaprod VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposNat SET Natlzaprod=@pmNatlzaprod,Inactivo=@pmInactivo
 WHERE IdNat=@pmIdNat
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposNivs @pmIdNiv VARCHAR(4),@pmNivelServicio VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposNivs SET NivelServicio=@pmNivelServicio,Inactivo=@pmInactivo
 WHERE IdNiv=@pmIdNiv
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposNom @pmIdNom VARCHAR(4),@pmTipoNomina VARCHAR(50),@pmAsistencia BIT,@pmNoDevenga BIT
,@pmTipoLiquida CHAR(1),@pmFechaActual SMALLDATETIME,@pmInactivo BIT,@pmCdCiaInt CHAR(2)
 AS 
 UPDATE TiposNom SET TipoNomina=@pmTipoNomina,Asistencia=@pmAsistencia,NoDevenga=@pmNoDevenga,TipoLiquida=@pmTipoLiquida
,FechaActual=@pmFechaActual,Inactivo=@pmInactivo,CdCiaInt=@pmCdCiaInt
 WHERE IdNom=@pmIdNom
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposNov @pmIdNov VARCHAR(4),@pmNovedad VARCHAR(50),@pmAdmon VARCHAR(3),@pmInactivo BIT
 AS 
 UPDATE TiposNov SET Novedad=@pmNovedad,Admon=@pmAdmon,Inactivo=@pmInactivo
 WHERE IdNov=@pmIdNov
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposPcnto @pmIdTipoPre VARCHAR(4),@pmPrecinto VARCHAR(50),@pmInactivo BIT
 AS 
 UPDATE TiposPcnto SET Precinto=@pmPrecinto,Inactivo=@pmInactivo
 WHERE IdTipoPre=@pmIdTipoPre

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposPpt @pmIdPpd VARCHAR(4),@pmTipoProp VARCHAR(50),@pmVehPropio BIT,@pmInactivo BIT
 AS 
 UPDATE TiposPpt SET TipoProp=@pmTipoProp,VehPropio=@pmVehPropio,Inactivo=@pmInactivo
 WHERE IdPpd=@pmIdPpd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpTiposTerc @pmIdTipoTerc VARCHAR(4),@pmTipoTercero VARCHAR(100),@pmInactivo BIT
 AS 
 UPDATE TiposTerc SET TipoTercero=@pmTipoTercero,Inactivo=@pmInactivo
 WHERE IdTipoTerc=@pmIdTipoTerc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTiposVeh @pmIdTipoVeh VARCHAR(4),@pmTipoVehiculo VARCHAR(50)
,@pmConfiguracion VARCHAR(6),@pmEjes INT,@pmLlantas INT,@pmInactivo BIT
 AS 
 UPDATE TiposVeh SET TipoVehiculo=@pmTipoVehiculo,Configuracion=@pmConfiguracion,Ejes=@pmEjes
,Llantas=@pmLlantas,Inactivo=@pmInactivo
 WHERE IdTipoVeh=@pmIdTipoVeh
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpTraslados @pmTipDoc VARCHAR(3),@pmTraslado INT,@pmIdCia CHAR(2),@pmFecha SMALLDATETIME,@pmIdConcepto VARCHAR(4),@pmIdTercero VARCHAR(16),@pmVrSubTotal MONEY,@pmCantidad DECIMAL(14,4)
,@pmNumDcmto INT,@pmNumOrden INT,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16),@pmModalidad VARCHAR(10),@pmTipCom VARCHAR(3),@pmComprobante INT,@pmIdCiaCom CHAR(2)
,@pmObservacion VARCHAR(250),@pmIdEstado VARCHAR(4),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Trn_Traslados SET Fecha=@pmFecha,IdConcepto=@pmIdConcepto,IdTercero=@pmIdTercero,VrSubTotal=@pmVrSubTotal,Cantidad=@pmCantidad,NumDcmto=@pmNumDcmto,NumOrden=@pmNumOrden
,IdCCosto=@pmIdCCosto,IdSubCos=@pmIdSubCos,Modalidad=@pmModalidad,TipCom=@pmTipCom,Comprobante=@pmComprobante,IdCiaCom=@pmIdCiaCom,Observacion=@pmObservacion,IdEstado=@pmIdEstado,FecUpdate=@pmFecUpdate
 WHERE TipDoc=@pmTipDoc AND Traslado=@pmTraslado AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUOpciones @pmIdUsuario VARCHAR(11),@pmNomOpcion VARCHAR(20),@pmValor VARCHAR(50)
 AS 
 UPDATE adm_UOpciones SET Valor=@pmValor
 WHERE IdUsuario=@pmIdUsuario AND NomOpcion=@pmNomOpcion
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUm @pmUndMed VARCHAR(10),@pmUnidad VARCHAR(50),@pmCarga BIT
 AS 
 UPDATE Sys_Um SET Unidad=@pmUnidad,Carga=@pmCarga
 WHERE UndMed=@pmUndMed
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUndMed @pmIdUnd VARCHAR(4),@pmUnidad VARCHAR(50),@pmIdMat VARCHAR(4),@pmIdEmp VARCHAR(4),@pmFormRecp VARCHAR(10),@pmClaseEmp VARCHAR(10),@pmDimAlto DECIMAL(14,4)
,@pmDimAncho DECIMAL(14,4),@pmDimLargo DECIMAL(14,4),@pmDimVolm DECIMAL(14,4),@pmUndMed VARCHAR(10),@pmPesoEpq DECIMAL(14,4),@pmCapcMax DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmSimbUnd VARCHAR(10)
,@pmObserv VARCHAR(250),@pmInactivo BIT
 AS 
 UPDATE UndMed SET Unidad=@pmUnidad,IdMat=@pmIdMat,IdEmp=@pmIdEmp,FormRecp=@pmFormRecp,ClaseEmp=@pmClaseEmp,DimAlto=@pmDimAlto,DimAncho=@pmDimAncho,DimLargo=@pmDimLargo,DimVolm=@pmDimVolm
,UndMed=@pmUndMed,PesoEpq=@pmPesoEpq,CapcMax=@pmCapcMax,UndCapc=@pmUndCapc,SimbUnd=@pmSimbUnd,Observ=@pmObserv,Inactivo=@pmInactivo
 WHERE IdUnd=@pmIdUnd
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUsuarios @pmIdUsuario VARCHAR(11),@pmUsuario VARCHAR(50),@pmPwdLog VARCHAR(30),@pmIdGrupo INT,@pmIdEstacion CHAR(2),@pmIdCargo VARCHAR(4),@pmConectado BIT,@pmFechaAcc SMALLDATETIME
,@pmFechaClave SMALLDATETIME,@pmCaducidad INT,@pmLoginSQL VARCHAR(20),@pmPwdSQL VARCHAR(20),@pmModoSQL VARCHAR(3),@pmInactivo BIT
 AS 
 UPDATE adm_Usuarios SET Usuario=@pmUsuario,PwdLog=@pmPwdLog,IdGrupo=@pmIdGrupo,IdEstacion=@pmIdEstacion,IdCargo=@pmIdCargo,Conectado=@pmConectado,FechaAcc=@pmFechaAcc,FechaClave=@pmFechaClave,Caducidad=@pmCaducidad
,LoginSQL=@pmLoginSQL,PwdSQL=@pmPwdSQL,ModoSQL=@pmModoSQL,Inactivo=@pmInactivo
 WHERE IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUsuariosCnn  @pmConectado BIT,@pmFechaAcc SMALLDATETIME,@pmIdUsuario VARCHAR(11)
AS
 IF @pmConectado<>0
 UPDATE adm_Usuarios SET Conectado=@pmConectado,FechaAcc=@pmFechaAcc WHERE IdUsuario=@pmIdUsuario
ELSE
 UPDATE adm_Usuarios SET Conectado=@pmConectado  WHERE IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUsuariosPwd  @pmPwdLog VARCHAR(30),@pmFechaClave SMALLDATETIME,@pmIdUsuario VARCHAR(11)
AS
UPDATE adm_Usuarios SET PwdLog=@pmPwdLog,FechaClave=@pmFechaClave
WHERE IdUsuario=@pmIdUsuario
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpUsucaja @pmIdUsuario VARCHAR(11),@pmnFormato VARCHAR(10),@pmCdCajero VARCHAR(11),@pmIdConcepto VARCHAR(4)
,@pmCdBodega VARCHAR(4),@pmLtaPrec CHAR(1),@pmIdVend VARCHAR(16),@pmCdCms VARCHAR(4),@pmCdCCosto VARCHAR(16)
,@pmCdSubCos VARCHAR(16),@pmAbreCajon VARCHAR(10)
 AS 
 UPDATE adm_Usucaja SET CdCajero=@pmCdCajero,IdConcepto=@pmIdConcepto,CdBodega=@pmCdBodega,LtaPrec=@pmLtaPrec,IdVend=@pmIdVend
,CdCms=@pmCdCms,CdCCosto=@pmCdCCosto,CdSubCos=@pmCdSubCos,AbreCajon=@pmAbreCajon
 WHERE IdUsuario=@pmIdUsuario AND nFormato=@pmnFormato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpValesDev @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_Vales SET  Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpValesDevItem @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
,@pmTipoRango VARCHAR(3),@pmIdRango INT,@pmNumSerie VARCHAR(5),@pmNumForma INT
,@pmAnulado BIT,@pmNumDev INT,@pmFecDev SMALLDATETIME
 AS 
 UPDATE Trn_Vales SET Anulado=@pmAnulado,NumDev=@pmNumDev,FecDev=@pmFecDev
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia 
 AND TipoRango=@pmTipoRango AND IdRango=@pmIdRango AND NumSerie=@pmNumSerie AND NumForma=@pmNumForma
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpValesDfa @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmItem INT
 AS 
 UPDATE Trn_Vales SET  TipFac='0',NumFactura=0,IdCiaFac='00'
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpValesFac @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2),@pmItem INT
,@pmTipFac VARCHAR(3),@pmNumFactura INT,@pmIdCiaFac CHAR(2),@pmFechaFac SMALLDATETIME
 AS 
 UPDATE Trn_Vales SET TipFac=@pmTipFac,NumFactura=@pmNumFactura,IdCiaFac=@pmIdCiaFac,FechaFac=@pmFechaFac
 WHERE TipDoc=@pmTipDoc AND nRadica=@pmnRadica AND IdCia=@pmIdCia AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehClientes @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmIdTipoVeh VARCHAR(4),@pmIdMarca VARCHAR(4),@pmLinMarca VARCHAR(30),@pmColorVeh VARCHAR(30),@pmIdTipoMot VARCHAR(4),@pmModelo VARCHAR(4),@pmClaseMat VARCHAR(10),@pmCilind DECIMAL(14,4),@pmIdCom VARCHAR(4),@pmIdLub VARCHAR(4)
,@pmIdMarLub VARCHAR(4),@pmIdTlla VARCHAR(4),@pmIdMarlla VARCHAR(4),@pmIdFiltro VARCHAR(4),@pmIdMarFil VARCHAR(4),@pmNumSoat VARCHAR(30),@pmFecSoat SMALLDATETIME,@pmVigSoat SMALLDATETIME,@pmNitEmpSoat VARCHAR(16),@pmRevTecMec VARCHAR(30),@pmFecTecMec SMALLDATETIME,@pmVigTecMec SMALLDATETIME
,@pmCertGases VARCHAR(30),@pmFecCertGas SMALLDATETIME,@pmVigCertGas SMALLDATETIME,@pmKmInicial INT,@pmKmActual INT,@pmNitCliente VARCHAR(16),@pmCondPago VARCHAR(10),@pmNumCredito VARCHAR(30),@pmObservacion VARCHAR(250),@pmPathFoto VARCHAR(30),@pmFecPriServ SMALLDATETIME,@pmFecUltServ SMALLDATETIME
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE VehClientes SET NumVeh=@pmNumVeh,IdTipoVeh=@pmIdTipoVeh,IdMarca=@pmIdMarca,LinMarca=@pmLinMarca,ColorVeh=@pmColorVeh,IdTipoMot=@pmIdTipoMot,Modelo=@pmModelo,ClaseMat=@pmClaseMat,Cilind=@pmCilind,IdCom=@pmIdCom,IdLub=@pmIdLub,IdMarLub=@pmIdMarLub,IdTlla=@pmIdTlla,IdMarlla=@pmIdMarlla,IdFiltro=@pmIdFiltro
,IdMarFil=@pmIdMarFil,NumSoat=@pmNumSoat,FecSoat=@pmFecSoat,VigSoat=@pmVigSoat,NitEmpSoat=@pmNitEmpSoat,RevTecMec=@pmRevTecMec,FecTecMec=@pmFecTecMec,VigTecMec=@pmVigTecMec,CertGases=@pmCertGases,FecCertGas=@pmFecCertGas,VigCertGas=@pmVigCertGas,KmInicial=@pmKmInicial,KmActual=@pmKmActual
,NitCliente=@pmNitCliente,CondPago=@pmCondPago,NumCredito=@pmNumCredito,Observacion=@pmObservacion,PathFoto=@pmPathFoto,FecPriServ=@pmFecPriServ,FecUltServ=@pmFecUltServ,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehPolizas @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmNumero VARCHAR(30)
,@pmFecEmision SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmNitCompania VARCHAR(16),@pmValor MONEY
,@pmComntarios VARCHAR(100),@pmDiasAviso INT
 AS 
 UPDATE VehPolizas SET Numero=@pmNumero,FecEmision=@pmFecEmision,FecVigencia=@pmFecVigencia,NitCompania=@pmNitCompania
,Valor=@pmValor,Comntarios=@pmComntarios,DiasAviso=@pmDiasAviso
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase=@pmIdClase
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehRemolq @pmIdRemque VARCHAR(10),@pmIdMarca VARCHAR(4),@pmIdColor VARCHAR(4),@pmIdCrceria VARCHAR(4),@pmModelo VARCHAR(4),@pmNEjes INT,@pmnLlantas INT,@pmAltura DECIMAL(14,4),@pmAncho DECIMAL(14,4),@pmLargo DECIMAL(14,4)
,@pmVlumen DECIMAL(14,4),@pmPesoKg DECIMAL(14,4),@pmCapneta DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmNumComp INT,@pmCapcComp VARCHAR(50),@pmNumSerial VARCHAR(30),@pmIdPropietario VARCHAR(16),@pmNitProv VARCHAR(16),@pmFecCompra SMALLDATETIME
,@pmVidaUtil INT,@pmVrComercial MONEY,@pmVrAvaludo MONEY,@pmVrAsegurado MONEY,@pmKilmetraje INT,@pmEquipoAdic VARCHAR(50),@pmDescripcion VARCHAR(100),@pmCdCenSer VARCHAR(4),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100),@pmObservacion VARCHAR(250)
,@pmIdEstado VARCHAR(4),@pmInactivo BIT,@pmTarjetaProp VARCHAR(30),@pmFecTarjProp  SMALLDATETIME,@pmVigTarjProp SMALLDATETIME,@pmConfEjes VARCHAR(5),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE VehRemolq SET IdMarca=@pmIdMarca,IdColor=@pmIdColor,IdCrceria=@pmIdCrceria,Modelo=@pmModelo,NEjes=@pmNEjes,nLlantas=@pmnLlantas,ConfEjes=@pmConfEjes,Altura=@pmAltura,Ancho=@pmAncho,Largo=@pmLargo,Vlumen=@pmVlumen,PesoKg=@pmPesoKg,Capneta=@pmCapneta,UndCapc=@pmUndCapc
,NumComp=@pmNumComp,CapcComp=@pmCapcComp,NumSerial=@pmNumSerial,IdPropietario=@pmIdPropietario,NitProv=@pmNitProv,FecCompra=@pmFecCompra,VidaUtil=@pmVidaUtil,VrComercial=@pmVrComercial,VrAvaludo=@pmVrAvaludo,VrAsegurado=@pmVrAsegurado,Kilmetraje=@pmKilmetraje
,EquipoAdic=@pmEquipoAdic,Descripcion=@pmDescripcion,CdCenSer=@pmCdCenSer,CdLocal=@pmCdLocal,Ubicacion=@pmUbicacion,Observacion=@pmObservacion,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,FecUpdate=@pmFecUpdate,TarjetaProp=@pmTarjetaProp,FecTarjProp=@pmFecTarjProp,VigTarjProp=@pmVigTarjProp
 WHERE IdRemque=@pmIdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehRemolqKmt @pmIdRemque VARCHAR(10),@pmKilmetraje INT,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE VehRemolq SET Kilmetraje=@pmKilmetraje,FecUpdate=@pmFecUpdate
 WHERE IdRemque=@pmIdRemque
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE paUpVeh_Contrato @pmNumContrato INT,@pmIdVehiculo VARCHAR(10),@pmIdAdmon VARCHAR(4),@pmIdTipCon VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecRetiro SMALLDATETIME
,@pmFecVigencia SMALLDATETIME,@pmObservaciones VARCHAR(250),@pmNumPagare VARCHAR(20),@pmArchPagare VARCHAR(50),@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Veh_Contrato SET IdVehiculo=@pmIdVehiculo,IdAdmon=@pmIdAdmon,IdTipCon=@pmIdTipCon,FecIngreso=@pmFecIngreso,FecRetiro=@pmFecRetiro
,FecVigencia=@pmFecVigencia,Observaciones=@pmObservaciones,NumPagare=@pmNumPagare,ArchPagare=@pmArchPagare,FecUpdate=@pmFecUpdate
 WHERE NumContrato=@pmNumContrato
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpVeh_Cupos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmValorCupo MONEY
,@pmValorSaldo MONEY,@pmFecUpdate SMALLDATETIME
 AS 
 UPDATE Veh_Cupos SET ValorCupo=@pmValorCupo,ValorSaldo=@pmValorSaldo,FecUpdate=@pmFecUpdate
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase=@pmIdClase


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVeh_CuposAll @pmIdVehiculo VARCHAR(10)=Null,@pmIdClase VARCHAR(4)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
 IF @pmIdVehiculo IS NULL
  UPDATE  Veh_Cupos SET   ValorSaldo=0
  FROM Vehiculos AS V INNER JOIN  Veh_Cupos AS C ON V.IdVehiculo=C.IdVehiculo
  WHERE   C.IdClase LIKE ISNULL(@pmIdClase,'%') AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%') 
 ELSE
  UPDATE  Veh_Cupos SET   ValorSaldo=0 
  WHERE IdVehiculo=@pmIdVehiculo AND IdClase LIKE ISNULL(@pmIdClase,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVeh_CuposSal @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmValorSaldo MONEY

AS
  UPDATE Veh_Cupos  SET ValorSaldo=@pmValorSaldo
  WHERE  IdVehiculo=@pmIdVehiculo AND IdClase=@pmIdClase

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUpVeh_Saldos @pmIdVehiculo VARCHAR(10),@pmIdClase VARCHAR(4),@pmIdCia CHAR(2),@pmValorSaldo MONEY
 AS 
 UPDATE Veh_Saldos SET ValorSaldo=@pmValorSaldo
 WHERE IdVehiculo=@pmIdVehiculo AND IdClase=@pmIdClase AND IdCia=@pmIdCia


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculos @pmIdVehiculo VARCHAR(10),@pmNumVeh VARCHAR(10),@pmClaseVeh VARCHAR(10),@pmIdTipoVeh VARCHAR(4),@pmIdMarca VARCHAR(4),@pmIdLinea VARCHAR(10),@pmIdColor VARCHAR(4),@pmIdTipoMot VARCHAR(4),@pmIdCrceria VARCHAR(4),@pmModelo VARCHAR(4),@pmFecRep SMALLDATETIME,@pmConfig VARCHAR(5),@pmVehArtic BIT,@pmNumLlan INT,@pmNumLlans INT,@pmIdCat VARCHAR(4),@pmCdCatv VARCHAR(4)
,@pmClaseMat VARCHAR(10),@pmCilind DECIMAL(14,4),@pmCapTanq DECIMAL(14,4),@pmIdCom VARCHAR(4),@pmIdLub VARCHAR(4),@pmIdTlla VARCHAR(4),@pmIdMarlla VARCHAR(4),@pmPesoVacio DECIMAL(14,4),@pmPesoMax DECIMAL(14,4),@pmNumMotor VARCHAR(30),@pmSerieChasis VARCHAR(30),@pmNumSerie VARCHAR(30),@pmCdRemque VARCHAR(10),@pmLongitud DECIMAL(14,4),@pmCarrAlto DECIMAL(14,4),@pmCarrAncho DECIMAL(14,4),@pmCarrLargo DECIMAL(14,4)
,@pmCarrCapac DECIMAL(14,4),@pmUndCapc VARCHAR(10),@pmComptmtos INT,@pmCapComp VARCHAR(50),@pmPasjerosPie INT,@pmPasjerosSen INT,@pmNitEmpresa VARCHAR(16),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16),@pmIdConductor VARCHAR(16),@pmIdPpd VARCHAR(4),@pmAdquisc VARCHAR(10),@pmNitProv VARCHAR(16),@pmFecCompra SMALLDATETIME,@pmVrComcial MONEY,@pmVrAseg MONEY,@pmVrAvaludo MONEY,@pmVidaUtil INT
,@pmFecSalida SMALLDATETIME,@pmNContrato INT,@pmIdAdmon VARCHAR(4),@pmIdNiv VARCHAR(4),@pmIdGrupo VARCHAR(4),@pmCdGrupR VARCHAR(4),@pmCdTarifa VARCHAR(4),@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME,@pmNumSoat VARCHAR(30),@pmFecSoat SMALLDATETIME,@pmVigSoat SMALLDATETIME,@pmNitEmpSoat VARCHAR(16),@pmTarjProp VARCHAR(30),@pmFecTProp SMALLDATETIME
,@pmVigTProp SMALLDATETIME,@pmCdLugTp VARCHAR(8),@pmUlttramite VARCHAR(150),@pmRespCivil VARCHAR(30),@pmFecRCivil SMALLDATETIME,@pmVigRCivil SMALLDATETIME,@pmRegNalCarga VARCHAR(30),@pmFecRegNal SMALLDATETIME,@pmVigRegNal SMALLDATETIME,@pmKmInicial INT,@pmKmActual INT,@pmKm2Actual INT,@pmRegtradora BIT,@pmCentInicial INT,@pmCentFinal INT,@pmVrLmtCred MONEY,@pmVrSaldoAct MONEY,@pmDescripcion VARCHAR(100)
,@pmObservacion VARCHAR(250),@pmCdCenSer VARCHAR(4),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100),@pmPathFoto VARCHAR(30),@pmFecPriServ SMALLDATETIME,@pmFecUltServ SMALLDATETIME,@pmFecUltAcc SMALLDATETIME,@pmTieneAcc BIT,@pmFecPagImp SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmTipoAfil VARCHAR(10),@pmRevTecMec VARCHAR(30),@pmFecTecMec SMALLDATETIME,@pmVigTecMec SMALLDATETIME,@pmCertGases VARCHAR(30),@pmFecCertGas SMALLDATETIME,@pmVigCertGas SMALLDATETIME,@pmTarjOper VARCHAR(30),@pmFecTarjOper SMALLDATETIME,@pmVigTarjOper SMALLDATETIME,@pmFechaUpdate SMALLDATETIME
,@pmValorCupo MONEY,@pmObligaTProd BIT,@pmGarantiaAcc BIT,@pmDocCompleta BIT,@pmCertMovilizacion VARCHAR(20),@pmFecCertMovil SMALLDATETIME,@pmVigCertMovil SMALLDATETIME,@pmCdRutaHab VARCHAR(4)
 AS 
 UPDATE Vehiculos SET NumVeh=@pmNumVeh,ClaseVeh=@pmClaseVeh,IdTipoVeh=@pmIdTipoVeh,IdMarca=@pmIdMarca,IdLinea=@pmIdLinea,IdColor=@pmIdColor,IdTipoMot=@pmIdTipoMot,IdCrceria=@pmIdCrceria,Modelo=@pmModelo,FecRep=@pmFecRep,Config=@pmConfig,VehArtic=@pmVehArtic,NumLlan=@pmNumLlan,NumLlans=@pmNumLlans,IdCat=@pmIdCat,CdCatv=@pmCdCatv,ClaseMat=@pmClaseMat,Cilind=@pmCilind,CapTanq=@pmCapTanq,IdCom=@pmIdCom,IdLub=@pmIdLub
,IdTlla=@pmIdTlla,IdMarlla=@pmIdMarlla,PesoVacio=@pmPesoVacio,PesoMax=@pmPesoMax,NumMotor=@pmNumMotor,SerieChasis=@pmSerieChasis,NumSerie=@pmNumSerie,CdRemque=@pmCdRemque,Longitud=@pmLongitud,CarrAlto=@pmCarrAlto,CarrAncho=@pmCarrAncho,CarrLargo=@pmCarrLargo,CarrCapac=@pmCarrCapac,UndCapc=@pmUndCapc,Comptmtos=@pmComptmtos,CapComp=@pmCapComp,PasjerosPie=@pmPasjerosPie,PasjerosSen=@pmPasjerosSen,NitEmpresa=@pmNitEmpresa
,IdPropietario=@pmIdPropietario,IdPoseedor=@pmIdPoseedor,IdConductor=@pmIdConductor,IdPpd=@pmIdPpd,Adquisc=@pmAdquisc,NitProv=@pmNitProv,FecCompra=@pmFecCompra,VrComcial=@pmVrComcial,VrAseg=@pmVrAseg,VrAvaludo=@pmVrAvaludo,VidaUtil=@pmVidaUtil,FecSalida=@pmFecSalida,NContrato=@pmNContrato,IdAdmon=@pmIdAdmon,IdNiv=@pmIdNiv,IdGrupo=@pmIdGrupo,CdGrupR=@pmCdGrupR,CdTarifa=@pmCdTarifa,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia
,FecRetiro=@pmFecRetiro,NumSoat=@pmNumSoat,FecSoat=@pmFecSoat,VigSoat=@pmVigSoat,NitEmpSoat=@pmNitEmpSoat,TarjProp=@pmTarjProp,FecTProp=@pmFecTProp,VigTProp=@pmVigTProp,CdLugTp=@pmCdLugTp,Ulttramite=@pmUlttramite,RespCivil=@pmRespCivil,FecRCivil=@pmFecRCivil,VigRCivil=@pmVigRCivil,RegNalCarga=@pmRegNalCarga,FecRegNal=@pmFecRegNal,VigRegNal=@pmVigRegNal,KmInicial=@pmKmInicial,KmActual=@pmKmActual,Km2Actual=@pmKm2Actual
,Regtradora=@pmRegtradora,CentInicial=@pmCentInicial,CentFinal=@pmCentFinal,VrLmtCred=@pmVrLmtCred,VrSaldoAct=@pmVrSaldoAct,Descripcion=@pmDescripcion,Observacion=@pmObservacion,CdCenSer=@pmCdCenSer,CdLocal=@pmCdLocal,Ubicacion=@pmUbicacion,PathFoto=@pmPathFoto,FecPriServ=@pmFecPriServ,FecUltServ=@pmFecUltServ,FecUltAcc=@pmFecUltAcc,TieneAcc=@pmTieneAcc,FecPagImp=@pmFecPagImp,IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,TipoAfil=@pmTipoAfil,RevTecMec=@pmRevTecMec,FecTecMec=@pmFecTecMec,VigTecMec=@pmVigTecMec,CertGases=@pmCertGases,FecCertGas=@pmFecCertGas,VigCertGas=@pmVigCertGas,TarjOper=@pmTarjOper,FecTarjOper=@pmFecTarjOper,VigTarjOper=@pmVigTarjOper,FechaUpdate=@pmFechaUpdate
,ValorCupo=@pmValorCupo,ObligaTProd=@pmObligaTProd,GarantiaAcc=@pmGarantiaAcc,DocCompleta=@pmDocCompleta,CertMovilizacion=@pmCertMovilizacion,FecCertMovil=@pmFecCertMovil,VigCertMovil=@pmVigCertMovil,CdRutaHab=@pmCdRutaHab
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosAcu @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
 IF @pmIdVehiculo IS NULL
  UPDATE Vehiculos SET VrSaldoAct=(SELECT ISNULL(SUM(ValorSaldo),0) FROM Veh_Saldos 
  WHERE Vehiculos.IdVehiculo=Veh_Saldos.IdVehiculo)
  WHERE  IdPropietario LIKE ISNULL(@pmIdPropietario,'%') 
ELSE
  UPDATE Vehiculos SET VrSaldoAct=(SELECT ISNULL(SUM(ValorSaldo),0) FROM Veh_Saldos 
  WHERE Vehiculos.IdVehiculo=Veh_Saldos.IdVehiculo)
  WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosCdt @pmIdVehiculo VARCHAR(10),@pmIdConductor VARCHAR(16),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Vehiculos SET IdConductor=@pmIdConductor,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosCen @pmIdVehiculo VARCHAR(10),@pmCentInicial INT,@pmCentFinal INT
AS
 UPDATE  Vehiculos  SET  CentInicial=@pmCentInicial,CentFinal=@pmCentFinal
 WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosCon @pmIdVehiculo VARCHAR(10),@pmNContrato INT,@pmIdAdmon VARCHAR(4)
,@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME
 AS 
 UPDATE Vehiculos SET NContrato=@pmNContrato,IdAdmon=@pmIdAdmon,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia
,FecRetiro=@pmFecRetiro
 WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosCup @pmIdVehiculo VARCHAR(10),@pmVrLmtCred MONEY,@pmVrSaldoAct MONEY
 AS 
 UPDATE Vehiculos SET  VrLmtCred=@pmVrLmtCred,VrSaldoAct=@pmVrSaldoAct
 WHERE IdVehiculo=@pmIdVehiculo


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosEcon @pmIdVehiculo VARCHAR(10),@pmNContrato INT,@pmIdAdmon VARCHAR(4)
,@pmFecIngreso SMALLDATETIME,@pmFecVigencia SMALLDATETIME,@pmFecRetiro SMALLDATETIME
 AS 
 UPDATE Vehiculos SET NContrato=0,IdAdmon=@pmIdAdmon,FecIngreso=@pmFecIngreso,FecVigencia=@pmFecVigencia
,FecRetiro=@pmFecRetiro
 WHERE IdVehiculo=@pmIdVehiculo AND NContrato=@pmNContrato

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosEst @pmIdVehiculo VARCHAR(10),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmObservacion VARCHAR(250),@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Vehiculos SET IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Observacion=@pmObservacion,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosKmt @pmIdVehiculo VARCHAR(10),@pmKmActual INT,@pmKm2Actual INT,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Vehiculos SET KmActual=@pmKmActual,Km2Actual=@pmKm2Actual,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosRet @pmIdVehiculo VARCHAR(10),@pmIdEstado VARCHAR(4),@pmInactivo BIT
,@pmObservacion VARCHAR(250),@pmFecRetiro SMALLDATETIME,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Vehiculos SET FecRetiro=@pmFecRetiro,IdEstado=@pmIdEstado,Inactivo=@pmInactivo
,Observacion=@pmObservacion,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosRetB  @pmIdVehiculo VARCHAR(10),@pmNumContrato INT
,@pmFecRetiro SMALLDATETIME,@pmIdEstado VARCHAR(4),@pmObservacion VARCHAR(250)
,@pmFechaUpdate SMALLDATETIME,@pmInactivo BIT
AS
 UPDATE  Vehiculos  SET  NContrato=@pmNumContrato,FecRetiro=@pmFecRetiro
 ,IdEstado=@pmIdEstado,Inactivo=@pmInactivo,Observacion=@pmObservacion,FechaUpdate=@pmFechaUpdate
 WHERE  IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosSCero @pmIdVehiculo VARCHAR(10)=Null,@pmIdPropietario VARCHAR(16)=Null
AS
  UPDATE Vehiculos  SET VrSaldoAct=0
  WHERE  IdVehiculo LIKE ISNULL(@pmIdVehiculo,'%') AND IdPropietario LIKE ISNULL(@pmIdPropietario,'%')

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUpVehiculosSal @pmIdVehiculo VARCHAR(10),@pmSaldoActual MONEY
AS
  UPDATE Vehiculos  SET VrSaldoAct=@pmSaldoActual
  WHERE  IdVehiculo=@pmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosTra @pmIdVehiculo VARCHAR(10),@pmIdPropietario VARCHAR(16),@pmIdPoseedor VARCHAR(16)
,@pmIdPpd VARCHAR(4),@pmVrComcial MONEY,@pmVrAseg MONEY,@pmIdGrupo VARCHAR(4),@pmTipoAfil VARCHAR(10)
,@pmFechaUpdate SMALLDATETIME
 AS 
 UPDATE Vehiculos SET IdPropietario=@pmIdPropietario,IdPoseedor=@pmIdPoseedor,IdPpd=@pmIdPpd,TipoAfil=@pmTipoAfil
,VrComcial=@pmVrComcial,VrAseg=@pmVrAseg,IdGrupo=@pmIdGrupo,FechaUpdate=@pmFechaUpdate
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVehiculosUbi @pmIdVehiculo VARCHAR(10),@pmCdLocal VARCHAR(8),@pmUbicacion VARCHAR(100)
,@pmCdCenSer VARCHAR(4),@pmIdEstado VARCHAR(4)
 AS 
 UPDATE Vehiculos SET CdLocal=@pmCdLocal,Ubicacion=@pmUbicacion
,CdCenSer=ISNULL(@pmCdCenSer,CdCenSer),IdEstado=ISNULL(@pmIdEstado,IdEstado)
 WHERE IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVencimientosAbo @pmTipDoc VARCHAR(3),@pmFactura INT,@pmIdCia CHAR(2)
,@pmItem INT,@pmVrAbonado MONEY,@pmFecUltPago SMALLDATETIME
 AS 
 IF @pmFecUltPago IS NULL
  UPDATE Trn_Vencimientos SET VrAbonado=@pmVrAbonado
  WHERE TipDoc=@pmTipDoc  AND Factura=@pmFactura  AND IdCia=@pmIdCia  AND Item=@pmItem
 ELSE
  UPDATE Trn_Vencimientos SET VrAbonado=@pmVrAbonado,FecUltPago=@pmFecUltPago
  WHERE TipDoc=@pmTipDoc  AND Factura=@pmFactura  AND IdCia=@pmIdCia  AND Item=@pmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVencimientosApl  @pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE Trn_Vencimientos SET  Trn_Vencimientos.VrAbonado=(SELECT ISNULL(SUM(Trn_VencAbonos.VrAbono),0)
   FROM Trn_VencAbonos  WHERE Trn_Vencimientos.TipDoc=Trn_VencAbonos.TipDoc AND Trn_Vencimientos.Factura=Trn_VencAbonos.Factura
   AND Trn_Vencimientos.IdCia=Trn_VencAbonos.IdCiaFac  AND Trn_Vencimientos.Item=Trn_VencAbonos.ItemFac)
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
   AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdVend LIKE ISNULL(@pmIdVend,'%')
 ELSE
   UPDATE Trn_Vencimientos SET  Trn_Vencimientos.VrAbonado=(SELECT ISNULL(SUM(Trn_VencAbonos.VrAbono),0)
   FROM Trn_VencAbonos  WHERE Trn_Vencimientos.TipDoc=Trn_VencAbonos.TipDoc AND Trn_Vencimientos.Factura=Trn_VencAbonos.Factura
   AND Trn_Vencimientos.IdCia=Trn_VencAbonos.IdCiaFac  AND Trn_Vencimientos.Item=Trn_VencAbonos.ItemFac)
   WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
   AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpVencimientosCero  @pmIdCliente VARCHAR(16)=Null,@pmIdCia CHAR(2)=Null
,@pmFechaIni SMALLDATETIME=Null,@pmFechaFin SMALLDATETIME=Null,@pmIdVend VARCHAR(16)=Null
 AS 
 IF ISDATE(@pmFechaIni)>0 AND ISDATE(@pmFechaFin)>0--POR FECHA
   UPDATE Trn_Vencimientos SET VrAbonado=0
   WHERE Fecha BETWEEN @pmFechaIni AND @pmFechaFin
   AND IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
  AND IdVend LIKE ISNULL(@pmIdVend,'%')
 ELSE
   UPDATE Trn_Vencimientos SET VrAbonado=0
   WHERE IdCliente LIKE ISNULL(@pmIdCliente,'%') AND IdCia LIKE ISNULL(@pmIdCia,'%%')
   AND IdVend LIKE ISNULL(@pmIdVend,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpZonas @pmIdZona VARCHAR(4),@pmZona VARCHAR(50),@pmCdSubCos VARCHAR(16),@pmInactivo BIT
 AS 
 UPDATE Zonas SET Zona=@pmZona,CdSubCos=@pmCdSubCos,Inactivo=@pmInactivo
 WHERE IdZona=@pmIdZona
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpmonedas @pmIdMneda VARCHAR(5),@pmMneda VARCHAR(50),@pmSimbolo VARCHAR(10)
,@pmMneLocal BIT,@pmVrCambio DECIMAL(12,6),@pmInactivo BIT
 AS 
 UPDATE adm_monedas SET Mneda=@pmMneda,Simbolo=@pmSimbolo,MneLocal=@pmMneLocal,VrCambio=@pmVrCambio,Inactivo=@pmInactivo
 WHERE IdMneda=@pmIdMneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpmonedasLoc @pmMneLocal BIT,@pmIdMneda VARCHAR(5)
 AS 
 UPDATE adm_monedas SET MneLocal=@pmMneLocal
 WHERE IdMneda<>@pmIdMneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUpmonedasTas @pmIdMneda VARCHAR(5),@pmVrCambio DECIMAL(12,6)
 AS 
 UPDATE adm_monedas SET VrCambio=@pmVrCambio
 WHERE IdMneda=@pmIdMneda
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptasas @pmIdMneda VARCHAR(5),@pmIdMnedaD VARCHAR(5),@pmFecha SMALLDATETIME,@pmTasaDia DECIMAL(14,6)
 AS 
 UPDATE Trn_tasas SET TasaDia=@pmTasaDia
 WHERE IdMneda=@pmIdMneda AND IdMnedaD=@pmIdMnedaD AND Fecha=@pmFecha
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_AportesDet @pmtmEst VARCHAR(5),@pmtmItem INT,@pmtmIdEmpleado VARCHAR(16),@pmtmApellido1 VARCHAR(50),@pmtmApellido2 VARCHAR(50),@pmtmNombre1 VARCHAR(50),@pmtmNombre2 VARCHAR(50),@pmtmNContto INT,@pmtmDiasPen DECIMAL(10,4),@pmtmDiasSal DECIMAL(10,4),@pmtmDiasRie DECIMAL(10,4),@pmtmDiasCom DECIMAL(10,4),@pmtmBasePen MONEY,@pmtmBaseSal MONEY,@pmtmBaseRie MONEY,@pmtmBaseCom MONEY,@pmtmBaseRef MONEY,@pmtmValorPen MONEY,@pmtmValorSal MONEY,@pmtmValorRie MONEY,@pmtmValorCom MONEY,@pmtmValorSena MONEY,@pmtmValorIcbf MONEY,@pmtmValorFsp MONEY,@pmtmValorFsu MONEY,@pmtmValorESAP MONEY,@pmtmValorMin MONEY,@pmtmTarfaPen DECIMAL(10,4),@pmtmTarfaSal DECIMAL(10,4),@pmtmTarfaRie DECIMAL(10,4)
,@pmtmTarfaFsp DECIMAL(10,4),@pmtmTarfaFsu DECIMAL(10,4),@pmtmTarfaEsa DECIMAL(10,4),@pmtmTarfaMin DECIMAL(10,4),@pmtmTarfaCom DECIMAL(10,4),@pmtmTarfaIcb DECIMAL(10,4),@pmtmTarfaSen DECIMAL(10,4),@pmtmVrIncap MONEY,@pmtmVrLicencia MONEY,@pmtmVrVolAfi MONEY,@pmtmVrVolApo MONEY,@pmtmVrNoReten MONEY
,@pmtmVrUpc MONEY,@pmtmVrIncIrp MONEY,@pmtmNAutLic VARCHAR(30),@pmtmNAutInc VARCHAR(30),@pmtmNAutIrp VARCHAR(30),@pmtmIdFonPen VARCHAR(8),@pmtmIdFonSal VARCHAR(8),@pmtmIdFonRie VARCHAR(8),@pmtmIdFonCaj VARCHAR(8),@pmtmIdInstala VARCHAR(4),@pmtmIdDep VARCHAR(4),@pmtmIdCCosto VARCHAR(16),@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmVehiculo VARCHAR(10),@pmtmVrBasico MONEY
,@pmtmCdFonAvp VARCHAR(8)
 AS 
 UPDATE tm_Aportes SET tmIdEmpleado=@pmtmIdEmpleado,tmApellido1=@pmtmApellido1,tmApellido2=@pmtmApellido2,tmNombre1=@pmtmNombre1,tmNombre2=@pmtmNombre2,tmNContto=@pmtmNContto,tmDiasPen=@pmtmDiasPen,tmDiasSal=@pmtmDiasSal,tmDiasRie=@pmtmDiasRie,tmDiasCom=@pmtmDiasCom,tmBasePen=@pmtmBasePen,tmBaseSal=@pmtmBaseSal,tmBaseRie=@pmtmBaseRie,tmBaseCom=@pmtmBaseCom,tmBaseRef=@pmtmBaseRef,tmValorPen=@pmtmValorPen,tmValorSal=@pmtmValorSal,tmValorRie=@pmtmValorRie,tmValorCom=@pmtmValorCom,tmValorSena=@pmtmValorSena,tmValorIcbf=@pmtmValorIcbf,tmValorFsp=@pmtmValorFsp,tmValorFsu=@pmtmValorFsu,tmValorESAP=@pmtmValorESAP
,tmValorMin=@pmtmValorMin,tmTarfaPen=@pmtmTarfaPen,tmTarfaSal=@pmtmTarfaSal,tmTarfaRie=@pmtmTarfaRie,tmTarfaFsp=@pmtmTarfaFsp,tmTarfaFsu=@pmtmTarfaFsu,tmTarfaEsa=@pmtmTarfaEsa,tmTarfaMin=@pmtmTarfaMin,tmTarfaCom=@pmtmTarfaCom,tmTarfaIcb=@pmtmTarfaIcb,tmTarfaSen=@pmtmTarfaSen,tmVrIncap=@pmtmVrIncap,tmVrLicencia=@pmtmVrLicencia,tmVrVolAfi=@pmtmVrVolAfi,tmVrVolApo=@pmtmVrVolApo,tmVrNoReten=@pmtmVrNoReten,tmVrUpc=@pmtmVrUpc,tmVrIncIrp=@pmtmVrIncIrp,tmNAutLic=@pmtmNAutLic,tmNAutInc=@pmtmNAutInc,tmNAutIrp=@pmtmNAutIrp,tmIdFonPen=@pmtmIdFonPen,tmIdFonSal=@pmtmIdFonSal,tmIdFonRie=@pmtmIdFonRie,tmIdFonCaj=@pmtmIdFonCaj,tmIdInstala=@pmtmIdInstala
,tmIdDep=@pmtmIdDep,tmIdCCosto=@pmtmIdCCosto,tmIdSubCos=@pmtmIdSubCos,tmIdNom=@pmtmIdNom,tmVehiculo=@pmtmVehiculo,tmVrBasico=@pmtmVrBasico,tmCdFonAvp=@pmtmCdFonAvp
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_AportesNov @pmtmEst VARCHAR(5),@pmtmItem INT,@pmtmNovIng INT,@pmtmNovRet INT,@pmtmNovTda INT,@pmtmNovTaa INT,@pmtmNovTdp INT,@pmtmNovTap INT
,@pmtmNovVsp INT,@pmtmNovVte INT,@pmtmNovVst INT,@pmtmNovSln INT,@pmtmNovIge INT,@pmtmNovLma INT,@pmtmNovVac INT,@pmtmNovAvp INT,@pmtmNovIrp INT,@pmtmNovVct INT
,@pmtmCodNewEps VARCHAR(8),@pmtmCodNewPen VARCHAR(8),@pmtmCodNewRie VARCHAR(8),@pmtmCodNewIns VARCHAR(4),@pmtmCodNewTar VARCHAR(4),@pmtmCodNewGru VARCHAR(4),@pmtmDiasNov DECIMAL(10,4)
 AS 
 UPDATE tm_Aportes SET tmNovIng=@pmtmNovIng,tmNovRet=@pmtmNovRet,tmNovTda=@pmtmNovTda,tmNovTaa=@pmtmNovTaa,tmNovTdp=@pmtmNovTdp,tmNovTap=@pmtmNovTap,tmNovVsp=@pmtmNovVsp,tmNovVte=@pmtmNovVte,tmNovVst=@pmtmNovVst,tmNovSln=@pmtmNovSln
,tmNovIge=@pmtmNovIge,tmNovLma=@pmtmNovLma,tmNovVac=@pmtmNovVac,tmNovAvp=@pmtmNovAvp,tmNovIrp=@pmtmNovIrp,tmNovVct=@pmtmNovVct,tmCodNewEps=@pmtmCodNewEps,tmCodNewPen=@pmtmCodNewPen,tmCodNewRie=@pmtmCodNewRie,tmCodNewIns=@pmtmCodNewIns
,tmCodNewTar=@pmtmCodNewTar,tmCodNewGru=@pmtmCodNewGru,tmDiasNov=@pmtmDiasNov
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_AutTot @pmtmEst VARCHAR(5),@pmtmItem INT,@pmtmTipAdm VARCHAR(3),@pmtmIdFondo VARCHAR(8),@pmtmCodTipoApo CHAR(2),@pmtmTotalCot MONEY,@pmtmTotalFsp MONEY,@pmtmTotalFsu MONEY,@pmtmTotalAva MONEY,@pmtmTotalAve MONEY
,@pmtmTotalUpc MONEY,@pmtmTotalLic MONEY,@pmtmTotalInc MONEY,@pmtmTotalOtr MONEY,@pmtmDiasMora INT,@pmtmTarifMora DECIMAL(10,4),@pmtmIntMora MONEY,@pmtmIMoraOtr MONEY,@pmtmTotalSAF MONEY,@pmtmSAFUpc MONEY,@pmtmTotalPagar MONEY,@pmtmTotalFon MONEY
,@pmtmTotalAfi INT,@pmtmNumAutLic VARCHAR(30),@pmtmNumAutInc VARCHAR(30),@pmtmNumPlaSAF VARCHAR(20)
 AS 
 UPDATE tm_AutTot SET tmTipAdm=@pmtmTipAdm,tmIdFondo=@pmtmIdFondo,tmCodTipoApo=@pmtmCodTipoApo,tmTotalCot=@pmtmTotalCot,tmTotalFsp=@pmtmTotalFsp,tmTotalFsu=@pmtmTotalFsu,tmTotalAva=@pmtmTotalAva,tmTotalAve=@pmtmTotalAve,tmTotalUpc=@pmtmTotalUpc,tmTotalLic=@pmtmTotalLic
,tmTotalInc=@pmtmTotalInc,tmTotalOtr=@pmtmTotalOtr,tmDiasMora=@pmtmDiasMora,tmTarifMora=@pmtmTarifMora,tmIntMora=@pmtmIntMora,tmIMoraOtr=@pmtmIMoraOtr,tmTotalSAF=@pmtmTotalSAF,tmSAFUpc=@pmtmSAFUpc,tmTotalPagar=@pmtmTotalPagar,tmTotalFon=@pmtmTotalFon,tmTotalAfi=@pmtmTotalAfi
,tmNumAutLic=@pmtmNumAutLic,tmNumAutInc=@pmtmNumAutInc,tmNumPlaSAF=@pmtmNumPlaSAF
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_AuxInvCpm @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdCia CHAR(2)
,@pmtmCostoProm MONEY
 AS 
 UPDATE tm_AuxInv SET  tmCostoProm=@pmtmCostoProm
 WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_AuxInvSan @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4),@pmtmIdCia CHAR(2)
,@pmtmNumLote VARCHAR(30),@pmtmSaldoAnt DECIMAL(14,4),@pmtmCostoAnt MONEY,@pmtmCostoProm MONEY
 AS 
 UPDATE tm_AuxInv SET tmSaldoAnt=@pmtmSaldoAnt,tmCostoAnt=@pmtmCostoAnt,tmCostoProm=@pmtmCostoProm
 WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto AND tmIdBodega=@pmtmIdBodega 
 AND tmIdCia=@pmtmIdCia AND tmNumLote=@pmtmNumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_AuxInvSanKar @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16),@pmtmIdBodega VARCHAR(4)
,@pmtmIdCia CHAR(2),@pmtmNumLote VARCHAR(30),@pmtmSaldoAnt DECIMAL(14,4),@pmtmCostoAnt MONEY
 AS 
 UPDATE tm_AuxInv SET tmSaldoAnt=@pmtmSaldoAnt,tmCostoAnt=@pmtmCostoAnt
 WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto AND tmIdBodega=@pmtmIdBodega 
 AND tmIdCia=@pmtmIdCia AND tmNumLote=@pmtmNumLote
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_BalComSan @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT
,@pmtmTipo VARCHAR(4),@pmtmSaldoAnterior MONEY,@pmtmNuevoSaldo MONEY
 AS 
 UPDATE tm_BalCom SET  tmSaldoAnterior=@pmtmSaldoAnterior,tmNuevoSaldo=@pmtmNuevoSaldo
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmItem=@pmtmItem AND tmTipo=@pmtmTipo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_BalComparaDos @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT
,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 UPDATE tm_BalCompara SET tmSalAnt2=@pmtmSalAnt2,tmDebitos2=@pmtmDebitos2,tmCreditos2=@pmtmCreditos2,tmSaldo2=@pmtmSaldo2
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_BalComparaTres @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT
,@pmtmSalAnt3 MONEY,@pmtmDebitos3 MONEY,@pmtmCreditos3 MONEY,@pmtmSaldo3 MONEY
 AS 
 UPDATE tm_BalCompara SET tmSalAnt3=@pmtmSalAnt3,tmDebitos3=@pmtmDebitos3,tmCreditos3=@pmtmCreditos3,tmSaldo3=@pmtmSaldo3
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_BalGenDos @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2)
,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 UPDATE tm_BalGen SET  tmSalAnt2=@pmtmSalAnt2,tmDebitos2=@pmtmDebitos2
,tmCreditos2=@pmtmCreditos2,tmSaldo2=@pmtmSaldo2
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia=@pmtmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_BalGenUno @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmIdCia CHAR(2)
,@pmtmSaldoAnterior MONEY,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY,@pmtmNuevoSaldo MONEY
 AS 
 UPDATE tm_BalGen SET tmSaldoAnterior=@pmtmSaldoAnterior,tmTotalDebitos=@pmtmTotalDebitos
,tmTotalCreditos=@pmtmTotalCreditos,tmNuevoSaldo=@pmtmNuevoSaldo
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia=@pmtmIdCia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUptm_CargosAnt @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmAuto BIT,@pmtmAbonado MONEY
,@pmtmTipDocAnt VARCHAR(3),@pmtmDocAnticipo INT,@pmtmCiaDocAnt  CHAR(2)
AS
 IF @pmAuto<>0 
  UPDATE tm_Cargos SET tmAbonado=(tmAbonado+@pmtmAbonado),tmTipDocAnt=@pmtmTipDocAnt, tmDocAnticipo=@pmtmDocAnticipo,tmCiaDocAnt=@pmtmCiaDocAnt
  WHERE  tmNumero=@pmtmNumero  AND  tmItem=@pmtmItem
 ELSE
  UPDATE tm_Cargos SET tmAbonado=@pmtmAbonado,tmTipDocAnt=@pmtmTipDocAnt, tmDocAnticipo=@pmtmDocAnticipo,tmCiaDocAnt=@pmtmCiaDocAnt
  WHERE  tmNumero=@pmtmNumero  AND  tmItem=@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_CxCobrarAbo @pmtmNumero VARCHAR(5),@pmtmItem INT
,@pmtmTotAbono MONEY
 AS 
 UPDATE tm_CxCobrar SET tmTotAbono=@pmtmTotAbono
 WHERE  tmNumero=@pmtmNumero AND tmItem=@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_EdsFormasTap @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipAplica VARCHAR(10)
 AS 
 UPDATE  tm_EdsFormas  SET tmTipAplica=@pmtmTipAplica
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUptm_EstCueCtePro @pmtmEst CHAR(2),@pmtmIdCliente VARCHAR(16),@pmtmIdClase VARCHAR(4),@pmtmIdCia CHAR(2)
,@pmtmIdVehiculo VARCHAR(10),@pmtmSaldoAnt MONEY,@pmFld VARCHAR(3)
AS
 IF @pmFld='CIA'
   UPDATE tm_EstCueCte SET tmSalAntProCia=@pmtmSaldoAnt
   WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente AND tmIdVehiculo=@pmtmIdVehiculo
 --AND tmIdClase=@pmtmIdClase AND  tmIdCia=@pmtmIdCia
  ELSE
 BEGIN
  IF @pmFld='CLA'
   UPDATE tm_EstCueCte SET tmSalAntProCla=@pmtmSaldoAnt
   WHERE tmEst=@pmtmEst AND  tmIdCliente=@pmtmIdCliente AND tmIdClase=@pmtmIdClase AND tmIdVehiculo=@pmtmIdVehiculo
  ELSE
   UPDATE tm_EstCueCte SET tmSalAntPro=@pmtmSaldoAnt
   WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente 
 END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUptm_EstCueCteVeh @pmtmEst CHAR(2),@pmtmIdVehiculo VARCHAR(10),@pmtmIdClase VARCHAR(4)
,@pmtmIdCia CHAR(2),@pmtmSaldoAnt MONEY,@pmFld VARCHAR(3)
AS
 IF @pmFld='CIA'
   UPDATE tm_EstCueCte SET tmSaldoAntCia=@pmtmSaldoAnt
   WHERE tmEst=@pmtmEst AND tmIdVehiculo=@pmtmIdVehiculo AND tmIdClase=@pmtmIdClase AND  tmIdCia=@pmtmIdCia
 ELSE
 BEGIN
  IF @pmFld='CLA'
   UPDATE tm_EstCueCte SET tmSaldoAntCla=@pmtmSaldoAnt
   WHERE tmEst=@pmtmEst AND tmIdVehiculo=@pmtmIdVehiculo AND tmIdClase=@pmtmIdClase
  ELSE
   UPDATE tm_EstCueCte SET tmSaldoAnt=@pmtmSaldoAnt
   WHERE tmEst=@pmtmEst AND tmIdVehiculo=@pmtmIdVehiculo
 END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_EstadoGypDos @pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmItem INT
,@pmtmSalAnt2 MONEY,@pmtmDebitos2 MONEY,@pmtmCreditos2 MONEY,@pmtmSaldo2 MONEY
 AS 
 UPDATE tm_EstadoGyp SET tmSalAnt2=@pmtmSalAnt2,tmDebitos2=@pmtmDebitos2,tmCreditos2=@pmtmCreditos2,tmSaldo2=@pmtmSaldo2
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUptm_ExtVehNit  @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16),@pmtmSalAntPro MONEY
AS
 UPDATE tm_ExtVeh SET tmSalAntPro=@pmtmSalAntPro
 WHERE tmEst=@pmtmEst AND tmIdPropietario=@pmtmIdPropietario

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE  paUptm_ExtVehPro @pmtmEst CHAR(2),@pmtmIdCliente VARCHAR(16)
,@pmtmIdVehiculo VARCHAR(10),@pmtmSaldoAnt MONEY
AS
 UPDATE tm_ExtVeh SET tmSaldoAnt=@pmtmSaldoAnt
 WHERE tmEst=@pmtmEst AND tmIdPropietario=@pmtmIdCliente AND tmIdVehiculo=@pmtmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUptm_ExtVehProd @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmTotalProducido MONEY,@pmtmMaxProducido MONEY,@pmtmMinProducido MONEY,@pmtmTotalRecrridos INT,@pmtmTotalPasaj INT,@pmtmTotalDias INT
 AS 
 UPDATE tm_ExtVeh SET tmTotalProducido=@pmtmTotalProducido,tmMaxProducido=@pmtmMaxProducido,tmMinProducido=@pmtmMinProducido,tmTotalRecrridos=@pmtmTotalRecrridos,tmTotalPasaj=@pmtmTotalPasaj,tmTotalDias=@pmtmTotalDias
 WHERE tmEst=@pmtmEst AND tmIdPropietario=@pmtmIdPropietario AND tmIdVehiculo=@pmtmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUptm_ExtVehVal @pmtmEst CHAR(2),@pmtmIdPropietario VARCHAR(16),@pmtmIdVehiculo VARCHAR(10),@pmtmTotaDebito MONEY,@pmtmTotaCredito MONEY
,@pmtmTotalCausac MONEY,@pmtmGalones DECIMAL(14,4),@pmtmValorComb MONEY
 AS 
 UPDATE tm_ExtVeh SET tmTotaDebito=@pmtmTotaDebito,tmTotaCredito=@pmtmTotaCredito,tmTotalCausac=@pmtmTotalCausac,tmGalones=@pmtmGalones,tmValorComb=@pmtmValorComb
 WHERE tmEst=@pmtmEst AND tmIdPropietario=@pmtmIdPropietario AND tmIdVehiculo=@pmtmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUptm_ExtVehVeh  @pmtmEst CHAR(2),@pmtmIdVehiculo VARCHAR(10),@pmtmSaldoAnt MONEY
AS
 UPDATE tm_ExtVeh SET tmSalAntProCla=@pmtmSaldoAnt
 WHERE tmEst=@pmtmEst AND tmIdVehiculo=@pmtmIdVehiculo

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


CREATE PROCEDURE paUptm_IntNomina @pmtmEst CHAR(2),@pmtmItem INT,@pmtmIdVehiculo VARCHAR(10),@pmtmIdEmpleado VARCHAR(16),@pmtmIdConductor VARCHAR(16)
,@pmtmIdPropietario VARCHAR(16),@pmtmVehPropio BIT,@pmtmConductor VARCHAR(100),@pmtmTercero VARCHAR(100),@pmtmCcosto VARCHAR(16),@pmtmValorAbono MONEY
 AS 
 UPDATE tm_IntNomina SET tmIdVehiculo=@pmtmIdVehiculo,tmIdEmpleado=@pmtmIdEmpleado,tmIdConductor=@pmtmIdConductor,tmIdPropietario=@pmtmIdPropietario,tmVehPropio=@pmtmVehPropio
,tmConductor=@pmtmConductor,tmTercero=@pmtmTercero,tmCcosto=@pmtmCcosto,tmValorAbono=@pmtmValorAbono
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_KdexExto @pmtmNumero VARCHAR(5),@pmTipo VARCHAR(3)
 AS 
 IF @pmTipo='RET'
  UPDATE  tm_Kdex SET tmVrRete=0,tmTarifaRet=0  WHERE  tmNumero=@pmtmNumero  AND  tmVrRete>0
 ELSE
 BEGIN
  IF @pmTipo='ICA'
   UPDATE  tm_Kdex SET tmVrIca=0,tmTarifaIca=0  WHERE  tmNumero=@pmtmNumero  AND  tmVrIca>0
  ELSE--IVA
   UPDATE  tm_Kdex SET tmVrIva=0,tmTarifaIva=0  WHERE  tmNumero=@pmtmNumero  AND  tmVrIva>0
 END   
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_LibOf @pmtmEst CHAR(2),@pmtmTipCom VARCHAR(3),@pmtmItem INT
,@pmtmTotalDebitos MONEY,@pmtmTotalCreditos MONEY
 AS 
 UPDATE tm_LibOf SET tmTotalDebitos=@pmtmTotalDebitos,tmTotalCreditos=@pmtmTotalCreditos
 WHERE tmEst=@pmtmEst AND tmTipCom=@pmtmTipCom AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_LibOfSan @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmtmIdCuenta VARCHAR(16),@pmtmSaldoAnterior MONEY
,@pmtmIdTercero VARCHAR(16)=Null,@pmtmIdCCosto VARCHAR(16)=Null,@pmtmIdSubCos VARCHAR(16)=Null,@pmtmIdCia CHAR(2)=Null
 AS 
 IF @pmTipo='CUE'
  UPDATE tm_LibOf SET tmSaldoAnterior=@pmtmSaldoAnterior  WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
 ELSE
   IF @pmTipo='NIT'
    UPDATE tm_LibOf SET tmSanNit=@pmtmSaldoAnterior  WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta
    AND tmIdTercero=@pmtmIdTercero  AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
   ELSE
    IF  @pmTipo='CC'
     UPDATE tm_LibOf SET tmSanNitcc=@pmtmSaldoAnterior  WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta
     AND tmIdTercero=@pmtmIdTercero AND tmIdCCosto=@pmtmIdCCosto AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
    ELSE
     IF  @pmTipo='SUB'
      UPDATE tm_LibOf SET tmSanNitCcSub=@pmtmSaldoAnterior  WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta
      AND tmIdTercero=@pmtmIdTercero AND tmIdCCosto=@pmtmIdCCosto AND tmIdSubCos=@pmtmIdSubCos AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
    
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_Liquida @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmIdEmpleado VARCHAR(16),@pmtmIdConcepto VARCHAR(4),@pmtmDetalle VARCHAR(100),@pmtmFecUltLiq SMALLDATETIME,@pmtmFecInicial SMALLDATETIME,@pmtmFecFinal SMALLDATETIME,@pmtmVrBase MONEY,@pmtmCantidad INT,@pmtmVrLiquida MONEY
,@pmtmVrDeducido MONEY,@pmtmFecIniPmd SMALLDATETIME,@pmtmFecFinPmd SMALLDATETIME,@pmtmDiasBase INT,@pmtmDiasNoRem INT,@pmtmTarifa DECIMAL(10,4),@pmtmNPrestamo INT,@pmtmNCuota INT,@pmtmLogPmdio VARCHAR(600),@pmtmNContrato INT,@pmtmpVehiculo VARCHAR(10),@pmtmIdDep VARCHAR(4),@pmtmIdCCosto VARCHAR(16)
,@pmtmIdSubCos VARCHAR(16),@pmtmIdNom VARCHAR(4),@pmtmVrSalario MONEY,@pmtmVrParcial MONEY
 AS 
 UPDATE tm_Liquida SET tmIdEmpleado=@pmtmIdEmpleado,tmIdConcepto=@pmtmIdConcepto,tmDetalle=@pmtmDetalle,tmFecUltLiq=@pmtmFecUltLiq,tmFecInicial=@pmtmFecInicial,tmFecFinal=@pmtmFecFinal,tmVrBase=@pmtmVrBase,tmCantidad=@pmtmCantidad,tmVrLiquida=@pmtmVrLiquida,tmVrDeducido=@pmtmVrDeducido,tmFecIniPmd=@pmtmFecIniPmd
,tmFecFinPmd=@pmtmFecFinPmd,tmDiasBase=@pmtmDiasBase,tmDiasNoRem=@pmtmDiasNoRem,tmTarifa=@pmtmTarifa,tmNPrestamo=@pmtmNPrestamo,tmNCuota=@pmtmNCuota,tmLogPmdio=@pmtmLogPmdio,tmNContrato=@pmtmNContrato,tmpVehiculo=@pmtmpVehiculo,tmIdDep=@pmtmIdDep,tmIdCCosto=@pmtmIdCCosto,tmIdSubCos=@pmtmIdSubCos
,tmIdNom=@pmtmIdNom,tmVrSalario=@pmtmVrSalario,tmVrParcial=@pmtmVrParcial
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_LiquidaInt @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmIdConcepto VARCHAR(4) ,@pmtmVrBase MONEY
,@pmtmVrLiquida MONEY,@pmtmVrDeducido MONEY
 AS 
 UPDATE tm_Liquida SET tmVrBase=@pmtmVrBase,tmVrLiquida=@pmtmVrLiquida,tmVrDeducido=@pmtmVrDeducido
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado AND tmIdConcepto=@pmtmIdConcepto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_LiquidaPar @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmFecInicial SMALLDATETIME,@pmtmFecFinal SMALLDATETIME,@pmtmVrBase MONEY,@pmtmCantidad INT,@pmtmVrLiquida MONEY,@pmtmVrDeducido MONEY,@pmtmDiasBase INT,@pmtmDiasNoRem INT
,@pmtmTarifa DECIMAL(10,4),@pmtmNPrestamo INT,@pmtmNCuota INT,@pmtmVrParcial MONEY
 AS 
 UPDATE tm_Liquida SET tmFecInicial=@pmtmFecInicial,tmFecFinal=@pmtmFecFinal,tmVrBase=@pmtmVrBase,tmCantidad=@pmtmCantidad,tmVrLiquida=@pmtmVrLiquida,tmVrDeducido=@pmtmVrDeducido,tmDiasBase=@pmtmDiasBase,tmDiasNoRem=@pmtmDiasNoRem
,tmTarifa=@pmtmTarifa,tmNPrestamo=@pmtmNPrestamo,tmNCuota=@pmtmNCuota,tmVrParcial=@pmtmVrParcial
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovClientesSan @pmtmEst CHAR(2),@pmTipo VARCHAR(3),@pmtmIdCliente VARCHAR(16),@pmtmIdAgencia VARCHAR(16),@pmtmIdCia CHAR(2),@pmSaldoAnt MONEY
 AS 
 IF @pmTipo='CLI'
   UPDATE tm_MovClientes SET tmSanClie=@pmSaldoAnt WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente
 ELSE
  BEGIN
   IF @pmTipo='AGE' 
    UPDATE tm_MovClientes SET tmSanAge=@pmSaldoAnt WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente AND tmIdAgencia=@pmtmIdAgencia
  ELSE
    BEGIN
      IF @pmTipo='CIA'
       UPDATE tm_MovClientes SET tmSanClieCia=@pmSaldoAnt WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente AND tmIdCia=@pmtmIdCia
      ELSE --AGC
        UPDATE tm_MovClientes SET tmSanAgeCia=@pmSaldoAnt WHERE tmEst=@pmtmEst AND tmIdCliente=@pmtmIdCliente AND tmIdAgencia=@pmtmIdAgencia AND  tmIdCia=@pmtmIdCia
     END
  END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueCc @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanCcCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia AND IdCCosto=@pmIdCCosto
 ELSE
  UPDATE tm_MovCue SET VrSanCc=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND   IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueCcSub @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanCcSubCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
 ELSE
  UPDATE tm_MovCue SET VrSanCcSub=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta
  AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNit @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdTercero VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia AND IdTercero=@pmIdTercero
 ELSE
  UPDATE tm_MovCue SET VrSanNit=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdTercero=@pmIdTercero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitAge @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitAgeCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia
 ELSE
  UPDATE tm_MovCue SET VrSanNitAge=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitAgeCc @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)
,@pmIdCCosto VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitAgeCcCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia AND IdCCosto=@pmIdCCosto
 ELSE
  UPDATE tm_MovCue SET VrSanNitAgeCc=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia AND IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitAgeCcSub @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmCodAgncia VARCHAR(16)
,@pmIdCCosto VARCHAR(16),@pmIdSubCos VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitAgeCcSubCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia 
  AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
 ELSE
  UPDATE tm_MovCue SET VrSanNitAgeCcSub=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero AND CodAgncia=@pmCodAgncia 
  AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitCc @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmIdCCosto VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitCcCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND IdCCosto=@pmIdCCosto
 ELSE
  UPDATE tm_MovCue SET VrSanNitCc=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero  AND IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitCcSub @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitCcSubCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
 ELSE
  UPDATE tm_MovCue SET VrSanNitCcSub=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero AND  IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitVeh @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitVehCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND IdVehiculo=@pmIdVehiculo
 ELSE
  UPDATE tm_MovCue SET VrSanNitVeh=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero AND  IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitVehCc @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10)
,@pmIdCCosto VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanNitVehCcCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdTercero=@pmIdTercero AND IdVehiculo=@pmIdVehiculo AND IdCCosto=@pmIdCCosto
 ELSE
  UPDATE tm_MovCue SET VrSanNitVehCc=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND IdTercero=@pmIdTercero AND IdVehiculo=@pmIdVehiculo AND IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueNitVehCcSub @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdTercero VARCHAR(16),@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanVehCcSubCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdVehiculo=@pmIdVehiculo  AND IdTercero=@pmIdTercero
   AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
 ELSE
  UPDATE tm_MovCue SET VrSanVehCcSub=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND  IdVehiculo=@pmIdVehiculo AND IdTercero=@pmIdTercero
  AND  IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueSan @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanCueCia=@pmVrSaldo WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
 ELSE
  UPDATE tm_MovCue SET VrSanCue=@pmVrSaldo WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueVeh @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdVehiculo VARCHAR(10),@pmIdCia CHAR(2),@pmVrSaldo MONEY
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanVehCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia AND IdVehiculo=@pmIdVehiculo
 ELSE
  UPDATE tm_MovCue SET VrSanVeh=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND  IdVehiculo=@pmIdVehiculo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueVehCc @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanVehCcCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdVehiculo=@pmIdVehiculo AND IdCCosto=@pmIdCCosto
 ELSE
  UPDATE tm_MovCue SET VrSanVehCc=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND  IdVehiculo=@pmIdVehiculo  AND IdCCosto=@pmIdCCosto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovCueVehCcSub @pmTipo VARCHAR(3),@pmtmEst CHAR(2),@pmIdCuenta VARCHAR(16)
,@pmIdCia CHAR(2),@pmVrSaldo MONEY,@pmIdVehiculo VARCHAR(10),@pmIdCCosto VARCHAR(16)
,@pmIdSubCos VARCHAR(16)
 AS 
 IF @pmTipo='CIA'
  UPDATE tm_MovCue SET VrSanVehCcSubCia=@pmVrSaldo
   WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta AND IdCia=@pmIdCia
   AND IdVehiculo=@pmIdVehiculo AND IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
 ELSE
  UPDATE tm_MovCue SET VrSanVehCcSub=@pmVrSaldo
  WHERE  tmEst=@pmtmEst AND IdCuenta=@pmIdCuenta 
  AND  IdVehiculo=@pmIdVehiculo AND  IdCCosto=@pmIdCCosto AND IdSubCos=@pmIdSubCos
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovInventDos @pmtmEst CHAR(2),@pmtmItem INT,@pmtmEntradas2 DECIMAL(14,4),@pmtmSalidas2 DECIMAL(14,4),@pmtmCostoEnt2 MONEY,@pmtmCostoSal2 MONEY,@pmtmValorEnt2 MONEY
,@pmtmValorSal2 MONEY,@pmtmDctosEnt2 MONEY,@pmtmDctosSal2 MONEY,@pmtmIvaEnt2 MONEY,@pmtmIvaSal2 MONEY,@pmtmOtrosImpEnt2 MONEY,@pmtmOtrosImpSal2 MONEY,@pmtmUnidVolumen2 DECIMAL(14,4)
 AS 
 UPDATE tm_MovInvent SET  tmEntradas2=@pmtmEntradas2,tmSalidas2=@pmtmSalidas2,tmCostoEnt2=@pmtmCostoEnt2,tmCostoSal2=@pmtmCostoSal2,tmValorEnt2=@pmtmValorEnt2,tmValorSal2=@pmtmValorSal2
,tmDctosEnt2=@pmtmDctosEnt2,tmDctosSal2=@pmtmDctosSal2,tmIvaEnt2=@pmtmIvaEnt2,tmIvaSal2=@pmtmIvaSal2,tmOtrosImpEnt2=@pmtmOtrosImpEnt2,tmOtrosImpSal2=@pmtmOtrosImpSal2,tmUnidVolumen2=@pmtmUnidVolumen2
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovTanquesFis @pmtmEst CHAR(2),@pmtmIdProducto VARCHAR(16)
,@pmtmIdCia CHAR(2),@pmtmItem INT,@pmtmSaldoAntDia DECIMAL(14,4),@pmtmCostoAntDia MONEY
,@pmtmNuevoSaldo DECIMAL(14,4),@pmtmCostoNsal MONEY,@pmtmCantFisico DECIMAL(14,4)
,@pmtmCantAgua DECIMAL(14,4),@pmtmDiferencia DECIMAL(14,4)
 AS 
 UPDATE tm_MovTanques SET tmSaldoAntDia=@pmtmSaldoAntDia,tmCostoAntDia=@pmtmCostoAntDia
,tmNuevoSaldo=@pmtmNuevoSaldo,tmCostoNsal=@pmtmCostoNsal,tmCantFisico=@pmtmCantFisico
,tmCantAgua=@pmtmCantAgua,tmDiferencia=@pmtmDiferencia
 WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto AND  tmItem=@pmtmItem
 AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovTanquesSan @pmtmEst CHAR(2),@pmTipo VARCHAR(3),@pmtmIdProducto VARCHAR(16),@pmtmIdTanque VARCHAR(4)
,@pmtmIdCia CHAR(2),@pmtmSaldoAnt DECIMAL(14,4),@pmtmCostoAnt MONEY
 AS 
 IF @pmTipo='PRO' 
  UPDATE tm_MovTanques SET tmSaldoAntPro=@pmtmSaldoAnt,tmCostoSanPro=@pmtmCostoAnt   WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto 
 ELSE
  BEGIN 
    IF @pmTipo='CIA'
     UPDATE tm_MovTanques SET tmSaldoAntCia=@pmtmSaldoAnt,tmCostoSanCia=@pmtmCostoAnt  WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto  AND tmIdCia=@pmtmIdCia
    ELSE
      BEGIN
       IF @pmTipo='TAN'
         UPDATE tm_MovTanques SET tmSaldoAntTan=@pmtmSaldoAnt,tmCostoSanTan=@pmtmCostoAnt  WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto  AND tmIdTanque=@pmtmIdTanque
       ELSE
         UPDATE tm_MovTanques SET tmSaldoAnt=@pmtmSaldoAnt,tmCostoAnt=@pmtmCostoAnt  WHERE tmEst=@pmtmEst AND tmIdProducto=@pmtmIdProducto  AND  tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') AND tmIdTanque LIKE ISNULL(@pmtmIdTanque,'%')
      END
  END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_MovVentasDos @pmtmEst CHAR(2),@pmtmItem INT,@pmtmEntradas2 DECIMAL(14,4),@pmtmSalidas2 DECIMAL(14,4),@pmtmCostoEnt2 MONEY,@pmtmCostoSal2 MONEY,@pmtmValorEnt2 MONEY
,@pmtmValorSal2 MONEY,@pmtmDctosEnt2 MONEY,@pmtmDctosSal2 MONEY,@pmtmIvaEnt2 MONEY,@pmtmIvaSal2 MONEY,@pmtmImpGlobalEnt2 MONEY,@pmtmImpGlobalSal2 MONEY,@pmtmOtrosEnt2 MONEY
,@pmtmOtrosSal2 MONEY,@pmtmUnidVolumen2 DECIMAL(14,4)
 AS 
 UPDATE tm_MovVentas SET  tmEntradas2=@pmtmEntradas2,tmSalidas2=@pmtmSalidas2,tmCostoEnt2=@pmtmCostoEnt2,tmCostoSal2=@pmtmCostoSal2,tmValorEnt2=@pmtmValorEnt2,tmValorSal2=@pmtmValorSal2
,tmDctosEnt2=@pmtmDctosEnt2,tmDctosSal2=@pmtmDctosSal2,tmIvaEnt2=@pmtmIvaEnt2,tmIvaSal2=@pmtmIvaSal2,tmImpGlobalEnt2=@pmtmImpGlobalEnt2,tmImpGlobalSal2=@pmtmImpGlobalSal2,tmOtrosEnt2=@pmtmOtrosEnt2
,tmOtrosSal2=@pmtmOtrosSal2,tmUnidVolumen2=@pmtmUnidVolumen2
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_Nomina @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT,@pmtmIdConcepto VARCHAR(4),@pmtmClaseCon VARCHAR(10),@pmtmDetalle VARCHAR(100),@pmtmCantDevg DECIMAL(10,4)
,@pmtmCantDed DECIMAL(10,4),@pmtmVrUnitario MONEY,@pmtmVrTotDevg MONEY,@pmtmVrTotDed MONEY,@pmtmUnidad VARCHAR(10),@pmtmClaseLiq VARCHAR(10),@pmtmVrOrigen MONEY,@pmtmTarifa DECIMAL(10,4),@pmtmDiasCalc DECIMAL(10,4)
,@pmtmDiasNov DECIMAL(10,4),@pmtmCodFondo VARCHAR(8),@pmtmNPrestamo INT,@pmtmNCuota INT,@pmtmOrigCargue VARCHAR(10),@pmtmVrBaseLiq MONEY,@pmtmNContto INT,@pmtmFecIni SMALLDATETIME,@pmtmFecFin SMALLDATETIME,@pmtmIdPeriodo VARCHAR(8)
,@pmtmVrPension MONEY,@pmtmVrSalud MONEY
 AS 
 UPDATE tm_Nomina SET tmIdConcepto=@pmtmIdConcepto,tmClaseCon=@pmtmClaseCon,tmDetalle=@pmtmDetalle,tmCantDevg=@pmtmCantDevg,tmCantDed=@pmtmCantDed,tmVrUnitario=@pmtmVrUnitario,tmVrTotDevg=@pmtmVrTotDevg,tmVrTotDed=@pmtmVrTotDed
,tmUnidad=@pmtmUnidad,tmClaseLiq=@pmtmClaseLiq,tmVrOrigen=@pmtmVrOrigen,tmTarifa=@pmtmTarifa,tmDiasCalc=@pmtmDiasCalc,tmDiasNov=@pmtmDiasNov,tmCodFondo=@pmtmCodFondo,tmNPrestamo=@pmtmNPrestamo,tmNCuota=@pmtmNCuota,tmOrigCargue=@pmtmOrigCargue
,tmVrBaseLiq=@pmtmVrBaseLiq,tmNContto=@pmtmNContto,tmFecIni=@pmtmFecIni,tmFecFin=@pmtmFecFin,tmIdPeriodo=@pmtmIdPeriodo,tmVrPension=@pmtmVrPension,tmVrSalud=@pmtmVrSalud
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_NominaPar @pmtmNumero VARCHAR(5),@pmtmIdEmpleado VARCHAR(16),@pmtmItem INT,@pmtmDetalle VARCHAR(100),@pmtmCantDevg DECIMAL(10,4),@pmtmCantDed DECIMAL(10,4),@pmtmVrUnitario MONEY,@pmtmVrTotDevg MONEY,@pmtmVrTotDed MONEY,@pmtmUnidad VARCHAR(10)
,@pmtmVrOrigen MONEY,@pmtmTarifa DECIMAL(10,4),@pmtmDiasCalc DECIMAL(10,4),@pmtmDiasNov DECIMAL(10,4),@pmtmVrBaseLiq MONEY,@pmtmFecIni SMALLDATETIME,@pmtmFecFin SMALLDATETIME,@pmtmFecUltLiq SMALLDATETIME
 AS 
 UPDATE tm_Nomina SET tmDetalle=@pmtmDetalle,tmCantDevg=@pmtmCantDevg,tmCantDed=@pmtmCantDed,tmVrUnitario=@pmtmVrUnitario,tmVrTotDevg=@pmtmVrTotDevg,tmVrTotDed=@pmtmVrTotDed,tmUnidad=@pmtmUnidad,tmVrOrigen=@pmtmVrOrigen
,tmTarifa=@pmtmTarifa,tmDiasCalc=@pmtmDiasCalc,tmDiasNov=@pmtmDiasNov,tmVrBaseLiq=@pmtmVrBaseLiq,tmFecIni=@pmtmFecIni,tmFecFin=@pmtmFecFin,tmFecUltLiq=@pmtmFecUltLiq
 WHERE tmNumero=@pmtmNumero AND tmIdEmpleado=@pmtmIdEmpleado AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_PagosCam @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmVrCambio MONEY
 AS 
 UPDATE  tm_Pagos  SET tmVrCambio=@pmtmVrCambio
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_PagosTap @pmtmNumero VARCHAR(5),@pmtmItem INT,@pmtmTipAplica VARCHAR(10)
 AS 
 UPDATE  tm_Pagos  SET tmTipAplica=@pmtmTipAplica
 WHERE tmNumero=@pmtmNumero AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_PlasueldosObs @pmtmIdPeriodo VARCHAR(8),@pmtmKeyRegistro VARCHAR(30),@pmtmObserv VARCHAR(1000)
 AS 
 UPDATE  tm_Plasueldos  SET tmObserv=@pmtmObserv
 WHERE tmIdPeriodo=@pmtmIdPeriodo AND tmKeyRegistro LIKE ISNULL(@pmtmKeyRegistro,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_PlasueldosVac @pmtmIdPeriodo VARCHAR(8),@pmtmKeyRegistro VARCHAR(30)
,@pmtmVrVacLiquida MONEY,@pmtmVrVacPen MONEY,@pmtmVrVacSal MONEY,@pmtmVrVacFsp MONEY,@pmtmVrVacOtr MONEY
 AS 
 UPDATE  tm_Plasueldos  SET tmVrVacLiquida=@pmtmVrVacLiquida,tmVrVacPen=@pmtmVrVacPen
,tmVrVacSal=@pmtmVrVacSal,tmVrVacFsp=@pmtmVrVacFsp,tmVrVacOtr=@pmtmVrVacOtr
 WHERE tmIdPeriodo=@pmtmIdPeriodo  AND tmKeyRegistro=@pmtmKeyRegistro
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_SaldosAnt @pmtmEst CHAR(2),@pmtmIdCia CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdTercero VARCHAR(16),@pmtmCdSubgrupo VARCHAR(8),@pmtmCostoAnt MONEY,@pmtmSaldoAnt DECIMAL(14,4)
 AS 
 UPDATE tm_Saldos SET tmCostoAnt=@pmtmCostoAnt,tmSaldoAnt=@pmtmSaldoAnt
 WHERE tmEst=@pmtmEst AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%') AND tmIdCuenta LIKE ISNULL(@pmtmIdCuenta,'%')
 AND tmIdTercero LIKE ISNULL(@pmtmIdTercero,'%') AND tmCdSubgrupo LIKE ISNULL(@pmtmCdSubgrupo,'%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_SaldosCos @pmtmEst CHAR(2),@pmtmIdCia CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdTercero VARCHAR(16),@pmtmCostoEnt MONEY,@pmtmCostoSal MONEY
 AS 
 UPDATE tm_Saldos SET tmCostoEnt=@pmtmCostoEnt,tmCostoSal=@pmtmCostoSal
 WHERE tmEst=@pmtmEst AND tmIdCia=@pmtmIdCia AND tmIdCuenta=@pmtmIdCuenta AND tmIdTercero=@pmtmIdTercero

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_SaldosCue @pmtmEst CHAR(2),@pmtmIdCia CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdTercero VARCHAR(16),@pmtmCdSubgrupo VARCHAR(8),@pmtmSanCue MONEY
 AS 
 UPDATE tm_Saldos SET tmSanCue=@pmtmSanCue
 WHERE tmEst=@pmtmEst AND tmIdCuenta=@pmtmIdCuenta  AND tmIdTercero=@pmtmIdTercero 
 AND tmCdSubgrupo LIKE ISNULL(@pmtmCdSubgrupo,'%') AND tmIdCia LIKE ISNULL(@pmtmIdCia,'%%')
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_SaldosMov @pmtmEst CHAR(2),@pmtmIdCia CHAR(2),@pmtmIdCuenta VARCHAR(16)
,@pmtmIdTercero VARCHAR(16),@pmtmCdSubgrupo VARCHAR(8),@pmtmDebitos MONEY,@pmtmCreditos MONEY
 AS 
 UPDATE tm_Saldos SET tmDebitos=@pmtmDebitos,tmCreditos=@pmtmCreditos
 WHERE tmEst=@pmtmEst AND tmIdCia=@pmtmIdCia AND tmIdCuenta=@pmtmIdCuenta
 AND tmIdTercero=@pmtmIdTercero AND tmCdSubgrupo=@pmtmCdSubgrupo
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE paUptm_VentasCom @pmtmEst CHAR(2),@pmtmItem INT,@pmtmUnidades2 DECIMAL(14,4),@pmtmVentaNeto2 MONEY
,@pmtmCostoNeto2 MONEY,@pmtmImpuestos2 MONEY,@pmtmImpGlobal2 MONEY,@pmtmSoldicom2 MONEY,@pmtmVolumen2 DECIMAL(14,4)
 AS 
 UPDATE tm_VentasCom SET tmUnidades2=@pmtmUnidades2,tmVentaNeto2=@pmtmVentaNeto2,tmCostoNeto2=@pmtmCostoNeto2
,tmImpuestos2=@pmtmImpuestos2,tmImpGlobal2=@pmtmImpGlobal2,tmSoldicom2=@pmtmSoldicom2,tmVolumen2=@pmtmVolumen2
 WHERE tmEst=@pmtmEst AND tmItem=@pmtmItem
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE paUptm_vce @pmtmTipDoc VARCHAR(3),@pmtmDocumento INT,@pmtmIdCia CHAR(2),@pmtmItem INT
,@pmtmValorAbono MONEY,@pmtmFecUltPago SMALLDATETIME
 AS 
 UPDATE tm_vce SET tmValorAbono=@pmtmValorAbono,tmFecUltPago=@pmtmFecUltPago
 WHERE tmTipDoc=@pmtmTipDoc AND tmDocumento=@pmtmDocumento AND tmIdCia=@pmtmIdCia AND tmItem=@pmtmItem

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spArticulos @Tanques BIT,@Fecha SMALLDATETIME
AS
 SELECT IdProducto,DescripProd,DescripAbrv,Referencia,CodBarras,VrCostPmd AS CostoPromed,Precio1,Precio2 
 ,Precio3,Precio4,Precio5,IvaInc,Tarifa AS TarifaIva,ExtciaMax,ExtciaAct,P.IdUnd AS CodUnidad,Unidad,IdEstado,P.Inactivo AS ProdInactivo
,FechaAdd AS FechaCreacion,FechaUpdate AS FechaActualizacion
 FROM  ProdMcias AS P INNER JOIN UndMed AS U ON P.IdUnd=U.IdUnd  LEFT JOIN Tablapor AS T ON P.IdTarIva=T.IdTarifa
 WHERE  TipoRef='PRODUCTO'  AND Tanques=@Tanques 
 AND ISNULL(FechaUpdate,FechaAdd)>@Fecha
ORDER BY IdProducto
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spClieSalarios @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16) 
 AS 
 SELECT nMes,nAnno,VrSalario,TarifDcto 
 FROM TercClieSalarios
 WHERE  IdClie=@pmIdClie AND IdAgencia=@pmIdAgencia  
 ORDER BY nAnno,nMes
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spClientes @Fecha SMALLDATETIME
AS
 SELECT  IdClie AS NitCliente,RazonSocial,Codigo,TipoId,Dv,Direccion,T.IdLocal AS CodLocalidad,Localidad,Telefono,TelMovil,Fax,e_mail
 ,VrCupo,VrSaldo,VrCupo-VrSaldo AS Disponible,Cmntario1,C.IdEstado AS CodEstado,C.Inactivo AS ClieInactivo
,C.FechaAdd AS FechaCreacion,C.FechaUpdate  AS FechaActualizacion
 FROM TercCliente AS C INNER JOIN Terceros AS T ON C.IdClie=T.IdTercero
 INNER JOIN Localidades AS L ON T.IdLocal=L.IdLocal
 WHERE ISNULL(C.FechaUpdate,C.FechaAdd)>@Fecha
 ORDER BY RazonSocial
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spFinCliente @pmIdClie VARCHAR(16),@pmIdAgencia VARCHAR(16) 
 AS 
 SELECT RazonSocial,TipoId,Dv,NomCial,e_mail,nPlazo,TipoPlazo,CupoCredito,MaxCupoSmlv,TipoCausac,DiasVence
,EtdoAprob,TipoAprob,DescAprob,EstadoAct,L.FechaCrea AS LinFechaCrea
,EsExento,FecIngreso,VrSalBasico,MaxDcto,TipoSalario,C.IdEstado AS EdoCliente,C.Inactivo AS ClieInactivo,C.FechaCrea AS ClieFechaCrea
 FROM TercClieLineas AS L INNER JOIN TercCliePrestamo AS C ON L.IdClie=C.IdClie AND L.IdAgencia=C.IdAgencia
 INNER JOIN Terceros AS T ON L.IdClie=T.IdTercero
 WHERE L.IdClie=@pmIdClie AND L.IdAgencia=@pmIdAgencia AND L.IdLinea='0005'
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spFinCuotas  @pmIdCliente VARCHAR(16),@pmIdAgencia  VARCHAR(16)
 AS 
 SELECT SUM(CASE Causacion WHEN 'QUINCENAL' THEN VrCuota*2 ELSE VrCuota END) AS TotalCuotas
 FROM Trn_FinPrestamo
 WHERE TipDoc='PR1' AND IdCliente=@pmIdCliente AND IdAgencia=@pmIdAgencia 
 AND TipoCredito IN ('PRESTAMO','TC') AND TipoCausac=0 AND IdEstado='0001' AND Anulado=0 AND VrPrestamo>AcumCapital

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spFinCuposSML @pmnAnno INT
AS
 SELECT   Item, nSmlvIni, nSmlvFin, Antig_Ini, Antig_Fin, MontoConGar, MontoSinGar 
 FROM   FinCuposEmp
 WHERE nAnno=@pmnAnno
 ORDER BY Item
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE spFinPrestamo  @pmIdCliente VARCHAR(16),@pmIdAgencia  VARCHAR(16)
 AS 
 SELECT  SUM(VrPrestamo) AS TotalPrestamos,SUM(TotalAbonos) AS TotalPagos
 FROM Trn_FinPrestamo AS P LEFT JOIN (SELECT A.TipDoc AS TipPrest,A.IdPrestamo AS NumPrest,A.IdCiaPre AS CdCiaPrest,SUM(A.TotalAbono) AS TotalAbonos
 FROM Trn_FinAbonos AS A INNER JOIN Trn_FinCuotas AS C ON A.TipDoc=C.TipDoc AND A.IdPrestamo=C.IdPrestamo AND A.IdCiaPre=C.IdCia AND A.ItemPre=C.Item
 WHERE A.IdCliente=@pmIdCliente AND A.IdAgencia=@pmIdAgencia AND C.Concepto='CAPITAL'  GROUP BY A.TipDoc,A.IdPrestamo,A.IdCiaPre)
  AS PG ON P.TipDoc=PG.TipPrest AND P.IdPrestamo=PG.NumPrest AND  P.IdCia=PG.CdCiaPrest
 WHERE  P.TipDoc='PR1' AND P.IdCliente=@pmIdCliente AND P.IdAgencia=@pmIdAgencia 
 AND P.IdLinea='0005' AND P.TipoCredito='TC'  AND P.TipoCausac=0  AND P.Anulado=0
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

