if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelDevVal]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelDevVal]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelFactVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelFactVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelRadicaVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelRadicaVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelTaxRutas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelTaxRutas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelTaxTarifClie]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelTaxTarifClie]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelTaxTarifas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelTaxTarifas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDeltm_ValesCred]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDeltm_ValesCred]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paDelValesRutas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paDelValesRutas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsDevVal]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsDevVal]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsFactVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsFactVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsRadicaVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsRadicaVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTaxRutas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsTaxRutas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTaxTarifClie]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsTaxTarifClie]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsTaxTarifas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsTaxTarifas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_ValesCred]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_ValesCred]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_ValesCred_Dev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_ValesCred_Dev]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInstm_ValesCred_Sel]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInstm_ValesCred_Sel]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paInsValesRutas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paInsValesRutas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevVal]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDevVal]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevValDoc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDevValDoc]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevValLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDevValLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryDevVal_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryDevVal_Cr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryFactVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryFactVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryRadicaVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryRadicaVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryRadicaValesLta]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryRadicaValesLta]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryRadicaVales_Cr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryRadicaVales_Cr]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTaxRutas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTaxRutas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTaxRutasDso]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTaxRutasDso]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTaxTarifClie]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTaxTarifClie]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTaxTarifas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTaxTarifas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryTiposComDsa]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryTiposComDsa]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_EstClientesMov]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_EstClientesMov]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQrytm_ValesCred]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQrytm_ValesCred]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paQryValesRad]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paQryValesRad]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpDevVal]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpDevVal]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpRadicaVales]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpRadicaVales]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpRadicaValesDev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpRadicaValesDev]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTaxRutas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpTaxRutas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTaxTarifClie]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpTaxTarifClie]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpTaxTarifas]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpTaxTarifas]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpValesDev]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpValesDev]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpValesDevItem]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpValesDevItem]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpValesDfa]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpValesDfa]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[paUpValesFac]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[paUpValesFac]
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 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 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 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 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 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 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,@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,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,@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 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 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,@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,OrigenAdd,TipCom,Comprobante,IdCiaCom,Anulado,NumDev,FecDev,Observacion,IdEstado,TimeSys,IdCiaCrea,IdUsuario)
 VALUES (@pmTipDoc,@pmnRadica,@pmIdCia,@pmFecha,@pmFechaVence,@pmIdConcepto,@pmIdTercero,@pmTotalPagos,@pmTotalCobros,@pmReferencia,@pmCxPagar,@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 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 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 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 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,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,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,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 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 paQryRadicaVales @pmTipDoc VARCHAR(3),@pmnRadica INT,@pmIdCia CHAR(2)
 AS 
 SELECT TipDoc,nRadica,IdCia,Fecha,FechaVence,IdConcepto,IdTercero,TotalPagos,TotalCobros,Referencia,CxPagar
,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
,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,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 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 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 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
 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_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 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 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,@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,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 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,@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
 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 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 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

